mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-17 18:10:42 +01:00
[database/mongo] Improve speed of sortedSetRank (#6229)
* [database/mongo] Improve speed of sortedSetRank * [database/mongo] Fix sortedSetRank to filter by _key
This commit is contained in:
committed by
Barış Soner Uşaklı
parent
c47987b305
commit
5b1ed21634
@@ -203,25 +203,36 @@ module.exports = function (db, module) {
|
||||
};
|
||||
|
||||
module.sortedSetRank = function (key, value, callback) {
|
||||
getSortedSetRank(module.getSortedSetRange, key, value, callback);
|
||||
getSortedSetRank(false, key, value, callback);
|
||||
};
|
||||
|
||||
module.sortedSetRevRank = function (key, value, callback) {
|
||||
getSortedSetRank(module.getSortedSetRevRange, key, value, callback);
|
||||
getSortedSetRank(true, key, value, callback);
|
||||
};
|
||||
|
||||
function getSortedSetRank(method, key, value, callback) {
|
||||
function getSortedSetRank(reverse, key, value, callback) {
|
||||
if (!key) {
|
||||
return callback();
|
||||
}
|
||||
value = helpers.valueToString(value);
|
||||
method(key, 0, -1, function (err, result) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
module.sortedSetScore(key, value, function (err, score) {
|
||||
if (err || score === null) {
|
||||
return callback(err, null);
|
||||
}
|
||||
|
||||
var rank = result.indexOf(value);
|
||||
callback(null, rank !== -1 ? rank : null);
|
||||
db.collection('objects').count({
|
||||
$or: [
|
||||
{
|
||||
_key: key,
|
||||
score: reverse ? { $gt: score } : { $lt: score },
|
||||
},
|
||||
{
|
||||
_key: key,
|
||||
score: score,
|
||||
value: reverse ? { $gt: value } : { $lt: value },
|
||||
},
|
||||
],
|
||||
}, function (err, rank) { callback(err, rank); });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -235,7 +246,7 @@ module.exports = function (db, module) {
|
||||
}
|
||||
|
||||
async.map(data, function (item, next) {
|
||||
getSortedSetRank(module.getSortedSetRange, item.key, item.value, next);
|
||||
getSortedSetRank(false, item.key, item.value, next);
|
||||
}, callback);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user