mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-06 07:40:43 +01:00
upgrade tags to sorted set
This commit is contained in:
@@ -5,7 +5,7 @@ var async = require('async');
|
||||
|
||||
module.exports = function(db, module) {
|
||||
var helpers = module.helpers.level;
|
||||
|
||||
|
||||
module.sortedSetAdd = function(key, score, value, callback) {
|
||||
module.getListRange(key, 0, -1, function(err, set) {
|
||||
set = set.filter(function(a) {return a.value !== value.toString();});
|
||||
@@ -57,6 +57,10 @@ module.exports = function(db, module) {
|
||||
});
|
||||
};
|
||||
|
||||
module.getSortedSetRevRangeWithScores = function(key, start, stop, callback) {
|
||||
// should return [{value:"test", score: 2}, {value: "foo", score: 1}, ...]
|
||||
};
|
||||
|
||||
module.getSortedSetRangeByScore = function(key, start, count, min, max, callback) {
|
||||
module.getListRange(key, 0, -1, function(err, list) {
|
||||
if (min && max) {
|
||||
|
||||
@@ -19,8 +19,8 @@ module.exports = function(db, module) {
|
||||
db.collection('objects').remove({_key:key, value:value}, helpers.done(callback));
|
||||
};
|
||||
|
||||
function getSortedSetRange(key, start, stop, sort, callback) {
|
||||
db.collection('objects').find({_key:key}, {fields:{value:1}})
|
||||
function getSortedSetRange(key, start, stop, sort, withScores, callback) {
|
||||
db.collection('objects').find({_key:key}, {fields: {_id: 0, value: 1, score: 1}})
|
||||
.limit(stop - start + 1)
|
||||
.skip(start)
|
||||
.sort({score: sort})
|
||||
@@ -29,20 +29,26 @@ module.exports = function(db, module) {
|
||||
return callback(err, null);
|
||||
}
|
||||
|
||||
data = data.map(function(item) {
|
||||
return item.value;
|
||||
});
|
||||
if (!withScores) {
|
||||
data = data.map(function(item) {
|
||||
return item.value;
|
||||
});
|
||||
}
|
||||
|
||||
callback(null, data);
|
||||
});
|
||||
}
|
||||
|
||||
module.getSortedSetRange = function(key, start, stop, callback) {
|
||||
getSortedSetRange(key, start, stop, 1, callback);
|
||||
getSortedSetRange(key, start, stop, 1, false, callback);
|
||||
};
|
||||
|
||||
module.getSortedSetRevRange = function(key, start, stop, callback) {
|
||||
getSortedSetRange(key, start, stop, -1, callback);
|
||||
getSortedSetRange(key, start, stop, -1, false, callback);
|
||||
};
|
||||
|
||||
module.getSortedSetRevRangeWithScores = function(key, start, stop, callback) {
|
||||
getSortedSetRange(key, start, stop, -1, true, callback)
|
||||
};
|
||||
|
||||
module.getSortedSetRangeByScore = function(key, start, count, min, max, callback) {
|
||||
|
||||
@@ -17,6 +17,19 @@ module.exports = function(redisClient, module) {
|
||||
redisClient.zrevrange(key, start, stop, callback);
|
||||
};
|
||||
|
||||
module.getSortedSetRevRangeWithScores = function(key, start, stop, callback) {
|
||||
redisClient.zrevrange([key, start, stop, 'WITHSCORES'], function(err, data) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
var objects = [];
|
||||
for(var i=0; i<data.length; i+=2) {
|
||||
objects.push({value: data[i], score: data[i+1]});
|
||||
}
|
||||
callback(null, objects);
|
||||
});
|
||||
};
|
||||
|
||||
module.getSortedSetRangeByScore = function(key, start, count, min, max, callback) {
|
||||
redisClient.zrangebyscore([key, min, max, 'LIMIT', start, count], callback);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user