ability to send an array of keys to getSortedSetRange
use getSortedSetRevRange instead of getSortedSetRevUnion
v1.18.x
barisusakli 9 years ago
parent b2b104dab3
commit 018f7ba65a

@ -53,18 +53,10 @@ module.exports = function(Categories) {
}; };
Categories.getTopicIds = function(set, reverse, start, stop, callback) { Categories.getTopicIds = function(set, reverse, start, stop, callback) {
if (Array.isArray(set)) { if (reverse) {
if (reverse) { db.getSortedSetRevRange(set, start, stop, callback);
db.getSortedSetRevUnion(set, start, stop, callback);
} else {
db.getSortedSetUnion(set, start, stop, callback);
}
} else { } else {
if (reverse) { db.getSortedSetRange(set, start, stop, callback);
db.getSortedSetRevRange(set, start, stop, callback);
} else {
db.getSortedSetRange(set, start, stop, callback);
}
} }
}; };

@ -125,6 +125,11 @@ module.exports = function(db, module) {
if (withScores) { if (withScores) {
fields.score = 1; fields.score = 1;
} }
if (Array.isArray(key)) {
key = {$in: key};
}
db.collection('objects').find({_key: key}, {fields: fields}) db.collection('objects').find({_key: key}, {fields: fields})
.limit(stop - start + 1) .limit(stop - start + 1)
.skip(start) .skip(start)
@ -459,6 +464,7 @@ module.exports = function(db, module) {
getSortedSetUnion(sets, -1, start, stop, callback); getSortedSetUnion(sets, -1, start, stop, callback);
}; };
function getSortedSetUnion(sets, sort, start, stop, callback) { function getSortedSetUnion(sets, sort, start, stop, callback) {
if (!Array.isArray(sets) || !sets.length) { if (!Array.isArray(sets) || !sets.length) {
return callback(); return callback();

@ -76,29 +76,41 @@ module.exports = function(redisClient, module) {
}; };
module.getSortedSetRange = function(key, start, stop, callback) { module.getSortedSetRange = function(key, start, stop, callback) {
redisClient.zrange(key, start, stop, callback); sortedSetRange('zrange', key, start, stop, false, callback);
}; };
module.getSortedSetRevRange = function(key, start, stop, callback) { module.getSortedSetRevRange = function(key, start, stop, callback) {
redisClient.zrevrange(key, start, stop, callback); sortedSetRange('zrevrange', key, start, stop, false, callback);
}; };
module.getSortedSetRangeWithScores = function(key, start, stop, callback) { module.getSortedSetRangeWithScores = function(key, start, stop, callback) {
sortedSetRangeWithScores('zrange', key, start, stop, callback); sortedSetRange('zrange', key, start, stop, true, callback);
}; };
module.getSortedSetRevRangeWithScores = function(key, start, stop, callback) { module.getSortedSetRevRangeWithScores = function(key, start, stop, callback) {
sortedSetRangeWithScores('zrevrange', key, start, stop, callback); sortedSetRange('zrevrange', key, start, stop, true, callback);
}; };
function sortedSetRangeWithScores(method, key, start, stop, callback) { function sortedSetRange(method, key, start, stop, withScores, callback) {
redisClient[method]([key, start, stop, 'WITHSCORES'], function(err, data) { if (Array.isArray(key)) {
return sortedSetUnion(method, key, start, stop, withScores, callback);
}
var params = [key, start, stop];
if (withScores) {
params.push('WITHSCORES');
}
redisClient[method](params, function(err, data) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
if (!withScores) {
return callback(null, data);
}
var objects = []; var objects = [];
for(var i=0; i<data.length; i+=2) { for(var i=0; i<data.length; i+=2) {
objects.push({value: data[i], score: data[i+1]}); objects.push({value: data[i], score: data[i + 1]});
} }
callback(null, objects); callback(null, objects);
}); });
@ -221,25 +233,39 @@ module.exports = function(redisClient, module) {
}; };
module.getSortedSetUnion = function(sets, start, stop, callback) { module.getSortedSetUnion = function(sets, start, stop, callback) {
sortedSetUnion(sets, false, start, stop, callback); sortedSetUnion('zrange', sets, start, stop, false, callback);
}; };
module.getSortedSetRevUnion = function(sets, start, stop, callback) { module.getSortedSetRevUnion = function(sets, start, stop, callback) {
sortedSetUnion(sets, true, start, stop, callback); sortedSetUnion('zrevrange', sets, start, stop, false, callback);
}; };
function sortedSetUnion(sets, reverse, start, stop, callback) { function sortedSetUnion(method, sets, start, stop, withScores, callback) {
var multi = redisClient.multi();
// zunionstore prep var tempSetName = 'temp_' + Date.now();
sets.unshift(sets.length);
sets.unshift('temp'); var params = [tempSetName, start, stop];
if (withScores) {
params.push('WITHSCORES');
}
multi.zunionstore.apply(multi, sets); var multi = redisClient.multi();
multi[reverse ? 'zrevrange' : 'zrange']('temp', start, stop); multi.zunionstore([tempSetName, sets.length].concat(sets));
multi.del('temp'); multi[method](params);
multi.del(tempSetName);
multi.exec(function(err, results) { multi.exec(function(err, results) {
callback(err, results ? results[1] : null); if (err) {
return callback(err);
}
if (!withScores) {
return callback(null, results ? results[1] : null);
}
results = results[1] || [];
var objects = [];
for(var i=0; i<results.length; i+=2) {
objects.push({value: results[i], score: results[i + 1]});
}
callback(null, objects);
}); });
} }

@ -357,7 +357,7 @@ var utils = require('../public/src/utils');
var keys = uids.map(function(uid) { var keys = uids.map(function(uid) {
return 'uid:' + uid + ':posts'; return 'uid:' + uid + ':posts';
}); });
db.getSortedSetRevUnion(keys, 0, max - 1, next); db.getSortedSetRevRange(keys, 0, max - 1, next);
}, },
function(pids, next) { function(pids, next) {
privileges.posts.filter('read', pids, uid, next); privileges.posts.filter('read', pids, uid, next);

Loading…
Cancel
Save