mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 12:05:57 +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) {
|
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 tempSetName = 'temp_' + Date.now();
|
||||||
|
|
||||||
|
var params = [tempSetName, start, stop];
|
||||||
|
if (withScores) {
|
||||||
|
params.push('WITHSCORES');
|
||||||
|
}
|
||||||
|
|
||||||
var multi = redisClient.multi();
|
var multi = redisClient.multi();
|
||||||
|
multi.zunionstore([tempSetName, sets.length].concat(sets));
|
||||||
// zunionstore prep
|
multi[method](params);
|
||||||
sets.unshift(sets.length);
|
multi.del(tempSetName);
|
||||||
sets.unshift('temp');
|
|
||||||
|
|
||||||
multi.zunionstore.apply(multi, sets);
|
|
||||||
multi[reverse ? 'zrevrange' : 'zrange']('temp', start, stop);
|
|
||||||
multi.del('temp');
|
|
||||||
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user