-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
451 lines (382 loc) · 10.7 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
447
448
449
450
451
'use strict';
// require('./platform');
var inherits = require('util').inherits;
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN;
var AbstractIterator = require('abstract-leveldown').AbstractIterator;
var Storage = require('./asyncstorage').Storage;
var StorageCore = require('./asyncstorage-core');
var utils = require('./utils');
var ltgt = require('ltgt');
// see http://stackoverflow.com/a/15349865/680742
var nextTick = global.setImmediate || process.nextTick;
var batchSize = 20
function ADIterator(db, options) {
AbstractIterator.call(this, db);
this._reverse = !!options.reverse;
this._endkey = options.end;
this._startkey = options.start;
this._gt = options.gt;
this._gte = options.gte;
this._lt = options.lt;
this._lte = options.lte;
this._keyAsBuffer = options.keyAsBuffer;
this._valueAsBuffer = options.valueAsBuffer;
this._limit = options.limit;
this._keysOnly = options.values === false
this._count = 0;
this._cache = [];
this._cacheExtinguished = false;
this.onInitCompleteListeners = [];
this.initStarted = true;
var self = this;
this.db.container.keys(function (err, keys) {
if (err) {
self.initError = err;
}
self._keys = keys;
if (keys.length === 0) {
self._pos = 0;
} else {
if (!self._reverse) {
self._startkey = ltgt.lowerBound(options)
self._endkey = ltgt.upperBound(options)
} else {
self._startkey = ltgt.upperBound(options)
self._endkey = ltgt.lowerBound(options)
}
if (self._startkey) {
self._pos = utils.sortedIndexOf(self._keys, self._startkey);
if (self._reverse) {
if (self._pos === self._keys.length) {
self._pos--;
}
else if (self._lt && utils.keyGte(self._keys[self._pos], self._lt)) {
self._pos--;
}
else if (self._lte && utils.keyGt(self._keys[self._pos], self._lte)) {
self._pos--;
}
else if (!self._lt && utils.keyGt(self._keys[self._pos], self._startkey)) {
self._pos--;
}
}
else {
if (self._pos < 0) {
self._pos = 0;
}
else if (self._gt && utils.keyLte(self._keys[self._pos], self._gt)) {
self._pos++;
}
else if (self._gte && utils.keyLt(self._keys[self._pos], self._gt)) {
self._pos++;
}
else if (!self._gt && utils.keyLt(self._keys[self._pos], self._startkey)) {
self._pos++;
}
}
} else {
self._pos = self._reverse ? self._keys.length - 1 : 0;
}
if (self._endkey) {
self._endIndex = utils.sortedIndexOf(self._keys, self._endkey);
if (self._reverse && utils.keyLt(self._keys[self._endIndex], self._endkey)) {
self._endIndex++;
}
else if (!self._reverse && utils.keyGt(self._keys[self._endIndex], self._endkey)) {
self._endIndex--;
}
}
}
self._fillCache(function () {
self.initCompleted = true;
var i = -1;
while (++i < self.onInitCompleteListeners.length) {
nextTick(self.onInitCompleteListeners[i]);
}
})
});
}
inherits(ADIterator, AbstractIterator);
ADIterator.prototype._fillCache = function fillCache(callback) {
var batch = []
for (var i = 0; i < batchSize; i++) {
if (this._limit === 0) {
break;
}
if (this._pos >= this._keys.length || this._pos < 0) { // done reading
break;
}
var key = this._keys[this._pos];
if (!!this._limit && this._limit > 0 && this._count++ >= this._limit) {
break;
}
if (typeof this._endIndex === 'number'
&& (this._reverse ? this._pos < this._endIndex : this._pos > this._endIndex)) {
break;
}
if ((this._lt && utils.keyGte(key, this._lt))
|| (this._lte && utils.keyGt(key, this._lte))
|| (this._gt && utils.keyLte(key, this._gt))
|| (this._gte && utils.keyLt(key, this._gte))) {
break;
}
this._pos += this._reverse ? -1 : 1;
batch.push(key)
}
if (!batch.length) {
this._cache = []
this._cacheExtinguished = true;
return callback()
}
if (this._keysOnly) {
this._cache = batch
return callback()
}
var self = this;
this.db.container.getItems(batch, function (errs, values) {
self._cache = values.map(function (v, idx) {
return {
key: batch[idx],
value: v,
error: errs[idx]
}
})
callback()
});
}
ADIterator.prototype._next = function (callback) {
var self = this;
callback = asyncify(callback)
if (self.initError) {
callback(self.initError);
return;
}
if (self.initStarted) {
if (self.initCompleted) {
getFromCache();
} else {
self.onInitCompleteListeners.push(getFromCache);
}
return;
}
function getFromCache() {
if (self._cacheExtinguished) {
return callback()
}
if (self._cache.length) {
var cached = self._cache.shift()
if (self._keysOnly) {
return callback(null, self._keyAsBuffer ? new Buffer(cached) : cached)
}
if (cached.error) {
if (cached.error.message == 'NotFound') {
return nextTick(function () {
self._next(callback)
})
} else {
return callback(cached.error)
}
}
callback(null,
self._keyAsBuffer ? new Buffer(cached.key) : cached.key,
self._valueAsBuffer ? new Buffer(cached.value) : cached.value)
return
} else {
self._fillCache(getFromCache)
}
}
};
function AD(location, opts) {
if (!(this instanceof AD)) {
return new AD(location, opts);
}
AbstractLevelDOWN.call(this, location);
this.container = new Storage(location, opts);
}
inherits(AD, AbstractLevelDOWN);
AD.prototype._open = function (options, callback) {
this.container.init(callback);
};
AD.prototype._multiPut = function (pairs, options, callback) {
var normalized = []
var err
pairs.every(([key, value]) => {
err = checkKeyValue(key, 'key');
if (err) return
if (checkKeyValue(value, 'value')) {
normalized.push([key, ''])
return true
}
if (value !== null
&& typeof value === 'object'
&& !Buffer.isBuffer(value)
&& value.buffer === undefined) {
var obj = {};
obj.storetype = 'json';
obj.data = value;
value = JSON.stringify(obj);
}
normalized.push([key, value])
return true
})
if (err) {
nextTick(() => callback(err))
} else {
this.container.setItems(normalized, callback)
}
}
AD.prototype._put = function (key, value, options, callback) {
return this._multiPut([[key, value]], options, callback)
};
AD.prototype._get = function (key, options, callback) {
var err = checkKeyValue(key, 'key');
if (err) {
return nextTick(() => callback(err));
}
if (!Buffer.isBuffer(key)) {
key = String(key);
}
this.container.getItem(key, function (err, value) {
if (err) {
return callback(err);
}
if (options.asBuffer !== false && !Buffer.isBuffer(value)) {
value = new Buffer(value);
}
if (options.asBuffer === false) {
if (value.indexOf('{"storetype":"json","data"') > -1) {
var res = JSON.parse(value);
value = res.data;
}
}
callback(null, value);
});
};
AD.prototype._multiDel = function (keys, options, callback) {
// Next tick so that any ADIterator has had enough time to make a snapshot.
// This is for sure a crappy solution, but neither iterator snapshot nor
// delete seem to be timely/urgent features. PRs welcomed :D
nextTick(() => {
var normalized = []
var err
keys.every((key) => {
err = checkKeyValue(key, 'key');
if (err) return
if (!Buffer.isBuffer(key)) {
key = String(key);
}
normalized.push(key)
return true
})
if (err) {
nextTick(() => callback(err))
} else {
this.container.removeItems(keys, callback)
}
})
}
AD.prototype._del = function (key, options, callback) {
return this._multiDel([key], options, callback)
};
AD.prototype._batch = function (array, options, callback) {
var self = this;
nextTick(function () {
var err;
var key;
var value;
if (!Array.isArray(array) || !array.length) return callback()
var toPut = []
var toDel = []
var deleted = {}
for (var i = 0; i < array.length; i++) {
var task = array[i];
if (!task) continue
key = Buffer.isBuffer(task.key) ? task.key : String(task.key);
err = checkKeyValue(key, 'key');
if (err) return callback(err)
if (task.type === 'del') {
deleted[task.key] = true
toDel.push(task.key)
} else if (task.type === 'put') {
value = Buffer.isBuffer(task.value) ? task.value : String(task.value);
if (checkKeyValue(value, 'value')) {
toPut.push([key, ''])
} else {
toPut.push([key, value])
}
}
}
var togo = 0
if (toDel.length) {
togo++
self._multiDel(toDel, null, checkDone)
}
if (toPut.length) {
toPut = toPut.filter(([key]) => !(key in deleted))
}
if (toPut.length) {
togo++
self._multiPut(toPut, null, checkDone)
}
togo++
checkDone() // kick things off
function checkDone (err) {
if (err) {
togo = 0
callback(err)
} else if (--togo === 0) {
callback()
}
}
});
};
AD.prototype._iterator = function (options) {
return new ADIterator(this, options);
};
AD.destroy = function (name, callback) {
StorageCore.destroy(name, callback);
};
function checkKeyValue(obj, type) {
if (obj === null || obj === undefined) {
return new Error(type + ' cannot be `null` or `undefined`');
}
if (obj instanceof Boolean) {
return new Error(type + ' cannot be a boolean');
}
if (obj === '') {
return new Error(type + ' cannot be an empty string');
}
if (obj.toString().indexOf('[object ArrayBuffer]') === 0) {
if (obj.byteLength === 0 || obj.byteLength === undefined) {
return new Error(type + ' cannot be an empty Buffer');
}
}
if (Buffer.isBuffer(obj)) {
if (obj.length === 0) {
return new Error(type + ' cannot be an empty Buffer');
}
} else if (String(obj) === '') {
return new Error(type + ' cannot be an empty String');
}
}
function asyncify (fn) {
if (fn._isAsync) {
return fn
}
var sync = true
nextTick(() => sync = false)
var ret = function () {
var ctx = this
var args = arguments
if (sync) {
nextTick(function () {
fn.apply(ctx, args)
})
} else {
fn.apply(ctx, args)
}
}
ret._isAsync = true
return ret
}
module.exports = AD;