diff --git a/src/categories/topics.js b/src/categories/topics.js index b38a861f91..8ee7105256 100644 --- a/src/categories/topics.js +++ b/src/categories/topics.js @@ -53,18 +53,10 @@ module.exports = function(Categories) { }; Categories.getTopicIds = function(set, reverse, start, stop, callback) { - if (Array.isArray(set)) { - if (reverse) { - db.getSortedSetRevUnion(set, start, stop, callback); - } else { - db.getSortedSetUnion(set, start, stop, callback); - } + if (reverse) { + db.getSortedSetRevRange(set, start, stop, callback); } else { - if (reverse) { - db.getSortedSetRevRange(set, start, stop, callback); - } else { - db.getSortedSetRange(set, start, stop, callback); - } + db.getSortedSetRange(set, start, stop, callback); } }; diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js index b79ab7918a..db1c698e0b 100644 --- a/src/database/mongo/sorted.js +++ b/src/database/mongo/sorted.js @@ -125,6 +125,11 @@ module.exports = function(db, module) { if (withScores) { fields.score = 1; } + + if (Array.isArray(key)) { + key = {$in: key}; + } + db.collection('objects').find({_key: key}, {fields: fields}) .limit(stop - start + 1) .skip(start) @@ -459,6 +464,7 @@ module.exports = function(db, module) { getSortedSetUnion(sets, -1, start, stop, callback); }; + function getSortedSetUnion(sets, sort, start, stop, callback) { if (!Array.isArray(sets) || !sets.length) { return callback(); diff --git a/src/database/redis/sorted.js b/src/database/redis/sorted.js index 8d9b35896d..0341c043f7 100644 --- a/src/database/redis/sorted.js +++ b/src/database/redis/sorted.js @@ -76,29 +76,41 @@ module.exports = function(redisClient, module) { }; 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) { - redisClient.zrevrange(key, start, stop, callback); + sortedSetRange('zrevrange', key, start, stop, false, 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) { - sortedSetRangeWithScores('zrevrange', key, start, stop, callback); + sortedSetRange('zrevrange', key, start, stop, true, callback); }; - function sortedSetRangeWithScores(method, key, start, stop, callback) { - redisClient[method]([key, start, stop, 'WITHSCORES'], function(err, data) { + function sortedSetRange(method, key, start, stop, withScores, callback) { + 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) { return callback(err); } + if (!withScores) { + return callback(null, data); + } var objects = []; for(var i=0; i