mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-17 04:52:51 +01:00
sortedSetIntersectionCard
This commit is contained in:
@@ -637,6 +637,24 @@ module.exports = function(db, module) {
|
||||
};
|
||||
|
||||
|
||||
module.sortedSetIntersectCard = function(keys, callback) {
|
||||
if (!Array.isArray(keys) || !keys.length) {
|
||||
return callback(null, 0);
|
||||
}
|
||||
|
||||
var pipeline = [
|
||||
{ $match: { _key: {$in: keys}} },
|
||||
{ $group: { _id: {value: '$value'}, count: {$sum: 1}} },
|
||||
{ $match: { count: keys.length} },
|
||||
{ $group: { _id: null, count: { $sum: 1 } } },
|
||||
{ $project: { _id: 0, count: '$count' } }
|
||||
];
|
||||
|
||||
db.collection('objects').aggregate(pipeline, function(err, data) {
|
||||
callback(err, Array.isArray(data) && data.length ? data[0].count : 0);
|
||||
});
|
||||
};
|
||||
|
||||
module.getSortedSetIntersect = function(params, callback) {
|
||||
params.sort = 1;
|
||||
getSortedSetRevIntersect(params, callback);
|
||||
|
||||
@@ -93,7 +93,7 @@ module.exports = function(redisClient, module) {
|
||||
|
||||
function sortedSetRange(method, key, start, stop, withScores, callback) {
|
||||
if (Array.isArray(key)) {
|
||||
return sortedSetUnion(method, key, start, stop, withScores, callback);
|
||||
return sortedSetUnion({method: method, sets: key, start: start, stop: stop, withScores: withScores}, callback);
|
||||
}
|
||||
|
||||
var params = [key, start, stop];
|
||||
@@ -265,7 +265,7 @@ module.exports = function(redisClient, module) {
|
||||
|
||||
var rangeParams = [tempSetName, params.start, params.stop];
|
||||
if (params.withScores) {
|
||||
params.push('WITHSCORES');
|
||||
rangeParams.push('WITHSCORES');
|
||||
}
|
||||
|
||||
var multi = redisClient.multi();
|
||||
@@ -302,6 +302,27 @@ module.exports = function(redisClient, module) {
|
||||
redisClient.zrangebylex([key, min, max, 'LIMIT', start, count], callback);
|
||||
};
|
||||
|
||||
module.sortedSetIntersectCard = function(keys, callback) {
|
||||
if (!Array.isArray(keys) || !keys.length) {
|
||||
return callback(null, 0);
|
||||
}
|
||||
var tempSetName = 'temp_' + Date.now();
|
||||
|
||||
var interParams = [tempSetName, keys.length].concat(keys);
|
||||
|
||||
var multi = redisClient.multi();
|
||||
multi.zinterstore(interParams);
|
||||
multi.zcard(tempSetName);
|
||||
multi.del(tempSetName);
|
||||
multi.exec(function(err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
callback(null, results[1] || 0);
|
||||
});
|
||||
};
|
||||
|
||||
module.getSortedSetIntersect = function(params, callback) {
|
||||
params.method = 'zrange';
|
||||
getSortedSetRevIntersect(params, callback);
|
||||
@@ -312,7 +333,7 @@ module.exports = function(redisClient, module) {
|
||||
getSortedSetRevIntersect(params, callback);
|
||||
};
|
||||
|
||||
function getSortedSetRevIntersect (params, callback) {
|
||||
function getSortedSetRevIntersect(params, callback) {
|
||||
var sets = params.sets;
|
||||
var start = params.hasOwnProperty('start') ? params.start : 0;
|
||||
var stop = params.hasOwnProperty('stop') ? params.stop : -1;
|
||||
|
||||
Reference in New Issue
Block a user