Skip to content

Commit

Permalink
More optimization for compound queries - $and and $not are processed …
Browse files Browse the repository at this point in the history
…first as they are likely to return fewer results
  • Loading branch information
davidgtonge committed Feb 6, 2012
1 parent 44c605c commit f6ad168
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
12 changes: 6 additions & 6 deletions js/backbone-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ May be freely distributed according to MIT license.
attr = model.get(q.key);
test = test_model_attribute(q.type, attr);
if (test) {
test = ((function() {
test = (function() {
var _ref;
switch (q.type) {
case "$equal":
Expand Down Expand Up @@ -131,7 +131,7 @@ May be freely distributed according to MIT license.
case "$cb":
return q.value.call(model, attr);
}
})());
})();
}
if (andOr === test) return andOr;
}
Expand Down Expand Up @@ -167,15 +167,15 @@ May be freely distributed according to MIT license.
};

get_models = function(collection, query) {
var compound_query, models, reduce_iterator, type;
compound_query = _(query).chain().keys().intersection(["$or", "$and", "$nor", "$not"]).value();
var compound_query, models, query_type, reduce_iterator;
compound_query = _.intersection(["$and", "$not", "$or", "$nor"], _(query).keys());
models = collection.models;
switch (compound_query.length) {
case 0:
return process_query.$and(models, query);
case 1:
type = compound_query[0];
return process_query[type](models, query[type]);
query_type = compound_query[0];
return process_query[query_type](models, query[query_type]);
default:
reduce_iterator = function(memo, query_type) {
return process_query[query_type](memo, query[query_type]);
Expand Down
2 changes: 1 addition & 1 deletion js/backbone-query.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions src/backbone-query.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ iterator = (models, query, andOr, filterReject) ->
# Check if the attribute value is the right type (some operators need a string, or an array)
test = test_model_attribute(q.type, attr)
# If the attribute test is true, perform the query
if test then test = (switch q.type
if test then test = switch q.type
when "$equal" then attr is q.value
when "$contains" then q.value in attr
when "$ne" then attr isnt q.value
Expand All @@ -75,7 +75,7 @@ iterator = (models, query, andOr, filterReject) ->
when "$exists", "$has" then model.has(q.key) is q.value
when "$like" then attr.indexOf(q.value) isnt -1
when "$regex" then q.value.test attr
when "$cb" then q.value.call model, attr)
when "$cb" then q.value.call model, attr

# If the query is an "or" query than as soon as a match is found we return "true"
# Whereas if the query is an "and" query then we return "false" as soon as a match isn't found.
Expand Down Expand Up @@ -114,9 +114,11 @@ get_cache = (collection, query, options) ->
get_models = (collection, query) ->

# Iterate through the query keys to check for any of the compound methods
compound_query = _(query).chain().keys().intersection(["$or", "$and", "$nor", "$not"]).value()
# The resulting array will have "$and" and "$not" first as it is better to use these
# operators first when performing a compound query as they are likely to return less results
compound_query = _.intersection ["$and", "$not", "$or", "$nor"], _(query).keys()

# Assign the collections models to a local variable to use in the follwoing switch
# Assign the collections models to a local variable to use in the following switch
models = collection.models

switch compound_query.length
Expand All @@ -125,8 +127,8 @@ get_models = (collection, query) ->

# If only 1 compound method then invoke just that method
when 1
type = compound_query[0]
process_query[type] models, query[type]
query_type = compound_query[0]
process_query[query_type] models, query[query_type]

# If more than 1 method is found, process each of the methods using underscore reduce
else
Expand Down

0 comments on commit f6ad168

Please sign in to comment.