forked from 1602/jugglingdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.js
403 lines (358 loc) · 11.9 KB
/
redis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**
* 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);
schema.client.on('connect', callback);
schema.adapter = new BridgeToRedis(schema.client);
};
function BridgeToRedis(client) {
this._models = {};
this.client = client;
this.indexes = {};
}
BridgeToRedis.prototype.define = function (descr) {
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);
};
BridgeToRedis.prototype.save = function (model, data, callback) {
deleteNulls(data);
var log = this.logger('HMSET ' + model + ':' + data.id + ' ...');
this.client.hmset(model + ':' + data.id, data, function (err) {
log();
if (err) return callback(err);
this.updateIndexes(model, data.id, data, callback);
}.bind(this));
};
BridgeToRedis.prototype.updateIndexes = function (model, id, 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 + ':' + id
]);
}
}.bind(this));
if (schedule.length) {
this.client.multi(schedule).exec(function (err) {
callback(err);
});
} else {
callback(null);
}
};
BridgeToRedis.prototype.create = function (model, data, callback) {
var log = this.logger('INCR id:' + model);
this.client.incr('id:' + model, function (err, id) {
log();
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);
}.bind(this));
};
BridgeToRedis.prototype.exists = function (model, id, callback) {
var log = this.logger('EXISTS ' + model + ':' + id);
this.client.exists(model + ':' + id, function (err, exists) {
log();
if (callback) {
callback(err, exists);
}
});
};
BridgeToRedis.prototype.find = function find(model, id, callback) {
var t1 = Date.now();
this.client.hgetall(model + ':' + id, function (err, data) {
this.log('HGETALL ' + model + ':' + id, t1);
if (data && data.id) {
data.id = id;
} else {
data = null;
}
callback(err, data);
}.bind(this));
};
BridgeToRedis.prototype.destroy = function destroy(model, id, callback) {
var t1 = Date.now();
this.client.del(model + ':' + id, function (err) {
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) {
if (!filter || Object.keys(filter.where || {}).length === 0) return false;
var foundIndex = [];
var noIndex = [];
Object.keys(filter.where).forEach(function (key) {
if (this.indexes[model][key] && (typeof filter.where[key] === 'string' || typeof filter.where[key] === 'number')) {
foundIndex.push('i:' + model + ':' + key + ':' + filter.where[key]);
} else {
noIndex.push(key);
}
}.bind(this));
return [foundIndex, noIndex];
};
BridgeToRedis.prototype.all = function all(model, filter, callback) {
var ts = Date.now();
var client = this.client;
var log = this.log;
var t1 = Date.now();
var cmd;
var that = this;
var sortCmd = [];
var props = this._models[model].properties;
var allNumeric = true;
// TODO: we need strict mode when filtration only possible when we have indexes
// WHERE
if (filter && filter.where) {
var pi = this.possibleIndexes(model, filter);
var indexes = pi[0];
var noIndexes = pi[1];
if (indexes.length) {
cmd = 'SINTER "' + indexes.join('" "') + '"';
if (noIndexes.length) {
log(model + ': no indexes found for ', noIndexes.join(', '),
'slow sorting and filtering');
}
indexes.push(noIndexes.length ? orderLimitStageBad : orderLimitStage);
client.sinter.apply(client, indexes);
} else {
// filter manually
cmd = 'KEYS ' + model + ':*';
client.keys(model + ':*', orderLimitStageBad);
}
} else {
// no filtering, just sort/limit (if any)
gotKeys('*');
}
// bad case when we trying to filter on non-indexed fields
// in bad case we need retrieve all data and filter/limit/sort manually
function orderLimitStageBad(err, keys) {
log(cmd, t1);
var t2 = Date.now();
if (err) {
return callback(err, []);
}
var query = keys.map(function (key) {
return ['hgetall', key];
});
client.multi(query).exec(function (err, replies) {
log(query, t2);
gotFilteredData(err, replies.filter(applyFilter(filter)));
});
function gotFilteredData(err, nodes) {
if (err) return callback(null);
if (filter.order) {
var allNumeric = true;
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;
}
});
if (allNumeric) {
nodes = nodes.sort(numerically.bind(orders));
} else {
nodes = nodes.sort(literally.bind(orders));
}
}
// LIMIT
if (filter && filter.limit) {
var from = (filter.offset || 0), to = from + filter.limit;
callback(null, nodes.slice(from, to));
} else {
callback(null, nodes);
}
}
}
function orderLimitStage(err, keys) {
log(cmd, t1);
var t2 = Date.now();
if (err) {
return callback(err, []);
}
gotKeys(keys);
}
function gotKeys(keys) {
// 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 (keys === '*' && filter && filter.limit){
var from = (filter.offset || 0), to = from + filter.limit;
sortCmd.push("LIMIT", from, to);
}
// 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');
}
if (sortCmd.length) {
sortCmd.unshift("s:" + model);
sortCmd.push("GET", "#");
cmd = "SORT " + sortCmd.join(" ");
var ttt = Date.now();
sortCmd.push(function(err, ids){
if (err) {
return callback(err, []);
}
log(cmd, ttt);
var sortedKeys = ids.map(function (i) {
return model + ":" + i;
});
handleKeys(err, intersect(sortedKeys, keys));
});
client.sort.apply(client, sortCmd);
} else {
// no sorting or filtering: just get all keys
if (keys === '*') {
cmd = 'KEYS ' + model + ':*';
client.keys(model + ':*', handleKeys);
} else {
handleKeys(null, keys);
}
}
}
function handleKeys(err, keys) {
var t2 = Date.now();
var query = keys.map(function (key) {
return ['hgetall', key];
});
client.multi(query).exec(function (err, replies) {
log(query, t2);
// console.log('Redis time: %dms', Date.now() - ts);
callback(err, filter ? replies.filter(applyFilter(filter)) : replies);
});
}
return;
function numerically(a, b) {
return a[this[0]] - b[this[0]];
}
function literally(a, b) {
return a[this[0]] > b[this[0]];
}
// TODO: find better intersection method
function intersect(sortedKeys, filteredKeys) {
if (filteredKeys === '*') return sortedKeys;
var index = {};
filteredKeys.forEach(function (x) {
index[x] = true;
});
return sortedKeys.filter(function (x) {
return index[x];
});
}
};
function applyFilter(filter) {
if (typeof filter.where === 'function') {
return filter.where;
}
var keys = Object.keys(filter.where || {});
return function (obj) {
var pass = true;
keys.forEach(function (key) {
if (!test(filter.where[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) {
var keysQuery = model + ':*';
var t1 = Date.now();
this.client.keys(keysQuery, function (err, keys) {
this.log('KEYS ' + keysQuery, t1);
if (err) {
return callback(err, []);
}
var query = keys.map(function (key) {
return ['del', key];
});
var t2 = Date.now();
this.client.multi(query).exec(function (err, replies) {
this.log(query, t2);
this.client.del('s:' + model, function () {
callback(err);
});
}.bind(this));
}.bind(this));
};
BridgeToRedis.prototype.count = function count(model, callback, where) {
var keysQuery = model + ':*';
var t1 = Date.now();
if (where && Object.keys(where).length) {
this.all(model, {where: where}, function (err, data) {
callback(err, err ? null : data.length);
});
} else {
this.client.keys(keysQuery, function (err, keys) {
this.log('KEYS ' + keysQuery, t1);
callback(err, err ? null : keys.length);
}.bind(this));
}
};
BridgeToRedis.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
var t1 = Date.now();
deleteNulls(data);
this.client.hmset(model + ':' + id, data, function () {
this.log('HMSET ' + model + ':' + id, t1);
this.updateIndexes(model, id, data, cb);
}.bind(this));
};
function deleteNulls(data) {
Object.keys(data).forEach(function (key) {
if (data[key] === null) delete data[key];
});
}
BridgeToRedis.prototype.disconnect = function disconnect() {
this.log('QUIT', Date.now());
this.client.quit();
};