Commit 4ead8961 authored by Tristan Cavelier's avatar Tristan Cavelier

gidstorage alldocs handles limit option

parent 83b0c1f2
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
*/ */
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global define, jIO */ /*global define, jIO, RSVP */
/** /**
* JIO GID Storage. Type = 'gid'. * JIO GID Storage. Type = 'gid'.
...@@ -51,8 +51,8 @@ ...@@ -51,8 +51,8 @@
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
return define(dependencies, module); return define(dependencies, module);
} }
module(jIO); module(jIO, RSVP);
}(['jio'], function (jIO) { }(['jio', 'rsvp'], function (jIO, RSVP) {
"use strict"; "use strict";
var dcmi_types, metadata_actions, content_type_re, tool; var dcmi_types, metadata_actions, content_type_re, tool;
...@@ -273,6 +273,56 @@ ...@@ -273,6 +273,56 @@
return object; return object;
} }
function doWhile(callback) {
var cancelled, p1 = RSVP.resolve(), p2;
return new RSVP.Promise(function (done, fail, notify) {
function next(value) {
if (value) {
try {
value = callback();
} catch (e) {
return fail(e);
}
if (cancelled) { return; }
if (value && typeof value.then === "function") {
p1 = value;
p2 = value.then(next, fail, notify);
} else {
p2 = p2.then(next.bind(null, value), fail, notify);
}
return;
}
done();
}
p2 = p1.then(next.bind(null, true));
}, function () {
cancelled = true;
if (typeof p1.cancel === "function") { p1.cancel(); }
if (typeof p2.cancel === "function") { p2.cancel(); }
});
}
function arrayExtend(aThis) {
/*jslint plusplus: true */
var nArgIndex, nArgLength, value, nValueIndex, nValueLength, nThisLength;
nThisLength = aThis.length;
for (nArgIndex = 1, nArgLength = arguments.length;
nArgIndex < nArgLength;
nArgIndex += 1) {
value = arguments[nArgIndex];
if (Array.isArray(value)) {
for (nValueIndex = 0, nValueLength = value.length;
nValueIndex < nValueLength;
nValueIndex += 1) {
aThis[nThisLength++] = value[nValueIndex];
}
} else {
aThis[nThisLength++] = value;
}
}
return aThis;
}
/** /**
* The gid storage used by JIO. * The gid storage used by JIO.
* *
...@@ -550,10 +600,31 @@ ...@@ -550,10 +600,31 @@
*/ */
that.allDocs = function (command, param, options) { that.allDocs = function (command, param, options) {
/*jslint unparam: true */ /*jslint unparam: true */
var include_docs; var include_docs, limit_start, limit_length, rows, i = 0;
include_docs = options.include_docs; include_docs = options.include_docs;
options.include_docs = true; options.include_docs = true;
command.storage(priv.sub_storage).allDocs(
if (Array.isArray(options.limit)) {
if (options.limit.length > 1) {
limit_start = options.limit[0];
limit_length = options.limit[1];
} else {
limit_start = 0;
limit_length = options.limit[0];
}
} else if (typeof options.limit === "number") {
limit_start = 0;
limit_length = options.limit;
}
function doAllDocs(i) {
if (limit_length) {
options.limit = [
limit_start + (limit_length * i),
limit_length
];
}
return command.storage(priv.sub_storage).allDocs(
options options
).then(function (response) { ).then(function (response) {
/*jslint ass: true */ /*jslint ass: true */
...@@ -575,16 +646,31 @@ ...@@ -575,16 +646,31 @@
} }
} }
} }
doc_gids = undefined; // free memory if (rows) {
row = undefined; arrayExtend(rows, result);
} else {
rows = result;
}
});
}
doWhile(function () {
/*jslint plusplus: true */
return doAllDocs(i++).then(function () {
if (limit_length) {
if (rows.length < limit_length) {
return true;
}
rows.length = limit_length;
}
return false;
});
}).then(function () {
command.success({"data": { command.success({"data": {
"total_rows": result.length, "total_rows": rows.length,
"rows": result "rows": rows
}}); }});
}, function (err) { }, command.error);
err.message = "Cannot get all documents";
return command.error(err);
});
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment