-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
446 lines (339 loc) · 11.2 KB
/
index.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
'use strict';
var isArray = require('util').isArray;
var Lock = require('lock');
var async = require('async');
var kvOps = {};
function atomics(db) {
var atomicsDb = {
db: db
};
// copy all functions from db
for (var k in db) {
if (typeof db[k] === 'function') {
atomicsDb[k] = db[k].bind(db);
}
}
// mix in the methods
for (k in kvOps) {
// some methods overlap, save the original ones under '_' + method
if (atomicsDb[k]) {
atomicsDb['_' + k] = atomicsDb[k];
}
atomicsDb[k] = kvOps[k];
}
atomicsDb._lock = new Lock();
// used to speed up concurrent increments
atomicsDb._counters = {};
// used to speedup concurrent gets of the same key
atomicsDb._fetchers = {};
return atomicsDb;
}
atomics.tuple = _tuple;
// kvOps.append = function (keys, fragment, callback) {
// };
kvOps.counter = function (tuples, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
var tasks = {},
initial;
if (options.hasOwnProperty('initial')) {
initial = options.initial;
delete options.initial;
}
_array(Object.keys(tuples)).forEach(function (key) {
// create async tasks
tasks[key] = function __tryCounter(callback) {
var committer = false;
// if there isn't another batch handling the counter, this will be the
// committer for this key
if (!this._counters[key]) {
// init batch
this._counters[key] = [];
committer = true;
}
var batch = this._counters[key];
batch.length;
// add this entry to batch
batch.push({
delta: parseInt(tuples[key], 10),
callback: function (err, value) {
callback(err, value);
}
});
// if this is the committer for the key, prepare to update it
if (committer) {
// lock and get key
_lockAndGet(this, key, options, function __handleLockAndGet(oldValue, callback) {
// if no oldValue, use initial
var newValue = (oldValue === undefined) ? initial : parseInt(oldValue, 10) + tuples[key]; // TODO: make initial required in doc
// remove batch
delete this._counters[key];
var tmp = newValue;
// prepare to call back others waiting, including own callback
batch[0].err = null;
batch[0].value = tmp;
for (var i = 1; i < batch.length; i++) {
tmp += batch[i].delta;
batch[i].err = null;
batch[i].value = tmp;
}
// update the key
this._put(key, tmp, options, function __handlePut(err) {
if (err) {
return callback(err, true); // true to signal to keep any current batch, since it's a new batch
}
_callbackCounters(this, batch);
// all done
return callback();
});
}.bind(this), function __handleCounterUpdate(err, keepBatch) {
if (err) {
for (var i = 0; i < batch.length; i++) {
batch[i].err = err;
batch[i].value = undefined;
}
if (!keepBatch) {
delete this._counters[key];
}
return _callbackCounters(this, batch);
}
}.bind(this));
}
}.bind(this);
}.bind(this));
// console.log('executing', tasks);
async.parallel(tasks, function __handleGets(err, res) {
if (err) {
return callback(err);
}
var misses = [];
var finalRes = {};
for (var k in res) {
if (res[k] === undefined) {
misses.push(k);
} else {
finalRes[k] = res[k];
}
}
return callback(null, finalRes, misses);
});
return this;
};
kvOps.del = function (keys, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
var tasks = {};
_array(keys).forEach(function (key) {
tasks[key] = _lockAndGet.bind(null, this, key, options, function __handle(value, callback) {
this._del(key, options, callback);
}.bind(this));
}.bind(this));
async.parallel(tasks, function (err) {
if (err) {
return callback(err);
}
return callback(null);
});
return this;
};
kvOps.get = function (keys, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
var tasks = {};
_array(keys).forEach(function (key) {
// tasks[key] = _get.bind(null, this, key, options);
tasks[key] = _collapseGet.bind(null, this, key, options);
}.bind(this));
async.parallel(tasks, function __handleGets(err, res) {
if (err) {
return callback(err);
}
var misses = [];
var finalRes = {};
for (var k in res) {
if (res[k] === undefined) {
misses.push(k);
} else {
finalRes[k] = res[k];
}
}
return callback(null, finalRes, misses);
});
return this;
};
kvOps.insert = function (tuples, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
var tasks = {};
_array(Object.keys(tuples)).forEach(function (key) {
tasks[key] = function __tryInsert(callback) {
// start by quickly checking if the key exists and give up before
// trying to lock it
this.get(key, options, function __handleGet(err, res) {
if (err) {
return callback(err);
}
var value = res[key];
// if key already exists, give up
if (value !== undefined) {
return callback(null, true); // key already existed
}
// there is a strong chance that the key does not exist, let's
// lock and get it
_lockAndGet(this, key, options, function __handle(value, callback) {
// if value already exists, insert fails
if (value !== undefined) {
return callback(null, true); // key already existed
}
this._put(key, tuples[key], options, function __handlePut(err) {
if (err) {
return callback(err);
}
return callback(null, false); // key did not exist
});
}.bind(this), callback);
}.bind(this));
}.bind(this);
}.bind(this));
async.parallel(tasks, function __handleInserts(err, res) {
if (err) {
return callback(err);
}
var existing = [];
for (var k in res) {
if (res[k]) {
existing.push(k);
}
}
return callback(null, existing);
});
return this;
};
// kvOps.prepend = function (keys, fragment, callback) {
// };
kvOps.put = function (tuples, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
var tasks = {};
_array(Object.keys(tuples)).forEach(function (key) {
tasks[key] = this._put.bind(this, key, tuples[key], options);
}.bind(this));
async.parallel(tasks, function __handlePuts(err) {
if (err) {
return callback(err);
}
return callback();
});
return this;
};
kvOps.replace = function (tuples, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
var tasks = {};
_array(Object.keys(tuples)).forEach(function (key) {
tasks[key] = _lockAndGet.bind(null, this, key, options, function __handle(value, callback) {
// if value does not exist, replace fails
if (value === undefined) {
return callback(null, true); // key missing
}
this._put(key, tuples[key], options, function __handlePut(err) {
if (err) {
return callback(err);
}
return callback(null, false); // key existed
});
}.bind(this));
}.bind(this));
async.parallel(tasks, function (err, res) {
if (err) {
return callback(err);
}
var existing = [];
for (var k in res) {
if (res[k]) {
existing.push(k);
}
}
return callback(null, existing);
});
return this;
};
// -----------------------------------------------------------------------------
function _lockAndGet(db, key, options, handler, callback) {
db._lock(key, function __handleLock(release) {
db.get(key, options, function __handleGet(err, res) {
if (err) {
return callback(err);
}
return handler(res[key], release(callback));
});
});
}
function _collapseGet(db, key, options, callback) {
var fetcher = false;
// if there isn't another batch fetching the key, this will be the
// fetcher for this key
if (!db._fetchers[key]) {
// init batch
db._fetchers[key] = [];
fetcher = true;
}
var batch = db._fetchers[key];
// add this callback to batch
var batchPosition = batch.push(callback);
batchPosition--; // fix 0 indexed position
// if this is the fetcher for the key, prepare to get it
if (fetcher) {
// get the key
db._get(key, options, function __handleGet(err, value) {
if (err && !err.notFound) {
return _callbackGets(db, key, err);
}
return _callbackGets(db, key, null, value);
});
}
}
function _array(x) {
return isArray(x) ? x : [x];
}
function _tuple(k, v) {
var res = {};
// if it's a set of keys and respective values
if (isArray(k)) {
for (var i = k.length - 1; i >= 0; i--) {
res[k[i]] = v[i];
}
return res;
}
// just a key and a value
res[k] = v;
return res;
}
function _callbackCounters(db, batch) {
for (var i = 0; i < batch.length; i++) {
setImmediate(batch[i].callback, batch[i].err, batch[i].value);
}
return;
}
function _callbackGets(db, key, err, value) {
var batch = db._fetchers[key];
for (var i = 0; i < batch.length; i++) {
setImmediate(batch[i], err, value);
}
// remove batch
delete db._fetchers[key];
return;
}
module.exports = atomics;