loopback-datasource-juggler/lib/adapters/redis.js

293 lines
8.4 KiB
JavaScript
Raw Normal View History

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
);
2011-11-05 09:55:11 +00:00
schema.client.auth(schema.settings.password);
schema.client.on('connect', callback);
2011-10-01 15:51:51 +00:00
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) {
2012-01-13 20:06:57 +00:00
deleteNulls(data);
2011-11-11 13:16:09 +00:00
var log = this.logger('HMSET ' + model + ':' + data.id + ' ...');
2011-10-04 20:17:06 +00:00
this.client.hmset(model + ':' + data.id, data, function (err) {
2011-11-11 13:16:09 +00:00
log();
2011-10-04 20:17:06 +00:00
if (err) return callback(err);
2011-10-19 17:17:48 +00:00
this.updateIndexes(model, data.id, data, callback);
2011-10-04 20:17:06 +00:00
}.bind(this));
};
2011-10-19 17:17:48 +00:00
BridgeToRedis.prototype.updateIndexes = function (model, id, data, callback) {
2011-10-04 20:17:06 +00:00
var i = this.indexes[model];
var schedule = [];
Object.keys(data).forEach(function (key) {
if (i[key]) {
schedule.push([
'sadd',
'i:' + model + ':' + key + ':' + data[key],
2011-10-19 17:17:48 +00:00
model + ':' + id
2011-10-04 20:17:06 +00:00
]);
}
}.bind(this));
if (schedule.length) {
2011-10-05 14:47:26 +00:00
this.client.multi(schedule).exec(function (err) {
callback(err);
});
2011-10-04 20:17:06 +00:00
} else {
callback(null);
}
2011-10-01 15:51:51 +00:00
};
BridgeToRedis.prototype.create = function (model, data, callback) {
2011-11-11 13:16:09 +00:00
var log = this.logger('INCR id:' + model);
2011-10-04 20:17:06 +00:00
this.client.incr('id:' + model, function (err, id) {
2011-11-11 13:16:09 +00:00
log();
2011-10-01 15:51:51 +00:00
data.id = id;
this.save(model, data, function (err) {
if (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);
2011-10-01 15:51:51 +00:00
}.bind(this));
};
BridgeToRedis.prototype.exists = function (model, id, callback) {
2011-11-11 13:16:09 +00:00
var log = this.logger('EXISTS ' + model + ':' + id);
2011-10-01 15:51:51 +00:00
this.client.exists(model + ':' + id, function (err, exists) {
2011-11-11 13:16:09 +00:00
log();
2011-10-01 15:51:51 +00:00
if (callback) {
callback(err, exists);
}
});
};
BridgeToRedis.prototype.find = function find(model, id, callback) {
2011-11-11 13:16:09 +00:00
var t1 = Date.now();
2011-10-01 15:51:51 +00:00
this.client.hgetall(model + ':' + id, function (err, data) {
2011-11-11 13:16:09 +00:00
this.log('HGETALL ' + model + ':' + id, t1);
2011-10-01 15:51:51 +00:00
if (data && data.id) {
data.id = id;
} else {
data = null;
}
callback(err, data);
2011-11-11 13:16:09 +00:00
}.bind(this));
2011-10-01 15:51:51 +00:00
};
BridgeToRedis.prototype.destroy = function destroy(model, id, callback) {
2011-11-11 13:16:09 +00:00
var t1 = Date.now();
2011-10-01 15:51:51 +00:00
this.client.del(model + ':' + id, function (err) {
2011-11-11 13:16:09 +00:00
this.log('DEL ' + model + ':' + id, t1);
2011-10-01 15:51:51 +00:00
callback(err);
2011-11-11 13:16:09 +00:00
}.bind(this));
this.log('SREM s:' + model, t1);
this.client.srem("s:" + model, id);
2011-10-01 15:51:51 +00:00
};
2011-10-05 14:47:26 +00:00
BridgeToRedis.prototype.possibleIndexes = function (model, filter) {
2011-11-14 08:46:48 +00:00
if (!filter || Object.keys(filter.where || {}).length === 0) return false;
2011-10-04 20:17:06 +00:00
2011-10-05 14:47:26 +00:00
var foundIndex = [];
Object.keys(filter.where).forEach(function (key) {
if (this.indexes[model][key] && typeof filter.where[key] === 'string') {
foundIndex.push('i:' + model + ':' + key + ':' + filter.where[key]);
2011-10-04 20:17:06 +00:00
}
}.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();
2011-10-05 14:47:26 +00:00
var client = this.client;
2011-11-11 13:16:09 +00:00
var log = this.log;
var t1 = Date.now();
var cmd;
var that = this;
var sortCmd = [];
var props = this._models[model].properties;
var allNumeric = true;
// ORDER
if (filter && filter.order){
var orders = filter.order;
if (typeof filter.order === "string"){
orders = [filter.order];
}
orders.forEach(function (key){
if (props[key].type.name !== 'Number' && props[key].type.name !== 'Date') {
allNumeric = false;
}
sortCmd.push("BY", model + ":*->" + key);
});
}
// LIMIT
if (filter && filter.limit){
var from = (filter.offset || 0), to = from + filter.limit;
sortCmd.push("LIMIT", from, to);
}
2011-10-04 20:17:06 +00:00
// we need ALPHA modifier when sorting string values
// the only case it's not required - we sort numbers
// TODO: check if we sort numbers
if (!allNumeric) {
sortCmd.push('ALPHA');
}
// 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){
if (err) {
return callback(err, []);
}
var keys = ids.map(function (i) {
return model + ":" + i;
});
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);
}
2011-10-04 20:17:06 +00:00
}
function handleKeys(err, keys) {
2011-11-11 13:16:09 +00:00
log(cmd, t1);
var t2 = Date.now();
2011-10-01 15:51:51 +00:00
if (err) {
return callback(err, []);
}
var query = keys.map(function (key) {
return ['hgetall', key];
});
2011-10-05 14:47:26 +00:00
client.multi(query).exec(function (err, replies) {
2011-11-11 13:16:09 +00:00
log(query, t2);
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.where === 'function') {
return filter.where;
2011-10-01 15:51:51 +00:00
}
2011-11-14 08:46:48 +00:00
var keys = Object.keys(filter.where || {});
2011-10-01 15:51:51 +00:00
return function (obj) {
var pass = true;
keys.forEach(function (key) {
if (!test(filter.where[key], obj[key])) {
2011-10-01 15:51:51 +00:00
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) {
2011-11-11 13:16:09 +00:00
var keysQuery = model + ':*';
var t1 = Date.now();
this.client.keys(keysQuery, function (err, keys) {
this.log('KEYS ' + keysQuery, t1);
2011-10-01 15:51:51 +00:00
if (err) {
return callback(err, []);
}
var query = keys.map(function (key) {
return ['del', key];
});
2011-11-11 13:16:09 +00:00
var t2 = Date.now();
2011-10-01 15:51:51 +00:00
this.client.multi(query).exec(function (err, replies) {
2011-11-11 13:16:09 +00:00
this.log(query, t2);
this.client.del('s:' + model, function () {
callback(err);
});
2011-11-11 13:16:09 +00:00
}.bind(this));
2011-10-01 15:51:51 +00:00
}.bind(this));
};
BridgeToRedis.prototype.count = function count(model, callback) {
2011-11-11 13:16:09 +00:00
var keysQuery = model + ':*';
var t1 = Date.now();
this.client.keys(keysQuery, function (err, keys) {
this.log('KEYS ' + keysQuery, t1);
2011-10-01 15:51:51 +00:00
callback(err, err ? null : keys.length);
2011-11-11 13:16:09 +00:00
}.bind(this));
2011-10-01 15:51:51 +00:00
};
BridgeToRedis.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
2011-11-11 13:16:09 +00:00
var t1 = Date.now();
2012-01-13 20:06:57 +00:00
deleteNulls(data);
2011-10-19 17:17:48 +00:00
this.client.hmset(model + ':' + id, data, function () {
2011-11-11 13:16:09 +00:00
this.log('HMSET ' + model + ':' + id, t1);
2011-10-19 17:17:48 +00:00
this.updateIndexes(model, id, data, cb);
}.bind(this));
2011-10-01 15:51:51 +00:00
};
2012-01-13 20:06:57 +00:00
function deleteNulls(data) {
Object.keys(data).forEach(function (key) {
if (data[key] === null) delete data[key];
});
}
BridgeToRedis.prototype.disconnect = function disconnect() {
2011-11-11 13:16:09 +00:00
this.log('QUIT', Date.now());
this.client.quit();
};