2014-04-11 15:44:53 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
2014-07-29 17:33:28 -04:00
|
|
|
var async = require('async');
|
2016-08-16 19:52:22 +03:00
|
|
|
var utils = require('../../../public/src/utils');
|
2014-07-29 17:33:28 -04:00
|
|
|
|
2014-04-11 15:44:53 -04:00
|
|
|
module.exports = function(db, module) {
|
|
|
|
|
var helpers = module.helpers.mongo;
|
|
|
|
|
|
|
|
|
|
module.sortedSetAdd = function(key, score, value, callback) {
|
2014-09-03 01:13:28 -04:00
|
|
|
callback = callback || helpers.noop;
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-09-08 23:03:37 -04:00
|
|
|
if (Array.isArray(score) && Array.isArray(value)) {
|
|
|
|
|
return sortedSetAddBulk(key, score, value, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-11 15:44:53 -04:00
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
|
2014-10-10 19:21:43 -04:00
|
|
|
db.collection('objects').update({_key: key, value: value}, {$set: {score: parseInt(score, 10)}}, {upsert:true, w: 1}, function(err) {
|
2014-09-03 20:19:51 -04:00
|
|
|
callback(err);
|
|
|
|
|
});
|
2014-04-11 15:44:53 -04:00
|
|
|
};
|
|
|
|
|
|
2014-09-08 23:03:37 -04:00
|
|
|
function sortedSetAddBulk(key, scores, values, callback) {
|
2014-10-03 14:14:41 -04:00
|
|
|
if (!scores.length || !values.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-09-08 23:03:37 -04:00
|
|
|
if (scores.length !== values.length) {
|
|
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
values = values.map(helpers.valueToString);
|
|
|
|
|
|
|
|
|
|
var bulk = db.collection('objects').initializeUnorderedBulkOp();
|
|
|
|
|
|
|
|
|
|
for(var i=0; i<scores.length; ++i) {
|
2014-10-14 20:56:52 -04:00
|
|
|
bulk.find({_key: key, value: values[i]}).upsert().updateOne({$set: {score: parseInt(scores[i], 10)}});
|
2014-09-08 23:03:37 -04:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 11:09:06 +02:00
|
|
|
bulk.execute(function(err) {
|
2014-09-08 23:03:37 -04:00
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-08 16:13:48 -04:00
|
|
|
module.sortedSetsAdd = function(keys, score, value, callback) {
|
|
|
|
|
callback = callback || helpers.noop;
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-09-08 16:13:48 -04:00
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
|
|
|
|
|
var bulk = db.collection('objects').initializeUnorderedBulkOp();
|
|
|
|
|
|
|
|
|
|
for(var i=0; i<keys.length; ++i) {
|
2014-10-10 19:21:43 -04:00
|
|
|
bulk.find({_key: keys[i], value: value}).upsert().updateOne({$set: {score: parseInt(score, 10)}});
|
2014-09-08 16:13:48 -04:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 11:09:06 +02:00
|
|
|
bulk.execute(function(err) {
|
2014-09-08 16:13:48 -04:00
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-11 15:44:53 -04:00
|
|
|
module.sortedSetRemove = function(key, value, callback) {
|
2014-10-15 21:55:31 -04:00
|
|
|
function done(err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
}
|
2014-09-03 01:13:28 -04:00
|
|
|
callback = callback || helpers.noop;
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-04-11 15:44:53 -04:00
|
|
|
|
2014-10-15 21:55:31 -04:00
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
value = value.map(helpers.valueToString);
|
|
|
|
|
db.collection('objects').remove({_key: key, value: {$in: value}}, done);
|
|
|
|
|
} else {
|
|
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
db.collection('objects').remove({_key: key, value: value}, done);
|
|
|
|
|
}
|
2014-06-21 22:37:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.sortedSetsRemove = function(keys, value, callback) {
|
2014-09-03 01:13:28 -04:00
|
|
|
callback = callback || helpers.noop;
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-06-21 22:37:46 -04:00
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
|
2016-03-15 11:09:06 +02:00
|
|
|
db.collection('objects').remove({_key: {$in: keys}, value: value}, function(err) {
|
2014-12-20 17:26:39 -05:00
|
|
|
callback(err);
|
|
|
|
|
});
|
2014-04-11 15:44:53 -04:00
|
|
|
};
|
|
|
|
|
|
2014-09-10 20:51:16 -04:00
|
|
|
module.sortedSetsRemoveRangeByScore = function(keys, min, max, callback) {
|
|
|
|
|
callback = callback || helpers.noop;
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2016-03-15 11:09:06 +02:00
|
|
|
var query = {_key: {$in: keys}};
|
2016-03-11 14:20:23 +02:00
|
|
|
|
|
|
|
|
if (min !== '-inf') {
|
2016-03-15 11:09:06 +02:00
|
|
|
query.score = {$gte: min};
|
2016-03-11 14:20:23 +02:00
|
|
|
}
|
|
|
|
|
if (max !== '+inf') {
|
2016-03-15 11:09:06 +02:00
|
|
|
query.score = query.score || {};
|
|
|
|
|
query.score.$lte = max;
|
2016-03-11 14:20:23 +02:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 11:09:06 +02:00
|
|
|
db.collection('objects').remove(query, function(err) {
|
2014-09-10 20:51:16 -04:00
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
2014-09-08 16:13:48 -04:00
|
|
|
|
2014-12-31 14:41:58 -05:00
|
|
|
module.getSortedSetRange = function(key, start, stop, callback) {
|
|
|
|
|
getSortedSetRange(key, start, stop, 1, false, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.getSortedSetRevRange = function(key, start, stop, callback) {
|
|
|
|
|
getSortedSetRange(key, start, stop, -1, false, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.getSortedSetRangeWithScores = function(key, start, stop, callback) {
|
|
|
|
|
getSortedSetRange(key, start, stop, 1, true, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.getSortedSetRevRangeWithScores = function(key, start, stop, callback) {
|
|
|
|
|
getSortedSetRange(key, start, stop, -1, true, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2014-05-22 13:06:19 -04:00
|
|
|
function getSortedSetRange(key, start, stop, sort, withScores, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-09-27 17:46:39 -04:00
|
|
|
|
|
|
|
|
var fields = {_id: 0, value: 1};
|
|
|
|
|
if (withScores) {
|
2014-10-08 12:22:39 -04:00
|
|
|
fields.score = 1;
|
2014-09-27 17:46:39 -04:00
|
|
|
}
|
2016-03-03 19:52:48 +02:00
|
|
|
|
|
|
|
|
if (Array.isArray(key)) {
|
|
|
|
|
key = {$in: key};
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-31 21:22:28 +03:00
|
|
|
var limit = stop - start + 1;
|
|
|
|
|
if (limit <= 0) {
|
|
|
|
|
limit = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-18 18:30:17 -04:00
|
|
|
db.collection('objects').find({_key: key}, {fields: fields})
|
2016-08-31 21:22:28 +03:00
|
|
|
.limit(limit)
|
2014-04-11 15:44:53 -04:00
|
|
|
.skip(start)
|
|
|
|
|
.sort({score: sort})
|
|
|
|
|
.toArray(function(err, data) {
|
|
|
|
|
if (err || !data) {
|
2014-05-23 14:30:59 -04:00
|
|
|
return callback(err);
|
2014-04-11 15:44:53 -04:00
|
|
|
}
|
|
|
|
|
|
2014-05-22 13:06:19 -04:00
|
|
|
if (!withScores) {
|
|
|
|
|
data = data.map(function(item) {
|
|
|
|
|
return item.value;
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-04-11 15:44:53 -04:00
|
|
|
|
|
|
|
|
callback(null, data);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.getSortedSetRangeByScore = function(key, start, count, min, max, callback) {
|
2014-09-27 17:41:49 -04:00
|
|
|
getSortedSetRangeByScore(key, start, count, min, max, 1, false, callback);
|
2014-04-11 15:44:53 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.getSortedSetRevRangeByScore = function(key, start, count, max, min, callback) {
|
2014-09-27 17:41:49 -04:00
|
|
|
getSortedSetRangeByScore(key, start, count, min, max, -1, false, callback);
|
2014-04-11 15:44:53 -04:00
|
|
|
};
|
|
|
|
|
|
2014-10-31 17:19:50 -04:00
|
|
|
module.getSortedSetRangeByScoreWithScores = function(key, start, count, min, max, callback) {
|
2014-12-31 15:59:57 -05:00
|
|
|
getSortedSetRangeByScore(key, start, count, min, max, 1, true, callback);
|
2014-10-31 17:19:50 -04:00
|
|
|
};
|
|
|
|
|
|
2014-09-27 17:41:49 -04:00
|
|
|
module.getSortedSetRevRangeByScoreWithScores = function(key, start, count, max, min, callback) {
|
|
|
|
|
getSortedSetRangeByScore(key, start, count, min, max, -1, true, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getSortedSetRangeByScore(key, start, count, min, max, sort, withScores, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-04-11 15:44:53 -04:00
|
|
|
if(parseInt(count, 10) === -1) {
|
|
|
|
|
count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-15 11:09:06 +02:00
|
|
|
var query = {_key: key};
|
|
|
|
|
|
2014-11-07 19:40:41 -05:00
|
|
|
if (min !== '-inf') {
|
2016-03-15 11:09:06 +02:00
|
|
|
query.score = {$gte: min};
|
2014-09-01 20:45:24 -04:00
|
|
|
}
|
2014-11-07 19:40:41 -05:00
|
|
|
if (max !== '+inf') {
|
2016-03-15 11:09:06 +02:00
|
|
|
query.score = query.score || {};
|
|
|
|
|
query.score.$lte = max;
|
2014-09-01 20:45:24 -04:00
|
|
|
}
|
|
|
|
|
|
2014-09-27 17:46:39 -04:00
|
|
|
var fields = {_id: 0, value: 1};
|
2014-09-27 17:41:49 -04:00
|
|
|
if (withScores) {
|
2014-10-08 12:22:39 -04:00
|
|
|
fields.score = 1;
|
2014-09-27 17:41:49 -04:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 11:09:06 +02:00
|
|
|
db.collection('objects').find(query, {fields: fields})
|
2014-04-11 15:44:53 -04:00
|
|
|
.limit(count)
|
|
|
|
|
.skip(start)
|
|
|
|
|
.sort({score: sort})
|
|
|
|
|
.toArray(function(err, data) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-27 17:41:49 -04:00
|
|
|
if (!withScores) {
|
|
|
|
|
data = data.map(function(item) {
|
|
|
|
|
return item.value;
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-04-11 15:44:53 -04:00
|
|
|
|
|
|
|
|
callback(err, data);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.sortedSetCount = function(key, min, max, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2015-03-06 15:12:55 -05:00
|
|
|
|
|
|
|
|
var query = {_key: key};
|
2014-12-31 16:09:33 -05:00
|
|
|
if (min !== '-inf') {
|
2015-03-06 15:12:55 -05:00
|
|
|
query.score = {$gte: min};
|
2014-12-31 16:09:33 -05:00
|
|
|
}
|
|
|
|
|
if (max !== '+inf') {
|
2015-03-06 15:12:55 -05:00
|
|
|
query.score = query.score || {};
|
|
|
|
|
query.score.$lte = max;
|
2014-12-31 16:09:33 -05:00
|
|
|
}
|
2015-03-06 15:12:55 -05:00
|
|
|
|
|
|
|
|
db.collection('objects').count(query, function(err, count) {
|
2014-04-11 15:44:53 -04:00
|
|
|
callback(err, count ? count : 0);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.sortedSetCard = function(key, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
2014-10-03 13:43:51 -04:00
|
|
|
return callback(null, 0);
|
2014-09-21 13:30:20 -04:00
|
|
|
}
|
|
|
|
|
db.collection('objects').count({_key: key}, function(err, count) {
|
2014-09-06 04:11:44 -04:00
|
|
|
count = parseInt(count, 10);
|
2014-04-11 15:44:53 -04:00
|
|
|
callback(err, count ? count : 0);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-14 21:12:12 -04:00
|
|
|
module.sortedSetsCard = function(keys, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-09-12 16:35:30 -04:00
|
|
|
var pipeline = [
|
|
|
|
|
{ $match : { _key : { $in: keys } } } ,
|
|
|
|
|
{ $group: { _id: {_key: '$_key'}, count: { $sum: 1 } } },
|
|
|
|
|
{ $project: { _id: 1, count: '$count' } }
|
|
|
|
|
];
|
|
|
|
|
db.collection('objects').aggregate(pipeline, function(err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(results)) {
|
|
|
|
|
results = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var map = {};
|
|
|
|
|
results.forEach(function(item) {
|
|
|
|
|
if (item && item._id._key) {
|
|
|
|
|
map[item._id._key] = item.count;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
results = keys.map(function(key) {
|
|
|
|
|
return map[key] || 0;
|
|
|
|
|
});
|
|
|
|
|
callback(null, results);
|
|
|
|
|
});
|
2014-08-14 21:12:12 -04:00
|
|
|
};
|
|
|
|
|
|
2014-04-11 15:44:53 -04:00
|
|
|
module.sortedSetRank = function(key, value, callback) {
|
|
|
|
|
getSortedSetRank(module.getSortedSetRange, key, value, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.sortedSetRevRank = function(key, value, callback) {
|
|
|
|
|
getSortedSetRank(module.getSortedSetRevRange, key, value, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getSortedSetRank(method, key, value, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-04-11 15:44:53 -04:00
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
method(key, 0, -1, function(err, result) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var rank = result.indexOf(value);
|
|
|
|
|
callback(null, rank !== -1 ? rank : null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 17:33:28 -04:00
|
|
|
module.sortedSetsRanks = function(keys, values, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
2014-07-29 17:33:28 -04:00
|
|
|
var data = new Array(values.length);
|
|
|
|
|
for (var i=0; i<values.length; ++i) {
|
|
|
|
|
data[i] = {key: keys[i], value: values[i]};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.map(data, function(item, next) {
|
|
|
|
|
getSortedSetRank(module.getSortedSetRange, item.key, item.value, next);
|
|
|
|
|
}, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-26 22:19:26 -04:00
|
|
|
module.sortedSetRanks = function(key, values, callback) {
|
|
|
|
|
module.getSortedSetRange(key, 0, -1, function(err, sortedSet) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = values.map(function(value) {
|
2014-10-02 18:57:15 -04:00
|
|
|
if (!value) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-09-26 22:19:26 -04:00
|
|
|
var index = sortedSet.indexOf(value.toString());
|
|
|
|
|
return index !== -1 ? index : null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
callback(null, result);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-11 15:44:53 -04:00
|
|
|
module.sortedSetScore = function(key, value, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-04-11 15:44:53 -04:00
|
|
|
value = helpers.valueToString(value);
|
2015-11-05 15:10:19 -05:00
|
|
|
db.collection('objects').findOne({_key: key, value: value}, {fields:{_id: 0, score: 1}}, function(err, result) {
|
2014-04-11 15:44:53 -04:00
|
|
|
callback(err, result ? result.score : null);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-15 11:48:01 -04:00
|
|
|
module.sortedSetsScore = function(keys, value, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-08-15 11:48:01 -04:00
|
|
|
value = helpers.valueToString(value);
|
2014-10-12 23:46:58 -04:00
|
|
|
db.collection('objects').find({_key:{$in:keys}, value: value}, {_id:0, _key:1, score: 1}).toArray(function(err, result) {
|
2014-08-15 11:48:01 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var map = helpers.toMap(result),
|
|
|
|
|
returnData = [],
|
|
|
|
|
item;
|
|
|
|
|
|
|
|
|
|
for(var i=0; i<keys.length; ++i) {
|
|
|
|
|
item = map[keys[i]];
|
|
|
|
|
returnData.push(item ? item.score : null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, returnData);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.sortedSetScores = function(key, values, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-08-15 11:48:01 -04:00
|
|
|
values = values.map(helpers.valueToString);
|
2014-10-12 23:46:58 -04:00
|
|
|
db.collection('objects').find({_key: key, value: {$in: values}}, {_id: 0, value: 1, score: 1}).toArray(function(err, result) {
|
2014-08-15 11:48:01 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var map = {};
|
|
|
|
|
result.forEach(function(item) {
|
|
|
|
|
map[item.value] = item.score;
|
|
|
|
|
});
|
|
|
|
|
|
2016-08-16 19:52:22 +03:00
|
|
|
var returnData = new Array(values.length);
|
|
|
|
|
var score;
|
2014-08-15 11:48:01 -04:00
|
|
|
|
|
|
|
|
for(var i=0; i<values.length; ++i) {
|
|
|
|
|
score = map[values[i]];
|
2016-08-16 19:52:22 +03:00
|
|
|
returnData[i] = utils.isNumber(score) ? score : null;
|
2014-08-15 11:48:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, returnData);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-11 15:44:53 -04:00
|
|
|
module.isSortedSetMember = function(key, value, callback) {
|
2015-04-20 23:26:02 -04:00
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
db.collection('objects').findOne({_key: key, value: value}, {_id: 0, value: 1}, function(err, result) {
|
|
|
|
|
callback(err, !!result);
|
2014-04-11 15:44:53 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-22 12:56:34 -04:00
|
|
|
module.isSortedSetMembers = function(key, values, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-07-22 12:56:34 -04:00
|
|
|
values = values.map(helpers.valueToString);
|
2014-10-12 23:46:58 -04:00
|
|
|
db.collection('objects').find({_key: key, value: {$in: values}}, {fields: {_id: 0, value: 1}}).toArray(function(err, results) {
|
2014-07-22 13:00:04 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2014-08-12 14:39:58 -04:00
|
|
|
|
2014-07-22 12:56:34 -04:00
|
|
|
results = results.map(function(item) {
|
|
|
|
|
return item.value;
|
|
|
|
|
});
|
2014-08-12 14:39:58 -04:00
|
|
|
|
2014-07-22 12:56:34 -04:00
|
|
|
values = values.map(function(value) {
|
|
|
|
|
return results.indexOf(value) !== -1;
|
|
|
|
|
});
|
2014-08-12 14:39:58 -04:00
|
|
|
callback(null, values);
|
2014-07-22 12:56:34 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-01-21 22:48:22 -05:00
|
|
|
module.isMemberOfSortedSets = function(keys, value, callback) {
|
|
|
|
|
if (!Array.isArray(keys)) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
db.collection('objects').find({_key: {$in: keys}, value: value}, {fields: {_id: 0, _key: 1, value: 1}}).toArray(function(err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results = results.map(function(item) {
|
|
|
|
|
return item._key;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
results = keys.map(function(key) {
|
|
|
|
|
return results.indexOf(key) !== -1;
|
|
|
|
|
});
|
|
|
|
|
callback(null, results);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.getSortedSetsMembers = function(keys, callback) {
|
|
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
db.collection('objects').find({_key: {$in: keys}}, {_id: 0, _key: 1, value: 1}).toArray(function(err, data) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sets = {};
|
|
|
|
|
data.forEach(function(set) {
|
|
|
|
|
sets[set._key] = sets[set._key] || [];
|
|
|
|
|
sets[set._key].push(set.value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var returnData = new Array(keys.length);
|
|
|
|
|
for(var i=0; i<keys.length; ++i) {
|
|
|
|
|
returnData[i] = sets[keys[i]] || [];
|
|
|
|
|
}
|
|
|
|
|
callback(null, returnData);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-08 17:59:59 +03:00
|
|
|
module.getSortedSetUnion = function(params, callback) {
|
|
|
|
|
params.sort = 1;
|
|
|
|
|
getSortedSetUnion(params, callback);
|
2014-05-23 14:30:59 -04:00
|
|
|
};
|
|
|
|
|
|
2016-09-08 17:59:59 +03:00
|
|
|
module.getSortedSetRevUnion = function(params, callback) {
|
|
|
|
|
params.sort = -1;
|
|
|
|
|
getSortedSetUnion(params, callback);
|
2014-05-23 14:30:59 -04:00
|
|
|
};
|
|
|
|
|
|
2016-03-03 19:52:48 +02:00
|
|
|
|
2016-09-08 17:59:59 +03:00
|
|
|
function getSortedSetUnion(params, callback) {
|
|
|
|
|
if (!Array.isArray(params.sets) || !params.sets.length) {
|
2014-09-21 13:30:20 -04:00
|
|
|
return callback();
|
|
|
|
|
}
|
2016-09-08 17:59:59 +03:00
|
|
|
var limit = params.stop - params.start + 1;
|
2014-05-23 17:03:53 -04:00
|
|
|
if (limit <= 0) {
|
2014-05-23 14:30:59 -04:00
|
|
|
limit = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 17:59:59 +03:00
|
|
|
var aggregate = {};
|
|
|
|
|
if (params.aggregate) {
|
|
|
|
|
aggregate['$' + params.aggregate.toLowerCase()] = '$score';
|
|
|
|
|
} else {
|
|
|
|
|
aggregate.$sum = '$score';
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-23 17:03:53 -04:00
|
|
|
var pipeline = [
|
2016-09-08 17:59:59 +03:00
|
|
|
{ $match: { _key: {$in: params.sets}} },
|
|
|
|
|
{ $group: { _id: {value: '$value'}, totalScore: aggregate} },
|
|
|
|
|
{ $sort: { totalScore: params.sort} }
|
2014-05-23 17:03:53 -04:00
|
|
|
];
|
2014-05-23 14:30:59 -04:00
|
|
|
|
2016-09-08 17:59:59 +03:00
|
|
|
if (params.start) {
|
|
|
|
|
pipeline.push({ $skip: params.start });
|
2014-05-23 17:03:53 -04:00
|
|
|
}
|
2014-05-23 14:30:59 -04:00
|
|
|
|
2014-05-23 17:03:53 -04:00
|
|
|
if (limit > 0) {
|
|
|
|
|
pipeline.push({ $limit: limit });
|
|
|
|
|
}
|
2014-05-23 15:36:54 -04:00
|
|
|
|
2016-09-08 17:59:59 +03:00
|
|
|
var project = { _id: 0, value: '$_id.value' };
|
|
|
|
|
if (params.withScores) {
|
|
|
|
|
project.score = '$totalScore';
|
|
|
|
|
}
|
|
|
|
|
pipeline.push({ $project: project });
|
2014-05-23 17:03:53 -04:00
|
|
|
|
|
|
|
|
db.collection('objects').aggregate(pipeline, function(err, data) {
|
|
|
|
|
if (err || !data) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 17:59:59 +03:00
|
|
|
if (!params.withScores) {
|
|
|
|
|
data = data.map(function(item) {
|
|
|
|
|
return item.value;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-23 17:03:53 -04:00
|
|
|
callback(null, data);
|
|
|
|
|
});
|
2014-05-23 14:30:59 -04:00
|
|
|
}
|
2014-09-27 15:48:16 -04:00
|
|
|
|
|
|
|
|
module.sortedSetIncrBy = function(key, increment, value, callback) {
|
|
|
|
|
callback = callback || helpers.noop;
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
var data = {};
|
2016-05-25 21:18:14 +03:00
|
|
|
value = helpers.valueToString(value);
|
2014-10-08 12:22:39 -04:00
|
|
|
data.score = parseInt(increment, 10);
|
2014-09-27 15:48:16 -04:00
|
|
|
|
2014-12-31 16:38:57 -05:00
|
|
|
db.collection('objects').findAndModify({_key: key, value: value}, {}, {$inc: data}, {new: true, upsert: true}, function(err, result) {
|
2016-03-24 21:01:20 +02:00
|
|
|
// if there is duplicate key error retry the upsert
|
|
|
|
|
// https://github.com/NodeBB/NodeBB/issues/4467
|
|
|
|
|
// https://jira.mongodb.org/browse/SERVER-14322
|
|
|
|
|
// https://docs.mongodb.org/manual/reference/command/findAndModify/#upsert-and-unique-index
|
|
|
|
|
if (err && err.message.startsWith('E11000 duplicate key error')) {
|
|
|
|
|
return module.sortedSetIncrBy(key, increment, value, callback);
|
|
|
|
|
}
|
2015-03-03 16:16:32 -05:00
|
|
|
callback(err, result && result.value ? result.value.score : null);
|
2014-09-27 15:48:16 -04:00
|
|
|
});
|
|
|
|
|
};
|
2015-05-19 23:04:28 -04:00
|
|
|
|
|
|
|
|
module.getSortedSetRangeByLex = function(key, min, max, start, count, callback) {
|
|
|
|
|
var query = {_key: key};
|
|
|
|
|
if (min !== '-') {
|
|
|
|
|
query.value = {$gte: min};
|
|
|
|
|
}
|
|
|
|
|
if (max !== '+') {
|
|
|
|
|
query.value = query.value || {};
|
|
|
|
|
query.value.$lte = max;
|
|
|
|
|
}
|
|
|
|
|
db.collection('objects').find(query, {_id: 0, value: 1})
|
|
|
|
|
.sort({value: 1})
|
|
|
|
|
.skip(start)
|
|
|
|
|
.limit(count === -1 ? 0 : count)
|
|
|
|
|
.toArray(function(err, data) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
data = data.map(function(item) {
|
|
|
|
|
return item && item.value;
|
|
|
|
|
});
|
|
|
|
|
callback(err, data);
|
|
|
|
|
});
|
|
|
|
|
};
|
2016-02-29 14:05:17 -06:00
|
|
|
|
|
|
|
|
module.processSortedSet = function(setKey, process, batch, callback) {
|
2016-03-01 21:38:36 +02:00
|
|
|
var done = false;
|
|
|
|
|
var ids = [];
|
|
|
|
|
var cursor = db.collection('objects').find({_key: setKey})
|
|
|
|
|
.sort({score: 1})
|
|
|
|
|
.project({_id: 0, value: 1})
|
|
|
|
|
.batchSize(batch);
|
2016-02-29 14:05:17 -06:00
|
|
|
|
|
|
|
|
async.whilst(
|
|
|
|
|
function() {
|
|
|
|
|
return !done;
|
|
|
|
|
},
|
|
|
|
|
function(next) {
|
|
|
|
|
cursor.next(function(err, item) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
if (item === null) {
|
|
|
|
|
done = true;
|
|
|
|
|
} else {
|
|
|
|
|
ids.push(item.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ids.length < batch && (!done || ids.length === 0)) {
|
|
|
|
|
return next(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process(ids, function(err) {
|
|
|
|
|
ids = [];
|
|
|
|
|
return next(err);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
callback
|
|
|
|
|
);
|
|
|
|
|
};
|
2016-08-15 19:23:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-15 19:26:18 +03:00
|
|
|
var pipeline = [{ $match: { _key: {$in: sets}} }];
|
2016-08-15 19:23:10 +03:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 14:05:17 -06:00
|
|
|
};
|