Permalink
Browse files

Merge branch 'master' of github.com:1602/jugglingdb

  • Loading branch information...
2 parents 680d123 + d05e018 commit 8d0685a466f80f80bced96c0a6fb3d0721a280e4 @1602 committed Jan 18, 2012
Showing with 54 additions and 9 deletions.
  1. +1 −1 README.md
  2. +53 −8 lib/adapters/redis.js
View
@@ -1,4 +1,4 @@
-## About
+## About <img src="https://secure.travis-ci.org/1602/jugglingdb.png" />
JugglingDB is cross-db ORM, providing **common interface** to access most popular database formats.
Currently supported are: mysql, mongodb, redis, neo4j and js-memory-storage (yep,
View
@@ -80,6 +80,11 @@ BridgeToRedis.prototype.create = function (model, data, callback) {
callback(err, id);
}
});
+
+ // push the id to the list of user ids for sorting
+ log('SADD s:' + model + ':' + data.id + ' ...');
+ this.client.sadd("s:" + model, data.id);
+
}.bind(this));
};
@@ -112,6 +117,8 @@ BridgeToRedis.prototype.destroy = function destroy(model, id, callback) {
this.log('DEL ' + model + ':' + id, t1);
callback(err);
}.bind(this));
+ this.log('SREM s:' + model, t1);
+ this.client.srem("s:" + model, id);
};
BridgeToRedis.prototype.possibleIndexes = function (model, filter) {
@@ -133,15 +140,53 @@ BridgeToRedis.prototype.all = function all(model, filter, callback) {
var log = this.log;
var t1 = Date.now();
var cmd;
+ var that = this;
+ var sortCmd = [];
- var indexes = this.possibleIndexes(model, filter);
- if (indexes.length) {
- cmd = 'SINTER "' + indexes.join('" "') + '"';
- indexes.push(handleKeys);
- client.sinter.apply(client, indexes);
- } else {
- cmd = 'KEYS ' + model + ':*';
- client.keys(model + ':*', handleKeys);
+ // ORDER
+ if (filter && filter.order){
+ var orders = filter.order;
+ if (typeof filter.order === "string"){
+ orders = [filter.order];
+ }
+ orders.forEach( function (key){
+ sortCmd.push("BY", model + ":*->" + key);
+ });
+ }
+ // LIMIT
+ if (filter && filter.limit){
+ var from = (filter.offset || 0), to = from + filter.limit;
+ sortCmd.push("LIMIT", from, to);
+ }
+
+ // do we need to sort or to query normally
+ if(sortCmd.length){
+ sortCmd.unshift("s:" + model);
+ sortCmd.push("GET", "#");
+ cmd = "sort " + sortCmd.join(" ");
+ sortCmd.push(function(err, ids){
+ console.log( ids);
+ if (err) {
+ return callback(err, []);
+ }
+ var keys = ids.map(function (i) {
+ return model + ":" + i;
+ });
+ console.log(keys);
+ handleKeys(err, keys);
+ });
+ client.sort.apply(client, sortCmd);
+ }else{
+ // Do a normal key lookup with possbible indexes
+ var indexes = this.possibleIndexes(model, filter);
+ if (indexes.length) {
+ cmd = 'SINTER "' + indexes.join('" "') + '"';
+ indexes.push(handleKeys);
+ client.sinter.apply(client, indexes, handleKeys);
+ } else {
+ cmd = 'KEYS ' + model + ':*';
+ client.keys(model + ':*', handleKeys);
+ }
}
function handleKeys(err, keys) {

0 comments on commit 8d0685a

Please sign in to comment.