2011-10-01 15:51:51 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
var redis = require('redis');
|
|
|
|
|
|
|
|
exports.initialize = function initializeSchema(schema, callback) {
|
|
|
|
schema.client = redis.createClient(
|
|
|
|
schema.settings.port,
|
|
|
|
schema.settings.host,
|
|
|
|
schema.settings.options
|
|
|
|
);
|
|
|
|
|
|
|
|
schema.client.auth(schema.settings.password, callback);
|
|
|
|
|
|
|
|
schema.adapter = new BridgeToRedis(schema.client);
|
|
|
|
};
|
|
|
|
|
|
|
|
function BridgeToRedis(client) {
|
|
|
|
this._models = {};
|
|
|
|
this.client = client;
|
2011-10-04 20:17:06 +00:00
|
|
|
this.indexes = {};
|
2011-10-01 15:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BridgeToRedis.prototype.define = function (descr) {
|
2011-10-04 20:17:06 +00:00
|
|
|
var m = descr.model.modelName;
|
|
|
|
this._models[m] = descr;
|
|
|
|
this.indexes[m] = {};
|
|
|
|
Object.keys(descr.properties).forEach(function (prop) {
|
|
|
|
if (descr.properties[prop].index) {
|
|
|
|
this.indexes[m][prop] = descr.properties[prop].type;
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
BridgeToRedis.prototype.defineForeignKey = function (model, key, cb) {
|
|
|
|
this.indexes[model][key] = Number;
|
|
|
|
cb(null, Number);
|
2011-10-01 15:51:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BridgeToRedis.prototype.save = function (model, data, callback) {
|
2011-10-04 20:17:06 +00:00
|
|
|
this.client.hmset(model + ':' + data.id, data, function (err) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
this.updateIndexes(model, data, callback);
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
BridgeToRedis.prototype.updateIndexes = function (model, data, callback) {
|
|
|
|
var i = this.indexes[model];
|
|
|
|
var schedule = [];
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
if (i[key]) {
|
|
|
|
schedule.push([
|
|
|
|
'sadd',
|
|
|
|
'i:' + model + ':' + key + ':' + data[key],
|
|
|
|
model + ':' + data.id
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
if (schedule.length) {
|
|
|
|
this.client.multi(schedule).exec(callback);
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
2011-10-01 15:51:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BridgeToRedis.prototype.create = function (model, data, callback) {
|
2011-10-04 20:17:06 +00:00
|
|
|
this.client.incr('id:' + model, function (err, id) {
|
2011-10-01 15:51:51 +00:00
|
|
|
data.id = id;
|
|
|
|
this.save(model, data, function (err) {
|
|
|
|
if (callback) {
|
|
|
|
callback(err, id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
BridgeToRedis.prototype.exists = function (model, id, callback) {
|
|
|
|
this.client.exists(model + ':' + id, function (err, exists) {
|
|
|
|
if (callback) {
|
|
|
|
callback(err, exists);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BridgeToRedis.prototype.find = function find(model, id, callback) {
|
|
|
|
this.client.hgetall(model + ':' + id, function (err, data) {
|
|
|
|
if (data && data.id) {
|
|
|
|
data.id = id;
|
|
|
|
} else {
|
|
|
|
data = null;
|
|
|
|
}
|
|
|
|
callback(err, data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BridgeToRedis.prototype.destroy = function destroy(model, id, callback) {
|
|
|
|
this.client.del(model + ':' + id, function (err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-10-04 20:17:06 +00:00
|
|
|
BridgeToRedis.prototype.possibleIndex = function possibleIndex(model, filter) {
|
|
|
|
if (!filter || Object.keys(filter).length === 0) return false;
|
|
|
|
|
|
|
|
var foundIndex = false;
|
|
|
|
Object.keys(filter).forEach(function (key) {
|
|
|
|
if (!foundIndex && this.indexes[model][key]) {
|
|
|
|
foundIndex = key;
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
return foundIndex;
|
|
|
|
};
|
|
|
|
|
2011-10-01 15:51:51 +00:00
|
|
|
BridgeToRedis.prototype.all = function all(model, filter, callback) {
|
2011-10-04 20:17:06 +00:00
|
|
|
var ts = Date.now();
|
|
|
|
|
|
|
|
var index = this.possibleIndex(model, filter);
|
|
|
|
if (false) {
|
|
|
|
// console.log('using index!', filter);
|
|
|
|
this.client.smembers('i:' + model + ':' + index + ':' + filter[index],
|
|
|
|
handleKeys.bind(this));
|
|
|
|
} else {
|
|
|
|
// console.log('without index', filter);
|
|
|
|
this.client.keys(model + ':*', handleKeys.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleKeys(err, keys) {
|
|
|
|
// console.log(arguments);
|
2011-10-01 15:51:51 +00:00
|
|
|
if (err) {
|
|
|
|
return callback(err, []);
|
|
|
|
}
|
|
|
|
var query = keys.map(function (key) {
|
|
|
|
return ['hgetall', key];
|
|
|
|
});
|
|
|
|
this.client.multi(query).exec(function (err, replies) {
|
2011-10-04 20:17:06 +00:00
|
|
|
// console.log('Redis time: %dms', Date.now() - ts);
|
2011-10-01 15:51:51 +00:00
|
|
|
callback(err, filter ? replies.filter(applyFilter(filter)) : replies);
|
|
|
|
});
|
2011-10-04 20:17:06 +00:00
|
|
|
}
|
2011-10-01 15:51:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function applyFilter(filter) {
|
|
|
|
if (typeof filter === 'function') {
|
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
var keys = Object.keys(filter);
|
|
|
|
return function (obj) {
|
|
|
|
var pass = true;
|
|
|
|
keys.forEach(function (key) {
|
|
|
|
if (!test(filter[key], obj[key])) {
|
|
|
|
pass = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
function test(example, value) {
|
|
|
|
if (typeof value === 'string' && example && example.constructor.name === 'RegExp') {
|
|
|
|
return value.match(example);
|
|
|
|
}
|
|
|
|
// not strict equality
|
|
|
|
return example == value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BridgeToRedis.prototype.destroyAll = function destroyAll(model, callback) {
|
|
|
|
this.client.keys(model + ':*', function (err, keys) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err, []);
|
|
|
|
}
|
|
|
|
var query = keys.map(function (key) {
|
|
|
|
return ['del', key];
|
|
|
|
});
|
|
|
|
this.client.multi(query).exec(function (err, replies) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
BridgeToRedis.prototype.count = function count(model, callback) {
|
|
|
|
this.client.keys(model + ':*', function (err, keys) {
|
|
|
|
callback(err, err ? null : keys.length);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BridgeToRedis.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
|
|
|
|
this.client.hmset(model + ':' + id, data, cb);
|
|
|
|
};
|
|
|
|
|