Commit e10057fd authored by Romain Courteaud's avatar Romain Courteaud

Queries: drop not needed asynchronous code.

Asynchronous queries are not used by any storage.
Improve allDocs speed for IndexedDB by 10x-20x.
parent a99b1dfb
...@@ -219,7 +219,6 @@ ...@@ -219,7 +219,6 @@
* second is the length. * second is the length.
*/ */
Query.prototype.exec = function (item_list, option) { Query.prototype.exec = function (item_list, option) {
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'");
} }
...@@ -230,36 +229,25 @@ ...@@ -230,36 +229,25 @@
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) { var context = this,
if (!item_list[i]) { i;
promises.push(RSVP.resolve(false)); for (i = item_list.length - 1; i >= 0; i -= 1) {
} else { if (!context.match(item_list[i])) {
promises.push(this.match(item_list[i])); item_list.splice(i, 1);
} }
} }
if (option.sort_on) {
sortOn(option.sort_on, item_list);
}
if (option.limit) {
limit(option.limit, item_list);
}
select(option.select_list || [], item_list);
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
return RSVP.all(promises);
})
.push(function (answers) {
var j;
for (j = answers.length - 1; j >= 0; j -= 1) {
if (!answers[j]) {
item_list.splice(j, 1);
}
}
if (option.sort_on) {
return sortOn(option.sort_on, item_list);
}
})
.push(function () {
if (option.limit) {
return limit(option.limit, item_list);
}
})
.push(function () {
return select(option.select_list || [], item_list);
})
.push(function () { .push(function () {
return item_list; return item_list;
}); });
...@@ -273,7 +261,7 @@ ...@@ -273,7 +261,7 @@
* @return {Boolean} true if match, false otherwise * @return {Boolean} true if match, false otherwise
*/ */
Query.prototype.match = function () { Query.prototype.match = function () {
return RSVP.resolve(true); return true;
}; };
/** /**
...@@ -534,30 +522,15 @@ ...@@ -534,30 +522,15 @@
* @return {Boolean} true if all match, false otherwise * @return {Boolean} true if all match, false otherwise
*/ */
ComplexQuery.prototype.AND = function (item) { ComplexQuery.prototype.AND = function (item) {
var queue = new RSVP.Queue(), var result = true,
context = this,
i = 0; i = 0;
function executeNextIfNotFalse(result) { while (result && (i !== this.query_list.length)) {
if (result === false) { result = this.query_list[i].match(item);
// No need to evaluate the other elements, as one is false i += 1;
return result;
}
if (context.query_list.length === i) {
// No new element to loop on
return true;
}
queue
.push(function () {
var sub_result = context.query_list[i].match(item);
i += 1;
return sub_result;
})
.push(executeNextIfNotFalse);
} }
return result;
executeNextIfNotFalse(true);
return queue;
}; };
/** /**
...@@ -568,31 +541,16 @@ ...@@ -568,31 +541,16 @@
* @param {Object} item The item to match * @param {Object} item The item to match
* @return {Boolean} true if one match, false otherwise * @return {Boolean} true if one match, false otherwise
*/ */
ComplexQuery.prototype.OR = function (item) { ComplexQuery.prototype.OR = function (item) {
var queue = new RSVP.Queue(), var result = false,
context = this,
i = 0; i = 0;
function executeNextIfNotTrue(result) { while ((!result) && (i !== this.query_list.length)) {
if (result === true) { result = this.query_list[i].match(item);
// No need to evaluate the other elements, as one is true i += 1;
return result;
}
if (context.query_list.length === i) {
// No new element to loop on
return false;
}
queue
.push(function () {
var sub_result = context.query_list[i].match(item);
i += 1;
return sub_result;
})
.push(executeNextIfNotTrue);
} }
executeNextIfNotTrue(false); return result;
return queue;
}; };
/** /**
...@@ -604,13 +562,7 @@ ...@@ -604,13 +562,7 @@
* @return {Boolean} true if one match, false otherwise * @return {Boolean} true if one match, false otherwise
*/ */
ComplexQuery.prototype.NOT = function (item) { ComplexQuery.prototype.NOT = function (item) {
return new RSVP.Queue() return !this.query_list[0].match(item);
.push(function () {
return this.query_list[0].match(item);
})
.push(function (answer) {
return !answer;
});
}; };
/** /**
...@@ -828,7 +780,7 @@ ...@@ -828,7 +780,7 @@
value = this.value; value = this.value;
} }
if (object_value === undefined || value === undefined) { if (object_value === undefined || value === undefined) {
return RSVP.resolve(false); return false;
} }
return matchMethod(object_value, value); return matchMethod(object_value, value);
}; };
...@@ -876,13 +828,13 @@ ...@@ -876,13 +828,13 @@
value = value.content; value = value.content;
} }
if (typeof value.cmp === "function") { if (typeof value.cmp === "function") {
return RSVP.resolve(value.cmp(comparison_value) === 0); return (value.cmp(comparison_value) === 0);
} }
if (comparison_value.toString() === value.toString()) { if (comparison_value.toString() === value.toString()) {
return RSVP.resolve(true); return true;
} }
} }
return RSVP.resolve(false); return false;
}; };
/** /**
...@@ -904,15 +856,15 @@ ...@@ -904,15 +856,15 @@
value = value.content; value = value.content;
} }
if (typeof value.cmp === "function") { if (typeof value.cmp === "function") {
return RSVP.resolve(value.cmp(comparison_value) === 0); return (value.cmp(comparison_value) === 0);
} }
if ( if (
searchTextToRegExp(comparison_value.toString()).test(value.toString()) searchTextToRegExp(comparison_value.toString()).test(value.toString())
) { ) {
return RSVP.resolve(true); return true;
} }
} }
return RSVP.resolve(false); return false;
}; };
/** /**
...@@ -934,13 +886,13 @@ ...@@ -934,13 +886,13 @@
value = value.content; value = value.content;
} }
if (typeof value.cmp === "function") { if (typeof value.cmp === "function") {
return RSVP.resolve(value.cmp(comparison_value) !== 0); return (value.cmp(comparison_value) !== 0);
} }
if (comparison_value.toString() === value.toString()) { if (comparison_value.toString() === value.toString()) {
return RSVP.resolve(false); return false;
} }
} }
return RSVP.resolve(true); return true;
}; };
/** /**
...@@ -961,9 +913,9 @@ ...@@ -961,9 +913,9 @@
value = value.content; value = value.content;
} }
if (typeof value.cmp === "function") { if (typeof value.cmp === "function") {
return RSVP.resolve(value.cmp(comparison_value) < 0); return (value.cmp(comparison_value) < 0);
} }
return RSVP.resolve(value < comparison_value); return (value < comparison_value);
}; };
/** /**
...@@ -985,9 +937,9 @@ ...@@ -985,9 +937,9 @@
value = value.content; value = value.content;
} }
if (typeof value.cmp === "function") { if (typeof value.cmp === "function") {
return RSVP.resolve(value.cmp(comparison_value) <= 0); return (value.cmp(comparison_value) <= 0);
} }
return RSVP.resolve(value <= comparison_value); return (value <= comparison_value);
}; };
/** /**
...@@ -1009,9 +961,9 @@ ...@@ -1009,9 +961,9 @@
value = value.content; value = value.content;
} }
if (typeof value.cmp === "function") { if (typeof value.cmp === "function") {
return RSVP.resolve(value.cmp(comparison_value) > 0); return (value.cmp(comparison_value) > 0);
} }
return RSVP.resolve(value > comparison_value); return (value > comparison_value);
}; };
/** /**
...@@ -1033,9 +985,9 @@ ...@@ -1033,9 +985,9 @@
value = value.content; value = value.content;
} }
if (typeof value.cmp === "function") { if (typeof value.cmp === "function") {
return RSVP.resolve(value.cmp(comparison_value) >= 0); return (value.cmp(comparison_value) >= 0);
} }
return RSVP.resolve(value >= comparison_value); return (value >= comparison_value);
}; };
query_class_dict.simple = SimpleQuery; query_class_dict.simple = SimpleQuery;
......
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