Permalink
Browse files

Simplify find to only once convert ids to int and only if necessary (…

…increment = count)

Add sort direction DESC test
  • Loading branch information...
1 parent be3ed35 commit ab30d4c5379d4aa6939945e2a40c6270949b0de5 @maritz committed Jan 16, 2012
Showing with 44 additions and 29 deletions.
  1. +20 −26 lib/retrieve.js
  2. +24 −3 test/findTests.js
View
46 lib/retrieve.js
@@ -16,6 +16,15 @@ exports.exists = function (id, callback) {
});
};
+var convertIdsToInt = function (ids, callback) {
+ if (this.idGenerator === 'increment' && Array.isArray(ids)) {
+ ids = ids.map(function (val) {
+ return parseInt(val, 10);
+ });
+ }
+ callback(ids);
+}
+
/**
* Retrieves the hash data by id and puts it into the properties.
*/
@@ -71,8 +80,12 @@ exports.find = function find(searches, callback) {
found.push(val);
}
});
+ } else if ( ! Array.isArray(values)) {
+ found = [values];
}
- callback.call(self, err, found);
+ convertIdsToInt(found, function (ids) {
+ callback.call(self, err, ids);
+ });
},
getSets = function (callback) {
self.getClient().sinter(sets, callback);
@@ -82,12 +95,6 @@ exports.find = function find(searches, callback) {
if (err) {
callback(err);
} else {
- values.forEach(function (val, key) {
- if (self.idGenerator === 'increment') {
- var parsed = parseInt(val, 10);
- if (!isNaN(parsed)) values[key] = parsed;
- }
- });
callback(null, values);
}
};
@@ -109,8 +116,8 @@ exports.find = function find(searches, callback) {
},
getZSets = function (callback) {
async.map(zsetKeys, getSingleZSet, function done (err, arr) {
- var test = h.idIntersection.apply(null, arr);
- callback(null, test.sort());
+ var ids = h.idIntersection.apply(null, arr);
+ callback(err, ids);
});
};
@@ -126,11 +133,10 @@ exports.find = function find(searches, callback) {
if ( ! searches[s].toLowerCase) {
return returnFunction('Invalid search parameters: Searching for a unique with a non-string value is not supported.');
}
- return this.getClient().mget(Nohm.prefix.unique + self.modelName + ':' + s +
- ':' + searches[s].toLowerCase(),
- returnFunction);
+ var key = Nohm.prefix.unique+self.modelName+':'+s+':'+searches[s].toLowerCase();
+ return this.getClient().get([key], returnFunction);
}
- var isNum = !isNaN(parseInt(searches[s], 10));
+ var isNum = ! isNaN(parseInt(searches[s], 10));
if (prop.index && ( ! prop.__numericIndex || isNum) ) {
sets.push(Nohm.prefix.index + self.modelName + ':' + s + ':' + searches[s]);
} else if (prop.__numericIndex) {
@@ -143,25 +149,13 @@ exports.find = function find(searches, callback) {
}
if (sets.length === 0 && zsetKeys.length === 0) {
// no specific searches, retrieve all ids
- this.getClient().smembers(Nohm.prefix.idsets + this.modelName, function (err, ids) {
- if (self.idGenerator === 'increment' && Array.isArray(ids)) {
- ids = ids.map(function (val) {
- return parseInt(val, 10);
- });
- }
- returnFunction(err, ids);
- });
+ this.getClient().smembers(Nohm.prefix.idsets + this.modelName, returnFunction);
} else if (zsetKeys.length === 0) {
getSets(returnFunction);
} else if (sets.length === 0) {
getZSets(returnFunction);
} else {
getSets(function (err, setids) {
- if (self.idGenerator === 'increment' && Array.isArray(setids)) {
- setids = setids.map(function (val) {
- return parseInt(val.toString(), 10);
- });
- }
getZSets(function (err2, zsetids) {
if (err2) {
err = [err, err2];
View
27 test/findTests.js
@@ -1,4 +1,3 @@
-var util = require('util');
var async = require('async');
var nohm = require(__dirname + '/../lib/nohm').Nohm;
var h = require(__dirname + '/helper.js');
@@ -313,7 +312,7 @@ loadArray: function (t) {
}
}, function(err, ids) {
errLogger(err);
- t.same(ids, [users[0].id, users[1].id], 'The found id did not match the id of the saved object.');
+ t.same(ids.sort(), [users[0].id, users[1].id].sort(), 'The found id did not match the id of the saved object.');
t.done();
});
},
@@ -342,7 +341,7 @@ loadArray: function (t) {
email: 'mixedindextest4@hurgel.de',
number: 1,
number2: 33
- }], function (users, ids) {
+ }], function (users, inserted_ids) {
findUser.find({
number: {
@@ -356,6 +355,7 @@ loadArray: function (t) {
if (err) {
console.dir(err);
}
+ console.log(ids.sort());
t.same(ids.sort(), [users[0].id, users[1].id].sort(), 'The found id did not match the id of the saved object.');
t.done();
});
@@ -673,6 +673,27 @@ loadArray: function (t) {
t.same(sorted_ids, ids, 'Sorting went wrong.');
t.done();
});
+ },
+ "all by number DESC": function (t) {
+ t.expect(2);
+
+ var sorted_ids = this.users.sort(function (a, b) {
+ var id_sort = a.id < b.id ? 1 : -1;
+ a = a.p('number');
+ b = b.p('number');
+ return a < b ? 1 : (a > b ? -1 : id_sort);
+ }).map(function (user) {
+ return ''+user.id;
+ });
+
+ UserFindMockup.sort({
+ field: 'number',
+ direction: 'DESC'
+ }, function (err, ids) {
+ t.same(null, err, 'Sorting caused an error: '+err);
+ t.same(sorted_ids, ids, 'Sorting went wrong.');
+ t.done();
+ });
}
/*
"provided and default": function (t) {

0 comments on commit ab30d4c

Please sign in to comment.