From 6022fd984a4169a2c259facc4645d2af9da683ee Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 15 Aug 2016 19:23:10 +0300 Subject: [PATCH] closes #4945 --- src/database/mongo/sorted.js | 80 ++++++++++++++++++++++++++ src/database/redis/sorted.js | 63 +++++++++++++++++++-- tests/database/sorted.js | 105 +++++++++++++++++++++++++++++++++++ 3 files changed, 243 insertions(+), 5 deletions(-) diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js index 9e9f72969e..a262ff9984 100644 --- a/src/database/mongo/sorted.js +++ b/src/database/mongo/sorted.js @@ -595,4 +595,84 @@ module.exports = function(db, module) { callback ); }; + + + module.getSortedSetIntersect = function(params, callback) { + params.sort = 1; + getSortedSetRevIntersect(params, callback); + }; + + module.getSortedSetRevIntersect = function(params, callback) { + params.sort = -1; + 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; + var weights = params.weights || []; + var aggregate = {}; + + if (params.aggregate) { + aggregate['$' + params.aggregate.toLowerCase()] = '$score'; + } else { + aggregate.$sum = '$score'; + } + + var limit = stop - start + 1; + if (limit <= 0) { + limit = 0; + } + + var pipeline = []; + + pipeline.push({ $match: { _key: {$in: sets}} }); + + weights.forEach(function(weight, index) { + if (weight !== 1) { + pipeline.push({ + $project: { + value: 1, + score: { + $cond: { if: { $eq: [ "$_key", sets[index] ] }, then: { $multiply: [ '$score', weight ] }, else: '$score' } + } + } + }); + } + }); + + pipeline.push({ $group: { _id: {value: '$value'}, totalScore: aggregate, count: {$sum: 1}} }); + pipeline.push({ $match: { count: sets.length} }); + pipeline.push({ $sort: { totalScore: params.sort} }); + + if (start) { + pipeline.push({ $skip: start }); + } + + if (limit > 0) { + pipeline.push({ $limit: limit }); + } + + var project = { _id: 0, value: '$_id.value'}; + if (params.withScores) { + project.score = '$totalScore'; + } + pipeline.push({ $project: project }); + + db.collection('objects').aggregate(pipeline, function(err, data) { + if (err || !data) { + return callback(err); + } + + if (!params.withScores) { + data = data.map(function(item) { + return item.value; + }); + } + + callback(null, data); + }); + } + }; diff --git a/src/database/redis/sorted.js b/src/database/redis/sorted.js index cd1587f494..d97b6b60cc 100644 --- a/src/database/redis/sorted.js +++ b/src/database/redis/sorted.js @@ -29,7 +29,7 @@ module.exports = function(redisClient, module) { args.push(scores[i], values[i]); } - redisClient.zadd(args, function(err, res) { + redisClient.zadd(args, function(err) { callback(err); }); } @@ -42,7 +42,7 @@ module.exports = function(redisClient, module) { multi.zadd(keys[i], score, value); } - multi.exec(function(err, res) { + multi.exec(function(err) { callback(err); }); }; @@ -53,13 +53,13 @@ module.exports = function(redisClient, module) { value = [value]; } - helpers.multiKeyValues(redisClient, 'zrem', key, value, function(err, result) { + helpers.multiKeyValues(redisClient, 'zrem', key, value, function(err) { callback(err); }); }; module.sortedSetsRemove = function(keys, value, callback) { - helpers.multiKeysValue(redisClient, 'zrem', keys, value, function(err, result) { + helpers.multiKeysValue(redisClient, 'zrem', keys, value, function(err) { callback(err); }); }; @@ -70,7 +70,7 @@ module.exports = function(redisClient, module) { for(var i=0; i