upgrade tags to sorted set

This commit is contained in:
barisusakli
2014-05-22 13:06:19 -04:00
parent 853acaa6c7
commit 746df87d89
7 changed files with 105 additions and 47 deletions

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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);
};