2017-02-18 01:56:23 -07:00
|
|
|
'use strict';
|
2014-04-11 15:44:53 -04:00
|
|
|
|
2014-07-29 17:33:28 -04:00
|
|
|
var async = require('async');
|
2017-04-08 20:22:21 -06:00
|
|
|
var utils = require('../../utils');
|
2014-07-29 17:33:28 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (db, module) {
|
2014-04-11 15:44:53 -04:00
|
|
|
var helpers = module.helpers.mongo;
|
|
|
|
|
|
2016-12-23 14:12:00 +03:00
|
|
|
require('./sorted/add')(db, module);
|
|
|
|
|
require('./sorted/remove')(db, module);
|
|
|
|
|
require('./sorted/union')(db, module);
|
|
|
|
|
require('./sorted/intersect')(db, module);
|
2014-09-08 16:13:48 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.getSortedSetRange = function (key, start, stop, callback) {
|
2014-12-31 14:41:58 -05:00
|
|
|
getSortedSetRange(key, start, stop, 1, false, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.getSortedSetRevRange = function (key, start, stop, callback) {
|
2014-12-31 14:41:58 -05:00
|
|
|
getSortedSetRange(key, start, stop, -1, false, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.getSortedSetRangeWithScores = function (key, start, stop, callback) {
|
2014-12-31 14:41:58 -05:00
|
|
|
getSortedSetRange(key, start, stop, 1, true, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.getSortedSetRevRangeWithScores = function (key, start, stop, callback) {
|
2014-12-31 14:41:58 -05:00
|
|
|
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
|
|
|
|
2017-02-18 12:30:49 -07:00
|
|
|
var fields = { _id: 0, value: 1 };
|
2014-09-27 17:46:39 -04:00
|
|
|
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)) {
|
2017-02-18 12:30:49 -07:00
|
|
|
key = { $in: key };
|
2016-03-03 19:52:48 +02:00
|
|
|
}
|
|
|
|
|
|
2017-12-07 20:43:02 -05:00
|
|
|
if (start < 0 && start > stop) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var reverse = false;
|
|
|
|
|
if (start === 0 && stop < -1) {
|
|
|
|
|
reverse = true;
|
|
|
|
|
sort *= -1;
|
|
|
|
|
start = Math.abs(stop + 1);
|
|
|
|
|
stop = -1;
|
|
|
|
|
} else if (start < 0 && stop > start) {
|
|
|
|
|
var tmp1 = Math.abs(stop + 1);
|
|
|
|
|
stop = Math.abs(start + 1);
|
|
|
|
|
start = tmp1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-31 21:22:28 +03:00
|
|
|
var limit = stop - start + 1;
|
|
|
|
|
if (limit <= 0) {
|
|
|
|
|
limit = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-18 12:30:49 -07: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)
|
2017-02-18 12:30:49 -07:00
|
|
|
.sort({ score: sort })
|
2016-10-13 11:43:39 +02:00
|
|
|
.toArray(function (err, data) {
|
2014-04-11 15:44:53 -04:00
|
|
|
if (err || !data) {
|
2014-05-23 14:30:59 -04:00
|
|
|
return callback(err);
|
2014-04-11 15:44:53 -04:00
|
|
|
}
|
2017-12-07 20:43:02 -05:00
|
|
|
if (reverse) {
|
|
|
|
|
data.reverse();
|
|
|
|
|
}
|
2014-05-22 13:06:19 -04:00
|
|
|
if (!withScores) {
|
2016-10-13 11:43:39 +02:00
|
|
|
data = data.map(function (item) {
|
2014-05-22 13:06:19 -04:00
|
|
|
return item.value;
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-04-11 15:44:53 -04:00
|
|
|
|
|
|
|
|
callback(null, data);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02: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
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02: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
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.getSortedSetRevRangeByScoreWithScores = function (key, start, count, max, min, callback) {
|
2014-09-27 17:41:49 -04:00
|
|
|
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();
|
|
|
|
|
}
|
2016-12-23 14:12:00 +03:00
|
|
|
if (parseInt(count, 10) === -1) {
|
2014-04-11 15:44:53 -04:00
|
|
|
count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-18 12:30:49 -07:00
|
|
|
var query = { _key: key };
|
2016-03-15 11:09:06 +02:00
|
|
|
|
2014-11-07 19:40:41 -05:00
|
|
|
if (min !== '-inf') {
|
2017-02-18 12:30:49 -07: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
|
|
|
}
|
|
|
|
|
|
2017-02-18 12:30:49 -07: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
|
|
|
}
|
|
|
|
|
|
2017-02-18 12:30:49 -07:00
|
|
|
db.collection('objects').find(query, { fields: fields })
|
2014-04-11 15:44:53 -04:00
|
|
|
.limit(count)
|
|
|
|
|
.skip(start)
|
2017-02-18 12:30:49 -07:00
|
|
|
.sort({ score: sort })
|
2016-10-13 11:43:39 +02:00
|
|
|
.toArray(function (err, data) {
|
2016-12-23 14:12:00 +03:00
|
|
|
if (err) {
|
2014-04-11 15:44:53 -04:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-27 17:41:49 -04:00
|
|
|
if (!withScores) {
|
2016-10-13 11:43:39 +02:00
|
|
|
data = data.map(function (item) {
|
2014-09-27 17:41:49 -04:00
|
|
|
return item.value;
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-04-11 15:44:53 -04:00
|
|
|
|
|
|
|
|
callback(err, data);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
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
|
|
|
|
2017-02-18 12:30:49 -07:00
|
|
|
var query = { _key: key };
|
2014-12-31 16:09:33 -05:00
|
|
|
if (min !== '-inf') {
|
2017-02-18 12:30:49 -07: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
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
db.collection('objects').count(query, function (err, count) {
|
2017-02-18 12:59:46 -07:00
|
|
|
callback(err, count || 0);
|
2014-04-11 15:44:53 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
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
|
|
|
}
|
2017-02-18 12:30:49 -07:00
|
|
|
db.collection('objects').count({ _key: key }, function (err, count) {
|
2014-09-06 04:11:44 -04:00
|
|
|
count = parseInt(count, 10);
|
2017-02-18 12:59:46 -07:00
|
|
|
callback(err, count || 0);
|
2014-04-11 15:44:53 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02: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 = [
|
2017-02-18 01:31:47 -07:00
|
|
|
{ $match: { _key: { $in: keys } } },
|
2017-02-18 12:30:49 -07:00
|
|
|
{ $group: { _id: { _key: '$_key' }, count: { $sum: 1 } } },
|
2017-02-17 19:31:21 -07:00
|
|
|
{ $project: { _id: 1, count: '$count' } },
|
2014-09-12 16:35:30 -04:00
|
|
|
];
|
2018-05-10 16:31:16 -04:00
|
|
|
db.collection('objects').aggregate(pipeline).toArray(function (err, results) {
|
2014-09-12 16:35:30 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(results)) {
|
|
|
|
|
results = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var map = {};
|
2016-10-13 11:43:39 +02:00
|
|
|
results.forEach(function (item) {
|
2014-09-12 16:35:30 -04:00
|
|
|
if (item && item._id._key) {
|
|
|
|
|
map[item._id._key] = item.count;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
results = keys.map(function (key) {
|
2014-09-12 16:35:30 -04:00
|
|
|
return map[key] || 0;
|
|
|
|
|
});
|
|
|
|
|
callback(null, results);
|
|
|
|
|
});
|
2014-08-14 21:12:12 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.sortedSetRank = function (key, value, callback) {
|
2018-01-12 12:32:07 -06:00
|
|
|
getSortedSetRank(false, key, value, callback);
|
2014-04-11 15:44:53 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.sortedSetRevRank = function (key, value, callback) {
|
2018-01-12 12:32:07 -06:00
|
|
|
getSortedSetRank(true, key, value, callback);
|
2014-04-11 15:44:53 -04:00
|
|
|
};
|
|
|
|
|
|
2018-01-12 12:32:07 -06:00
|
|
|
function getSortedSetRank(reverse, 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);
|
2018-01-12 12:32:07 -06:00
|
|
|
module.sortedSetScore(key, value, function (err, score) {
|
|
|
|
|
if (err || score === null) {
|
|
|
|
|
return callback(err, null);
|
2014-04-11 15:44:53 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-12 12:32:07 -06:00
|
|
|
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); });
|
2014-04-11 15:44:53 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02: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);
|
2017-02-18 01:12:18 -07:00
|
|
|
for (var i = 0; i < values.length; i += 1) {
|
2017-02-18 12:30:49 -07:00
|
|
|
data[i] = { key: keys[i], value: values[i] };
|
2014-07-29 17:33:28 -04:00
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
async.map(data, function (item, next) {
|
2018-01-12 12:32:07 -06:00
|
|
|
getSortedSetRank(false, item.key, item.value, next);
|
2014-07-29 17:33:28 -04:00
|
|
|
}, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.sortedSetRanks = function (key, values, callback) {
|
|
|
|
|
module.getSortedSetRange(key, 0, -1, function (err, sortedSet) {
|
2014-09-26 22:19:26 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.sortedSetScore = function (key, value, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
2017-04-13 16:36:02 -04:00
|
|
|
return callback(null, null);
|
2014-09-21 13:30:20 -04:00
|
|
|
}
|
2014-04-11 15:44:53 -04:00
|
|
|
value = helpers.valueToString(value);
|
2017-02-18 12:30:49 -07: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);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02: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);
|
2017-02-18 12:30:49 -07: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);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 20:20:42 -07:00
|
|
|
var map = helpers.toMap(result);
|
|
|
|
|
var returnData = [];
|
|
|
|
|
var item;
|
2014-08-15 11:48:01 -04:00
|
|
|
|
2017-02-18 01:52:56 -07:00
|
|
|
for (var i = 0; i < keys.length; i += 1) {
|
2014-08-15 11:48:01 -04:00
|
|
|
item = map[keys[i]];
|
|
|
|
|
returnData.push(item ? item.score : null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, returnData);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.sortedSetScores = function (key, values, callback) {
|
2014-09-21 13:30:20 -04:00
|
|
|
if (!key) {
|
2017-04-13 16:36:02 -04:00
|
|
|
return callback(null, null);
|
2014-09-21 13:30:20 -04:00
|
|
|
}
|
2014-08-15 11:48:01 -04:00
|
|
|
values = values.map(helpers.valueToString);
|
2017-02-18 12:30:49 -07: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 = {};
|
2016-10-13 11:43:39 +02:00
|
|
|
result.forEach(function (item) {
|
2014-08-15 11:48:01 -04:00
|
|
|
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
|
|
|
|
2017-02-18 01:52:56 -07:00
|
|
|
for (var i = 0; i < values.length; i += 1) {
|
2014-08-15 11:48:01 -04:00
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.isSortedSetMember = function (key, value, callback) {
|
2015-04-20 23:26:02 -04:00
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
value = helpers.valueToString(value);
|
2017-02-18 12:30:49 -07:00
|
|
|
db.collection('objects').findOne({ _key: key, value: value }, { _id: 0, value: 1 }, function (err, result) {
|
2015-04-20 23:26:02 -04:00
|
|
|
callback(err, !!result);
|
2014-04-11 15:44:53 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02: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);
|
2017-02-18 12:30:49 -07: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
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
results = results.map(function (item) {
|
2014-07-22 12:56:34 -04:00
|
|
|
return item.value;
|
|
|
|
|
});
|
2014-08-12 14:39:58 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
values = values.map(function (value) {
|
2014-07-22 12:56:34 -04:00
|
|
|
return results.indexOf(value) !== -1;
|
|
|
|
|
});
|
2014-08-12 14:39:58 -04:00
|
|
|
callback(null, values);
|
2014-07-22 12:56:34 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.isMemberOfSortedSets = function (keys, value, callback) {
|
2015-01-21 22:48:22 -05:00
|
|
|
if (!Array.isArray(keys)) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
value = helpers.valueToString(value);
|
2017-02-18 12:30:49 -07:00
|
|
|
db.collection('objects').find({ _key: { $in: keys }, value: value }, { fields: { _id: 0, _key: 1, value: 1 } }).toArray(function (err, results) {
|
2015-01-21 22:48:22 -05:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
results = results.map(function (item) {
|
2015-01-21 22:48:22 -05:00
|
|
|
return item._key;
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
results = keys.map(function (key) {
|
2015-01-21 22:48:22 -05:00
|
|
|
return results.indexOf(key) !== -1;
|
|
|
|
|
});
|
|
|
|
|
callback(null, results);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.getSortedSetsMembers = function (keys, callback) {
|
2015-01-21 22:48:22 -05:00
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
2018-03-17 18:49:33 -04:00
|
|
|
db.collection('objects').find({ _key: { $in: keys } }, { _id: 0, _key: 1, value: 1 }).sort({ score: 1 }).toArray(function (err, data) {
|
2015-01-21 22:48:22 -05:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sets = {};
|
2016-10-13 11:43:39 +02:00
|
|
|
data.forEach(function (set) {
|
2017-02-18 14:42:15 -07:00
|
|
|
sets[set._key] = sets[set._key] || [];
|
|
|
|
|
sets[set._key].push(set.value);
|
2015-01-21 22:48:22 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var returnData = new Array(keys.length);
|
2017-02-18 01:52:56 -07:00
|
|
|
for (var i = 0; i < keys.length; i += 1) {
|
2017-02-18 14:42:15 -07:00
|
|
|
returnData[i] = sets[keys[i]] || [];
|
2015-01-21 22:48:22 -05:00
|
|
|
}
|
|
|
|
|
callback(null, returnData);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.sortedSetIncrBy = function (key, increment, value, callback) {
|
2014-09-27 15:48:16 -04:00
|
|
|
callback = callback || helpers.noop;
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
var data = {};
|
2016-05-25 21:18:14 +03:00
|
|
|
value = helpers.valueToString(value);
|
2016-12-11 15:58:57 +01:00
|
|
|
data.score = parseFloat(increment);
|
2014-09-27 15:48:16 -04:00
|
|
|
|
2017-02-18 12:30:49 -07: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')) {
|
2016-10-16 21:51:42 +03:00
|
|
|
return process.nextTick(module.sortedSetIncrBy, key, increment, value, callback);
|
2016-03-24 21:01:20 +02:00
|
|
|
}
|
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
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.getSortedSetRangeByLex = function (key, min, max, start, count, callback) {
|
2016-10-16 20:37:48 -04:00
|
|
|
sortedSetLex(key, min, max, 1, start, count, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-17 16:35:33 -04:00
|
|
|
module.getSortedSetRevRangeByLex = function (key, max, min, start, count, callback) {
|
2016-10-16 20:37:48 -04:00
|
|
|
sortedSetLex(key, min, max, -1, start, count, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.sortedSetLexCount = function (key, min, max, callback) {
|
|
|
|
|
sortedSetLex(key, min, max, 1, 0, 0, function (err, data) {
|
|
|
|
|
callback(err, data ? data.length : null);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-17 14:38:11 -04:00
|
|
|
function sortedSetLex(key, min, max, sort, start, count, callback) {
|
|
|
|
|
if (!callback) {
|
|
|
|
|
callback = start;
|
|
|
|
|
start = 0;
|
|
|
|
|
count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-18 12:30:49 -07:00
|
|
|
var query = { _key: key };
|
2016-12-23 14:12:00 +03:00
|
|
|
buildLexQuery(query, min, max);
|
2016-10-16 20:37:48 -04:00
|
|
|
|
2017-02-18 12:30:49 -07:00
|
|
|
db.collection('objects').find(query, { _id: 0, value: 1 })
|
|
|
|
|
.sort({ value: sort })
|
2015-05-19 23:04:28 -04:00
|
|
|
.skip(start)
|
|
|
|
|
.limit(count === -1 ? 0 : count)
|
2016-10-13 11:43:39 +02:00
|
|
|
.toArray(function (err, data) {
|
2015-05-19 23:04:28 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2016-10-13 11:43:39 +02:00
|
|
|
data = data.map(function (item) {
|
2015-05-19 23:04:28 -04:00
|
|
|
return item && item.value;
|
|
|
|
|
});
|
|
|
|
|
callback(err, data);
|
2017-02-18 02:38:03 -07:00
|
|
|
});
|
2016-10-16 20:37:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.sortedSetRemoveRangeByLex = function (key, min, max, callback) {
|
|
|
|
|
callback = callback || helpers.noop;
|
|
|
|
|
|
2017-02-18 12:30:49 -07:00
|
|
|
var query = { _key: key };
|
2016-12-23 14:12:00 +03:00
|
|
|
buildLexQuery(query, min, max);
|
|
|
|
|
|
|
|
|
|
db.collection('objects').remove(query, function (err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
2016-10-16 20:37:48 -04:00
|
|
|
|
2016-12-23 14:12:00 +03:00
|
|
|
function buildLexQuery(query, min, max) {
|
2016-10-16 20:37:48 -04:00
|
|
|
if (min !== '-') {
|
2016-10-16 21:53:02 -04:00
|
|
|
if (min.match(/^\(/)) {
|
2017-02-18 12:30:49 -07:00
|
|
|
query.value = { $gt: min.slice(1) };
|
2016-10-16 21:53:02 -04:00
|
|
|
} else if (min.match(/^\[/)) {
|
2017-02-18 12:30:49 -07:00
|
|
|
query.value = { $gte: min.slice(1) };
|
2016-10-16 21:29:45 -04:00
|
|
|
} else {
|
2017-02-18 12:30:49 -07:00
|
|
|
query.value = { $gte: min };
|
2016-10-16 21:29:45 -04:00
|
|
|
}
|
2016-10-16 20:37:48 -04:00
|
|
|
}
|
|
|
|
|
if (max !== '+') {
|
|
|
|
|
query.value = query.value || {};
|
2016-10-16 21:53:02 -04:00
|
|
|
if (max.match(/^\(/)) {
|
|
|
|
|
query.value.$lt = max.slice(1);
|
|
|
|
|
} else if (max.match(/^\[/)) {
|
|
|
|
|
query.value.$lte = max.slice(1);
|
2016-10-16 21:29:45 -04:00
|
|
|
} else {
|
2016-10-16 21:53:02 -04:00
|
|
|
query.value.$lte = max;
|
2016-10-16 21:29:45 -04:00
|
|
|
}
|
2016-10-16 20:37:48 -04:00
|
|
|
}
|
2016-12-23 14:12:00 +03:00
|
|
|
}
|
2016-02-29 14:05:17 -06:00
|
|
|
|
2017-11-15 11:50:44 -05:00
|
|
|
module.processSortedSet = function (setKey, processFn, options, callback) {
|
2016-03-01 21:38:36 +02:00
|
|
|
var done = false;
|
|
|
|
|
var ids = [];
|
2018-06-22 16:28:19 -04:00
|
|
|
var project = { _id: 0, value: 1 };
|
|
|
|
|
if (options.withScores) {
|
|
|
|
|
project.score = 1;
|
|
|
|
|
}
|
2017-02-18 12:30:49 -07:00
|
|
|
var cursor = db.collection('objects').find({ _key: setKey })
|
|
|
|
|
.sort({ score: 1 })
|
2018-06-22 16:28:19 -04:00
|
|
|
.project(project)
|
2017-06-23 18:18:34 -04:00
|
|
|
.batchSize(options.batch);
|
2016-02-29 14:05:17 -06:00
|
|
|
|
|
|
|
|
async.whilst(
|
2016-10-13 11:43:39 +02:00
|
|
|
function () {
|
2016-02-29 14:05:17 -06:00
|
|
|
return !done;
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2017-07-06 12:07:56 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
cursor.next(next);
|
|
|
|
|
},
|
|
|
|
|
function (item, _next) {
|
|
|
|
|
if (item === null) {
|
|
|
|
|
done = true;
|
|
|
|
|
} else {
|
2018-06-22 16:28:19 -04:00
|
|
|
ids.push(options.withScores ? item : item.value);
|
2017-07-06 12:07:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ids.length < options.batch && (!done || ids.length === 0)) {
|
2017-11-15 11:35:25 -05:00
|
|
|
return process.nextTick(next, null);
|
2017-06-23 18:18:34 -04:00
|
|
|
}
|
2017-11-15 11:50:44 -05:00
|
|
|
processFn(ids, function (err) {
|
2017-07-06 12:07:56 -04:00
|
|
|
_next(err);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
2016-02-29 14:05:17 -06:00
|
|
|
ids = [];
|
2017-06-23 18:18:34 -04:00
|
|
|
if (options.interval) {
|
|
|
|
|
setTimeout(next, options.interval);
|
|
|
|
|
} else {
|
2017-11-15 13:06:23 -05:00
|
|
|
process.nextTick(next);
|
2017-06-23 18:18:34 -04:00
|
|
|
}
|
2017-07-06 12:07:56 -04:00
|
|
|
},
|
|
|
|
|
], next);
|
2016-02-29 14:05:17 -06:00
|
|
|
},
|
2017-02-17 20:20:42 -07:00
|
|
|
callback
|
2016-02-29 14:05:17 -06:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
};
|