mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
closes #4307
ability to send an array of keys to getSortedSetRange use getSortedSetRevRange instead of getSortedSetRevUnion
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<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);
|
||||
});
|
||||
@@ -221,25 +233,39 @@ module.exports = function(redisClient, module) {
|
||||
};
|
||||
|
||||
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) {
|
||||
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 tempSetName = 'temp_' + Date.now();
|
||||
|
||||
var params = [tempSetName, start, stop];
|
||||
if (withScores) {
|
||||
params.push('WITHSCORES');
|
||||
}
|
||||
|
||||
var multi = redisClient.multi();
|
||||
|
||||
// zunionstore prep
|
||||
sets.unshift(sets.length);
|
||||
sets.unshift('temp');
|
||||
|
||||
multi.zunionstore.apply(multi, sets);
|
||||
multi[reverse ? 'zrevrange' : 'zrange']('temp', start, stop);
|
||||
multi.del('temp');
|
||||
multi.zunionstore([tempSetName, sets.length].concat(sets));
|
||||
multi[method](params);
|
||||
multi.del(tempSetName);
|
||||
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) {
|
||||
return 'uid:' + uid + ':posts';
|
||||
});
|
||||
db.getSortedSetRevUnion(keys, 0, max - 1, next);
|
||||
db.getSortedSetRevRange(keys, 0, max - 1, next);
|
||||
},
|
||||
function(pids, next) {
|
||||
privileges.posts.filter('read', pids, uid, next);
|
||||
|
||||
Reference in New Issue
Block a user