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,41 +600,77 @@ ...@@ -550,41 +600,77 @@
*/ */
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(
options if (Array.isArray(options.limit)) {
).then(function (response) { if (options.limit.length > 1) {
/*jslint ass: true */ limit_start = options.limit[0];
var result = [], doc_gids = {}, row, gid; limit_length = options.limit[1];
response = response.data; } else {
while ((row = response.rows.shift()) !== undefined) { limit_start = 0;
gid = gidFormat(row.doc, priv.constraints); limit_length = options.limit[0];
if (gid !== undefined) { }
if (!doc_gids[gid]) { } else if (typeof options.limit === "number") {
doc_gids[gid] = true; limit_start = 0;
row.id = gid; limit_length = options.limit;
delete row.key; }
result[result.length] = row;
if (include_docs === true) { function doAllDocs(i) {
row.doc._id = gid; if (limit_length) {
} else { options.limit = [
delete row.doc; limit_start + (limit_length * i),
limit_length
];
}
return command.storage(priv.sub_storage).allDocs(
options
).then(function (response) {
/*jslint ass: true */
var result = [], doc_gids = {}, row, gid;
response = response.data;
while ((row = response.rows.shift()) !== undefined) {
gid = gidFormat(row.doc, priv.constraints);
if (gid !== undefined) {
if (!doc_gids[gid]) {
doc_gids[gid] = true;
row.id = gid;
delete row.key;
result[result.length] = row;
if (include_docs === true) {
row.doc._id = gid;
} else {
delete row.doc;
}
} }
} }
} }
} if (rows) {
doc_gids = undefined; // free memory arrayExtend(rows, result);
row = undefined; } 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