Commit 84107abc authored by Tristan Cavelier's avatar Tristan Cavelier

query parse documents sequentially

Waits 4 ms every 50 ms.
parent b9ed2084
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
/*global parseStringToObject: true, emptyFunction: true, sortOn: true, limit: /*global parseStringToObject: true, emptyFunction: true, sortOn: true, limit:
true, select: true, exports, stringEscapeRegexpCharacters: true, true, select: true, exports, stringEscapeRegexpCharacters: true,
deepClone, RSVP, sequence, background, jIO, metadataValueToStringArray, deepClone, RSVP, sequence, background, jIO, metadataValueToStringArray,
sortFunction */ sortFunction, setTimeout, clearTimeout */
/** /**
* The query to use to filter a list of objects. * The query to use to filter a list of objects.
...@@ -68,13 +68,11 @@ Query.prototype.exec = function (item_list, option) { ...@@ -68,13 +68,11 @@ Query.prototype.exec = function (item_list, option) {
background.removeUnmatchedSortLimitAndSelect = function (event) { background.removeUnmatchedSortLimitAndSelect = function (event) {
var j, task = event.data.option, array = event.data.item_list; var j, task = event.data.option, array = event.data.item_list;
// remove unmatched documents // remove unmatched documents
if (task.match_list) { for (j = array.length - 1; j >= 0; j -= 1) {
for (j = task.match_list.length - 1; j >= 0; j -= 1) { if (!array[j]) {
if (!task.match_list[j]) {
array.splice(j, 1); array.splice(j, 1);
} }
} }
}
// sort documents // sort documents
if (task.sort_on) { if (task.sort_on) {
sortOn(task.sort_on, array); sortOn(task.sort_on, array);
...@@ -97,7 +95,6 @@ Query.prototype.exec = function (item_list, option) { ...@@ -97,7 +95,6 @@ Query.prototype.exec = function (item_list, option) {
"onmessage = " + background.removeUnmatchedSortLimitAndSelect.toString() "onmessage = " + background.removeUnmatchedSortLimitAndSelect.toString()
); );
} }
var i, promises = [];
if (!Array.isArray(item_list)) { if (!Array.isArray(item_list)) {
throw new TypeError("Query().exec(): Argument 1 is not of type 'array'"); throw new TypeError("Query().exec(): Argument 1 is not of type 'array'");
} }
...@@ -108,18 +105,54 @@ Query.prototype.exec = function (item_list, option) { ...@@ -108,18 +105,54 @@ Query.prototype.exec = function (item_list, option) {
throw new TypeError("Query().exec(): " + throw new TypeError("Query().exec(): " +
"Optional argument 2 is not of type 'object'"); "Optional argument 2 is not of type 'object'");
} }
for (i = 0; i < item_list.length; i += 1) { function sleep(delay, value) {
if (!item_list[i]) { var ident;
promises.push(RSVP.resolve(false)); return new RSVP.Promise(function (done) {
} else { ident = setTimeout(done, delay, value);
promises.push(this.match(item_list[i])); }, function () {
} clearTimeout(ident);
});
} }
var this_ = this;
return sequence([function () {
// sequential operation
var begin = Date.now();
return jIO.util.forEach(item_list, function (value, i) {
if (value) {
return sequence([function () { return sequence([function () {
return RSVP.all(promises); return this_.match(item_list[i]);
}, function (answers) { }, function (answer) {
if (!answer) {
item_list[i] = false;
}
var now = Date.now();
if (begin <= now - 50) {
begin = now;
return sleep(4);
}
}]);
}
item_list[i] = false;
});
// // parallel operation
// var i, promises = [];
// function setToFalseIfNotMatch(i) {
// return function (a) {
// if (!a) {
// item_list[i] = false;
// }
// };
// }
// for (i = 0; i < item_list.length; i += 1) {
// if (!item_list[i]) {
// promises.push(RSVP.resolve(false));
// } else {
// promises.push(this_.match(item_list[i]).then(setToFalseIfNotMatch(i)));
// }
// }
// return RSVP.all(promises);
}, function () {
option = { option = {
"match_list": answers,
"limit": option.limit, "limit": option.limit,
"sort_on": option.sort_on, "sort_on": option.sort_on,
"select_list": option.select_list "select_list": option.select_list
......
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