|
|
|
@ -7,6 +7,9 @@ module.exports = function(db, module) {
|
|
|
|
|
|
|
|
|
|
module.sortedSetAdd = function(key, score, value, callback) {
|
|
|
|
|
callback = callback || helpers.noop;
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(score) && Array.isArray(value)) {
|
|
|
|
|
return sortedSetAddBulk(key, score, value, callback);
|
|
|
|
|
}
|
|
|
|
@ -42,7 +45,9 @@ module.exports = function(db, module) {
|
|
|
|
|
|
|
|
|
|
module.sortedSetsAdd = function(keys, score, value, callback) {
|
|
|
|
|
callback = callback || helpers.noop;
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
var data = {
|
|
|
|
|
score: parseInt(score, 10),
|
|
|
|
@ -62,6 +67,9 @@ module.exports = function(db, module) {
|
|
|
|
|
|
|
|
|
|
module.sortedSetRemove = function(key, value, callback) {
|
|
|
|
|
callback = callback || helpers.noop;
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
if (!Array.isArray(value)) {
|
|
|
|
|
value = [value];
|
|
|
|
|
}
|
|
|
|
@ -74,6 +82,9 @@ module.exports = function(db, module) {
|
|
|
|
|
|
|
|
|
|
module.sortedSetsRemove = function(keys, value, callback) {
|
|
|
|
|
callback = callback || helpers.noop;
|
|
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
|
|
|
|
|
db.collection('objects').remove({_key: {$in: keys}, value: value}, callback);
|
|
|
|
@ -81,12 +92,18 @@ module.exports = function(db, module) {
|
|
|
|
|
|
|
|
|
|
module.sortedSetsRemoveRangeByScore = function(keys, min, max, callback) {
|
|
|
|
|
callback = callback || helpers.noop;
|
|
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
db.collection('objects').remove({_key: {$in: keys}, score: {$lte: max, $gte: min}}, function(err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getSortedSetRange(key, start, stop, sort, withScores, callback) {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
db.collection('objects').find({_key:key}, {fields: {_id: 0, value: 1, score: 1}})
|
|
|
|
|
.limit(stop - start + 1)
|
|
|
|
|
.skip(start)
|
|
|
|
@ -127,6 +144,9 @@ module.exports = function(db, module) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getSortedSetRangeByScore(key, start, count, min, max, sort, callback) {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
if(parseInt(count, 10) === -1) {
|
|
|
|
|
count = 0;
|
|
|
|
|
}
|
|
|
|
@ -157,19 +177,28 @@ module.exports = function(db, module) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.sortedSetCount = function(key, min, max, callback) {
|
|
|
|
|
db.collection('objects').count({_key:key, score: {$gte:min, $lte:max}}, function(err, count) {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
db.collection('objects').count({_key: key, score: {$gte: min, $lte: max}}, function(err, count) {
|
|
|
|
|
callback(err, count ? count : 0);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.sortedSetCard = function(key, callback) {
|
|
|
|
|
db.collection('objects').count({_key:key}, function(err, count) {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
db.collection('objects').count({_key: key}, function(err, count) {
|
|
|
|
|
count = parseInt(count, 10);
|
|
|
|
|
callback(err, count ? count : 0);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.sortedSetsCard = function(keys, callback) {
|
|
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
var pipeline = [
|
|
|
|
|
{ $match : { _key : { $in: keys } } } ,
|
|
|
|
|
{ $group: { _id: {_key: '$_key'}, count: { $sum: 1 } } },
|
|
|
|
@ -207,6 +236,9 @@ module.exports = function(db, module) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getSortedSetRank(method, key, value, callback) {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
method(key, 0, -1, function(err, result) {
|
|
|
|
|
if(err) {
|
|
|
|
@ -219,6 +251,9 @@ module.exports = function(db, module) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.sortedSetsRanks = function(keys, values, callback) {
|
|
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
var data = new Array(values.length);
|
|
|
|
|
for (var i=0; i<values.length; ++i) {
|
|
|
|
|
data[i] = {key: keys[i], value: values[i]};
|
|
|
|
@ -230,13 +265,19 @@ module.exports = function(db, module) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.sortedSetScore = function(key, value, callback) {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
db.collection('objects').findOne({_key:key, value: value}, {fields:{score:1}}, function(err, result) {
|
|
|
|
|
db.collection('objects').findOne({_key:key, value: value}, {fields:{score: 1}}, function(err, result) {
|
|
|
|
|
callback(err, result ? result.score : null);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.sortedSetsScore = function(keys, value, callback) {
|
|
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
value = helpers.valueToString(value);
|
|
|
|
|
db.collection('objects').find({_key:{$in:keys}, value: value}).toArray(function(err, result) {
|
|
|
|
|
if (err) {
|
|
|
|
@ -257,6 +298,9 @@ module.exports = function(db, module) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.sortedSetScores = function(key, values, callback) {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
values = values.map(helpers.valueToString);
|
|
|
|
|
db.collection('objects').find({_key: key, value: {$in: values}}).toArray(function(err, result) {
|
|
|
|
|
if (err) {
|
|
|
|
@ -287,6 +331,9 @@ module.exports = function(db, module) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.isSortedSetMembers = function(key, values, callback) {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
values = values.map(helpers.valueToString);
|
|
|
|
|
db.collection('objects').find({_key: key, value: {$in: values}}).toArray(function(err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
@ -313,6 +360,9 @@ module.exports = function(db, module) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getSortedSetUnion(sets, sort, start, stop, callback) {
|
|
|
|
|
if (!Array.isArray(sets) || !sets.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
var limit = stop - start + 1;
|
|
|
|
|
if (limit <= 0) {
|
|
|
|
|
limit = 0;
|
|
|
|
|