Node.js based forum software built for the modern web
sortedSetUnionCard
barisusaklie1263f1057fc
diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.jsindex 4969fd8030..237a33deee 100644--- a/src/database/mongo/sorted.js+++ b/src/database/mongo/sorted.js@@ -474,6 +474,26 @@ module.exports = function(db, module) {});};+ module.sortedSetUnionCard = function(keys, callback) {+ if (!Array.isArray(keys) || !keys.length) {+ return callback(null, 0);+ }++ var pipeline = [+ { $match: { _key: {$in: keys} } },+ { $group: { _id: {value: '$value' } } },+ { $group: { _id: null, count: { $sum: 1 } } }+ ];++ var project = { _id: 0, count: '$count' };+ pipeline.push({ $project: project });++ db.collection('objects').aggregate(pipeline, function(err, data) {+ console.log(data);+ callback(err, Array.isArray(data) && data.length ? data[0].count : 0);+ });+ };+module.getSortedSetUnion = function(params, callback) {params.sort = 1;getSortedSetUnion(params, callback);@@ -484,7 +504,6 @@ module.exports = function(db, module) {getSortedSetUnion(params, callback);};-function getSortedSetUnion(params, callback) {if (!Array.isArray(params.sets) || !params.sets.length) {return callback();@@ -629,7 +648,7 @@ module.exports = function(db, 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;diff --git a/src/database/redis/sorted.js b/src/database/redis/sorted.jsindex 20a3d7b0ff..f39189cf0f 100644--- a/src/database/redis/sorted.js+++ b/src/database/redis/sorted.js@@ -232,6 +232,23 @@ module.exports = function(redisClient, module) {multi.exec(callback);};++ module.sortedSetUnionCard = function(keys, callback) {+ var tempSetName = 'temp_' + Date.now();++ var multi = redisClient.multi();+ multi.zunionstore([tempSetName, keys.length].concat(keys));+ multi.zcard(tempSetName);+ multi.del(tempSetName);+ multi.exec(function(err, results) {+ if (err) {+ return callback(err);+ }++ callback(null, Array.isArray(results) && results.length ? results[1] : 0);+ });+ };+module.getSortedSetUnion = function(params, callback) {params.method = 'zrange';sortedSetUnion(params, callback);diff --git a/tests/database/sorted.js b/tests/database/sorted.jsindex 9f9d72f580..91da2c657c 100644--- a/tests/database/sorted.js+++ b/tests/database/sorted.js@@ -436,6 +436,16 @@ describe('Sorted Set methods', function() {});});+ describe('sortedSetUnionCard', function() {+ it('should return the number of elements in the union', function(done) {+ db.sortedSetUnionCard(['sortedSetTest2', 'sortedSetTest3'], function(err, count) {+ assert.ifError(err);+ assert.equal(count, 3);+ done();+ });+ });+ });+describe('getSortedSetUnion()', function() {it('should return an array of values from both sorted sets sorted by scores lowest to highest', function(done) {db.getSortedSetUnion({sets: ['sortedSetTest2', 'sortedSetTest3'], start: 0, stop: -1}, function(err, values) {