Files
NodeBB/src/database/mongo/sorted.js

266 lines
6.8 KiB
JavaScript
Raw Normal View History

2014-04-11 15:44:53 -04:00
"use strict";
2014-07-29 17:33:28 -04:00
var async = require('async');
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-04-11 15:44:53 -04:00
value = helpers.valueToString(value);
var data = {
score: parseInt(score, 10),
value: value
};
2014-09-03 01:13:28 -04:00
db.collection('objects').update({_key: key, value: value}, {$set: data}, {upsert:true, w: 1}, callback);
2014-04-11 15:44:53 -04:00
};
module.sortedSetRemove = function(key, value, callback) {
2014-09-03 01:13:28 -04:00
callback = callback || helpers.noop;
2014-04-11 15:44:53 -04:00
value = helpers.valueToString(value);
2014-09-03 01:13:28 -04:00
db.collection('objects').remove({_key: key, value: value}, callback);
};
module.sortedSetsRemove = function(keys, value, callback) {
2014-09-03 01:13:28 -04:00
callback = callback || helpers.noop;
value = helpers.valueToString(value);
2014-09-03 01:13:28 -04:00
db.collection('objects').remove({_key: {$in: keys}, value: value}, callback);
2014-04-11 15:44:53 -04:00
};
2014-05-22 13:06:19 -04:00
function getSortedSetRange(key, start, stop, sort, withScores, callback) {
db.collection('objects').find({_key:key}, {fields: {_id: 0, value: 1, score: 1}})
2014-04-11 15:44:53 -04:00
.limit(stop - start + 1)
.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.getSortedSetRange = function(key, start, stop, callback) {
2014-05-22 13:06:19 -04:00
getSortedSetRange(key, start, stop, 1, false, callback);
2014-04-11 15:44:53 -04:00
};
module.getSortedSetRevRange = function(key, start, stop, callback) {
2014-05-22 13:06:19 -04:00
getSortedSetRange(key, start, stop, -1, false, callback);
};
module.getSortedSetRevRangeWithScores = function(key, start, stop, callback) {
2014-05-23 14:30:59 -04:00
getSortedSetRange(key, start, stop, -1, true, callback);
2014-04-11 15:44:53 -04:00
};
module.getSortedSetRangeByScore = function(key, start, count, min, max, callback) {
getSortedSetRangeByScore(key, start, count, min, max, 1, callback);
};
module.getSortedSetRevRangeByScore = function(key, start, count, max, min, callback) {
getSortedSetRangeByScore(key, start, count, min, max, -1, callback);
};
function getSortedSetRangeByScore(key, start, count, min, max, sort, callback) {
if(parseInt(count, 10) === -1) {
count = 0;
}
2014-09-01 20:45:24 -04:00
var scoreQuery = {};
if (min !== -Infinity) {
scoreQuery['$gte'] = min;
}
if (max !== Infinity) {
scoreQuery['$lte'] = max;
}
db.collection('objects').find({_key:key, score: scoreQuery}, {fields:{value:1}})
2014-04-11 15:44:53 -04:00
.limit(count)
.skip(start)
.sort({score: sort})
.toArray(function(err, data) {
if(err) {
return callback(err);
}
data = data.map(function(item) {
return item.value;
});
callback(err, data);
});
}
module.sortedSetCount = function(key, min, max, 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) {
callback(err, count ? count : 0);
});
};
2014-08-14 21:12:12 -04:00
module.sortedSetsCard = function(keys, callback) {
async.map(keys, module.sortedSetCard, callback);
};
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) {
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) {
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-04-11 15:44:53 -04:00
module.sortedSetScore = function(key, value, callback) {
value = helpers.valueToString(value);
db.collection('objects').findOne({_key:key, value: value}, {fields:{score:1}}, function(err, result) {
callback(err, result ? result.score : null);
});
};
2014-08-15 11:48:01 -04:00
module.sortedSetsScore = function(keys, value, callback) {
value = helpers.valueToString(value);
db.collection('objects').find({_key:{$in:keys}, value: value}).toArray(function(err, result) {
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) {
values = values.map(helpers.valueToString);
db.collection('objects').find({_key: key, value: {$in: values}}).toArray(function(err, result) {
if (err) {
return callback(err);
}
var map = {};
result.forEach(function(item) {
map[item.value] = item.score;
});
var returnData = new Array(values.length),
score;
for(var i=0; i<values.length; ++i) {
score = map[values[i]];
returnData[i] = score ? score : null;
}
callback(null, returnData);
});
};
2014-04-11 15:44:53 -04:00
module.isSortedSetMember = function(key, value, callback) {
module.sortedSetScore(key, value, function(err, score) {
callback(err, !!score);
});
};
2014-07-22 12:56:34 -04:00
module.isSortedSetMembers = function(key, values, callback) {
values = values.map(helpers.valueToString);
db.collection('objects').find({_key: key, value: {$in: values}}).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
});
};
2014-05-23 14:30:59 -04:00
module.getSortedSetUnion = function(sets, start, stop, callback) {
getSortedSetUnion(sets, 1, start, stop, callback);
};
module.getSortedSetRevUnion = function(sets, start, stop, callback) {
getSortedSetUnion(sets, -1, start, stop, callback);
};
function getSortedSetUnion(sets, sort, start, stop, callback) {
var limit = stop - start + 1;
2014-05-23 17:03:53 -04:00
if (limit <= 0) {
2014-05-23 14:30:59 -04:00
limit = 0;
}
2014-05-23 17:03:53 -04:00
var pipeline = [
{ $match: { _key: {$in: sets}} },
{ $group: { _id: {value: '$value'}, totalScore: {$sum : "$score"}} },
{ $sort: { totalScore: sort} }
];
2014-05-23 14:30:59 -04:00
2014-05-23 17:03:53 -04:00
if (start) {
pipeline.push({ $skip: start });
}
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
2014-05-23 17:03:53 -04:00
pipeline.push({ $project: { _id: 0, value: '$_id.value' }});
db.collection('objects').aggregate(pipeline, function(err, data) {
if (err || !data) {
return callback(err);
}
data = data.map(function(item) {
return item.value;
2014-05-23 14:30:59 -04:00
});
2014-05-23 17:03:53 -04:00
callback(null, data);
});
2014-05-23 14:30:59 -04:00
}
2014-04-11 15:44:53 -04:00
};