can haz sorted sets?

v1.18.x
Baris Soner Usakli 11 years ago
parent b5770be71f
commit 28c75e09a9

@ -26,15 +26,13 @@
});
db.createCollection('objects', function(err, collection) {
});
db.createCollection('objects', function(err, _collection) {
db.createCollection('sets', function(err, collection) {
});
callback(err);
});
// look up how its done in mongo
/*if (nconf.get('mongo:password')) {
redisClient.auth(nconf.get('mongo:password'));
@ -68,7 +66,7 @@
throw new Error('not-implemented');
}
module.set = function(key, callback) {
module.set = function(key, value, callback) {
throw new Error('not-implemented');
}
@ -124,15 +122,20 @@
return callback(err);
}
var data = {};
if(item === null) {
item = {};
for(var i=0; i<fields.length; ++i) {
data[fields[i]] = null;
item[fields[i]] = null;
}
} else {
for(var i=0; i<fields.length; ++i) {
if(item[fields[i]] === null || item[fields[i]] === undefined) {
item[fields[i]] = null;
}
}
return callback(null, data);
}
if(item._id) {
if(item && item._id) {
delete item._id;
}
callback(err, item);
@ -238,23 +241,73 @@
// sorted sets
module.sortedSetAdd = function(key, score, value, callback) {
throw new Error('not-implemented');
var data = {
score:score,
value:value
};
data.setName = key
module.setObject(key+':'+value, data, callback);
}
module.sortedSetRemove = function(key, value, callback) {
throw new Error('not-implemented');
}
function getSortedSetRange(key, start, stop, sort, callback) {
db.collection('objects').find({setName:key}, {fields:{value:1}})
.limit(stop - start + 1)
.skip(start)
.sort({score: sort})
.toArray(function(err, data) {
if(err) {
return callback(err);
}
// maybe this can be done with mongo?
data = data.map(function(item) {
return item.value;
});
callback(err, data);
});
}
module.getSortedSetRange = function(key, start, stop, callback) {
throw new Error('not-implemented');
getSortedSetRange(key, start, stop, 1, callback);
}
module.getSortedSetRevRange = function(key, start, stop, callback) {
throw new Error('not-implemented');
getSortedSetRange(key, start, stop, -1, callback);
}
module.getSortedSetRevRangeByScore = function(args, callback) {
throw new Error('not-implemented');
//var args = ['topics:recent', '+inf', timestamp - since, 'LIMIT', start, end - start + 1];
var key = args[0],
max = (args[1] === '+inf')?Number.MAX_VALUE:args[1],
min = args[2],
start = args[4],
stop = args[5];
db.collection('objects').find({setName:key, score: {$gt:min, $lt:max}}, {fields:{value:1}})
.limit(stop - start + 1)
.skip(start)
.sort({score: -1})
.toArray(function(err, data) {
if(err) {
return callback(err);
}
// maybe this can be done with mongo?
data = data.map(function(item) {
return item.value;
});
callback(err, data);
});
}
module.sortedSetCount = function(key, min, max, callback) {

@ -132,8 +132,8 @@
redisClient.get(key, callback);
}
module.set = function(key, callback) {
redisClient.get(key, callback);
module.set = function(key, value, callback) {
redisClient.set(key, value, callback);
}
module.keys = function(key, callback) {

@ -83,7 +83,7 @@ var DebugRoute = function(app) {
app.get('/mongo', function(req, res) {
var db = require('./../database');
var objectKey = 'someotherObj';
var objectKey = 'testing4';
function createUser(callback) {
user.create('baris','123456', 'barisusakli@gmail.com', callback);
@ -156,7 +156,27 @@ var DebugRoute = function(app) {
});
}
var tasks = [
function sortedSetAdd(callback) {
db.sortedSetAdd('sortedSet2', 1, 12, function(err, data) {
console.log('sortedSetAdd return', data);
callback(err, {'sortedSetAdd': data});
});
}
function getSortedSetRange(callback) {
db.getSortedSetRevRange('sortedSet2', 0, -1, function(err, data) {
console.log('getSortedSetRange return', data);
callback(err, {'getSortedSetRange': data});
});
/*var args = ['sortedSet2', '+inf', 100, 'LIMIT', 0, 10];
db.getSortedSetRevRangeByScore(args, function(err, data) {
console.log('getSortedSetRevRangeByScore return', data);
callback(err, {'getSortedSetRevRangeByScore': data});
});*/
}
var objectTasks = [
//createUser,
getUser,
setObject,
@ -175,7 +195,15 @@ var DebugRoute = function(app) {
getObject
];
require('async').series(tasks, function(err, results) {
var sortedSetTasks = [
//sortedSetAdd,
getSortedSetRange,
sortedSetAdd,
getSortedSetRange
];
require('async').series(sortedSetTasks, function(err, results) {
if(err) {
console.log(err);
res.send(err.message);
@ -184,6 +212,8 @@ var DebugRoute = function(app) {
}
});
});
});
};

Loading…
Cancel
Save