Permalink
Browse files

Refactor sort a bit

  • Loading branch information...
1 parent 54d24ec commit 640191c2371e5e26f5c1707ab5b25d01034c1ff9 @maritz committed Jan 18, 2012
Showing with 17 additions and 17 deletions.
  1. +17 −17 lib/retrieve.js
View
@@ -202,40 +202,40 @@ exports.sort = function (options, ids) {
}
var idset_key = Nohm.prefix.idsets+this.modelName;
var zset_key = Nohm.prefix.scoredindex+this.modelName+':'+options.field;
+ var client = this.getClient();
+ var tmp_key;
if (ids) {
// to get the intersection of the given ids and all ids on the server we first
// temporarily store the given ids either in a set or sorted set and then return the intersection
- var multi = this.getClient().multi();
- var tmp_key;
+ client = client.multi();
if (scored) {
tmp_key = zset_key+':tmp_sort:'+(+ new Date()) + Math.ceil(Math.random()*1000);
var tmp_zadd_args = [tmp_key];
ids.forEach(function (id) {
tmp_zadd_args.push(0, id);
});
- multi.zadd(tmp_zadd_args);
- multi.zinterstore([tmp_key, 2, tmp_key, zset_key]);
- sortScored.call(this, multi, tmp_key, direction, start, stop, callback);
+ client.zadd(tmp_zadd_args);
+ client.zinterstore([tmp_key, 2, tmp_key, zset_key]);
+ zset_key = tmp_key;
} else {
tmp_key = idset_key+':tmp_sort:'+(+ new Date()) + Math.ceil(Math.random()*1000);
ids.unshift(tmp_key);
- multi.SADD(ids);
- multi.SINTERSTORE([tmp_key, tmp_key, idset_key]);
- sortNormal.call(this, multi, tmp_key, options.field, alpha, direction, start, stop, callback);
+ client.SADD(ids);
+ client.SINTERSTORE([tmp_key, tmp_key, idset_key]);
+ idset_key = tmp_key;
}
- multi.del(tmp_key);
- multi.exec(Nohm.logError);
-
+ }
+ if (scored) {
+ sortScored.call(this, client, zset_key, direction, start, stop, callback);
} else {
- // no ids provided
- if (scored) {
- sortScored.call(this, this.getClient(), zset_key, direction, start, stop, callback);
- } else {
- sortNormal.call(this, this.getClient(), idset_key, options.field, alpha, direction, start, stop, callback);
- }
+ sortNormal.call(this, client, idset_key, options.field, alpha, direction, start, stop, callback);
+ }
+ if (ids) {
+ client.del(tmp_key);
+ client.exec(Nohm.logError);
}
};

0 comments on commit 640191c

Please sign in to comment.