/
LevelDBBackend.js
714 lines (658 loc) · 25.6 KB
/
LevelDBBackend.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/**
* This is a wrapper around the levelup interface for the LevelDB.
* It manages the access to a single table/object store.
* @implements {IBackend}
*/
class LevelDBBackend {
/**
* Creates a wrapper given a JungleDB, the table's name, the database directory and an encoding.
* @param {JungleDB} db The JungleDB object managing the connection.
* @param {string} tableName The table name this object store represents.
* @param {ICodec} [codec] A default codec for the object store.
*/
constructor(db, tableName, codec=null) {
this._db = db;
this._tableName = tableName;
/** @type {Map.<string,PersistentIndex>} */
this._indices = new Map();
this._indicesToCreate = new Map();
this._indicesToDelete = [];
this._codec = codec;
}
/** @type {boolean} */
get connected() {
return this._db.connected;
}
get _valueEncoding() {
return this._codec === null ? JungleDB.JSON_ENCODING : this._codec.valueEncoding;
}
/**
* Initialises the persisted indices of the object store.
* @param {number} oldVersion
* @param {number} newVersion
* @param {*} [keyEncoding]
* @returns {Promise.<Array.<PersistentIndex>>} The list of indices.
*/
async init(oldVersion, newVersion, keyEncoding) {
const encoding = { valueEncoding: this._valueEncoding };
if (keyEncoding) {
encoding.keyEncoding = keyEncoding;
}
this._dbBackend = this._db.backend.sublevel(this._tableName, encoding);
let indexPromises = [];
// Delete indices.
for (const { indexName, upgradeCondition } of this._indicesToDelete) {
if (upgradeCondition === null || upgradeCondition === true || upgradeCondition(oldVersion, newVersion)) {
const index = new PersistentIndex(this, indexName, '');
indexPromises.push(index.destroy());
}
}
this._indicesToDelete = [];
await Promise.all(indexPromises);
indexPromises = [];
for (const [indexName, { index, upgradeCondition }] of this._indicesToCreate) {
indexPromises.push(index.init(oldVersion, newVersion,
upgradeCondition === null || upgradeCondition === true || upgradeCondition(oldVersion, newVersion)));
}
return Promise.all(indexPromises);
}
/**
* A map of index names to indices.
* The index names can be used to access an index.
* @type {Map.<string,IIndex>}
*/
get indices() {
return this._indices;
}
/**
* Internal method called to decode a single value.
* @param {*} value Value to be decoded.
* @param {string} key Key corresponding to the value.
* @returns {*} The decoded value, either by the object store's default or the overriding decoder if given.
*/
decode(value, key) {
if (value === undefined) {
return undefined;
}
if (this._codec !== null && this._codec !== undefined) {
return this._codec.decode(value, key);
}
return value;
}
/**
* Internal method called to encode a single value.
* @param {*} value Value to be encoded.
* @returns {*} The encoded value, either by the object store's default or the overriding decoder if given.
*/
encode(value) {
if (value === undefined) {
return undefined;
}
if (this._codec !== null && this._codec !== undefined) {
return this._codec.encode(value);
}
return value;
}
/**
* Returns a promise of the object stored under the given primary key.
* Resolves to undefined if the key is not present in the object store.
* @param {string} key The primary key to look for.
* @returns {Promise.<*>} A promise of the object stored under the given key, or undefined if not present.
*/
async get(key) {
return new Promise((resolve, error) => {
this._dbBackend.get(key, (err, value) => {
if (err) {
resolve(undefined);
return;
}
try {
resolve(this.decode(value, key));
} catch (e) {
error(e);
}
});
});
}
/**
* Inserts or replaces a key-value pair.
* @param {string} key The primary key to associate the value with.
* @param {*} value The value to write.
* @returns {Promise} The promise resolves after writing to the current object store finished.
*/
async put(key, value) {
const oldValue = await this.get(key);
let batch = [];
batch.push({key: key, value: this.encode(value), type: 'put'});
for (const index of this._indices.values()) {
batch = batch.concat(await index.put(key, value, oldValue));
}
return new Promise((resolve, error) => {
this._dbBackend.batch(batch, err => {
if (err) {
error(err);
return;
}
resolve();
});
});
}
/**
* Removes the key-value pair of the given key from the object store.
* @param {string} key The primary key to delete along with the associated object.
* @returns {Promise} The promise resolves after writing to the current object store finished.
*/
async remove(key) {
const oldValue = await this.get(key);
// Only update if there was a value with that key.
if (oldValue === undefined) {
return Promise.resolve();
}
let batch = [];
batch.push({key: key, type: 'del'});
for (const index of this._indices.values()) {
batch = batch.concat(await index.remove(key, oldValue));
}
return new Promise((resolve, error) => {
this._dbBackend.batch(batch, err => {
if (err) {
error(err);
return;
}
resolve();
});
});
}
/**
* Returns a promise of an array of objects whose primary keys fulfill the given query.
* If the optional query is not given, it returns all objects in the object store.
* If the query is of type KeyRange, it returns all objects whose primary keys are within this range.
* If the query is of type Query, it returns all objects whose primary keys fulfill the query.
* @param {Query|KeyRange} [query] Optional query to check keys against.
* @returns {Promise.<Array.<*>>} A promise of the array of objects relevant to the query.
*/
values(query=null) {
if (query !== null && query instanceof Query) {
return query.values(this);
}
return new Promise((resolve, error) => {
const result = [];
this._dbBackend.createReadStream(LevelDBTools.convertKeyRange(query, { 'values': true, 'keys': true }))
.on('data', data => {
try {
result.push(this.decode(data.value, data.key));
} catch (e) {
error(e);
}
})
.on('error', err => {
error(err);
})
.on('end', () => {
resolve(result);
});
});
}
/**
* Returns a promise of a set of keys fulfilling the given query.
* If the optional query is not given, it returns all keys in the object store.
* If the query is of type KeyRange, it returns all keys of the object store being within this range.
* If the query is of type Query, it returns all keys fulfilling the query.
* @param {Query|KeyRange} [query] Optional query to check keys against.
* @returns {Promise.<Set.<string>>} A promise of the set of keys relevant to the query.
*/
keys(query=null) {
if (query !== null && query instanceof Query) {
return query.keys(this);
}
return new Promise((resolve, error) => {
const result = new Set();
this._dbBackend.createReadStream(LevelDBTools.convertKeyRange(query, { 'values': false, 'keys': true }))
.on('data', data => {
result.add(data);
})
.on('error', err => {
error(err);
})
.on('end', () => {
resolve(result);
})
.on('close', () => {
resolve(result);
});
});
}
/**
* Iterates over the keys in a given range and direction.
* The callback is called for each primary key fulfilling the query
* until it returns false and stops the iteration.
* @param {function(key:string):boolean} callback A predicate called for each key until returning false.
* @param {boolean} ascending Determines the direction of traversal.
* @param {KeyRange} query An optional KeyRange to narrow down the iteration space.
* @returns {Promise} The promise resolves after all elements have been streamed.
*/
keyStream(callback, ascending=true, query=null) {
return new Promise((resolve, error) => {
const stream = this._dbBackend.createReadStream(LevelDBTools.convertKeyRange(query, { 'values': false, 'keys': true, 'reverse': !ascending }));
let stopped = false;
stream.on('data', data => {
try {
if (!callback(data)) {
stopped = true;
stream.pause();
stream.destroy();
}
} catch (e) {
error(e);
}
})
.on('error', err => {
if (!stopped) {
error(err);
}
})
.on('end', () => {
resolve();
})
.on('close', () => {
resolve();
});
});
}
/**
* Iterates over the keys and values in a given range and direction.
* The callback is called for each value and primary key fulfilling the query
* until it returns false and stops the iteration.
* @param {function(value:*, key:string):boolean} callback A predicate called for each value and key until returning false.
* @param {boolean} ascending Determines the direction of traversal.
* @param {KeyRange} query An optional KeyRange to narrow down the iteration space.
* @returns {Promise} The promise resolves after all elements have been streamed.
*/
valueStream(callback, ascending=true, query=null) {
return new Promise((resolve, error) => {
const stream = this._dbBackend.createReadStream(LevelDBTools.convertKeyRange(query, { 'values': true, 'keys': true, 'reverse': !ascending }));
let stopped = false;
stream.on('data', data => {
try {
if (!callback(this.decode(data.value, data.key), data.key)) {
stopped = true;
stream.pause();
stream.destroy();
}
} catch (e) {
stopped = true;
stream.pause();
stream.destroy();
error(e);
}
})
.on('error', err => {
if (!stopped) {
error(err);
} else {
resolve();
}
})
.on('end', () => {
resolve();
})
.on('close', () => {
resolve();
});
});
}
/**
* Returns a promise of the object whose primary key is maximal for the given range.
* If the optional query is not given, it returns the object whose key is maximal.
* If the query is of type KeyRange, it returns the object whose primary key is maximal for the given range.
* @param {KeyRange} [query] Optional query to check keys against.
* @returns {Promise.<*>} A promise of the object relevant to the query.
*/
maxValue(query=null) {
return new Promise((resolve, error) => {
this._dbBackend.createReadStream(LevelDBTools.convertKeyRange(query, { 'values': true, 'keys': true, 'limit': 1, 'reverse': true }))
.on('data', data => {
try {
resolve(this.decode(data.value, data.key));
} catch (e) {
error(e);
}
})
.on('error', err => {
error(err);
});
});
}
/**
* Returns a promise of the key being maximal for the given range.
* If the optional query is not given, it returns the maximal key.
* If the query is of type KeyRange, it returns the key being maximal for the given range.
* @param {KeyRange} [query] Optional query to check keys against.
* @returns {Promise.<string>} A promise of the key relevant to the query.
*/
maxKey(query=null) {
return new Promise((resolve, error) => {
this._dbBackend.createReadStream(LevelDBTools.convertKeyRange(query, { 'values': false, 'keys': true, 'limit': 1, 'reverse': true }))
.on('data', data => {
resolve(data);
})
.on('error', err => {
error(err);
});
});
}
/**
* Returns a promise of the object whose primary key is minimal for the given range.
* If the optional query is not given, it returns the object whose key is minimal.
* If the query is of type KeyRange, it returns the object whose primary key is minimal for the given range.
* @param {KeyRange} [query] Optional query to check keys against.
* @returns {Promise.<*>} A promise of the object relevant to the query.
*/
minValue(query=null) {
return new Promise((resolve, error) => {
this._dbBackend.createReadStream(LevelDBTools.convertKeyRange(query, { 'values': true, 'keys': true, 'limit': 1, 'reverse': false }))
.on('data', data => {
try {
resolve(this.decode(data.value, data.key));
} catch (e) {
error(e);
}
})
.on('error', err => {
error(err);
});
});
}
/**
* Returns a promise of the key being minimal for the given range.
* If the optional query is not given, it returns the minimal key.
* If the query is of type KeyRange, it returns the key being minimal for the given range.
* @param {KeyRange} [query] Optional query to check keys against.
* @returns {Promise.<string>} A promise of the key relevant to the query.
*/
minKey(query=null) {
return new Promise((resolve, error) => {
this._dbBackend.createReadStream(LevelDBTools.convertKeyRange(query, { 'values': false, 'keys': true, 'limit': 1, 'reverse': false }))
.on('data', data => {
resolve(data);
})
.on('error', err => {
error(err);
});
});
}
/**
* Returns the count of entries in the given range.
* If the optional query is not given, it returns the count of entries in the object store.
* If the query is of type KeyRange, it returns the count of entries within the given range.
* @param {KeyRange} [query]
* @returns {Promise.<number>}
*/
async count(query=null) {
return (await this.keys(query)).size;
}
/**
* The wrapper object stores do not support this functionality
* as it is managed by the ObjectStore.
* @param {Transaction} [tx]
* @returns {Promise.<boolean>}
*/
async commit(tx) {
throw new Error('Unsupported operation');
}
/**
* The wrapper object stores do not support this functionality
* as it is managed by the ObjectStore.
* @param {Transaction} [tx]
*/
async abort(tx) {
throw new Error('Unsupported operation');
}
/**
* Returns the index of the given name.
* If the index does not exist, it returns undefined.
* @param {string} indexName The name of the requested index.
* @returns {IIndex} The index associated with the given name.
*/
index(indexName) {
return this._indices.get(indexName);
}
/** @type {string} The own table name. */
get tableName() {
return this._tableName;
}
/**
* Internally applies a transaction to the store's state.
* This needs to be done in batch (as a db level transaction), i.e., either the full state is updated
* or no changes are applied.
* @param {Transaction} tx The transaction to apply.
* @returns {Promise} The promise resolves after applying the transaction.
* @protected
*/
async _apply(tx) {
let batch = await this.applyCombined(tx);
return new Promise((resolve, error) => {
this._dbBackend.batch(batch, err => {
if (err) {
error(err);
return;
}
resolve(true);
});
});
}
/**
* Returns the necessary information in order to flush a combined transaction.
* @param {Transaction} tx The transaction that should be applied to this backend.
* @returns {Promise.<Array>} An array containing the batch operations.
*/
async applyCombined(tx) {
let batch = [];
if (tx._truncated) {
batch = await this._truncate();
}
for (const key of tx._removed) {
batch.push({key: key, type: 'del', prefix: this._dbBackend, valueEncoding: this._valueEncoding});
}
for (const [key, value] of tx._modified) {
batch.push({key: key, value: this.encode(value), type: 'put', prefix: this._dbBackend, valueEncoding: this._valueEncoding});
}
for (const index of this._indices.values()) {
batch = batch.concat(await index._apply(tx));
}
return batch;
}
/**
* Truncates a sublevel object store.
* @param db A levelDB instance.
* @param {string} tableName A table's name.
* @returns {Promise.<void>}
*/
static async truncate(db, tableName) {
const sub = db.sublevel(tableName);
const batch = await LevelDBBackend._truncate(sub);
return new Promise((resolve, error) => {
sub.batch(batch, err => {
if (err) {
error(err);
return;
}
resolve(true);
});
});
}
/**
* Prepares a batch operation to truncate a sublevel.
* @param sub A levelDB sublevel instance.
* @param {string} tableName A table's name.
* @returns {Promise.<Array>}
*/
static async _truncate(sub) {
return new Promise((resolve, error) => {
const batch = [];
sub.createReadStream({ 'values': false, 'keys': true })
.on('data', data => {
batch.push({key: data, type: 'del', prefix: this._dbBackend, valueEncoding: this._valueEncoding});
})
.on('error', err => {
error(err);
})
.on('end', () => {
resolve(batch);
});
});
}
/**
* Empties the object store.
* @returns {Promise} The promise resolves after emptying the object store.
*/
async truncate() {
const batch = await this._truncate();
return new Promise((resolve, error) => {
this._dbBackend.batch(batch, err => {
if (err) {
error(err);
return;
}
resolve(true);
});
});
}
/**
* Empties the object store.
* @returns {Promise} The promise resolves after emptying the object store.
*/
async _truncate() {
let batch = await LevelDBBackend._truncate(this._dbBackend);
// Truncate all indices.
for (const index of this._indices.values()) {
batch = batch.concat(index._truncate());
}
return batch;
}
/**
* Fully deletes the object store and its indices.
* @returns {Promise} The promise resolves after deleting the object store and its indices.
*/
destroy() {
return this.truncate();
}
/**
* Removes all database related files from the folder.
* @returns {Promise} The promise resolves after deleting the object store and its indices.
*/
static async destroy(databaseDirectory) {
return new Promise((resolve, error) => {
require('leveldown').destroy(databaseDirectory, err => {
if (err) {
error(err);
return;
}
resolve();
});
});
}
/**
* Internal method to close the backend's connection.
* @returns {Promise} The promise resolves after closing the object store.
* @private
*/
_close() {
return new Promise((resolve, error) => {
this._dbBackend.close(err => {
if (err) {
error(err);
return;
}
resolve();
});
});
}
/**
* Applies a function on all key-value pairs in the database.
* This method uses a levelDB read stream to accomplish its task.
* @param {function(key:string, value:*)} func The function to apply.
* @returns {Promise} The promise resolves after applying the function to all entries.
*/
map(func) {
return new Promise((resolve, error) => {
this._dbBackend.createReadStream({ 'values': true, 'keys': true })
.on('data', data => {
try {
func(data.key, this.decode(data.value, data.key));
} catch (e) {
error(e);
}
})
.on('error', err => {
error(err);
})
.on('end', () => {
resolve();
});
});
}
/**
* Creates a new secondary index on the object store.
* Currently, all secondary indices are non-unique.
* They are defined by a key within the object or alternatively a path through the object to a specific subkey.
* For example, ['a', 'b'] could be used to use 'key' as the key in the following object:
* { 'a': { 'b': 'key' } }
* Secondary indices may be multiEntry, i.e., if the keyPath resolves to an iterable object, each item within can
* be used to find this entry.
* If a new object does not possess the key path associated with that index, it is simply ignored.
*
* This function may only be called before the database is connected.
* Moreover, it is only executed on database version updates or on first creation.
* @param {string} indexName The name of the index.
* @param {string|Array.<string>} [keyPath] The path to the key within the object. May be an array for multiple levels.
* @param {{multiEntry:?boolean, upgradeCondition:?boolean|?function(oldVersion:number, newVersion:number):boolean}|boolean} [options] An options object (for deprecated usage: multiEntry boolean).
*/
createIndex(indexName, keyPath, options=false) {
let { multiEntry = false, upgradeCondition = null } = (typeof options === 'object' && options !== null) ? options : {};
if (typeof options !== 'object' && options !== null) multiEntry = options;
if (this._db.connected) throw new Error('Cannot create index while connected');
keyPath = keyPath || indexName;
const index = new PersistentIndex(this, this._db, indexName, keyPath, multiEntry);
this._indices.set(indexName, index);
this._indicesToCreate.set(indexName, { index, upgradeCondition });
}
/**
* Deletes a secondary index from the object store.
* @param indexName
* @param {{upgradeCondition:?boolean|?function(oldVersion:number, newVersion:number):boolean}} [options]
*/
deleteIndex(indexName, options={}) {
let { upgradeCondition = null } = options || {};
if (this._db.connected) throw new Error('Cannot delete index while connected');
this._indicesToDelete.push({ indexName, upgradeCondition });
}
/**
* Closes the object store and potential connections.
* @returns {Promise} The promise resolves after closing the object store.
*/
async close() {
await this._close();
const indexPromises = [];
for (const index of this._indices.values()) {
indexPromises.close(index.close());
}
return Promise.all(indexPromises);
}
/**
* Creates a new transaction, ensuring read isolation
* on the most recently successfully committed state.
* @returns {Transaction} The transaction object.
*/
transaction() {
throw new Error('Unsupported operation');
}
/**
* Checks whether an object store implements the ISynchronousObjectStore interface.
* @returns {boolean} The transaction object.
*/
isSynchronous() {
return false;
}
}
Class.register(LevelDBBackend);