jexidb 1.0.8 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/Database.cjs +388 -312
- package/package.json +1 -1
- package/src/Database.mjs +42 -42
- package/test/test-json-compressed.jdb +0 -0
- package/test/test-json.jdb +0 -0
- package/test/test-v8-compressed.jdb +0 -0
- package/test/test.mjs +8 -3
package/README.md
CHANGED
|
@@ -26,7 +26,8 @@ To create a new instance of the database, you need to provide a file path where
|
|
|
26
26
|
import { Database } from 'jexidb'; // ESM
|
|
27
27
|
|
|
28
28
|
const db = new Database('path/to/database.jdb', { // file will be created if it does not already exist
|
|
29
|
-
v8: false, //
|
|
29
|
+
v8: false, //
|
|
30
|
+
create: true, // create the file if it does not exist (default is true)
|
|
30
31
|
compress: false, // set to true to compress each entry
|
|
31
32
|
compressIndex: false, // set to true to compress the index only
|
|
32
33
|
indexes: { // keys to use in queries, only those key values are kept in memory, so fewer specified keys lead to improved performance
|
package/dist/Database.cjs
CHANGED
|
@@ -53,10 +53,12 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
53
53
|
_this2 = _callSuper(this, Database);
|
|
54
54
|
_this2.opts = Object.assign({
|
|
55
55
|
v8: false,
|
|
56
|
+
create: true,
|
|
57
|
+
indexes: {},
|
|
56
58
|
index: {
|
|
57
59
|
data: {}
|
|
58
60
|
},
|
|
59
|
-
|
|
61
|
+
includeLinePosition: true,
|
|
60
62
|
compress: false,
|
|
61
63
|
compressIndex: false,
|
|
62
64
|
maxMemoryUsage: 64 * 1024 // 64KB
|
|
@@ -122,66 +124,86 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
122
124
|
throw new Error('Cleared, empty file');
|
|
123
125
|
case 14:
|
|
124
126
|
_context.next = 16;
|
|
125
|
-
return this.fileHandler.readLastLine()
|
|
127
|
+
return this.fileHandler.readLastLine()["catch"](function () {
|
|
128
|
+
return 0;
|
|
129
|
+
});
|
|
126
130
|
case 16:
|
|
127
131
|
lastLine = _context.sent;
|
|
128
132
|
if (!(!lastLine || !lastLine.length)) {
|
|
129
|
-
_context.next =
|
|
133
|
+
_context.next = 23;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
if (!this.opts.create) {
|
|
137
|
+
_context.next = 22;
|
|
130
138
|
break;
|
|
131
139
|
}
|
|
132
140
|
throw new Error('File does not exists or is a empty file');
|
|
133
|
-
case
|
|
134
|
-
|
|
141
|
+
case 22:
|
|
142
|
+
throw new Error('File is not a valid database file, expected offsets at the end of the file');
|
|
143
|
+
case 23:
|
|
144
|
+
_context.next = 25;
|
|
135
145
|
return this.serializer.deserialize(lastLine, {
|
|
136
146
|
compress: this.opts.compressIndex
|
|
137
147
|
});
|
|
138
|
-
case
|
|
148
|
+
case 25:
|
|
139
149
|
offsets = _context.sent;
|
|
140
150
|
if (Array.isArray(offsets)) {
|
|
141
|
-
_context.next =
|
|
151
|
+
_context.next = 30;
|
|
142
152
|
break;
|
|
143
153
|
}
|
|
144
|
-
|
|
145
|
-
|
|
154
|
+
if (!this.opts.create) {
|
|
155
|
+
_context.next = 29;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
throw new Error('File does not exists or is a empty file');
|
|
159
|
+
case 29:
|
|
160
|
+
throw new Error('File is not a valid database file, expected offsets at the end of the file to be an array');
|
|
161
|
+
case 30:
|
|
146
162
|
this.indexOffset = offsets[offsets.length - 2];
|
|
147
163
|
this.offsets = offsets;
|
|
148
164
|
ptr = this.locate(offsets.length - 2);
|
|
149
165
|
this.offsets = this.offsets.slice(0, -2);
|
|
150
166
|
this.shouldTruncate = true;
|
|
151
|
-
_context.next =
|
|
167
|
+
_context.next = 37;
|
|
152
168
|
return (_this$fileHandler = this.fileHandler).readRange.apply(_this$fileHandler, _toConsumableArray(ptr));
|
|
153
|
-
case
|
|
169
|
+
case 37:
|
|
154
170
|
indexLine = _context.sent;
|
|
155
|
-
_context.next =
|
|
171
|
+
_context.next = 40;
|
|
156
172
|
return this.serializer.deserialize(indexLine, {
|
|
157
173
|
compress: this.opts.compressIndex
|
|
158
174
|
});
|
|
159
|
-
case
|
|
175
|
+
case 40:
|
|
160
176
|
index = _context.sent;
|
|
161
177
|
index && this.indexManager.load(index);
|
|
162
|
-
_context.next =
|
|
178
|
+
_context.next = 53;
|
|
163
179
|
break;
|
|
164
|
-
case
|
|
165
|
-
_context.prev =
|
|
180
|
+
case 44:
|
|
181
|
+
_context.prev = 44;
|
|
166
182
|
_context.t0 = _context["catch"](9);
|
|
167
183
|
if (Array.isArray(this.offsets)) {
|
|
168
184
|
this.offsets = [];
|
|
169
185
|
}
|
|
170
186
|
this.indexOffset = 0;
|
|
187
|
+
if (!(!this.opts.create && !this.opts.clear)) {
|
|
188
|
+
_context.next = 52;
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
throw _context.t0;
|
|
192
|
+
case 52:
|
|
171
193
|
if (!String(_context.t0).includes('empty file')) {
|
|
172
194
|
console.error('Error loading database:', _context.t0);
|
|
173
195
|
}
|
|
174
|
-
case
|
|
175
|
-
_context.prev =
|
|
196
|
+
case 53:
|
|
197
|
+
_context.prev = 53;
|
|
176
198
|
this.initializing = false;
|
|
177
199
|
this.initialized = true;
|
|
178
200
|
this.emit('init');
|
|
179
|
-
return _context.finish(
|
|
180
|
-
case
|
|
201
|
+
return _context.finish(53);
|
|
202
|
+
case 58:
|
|
181
203
|
case "end":
|
|
182
204
|
return _context.stop();
|
|
183
205
|
}
|
|
184
|
-
}, _callee, this, [[9,
|
|
206
|
+
}, _callee, this, [[9, 44, 53, 58]]);
|
|
185
207
|
}));
|
|
186
208
|
function init() {
|
|
187
209
|
return _init.apply(this, arguments);
|
|
@@ -336,84 +358,38 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
336
358
|
return n !== undefined;
|
|
337
359
|
});
|
|
338
360
|
}
|
|
339
|
-
}, {
|
|
340
|
-
key: "readLines",
|
|
341
|
-
value: function () {
|
|
342
|
-
var _readLines = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(map, ranges) {
|
|
343
|
-
var results, i, start;
|
|
344
|
-
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
345
|
-
while (1) switch (_context4.prev = _context4.next) {
|
|
346
|
-
case 0:
|
|
347
|
-
if (!ranges) ranges = this.getRanges(map);
|
|
348
|
-
_context4.next = 3;
|
|
349
|
-
return this.fileHandler.readRanges(ranges, this.serializer.deserialize.bind(this.serializer));
|
|
350
|
-
case 3:
|
|
351
|
-
results = _context4.sent;
|
|
352
|
-
i = 0;
|
|
353
|
-
_context4.t0 = _regeneratorRuntime().keys(results);
|
|
354
|
-
case 6:
|
|
355
|
-
if ((_context4.t1 = _context4.t0()).done) {
|
|
356
|
-
_context4.next = 14;
|
|
357
|
-
break;
|
|
358
|
-
}
|
|
359
|
-
start = _context4.t1.value;
|
|
360
|
-
if (!(!results[start] || results[start]._ !== undefined)) {
|
|
361
|
-
_context4.next = 10;
|
|
362
|
-
break;
|
|
363
|
-
}
|
|
364
|
-
return _context4.abrupt("continue", 6);
|
|
365
|
-
case 10:
|
|
366
|
-
while (this.offsets[i] != start && i < map.length) i++; // weak comparison as 'start' is a string
|
|
367
|
-
results[start]._ = map[i++];
|
|
368
|
-
_context4.next = 6;
|
|
369
|
-
break;
|
|
370
|
-
case 14:
|
|
371
|
-
return _context4.abrupt("return", Object.values(results).filter(function (r) {
|
|
372
|
-
return r !== undefined;
|
|
373
|
-
}));
|
|
374
|
-
case 15:
|
|
375
|
-
case "end":
|
|
376
|
-
return _context4.stop();
|
|
377
|
-
}
|
|
378
|
-
}, _callee4, this);
|
|
379
|
-
}));
|
|
380
|
-
function readLines(_x, _x2) {
|
|
381
|
-
return _readLines.apply(this, arguments);
|
|
382
|
-
}
|
|
383
|
-
return readLines;
|
|
384
|
-
}()
|
|
385
361
|
}, {
|
|
386
362
|
key: "insert",
|
|
387
363
|
value: function () {
|
|
388
|
-
var _insert = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function
|
|
364
|
+
var _insert = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(data) {
|
|
389
365
|
var line, position;
|
|
390
|
-
return _regeneratorRuntime().wrap(function
|
|
391
|
-
while (1) switch (
|
|
366
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
367
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
392
368
|
case 0:
|
|
393
369
|
if (!this.destroyed) {
|
|
394
|
-
|
|
370
|
+
_context4.next = 2;
|
|
395
371
|
break;
|
|
396
372
|
}
|
|
397
373
|
throw new Error('Database is destroyed');
|
|
398
374
|
case 2:
|
|
399
375
|
if (this.initialized) {
|
|
400
|
-
|
|
376
|
+
_context4.next = 5;
|
|
401
377
|
break;
|
|
402
378
|
}
|
|
403
|
-
|
|
379
|
+
_context4.next = 5;
|
|
404
380
|
return this.init();
|
|
405
381
|
case 5:
|
|
406
382
|
if (this.shouldTruncate) {
|
|
407
383
|
this.writeBuffer.push(this.indexOffset);
|
|
408
384
|
this.shouldTruncate = false;
|
|
409
385
|
}
|
|
410
|
-
|
|
386
|
+
_context4.next = 8;
|
|
411
387
|
return this.serializer.serialize(data, {
|
|
412
388
|
compress: this.opts.compress,
|
|
413
389
|
v8: this.opts.v8
|
|
414
390
|
});
|
|
415
391
|
case 8:
|
|
416
|
-
line =
|
|
392
|
+
line = _context4.sent;
|
|
417
393
|
// using Buffer for offsets accuracy
|
|
418
394
|
position = this.offsets.length;
|
|
419
395
|
this.offsets.push(this.indexOffset);
|
|
@@ -421,21 +397,21 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
421
397
|
this.emit('insert', data, position);
|
|
422
398
|
this.writeBuffer.push(line);
|
|
423
399
|
if (!(!this.flushing && this.currentWriteBufferSize() > this.opts.maxMemoryUsage)) {
|
|
424
|
-
|
|
400
|
+
_context4.next = 17;
|
|
425
401
|
break;
|
|
426
402
|
}
|
|
427
|
-
|
|
403
|
+
_context4.next = 17;
|
|
428
404
|
return this.flush();
|
|
429
405
|
case 17:
|
|
430
406
|
this.indexManager.add(data, position);
|
|
431
407
|
this.shouldSave = true;
|
|
432
408
|
case 19:
|
|
433
409
|
case "end":
|
|
434
|
-
return
|
|
410
|
+
return _context4.stop();
|
|
435
411
|
}
|
|
436
|
-
},
|
|
412
|
+
}, _callee4, this);
|
|
437
413
|
}));
|
|
438
|
-
function insert(
|
|
414
|
+
function insert(_x) {
|
|
439
415
|
return _insert.apply(this, arguments);
|
|
440
416
|
}
|
|
441
417
|
return insert;
|
|
@@ -474,23 +450,23 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
474
450
|
}, {
|
|
475
451
|
key: "_flush",
|
|
476
452
|
value: function () {
|
|
477
|
-
var _flush2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function
|
|
453
|
+
var _flush2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
|
|
478
454
|
var release, fd, data, pos, err;
|
|
479
|
-
return _regeneratorRuntime().wrap(function
|
|
480
|
-
while (1) switch (
|
|
455
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
456
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
481
457
|
case 0:
|
|
482
|
-
|
|
458
|
+
_context5.next = 2;
|
|
483
459
|
return this.mutex.acquire();
|
|
484
460
|
case 2:
|
|
485
|
-
release =
|
|
486
|
-
|
|
461
|
+
release = _context5.sent;
|
|
462
|
+
_context5.next = 5;
|
|
487
463
|
return _fs["default"].promises.open(this.fileHandler.file, 'a');
|
|
488
464
|
case 5:
|
|
489
|
-
fd =
|
|
490
|
-
|
|
465
|
+
fd = _context5.sent;
|
|
466
|
+
_context5.prev = 6;
|
|
491
467
|
case 7:
|
|
492
468
|
if (!this.writeBuffer.length) {
|
|
493
|
-
|
|
469
|
+
_context5.next = 26;
|
|
494
470
|
break;
|
|
495
471
|
}
|
|
496
472
|
data = void 0;
|
|
@@ -498,20 +474,20 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
498
474
|
return typeof b === 'number';
|
|
499
475
|
});
|
|
500
476
|
if (!(pos === 0)) {
|
|
501
|
-
|
|
477
|
+
_context5.next = 21;
|
|
502
478
|
break;
|
|
503
479
|
}
|
|
504
|
-
|
|
480
|
+
_context5.next = 13;
|
|
505
481
|
return fd.close();
|
|
506
482
|
case 13:
|
|
507
|
-
|
|
483
|
+
_context5.next = 15;
|
|
508
484
|
return this.fileHandler.truncate(this.writeBuffer.shift());
|
|
509
485
|
case 15:
|
|
510
|
-
|
|
486
|
+
_context5.next = 17;
|
|
511
487
|
return _fs["default"].promises.open(this.fileHandler.file, 'a');
|
|
512
488
|
case 17:
|
|
513
|
-
fd =
|
|
514
|
-
return
|
|
489
|
+
fd = _context5.sent;
|
|
490
|
+
return _context5.abrupt("continue", 7);
|
|
515
491
|
case 21:
|
|
516
492
|
if (pos === -1) {
|
|
517
493
|
data = Buffer.concat(this.writeBuffer);
|
|
@@ -521,34 +497,34 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
521
497
|
this.writeBuffer.splice(0, pos);
|
|
522
498
|
}
|
|
523
499
|
case 22:
|
|
524
|
-
|
|
500
|
+
_context5.next = 24;
|
|
525
501
|
return fd.write(data);
|
|
526
502
|
case 24:
|
|
527
|
-
|
|
503
|
+
_context5.next = 7;
|
|
528
504
|
break;
|
|
529
505
|
case 26:
|
|
530
506
|
this.shouldSave = true;
|
|
531
|
-
|
|
507
|
+
_context5.next = 32;
|
|
532
508
|
break;
|
|
533
509
|
case 29:
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
console.error('Error flushing:',
|
|
510
|
+
_context5.prev = 29;
|
|
511
|
+
_context5.t0 = _context5["catch"](6);
|
|
512
|
+
console.error('Error flushing:', _context5.t0);
|
|
537
513
|
case 32:
|
|
538
|
-
|
|
539
|
-
|
|
514
|
+
_context5.prev = 32;
|
|
515
|
+
_context5.next = 35;
|
|
540
516
|
return fd.close()["catch"](function (e) {
|
|
541
517
|
return err = e;
|
|
542
518
|
});
|
|
543
519
|
case 35:
|
|
544
520
|
release();
|
|
545
521
|
err && console.error('Error closing file:', err);
|
|
546
|
-
return
|
|
522
|
+
return _context5.finish(32);
|
|
547
523
|
case 38:
|
|
548
524
|
case "end":
|
|
549
|
-
return
|
|
525
|
+
return _context5.stop();
|
|
550
526
|
}
|
|
551
|
-
},
|
|
527
|
+
}, _callee5, this, [[6, 29, 32, 38]]);
|
|
552
528
|
}));
|
|
553
529
|
function _flush() {
|
|
554
530
|
return _flush2.apply(this, arguments);
|
|
@@ -560,9 +536,9 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
560
536
|
value: function walk(map) {
|
|
561
537
|
var _this = this;
|
|
562
538
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
563
|
-
return _wrapAsyncGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function
|
|
564
|
-
var ranges, groupedRanges, fd,
|
|
565
|
-
return _regeneratorRuntime().wrap(function
|
|
539
|
+
return _wrapAsyncGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
|
|
540
|
+
var ranges, groupedRanges, fd, count, _iterator4, _step4, groupedRange, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _loop, _ret, _iterator, _step;
|
|
541
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context7) {
|
|
566
542
|
while (1) switch (_context7.prev = _context7.next) {
|
|
567
543
|
case 0:
|
|
568
544
|
if (!_this.destroyed) {
|
|
@@ -611,129 +587,181 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
611
587
|
case 18:
|
|
612
588
|
fd = _context7.sent;
|
|
613
589
|
_context7.prev = 19;
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
590
|
+
count = 0;
|
|
591
|
+
_iterator4 = _createForOfIteratorHelper(groupedRanges);
|
|
592
|
+
_context7.prev = 22;
|
|
593
|
+
_iterator4.s();
|
|
594
|
+
case 24:
|
|
595
|
+
if ((_step4 = _iterator4.n()).done) {
|
|
596
|
+
_context7.next = 63;
|
|
620
597
|
break;
|
|
621
598
|
}
|
|
622
|
-
groupedRange =
|
|
599
|
+
groupedRange = _step4.value;
|
|
600
|
+
if (!(options.limit && count >= options.limit)) {
|
|
601
|
+
_context7.next = 28;
|
|
602
|
+
break;
|
|
603
|
+
}
|
|
604
|
+
return _context7.abrupt("break", 63);
|
|
605
|
+
case 28:
|
|
623
606
|
_iteratorAbruptCompletion = false;
|
|
624
607
|
_didIteratorError = false;
|
|
625
|
-
_context7.prev =
|
|
608
|
+
_context7.prev = 30;
|
|
609
|
+
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
|
|
610
|
+
var row, entry;
|
|
611
|
+
return _regeneratorRuntime().wrap(function _loop$(_context6) {
|
|
612
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
613
|
+
case 0:
|
|
614
|
+
row = _step.value;
|
|
615
|
+
if (!(options.limit && count >= options.limit)) {
|
|
616
|
+
_context6.next = 3;
|
|
617
|
+
break;
|
|
618
|
+
}
|
|
619
|
+
return _context6.abrupt("return", 0);
|
|
620
|
+
case 3:
|
|
621
|
+
_context6.next = 5;
|
|
622
|
+
return _awaitAsyncGenerator(_this.serializer.deserialize(row.line, {
|
|
623
|
+
compress: _this.opts.compress,
|
|
624
|
+
v8: _this.opts.v8
|
|
625
|
+
}));
|
|
626
|
+
case 5:
|
|
627
|
+
entry = _context6.sent;
|
|
628
|
+
if (!(entry === null)) {
|
|
629
|
+
_context6.next = 8;
|
|
630
|
+
break;
|
|
631
|
+
}
|
|
632
|
+
return _context6.abrupt("return", 1);
|
|
633
|
+
case 8:
|
|
634
|
+
count++;
|
|
635
|
+
if (!options.includeOffsets) {
|
|
636
|
+
_context6.next = 14;
|
|
637
|
+
break;
|
|
638
|
+
}
|
|
639
|
+
_context6.next = 12;
|
|
640
|
+
return {
|
|
641
|
+
entry: entry,
|
|
642
|
+
start: row.start,
|
|
643
|
+
_: row._ || _this.offsets.findIndex(function (n) {
|
|
644
|
+
return n === row.start;
|
|
645
|
+
})
|
|
646
|
+
};
|
|
647
|
+
case 12:
|
|
648
|
+
_context6.next = 17;
|
|
649
|
+
break;
|
|
650
|
+
case 14:
|
|
651
|
+
if (_this.opts.includeLinePosition) {
|
|
652
|
+
entry._ = row._ || _this.offsets.findIndex(function (n) {
|
|
653
|
+
return n === row.start;
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
_context6.next = 17;
|
|
657
|
+
return entry;
|
|
658
|
+
case 17:
|
|
659
|
+
case "end":
|
|
660
|
+
return _context6.stop();
|
|
661
|
+
}
|
|
662
|
+
}, _loop);
|
|
663
|
+
});
|
|
626
664
|
_iterator = _asyncIterator(_this.fileHandler.readGroupedRange(groupedRange, fd));
|
|
627
|
-
case
|
|
628
|
-
_context7.next =
|
|
665
|
+
case 33:
|
|
666
|
+
_context7.next = 35;
|
|
629
667
|
return _awaitAsyncGenerator(_iterator.next());
|
|
630
|
-
case
|
|
668
|
+
case 35:
|
|
631
669
|
if (!(_iteratorAbruptCompletion = !(_step = _context7.sent).done)) {
|
|
632
|
-
_context7.next =
|
|
670
|
+
_context7.next = 45;
|
|
633
671
|
break;
|
|
634
672
|
}
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
}));
|
|
641
|
-
case 35:
|
|
642
|
-
entry = _context7.sent;
|
|
643
|
-
if (!(entry === null)) {
|
|
644
|
-
_context7.next = 38;
|
|
673
|
+
return _context7.delegateYield(_loop(), "t1", 37);
|
|
674
|
+
case 37:
|
|
675
|
+
_ret = _context7.t1;
|
|
676
|
+
if (!(_ret === 0)) {
|
|
677
|
+
_context7.next = 40;
|
|
645
678
|
break;
|
|
646
679
|
}
|
|
647
|
-
return _context7.abrupt("
|
|
648
|
-
case
|
|
649
|
-
if (!
|
|
650
|
-
_context7.next =
|
|
680
|
+
return _context7.abrupt("break", 45);
|
|
681
|
+
case 40:
|
|
682
|
+
if (!(_ret === 1)) {
|
|
683
|
+
_context7.next = 42;
|
|
651
684
|
break;
|
|
652
685
|
}
|
|
653
|
-
_context7.
|
|
654
|
-
|
|
655
|
-
entry: entry,
|
|
656
|
-
start: row.start
|
|
657
|
-
};
|
|
658
|
-
case 41:
|
|
659
|
-
_context7.next = 45;
|
|
660
|
-
break;
|
|
661
|
-
case 43:
|
|
662
|
-
_context7.next = 45;
|
|
663
|
-
return entry;
|
|
664
|
-
case 45:
|
|
686
|
+
return _context7.abrupt("continue", 42);
|
|
687
|
+
case 42:
|
|
665
688
|
_iteratorAbruptCompletion = false;
|
|
666
|
-
_context7.next =
|
|
689
|
+
_context7.next = 33;
|
|
667
690
|
break;
|
|
668
|
-
case
|
|
669
|
-
_context7.next =
|
|
691
|
+
case 45:
|
|
692
|
+
_context7.next = 51;
|
|
670
693
|
break;
|
|
671
|
-
case
|
|
672
|
-
_context7.prev =
|
|
673
|
-
_context7.
|
|
694
|
+
case 47:
|
|
695
|
+
_context7.prev = 47;
|
|
696
|
+
_context7.t2 = _context7["catch"](30);
|
|
674
697
|
_didIteratorError = true;
|
|
675
|
-
_iteratorError = _context7.
|
|
676
|
-
case
|
|
677
|
-
_context7.prev =
|
|
678
|
-
_context7.prev =
|
|
698
|
+
_iteratorError = _context7.t2;
|
|
699
|
+
case 51:
|
|
700
|
+
_context7.prev = 51;
|
|
701
|
+
_context7.prev = 52;
|
|
679
702
|
if (!(_iteratorAbruptCompletion && _iterator["return"] != null)) {
|
|
680
|
-
_context7.next =
|
|
703
|
+
_context7.next = 56;
|
|
681
704
|
break;
|
|
682
705
|
}
|
|
683
|
-
_context7.next =
|
|
706
|
+
_context7.next = 56;
|
|
684
707
|
return _awaitAsyncGenerator(_iterator["return"]());
|
|
685
|
-
case
|
|
686
|
-
_context7.prev =
|
|
708
|
+
case 56:
|
|
709
|
+
_context7.prev = 56;
|
|
687
710
|
if (!_didIteratorError) {
|
|
688
|
-
_context7.next =
|
|
711
|
+
_context7.next = 59;
|
|
689
712
|
break;
|
|
690
713
|
}
|
|
691
714
|
throw _iteratorError;
|
|
692
|
-
case
|
|
693
|
-
return _context7.finish(
|
|
694
|
-
case
|
|
695
|
-
return _context7.finish(
|
|
696
|
-
case
|
|
697
|
-
_context7.next =
|
|
715
|
+
case 59:
|
|
716
|
+
return _context7.finish(56);
|
|
717
|
+
case 60:
|
|
718
|
+
return _context7.finish(51);
|
|
719
|
+
case 61:
|
|
720
|
+
_context7.next = 24;
|
|
698
721
|
break;
|
|
699
|
-
case
|
|
700
|
-
_context7.next =
|
|
722
|
+
case 63:
|
|
723
|
+
_context7.next = 68;
|
|
701
724
|
break;
|
|
725
|
+
case 65:
|
|
726
|
+
_context7.prev = 65;
|
|
727
|
+
_context7.t3 = _context7["catch"](22);
|
|
728
|
+
_iterator4.e(_context7.t3);
|
|
702
729
|
case 68:
|
|
703
730
|
_context7.prev = 68;
|
|
704
|
-
|
|
705
|
-
|
|
731
|
+
_iterator4.f();
|
|
732
|
+
return _context7.finish(68);
|
|
706
733
|
case 71:
|
|
707
734
|
_context7.prev = 71;
|
|
708
|
-
|
|
709
|
-
return _context7.finish(71);
|
|
710
|
-
case 74:
|
|
711
|
-
_context7.prev = 74;
|
|
712
|
-
_context7.next = 77;
|
|
735
|
+
_context7.next = 74;
|
|
713
736
|
return _awaitAsyncGenerator(fd.close());
|
|
714
|
-
case
|
|
715
|
-
return _context7.finish(
|
|
716
|
-
case
|
|
737
|
+
case 74:
|
|
738
|
+
return _context7.finish(71);
|
|
739
|
+
case 75:
|
|
717
740
|
case "end":
|
|
718
741
|
return _context7.stop();
|
|
719
742
|
}
|
|
720
|
-
},
|
|
743
|
+
}, _callee6, null, [[19,, 71, 75], [22, 65, 68, 71], [30, 47, 51, 61], [52,, 56, 60]]);
|
|
721
744
|
}))();
|
|
722
745
|
}
|
|
723
746
|
}, {
|
|
724
747
|
key: "query",
|
|
725
748
|
value: function () {
|
|
726
|
-
var _query = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function
|
|
749
|
+
var _query = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(criteria) {
|
|
727
750
|
var options,
|
|
728
751
|
results,
|
|
752
|
+
_iteratorAbruptCompletion2,
|
|
753
|
+
_didIteratorError2,
|
|
754
|
+
_iteratorError2,
|
|
755
|
+
_iterator2,
|
|
756
|
+
_step2,
|
|
757
|
+
entry,
|
|
729
758
|
_options$orderBy$spli,
|
|
730
759
|
_options$orderBy$spli2,
|
|
731
760
|
field,
|
|
732
761
|
_options$orderBy$spli3,
|
|
733
762
|
direction,
|
|
734
|
-
matchingLines,
|
|
735
763
|
_args8 = arguments;
|
|
736
|
-
return _regeneratorRuntime().wrap(function
|
|
764
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context8) {
|
|
737
765
|
while (1) switch (_context8.prev = _context8.next) {
|
|
738
766
|
case 0:
|
|
739
767
|
options = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {};
|
|
@@ -758,14 +786,54 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
758
786
|
_context8.next = 10;
|
|
759
787
|
return this.save()["catch"](console.error);
|
|
760
788
|
case 10:
|
|
761
|
-
|
|
762
|
-
|
|
789
|
+
results = [];
|
|
790
|
+
_iteratorAbruptCompletion2 = false;
|
|
791
|
+
_didIteratorError2 = false;
|
|
792
|
+
_context8.prev = 13;
|
|
793
|
+
_iterator2 = _asyncIterator(this.walk(criteria, options));
|
|
794
|
+
case 15:
|
|
795
|
+
_context8.next = 17;
|
|
796
|
+
return _iterator2.next();
|
|
797
|
+
case 17:
|
|
798
|
+
if (!(_iteratorAbruptCompletion2 = !(_step2 = _context8.sent).done)) {
|
|
799
|
+
_context8.next = 23;
|
|
763
800
|
break;
|
|
764
801
|
}
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
case
|
|
768
|
-
|
|
802
|
+
entry = _step2.value;
|
|
803
|
+
results.push(entry);
|
|
804
|
+
case 20:
|
|
805
|
+
_iteratorAbruptCompletion2 = false;
|
|
806
|
+
_context8.next = 15;
|
|
807
|
+
break;
|
|
808
|
+
case 23:
|
|
809
|
+
_context8.next = 29;
|
|
810
|
+
break;
|
|
811
|
+
case 25:
|
|
812
|
+
_context8.prev = 25;
|
|
813
|
+
_context8.t1 = _context8["catch"](13);
|
|
814
|
+
_didIteratorError2 = true;
|
|
815
|
+
_iteratorError2 = _context8.t1;
|
|
816
|
+
case 29:
|
|
817
|
+
_context8.prev = 29;
|
|
818
|
+
_context8.prev = 30;
|
|
819
|
+
if (!(_iteratorAbruptCompletion2 && _iterator2["return"] != null)) {
|
|
820
|
+
_context8.next = 34;
|
|
821
|
+
break;
|
|
822
|
+
}
|
|
823
|
+
_context8.next = 34;
|
|
824
|
+
return _iterator2["return"]();
|
|
825
|
+
case 34:
|
|
826
|
+
_context8.prev = 34;
|
|
827
|
+
if (!_didIteratorError2) {
|
|
828
|
+
_context8.next = 37;
|
|
829
|
+
break;
|
|
830
|
+
}
|
|
831
|
+
throw _iteratorError2;
|
|
832
|
+
case 37:
|
|
833
|
+
return _context8.finish(34);
|
|
834
|
+
case 38:
|
|
835
|
+
return _context8.finish(29);
|
|
836
|
+
case 39:
|
|
769
837
|
if (options.orderBy) {
|
|
770
838
|
_options$orderBy$spli = options.orderBy.split(' '), _options$orderBy$spli2 = _slicedToArray(_options$orderBy$spli, 2), field = _options$orderBy$spli2[0], _options$orderBy$spli3 = _options$orderBy$spli2[1], direction = _options$orderBy$spli3 === void 0 ? 'asc' : _options$orderBy$spli3;
|
|
771
839
|
results.sort(function (a, b) {
|
|
@@ -774,32 +842,14 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
774
842
|
return 0;
|
|
775
843
|
});
|
|
776
844
|
}
|
|
777
|
-
if (options.limit) {
|
|
778
|
-
results = results.slice(0, options.limit);
|
|
779
|
-
}
|
|
780
845
|
return _context8.abrupt("return", results);
|
|
781
|
-
case
|
|
782
|
-
_context8.next = 21;
|
|
783
|
-
return this.indexManager.query(criteria, options);
|
|
784
|
-
case 21:
|
|
785
|
-
matchingLines = _context8.sent;
|
|
786
|
-
if (!(!matchingLines || !matchingLines.size)) {
|
|
787
|
-
_context8.next = 24;
|
|
788
|
-
break;
|
|
789
|
-
}
|
|
790
|
-
return _context8.abrupt("return", []);
|
|
791
|
-
case 24:
|
|
792
|
-
_context8.next = 26;
|
|
793
|
-
return this.query(_toConsumableArray(matchingLines), options);
|
|
794
|
-
case 26:
|
|
795
|
-
return _context8.abrupt("return", _context8.sent);
|
|
796
|
-
case 27:
|
|
846
|
+
case 41:
|
|
797
847
|
case "end":
|
|
798
848
|
return _context8.stop();
|
|
799
849
|
}
|
|
800
|
-
},
|
|
850
|
+
}, _callee7, this, [[13, 25, 29, 39], [30,, 34, 38]]);
|
|
801
851
|
}));
|
|
802
|
-
function query(
|
|
852
|
+
function query(_x2) {
|
|
803
853
|
return _query.apply(this, arguments);
|
|
804
854
|
}
|
|
805
855
|
return query;
|
|
@@ -807,119 +857,145 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
807
857
|
}, {
|
|
808
858
|
key: "update",
|
|
809
859
|
value: function () {
|
|
810
|
-
var _update = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function
|
|
860
|
+
var _update = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(criteria, data) {
|
|
811
861
|
var _this8 = this;
|
|
812
862
|
var options,
|
|
813
863
|
matchingLines,
|
|
814
864
|
ranges,
|
|
815
865
|
validMatchingLines,
|
|
816
866
|
entries,
|
|
817
|
-
|
|
867
|
+
_iteratorAbruptCompletion3,
|
|
868
|
+
_didIteratorError3,
|
|
869
|
+
_iteratorError3,
|
|
818
870
|
_iterator3,
|
|
819
871
|
_step3,
|
|
820
|
-
|
|
872
|
+
entry,
|
|
873
|
+
lines,
|
|
874
|
+
_i,
|
|
875
|
+
_entries,
|
|
876
|
+
_entry,
|
|
877
|
+
updated,
|
|
878
|
+
ret,
|
|
821
879
|
offsets,
|
|
822
880
|
byteOffset,
|
|
823
881
|
k,
|
|
824
|
-
|
|
825
|
-
return _regeneratorRuntime().wrap(function
|
|
826
|
-
while (1) switch (
|
|
882
|
+
_args9 = arguments;
|
|
883
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context9) {
|
|
884
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
827
885
|
case 0:
|
|
828
|
-
options =
|
|
886
|
+
options = _args9.length > 2 && _args9[2] !== undefined ? _args9[2] : {};
|
|
829
887
|
if (this.shouldTruncate) {
|
|
830
888
|
this.writeBuffer.push(this.indexOffset);
|
|
831
889
|
this.shouldTruncate = false;
|
|
832
890
|
}
|
|
833
891
|
if (!this.destroyed) {
|
|
834
|
-
|
|
892
|
+
_context9.next = 4;
|
|
835
893
|
break;
|
|
836
894
|
}
|
|
837
895
|
throw new Error('Database is destroyed');
|
|
838
896
|
case 4:
|
|
839
897
|
if (this.initialized) {
|
|
840
|
-
|
|
898
|
+
_context9.next = 7;
|
|
841
899
|
break;
|
|
842
900
|
}
|
|
843
|
-
|
|
901
|
+
_context9.next = 7;
|
|
844
902
|
return this.init();
|
|
845
903
|
case 7:
|
|
846
|
-
|
|
847
|
-
if (!
|
|
848
|
-
|
|
904
|
+
_context9.t0 = this.shouldSave;
|
|
905
|
+
if (!_context9.t0) {
|
|
906
|
+
_context9.next = 11;
|
|
849
907
|
break;
|
|
850
908
|
}
|
|
851
|
-
|
|
909
|
+
_context9.next = 11;
|
|
852
910
|
return this.save()["catch"](console.error);
|
|
853
911
|
case 11:
|
|
854
|
-
|
|
912
|
+
_context9.next = 13;
|
|
855
913
|
return this.indexManager.query(criteria, options);
|
|
856
914
|
case 13:
|
|
857
|
-
matchingLines =
|
|
915
|
+
matchingLines = _context9.sent;
|
|
858
916
|
if (!(!matchingLines || !matchingLines.size)) {
|
|
859
|
-
|
|
917
|
+
_context9.next = 16;
|
|
860
918
|
break;
|
|
861
919
|
}
|
|
862
|
-
return
|
|
920
|
+
return _context9.abrupt("return", []);
|
|
863
921
|
case 16:
|
|
864
922
|
ranges = this.getRanges(_toConsumableArray(matchingLines));
|
|
865
923
|
validMatchingLines = new Set(ranges.map(function (r) {
|
|
866
924
|
return r.index;
|
|
867
925
|
}));
|
|
868
926
|
if (validMatchingLines.size) {
|
|
869
|
-
|
|
927
|
+
_context9.next = 20;
|
|
870
928
|
break;
|
|
871
929
|
}
|
|
872
|
-
return
|
|
930
|
+
return _context9.abrupt("return", []);
|
|
873
931
|
case 20:
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
case 0:
|
|
886
|
-
entry = _step3.value;
|
|
887
|
-
updated = Object.assign(entry, data);
|
|
888
|
-
_context9.next = 4;
|
|
889
|
-
return _this8.serializer.serialize(updated)["catch"](function (e) {
|
|
890
|
-
return err = e;
|
|
891
|
-
});
|
|
892
|
-
case 4:
|
|
893
|
-
ret = _context9.sent;
|
|
894
|
-
err || lines.push(ret);
|
|
895
|
-
case 6:
|
|
896
|
-
case "end":
|
|
897
|
-
return _context9.stop();
|
|
898
|
-
}
|
|
899
|
-
}, _loop);
|
|
900
|
-
});
|
|
901
|
-
_iterator3.s();
|
|
902
|
-
case 28:
|
|
903
|
-
if ((_step3 = _iterator3.n()).done) {
|
|
904
|
-
_context10.next = 32;
|
|
932
|
+
entries = [];
|
|
933
|
+
_iteratorAbruptCompletion3 = false;
|
|
934
|
+
_didIteratorError3 = false;
|
|
935
|
+
_context9.prev = 23;
|
|
936
|
+
_iterator3 = _asyncIterator(this.walk(criteria, options));
|
|
937
|
+
case 25:
|
|
938
|
+
_context9.next = 27;
|
|
939
|
+
return _iterator3.next();
|
|
940
|
+
case 27:
|
|
941
|
+
if (!(_iteratorAbruptCompletion3 = !(_step3 = _context9.sent).done)) {
|
|
942
|
+
_context9.next = 33;
|
|
905
943
|
break;
|
|
906
944
|
}
|
|
907
|
-
|
|
945
|
+
entry = _step3.value;
|
|
946
|
+
entries.push(entry);
|
|
908
947
|
case 30:
|
|
909
|
-
|
|
948
|
+
_iteratorAbruptCompletion3 = false;
|
|
949
|
+
_context9.next = 25;
|
|
910
950
|
break;
|
|
911
|
-
case
|
|
912
|
-
|
|
951
|
+
case 33:
|
|
952
|
+
_context9.next = 39;
|
|
913
953
|
break;
|
|
914
|
-
case
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
954
|
+
case 35:
|
|
955
|
+
_context9.prev = 35;
|
|
956
|
+
_context9.t1 = _context9["catch"](23);
|
|
957
|
+
_didIteratorError3 = true;
|
|
958
|
+
_iteratorError3 = _context9.t1;
|
|
959
|
+
case 39:
|
|
960
|
+
_context9.prev = 39;
|
|
961
|
+
_context9.prev = 40;
|
|
962
|
+
if (!(_iteratorAbruptCompletion3 && _iterator3["return"] != null)) {
|
|
963
|
+
_context9.next = 44;
|
|
964
|
+
break;
|
|
965
|
+
}
|
|
966
|
+
_context9.next = 44;
|
|
967
|
+
return _iterator3["return"]();
|
|
968
|
+
case 44:
|
|
969
|
+
_context9.prev = 44;
|
|
970
|
+
if (!_didIteratorError3) {
|
|
971
|
+
_context9.next = 47;
|
|
972
|
+
break;
|
|
973
|
+
}
|
|
974
|
+
throw _iteratorError3;
|
|
975
|
+
case 47:
|
|
976
|
+
return _context9.finish(44);
|
|
977
|
+
case 48:
|
|
978
|
+
return _context9.finish(39);
|
|
979
|
+
case 49:
|
|
980
|
+
lines = [];
|
|
981
|
+
_i = 0, _entries = entries;
|
|
982
|
+
case 51:
|
|
983
|
+
if (!(_i < _entries.length)) {
|
|
984
|
+
_context9.next = 61;
|
|
985
|
+
break;
|
|
986
|
+
}
|
|
987
|
+
_entry = _entries[_i];
|
|
988
|
+
updated = Object.assign(_entry, data);
|
|
989
|
+
_context9.next = 56;
|
|
990
|
+
return this.serializer.serialize(updated);
|
|
991
|
+
case 56:
|
|
992
|
+
ret = _context9.sent;
|
|
993
|
+
lines.push(ret);
|
|
994
|
+
case 58:
|
|
995
|
+
_i++;
|
|
996
|
+
_context9.next = 51;
|
|
997
|
+
break;
|
|
998
|
+
case 61:
|
|
923
999
|
offsets = [];
|
|
924
1000
|
byteOffset = 0, k = 0;
|
|
925
1001
|
this.offsets.forEach(function (n, i) {
|
|
@@ -933,22 +1009,22 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
933
1009
|
});
|
|
934
1010
|
this.offsets = offsets;
|
|
935
1011
|
this.indexOffset += byteOffset;
|
|
936
|
-
|
|
1012
|
+
_context9.next = 68;
|
|
937
1013
|
return this.fileHandler.replaceLines(ranges, lines);
|
|
938
|
-
case
|
|
1014
|
+
case 68:
|
|
939
1015
|
_toConsumableArray(validMatchingLines).forEach(function (lineNumber, i) {
|
|
940
1016
|
_this8.indexManager.dryRemove(lineNumber);
|
|
941
1017
|
_this8.indexManager.add(entries[i], lineNumber);
|
|
942
1018
|
});
|
|
943
1019
|
this.shouldSave = true;
|
|
944
|
-
return
|
|
945
|
-
case
|
|
1020
|
+
return _context9.abrupt("return", entries);
|
|
1021
|
+
case 71:
|
|
946
1022
|
case "end":
|
|
947
|
-
return
|
|
1023
|
+
return _context9.stop();
|
|
948
1024
|
}
|
|
949
|
-
},
|
|
1025
|
+
}, _callee8, this, [[23, 35, 39, 49], [40,, 44, 48]]);
|
|
950
1026
|
}));
|
|
951
|
-
function update(
|
|
1027
|
+
function update(_x3, _x4) {
|
|
952
1028
|
return _update.apply(this, arguments);
|
|
953
1029
|
}
|
|
954
1030
|
return update;
|
|
@@ -956,7 +1032,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
956
1032
|
}, {
|
|
957
1033
|
key: "delete",
|
|
958
1034
|
value: function () {
|
|
959
|
-
var _delete2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function
|
|
1035
|
+
var _delete2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(criteria) {
|
|
960
1036
|
var options,
|
|
961
1037
|
matchingLines,
|
|
962
1038
|
ranges,
|
|
@@ -964,51 +1040,51 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
964
1040
|
offsets,
|
|
965
1041
|
byteOffset,
|
|
966
1042
|
k,
|
|
967
|
-
|
|
968
|
-
return _regeneratorRuntime().wrap(function
|
|
969
|
-
while (1) switch (
|
|
1043
|
+
_args10 = arguments;
|
|
1044
|
+
return _regeneratorRuntime().wrap(function _callee9$(_context10) {
|
|
1045
|
+
while (1) switch (_context10.prev = _context10.next) {
|
|
970
1046
|
case 0:
|
|
971
|
-
options =
|
|
1047
|
+
options = _args10.length > 1 && _args10[1] !== undefined ? _args10[1] : {};
|
|
972
1048
|
if (this.shouldTruncate) {
|
|
973
1049
|
this.writeBuffer.push(this.indexOffset);
|
|
974
1050
|
this.shouldTruncate = false;
|
|
975
1051
|
}
|
|
976
1052
|
if (!this.destroyed) {
|
|
977
|
-
|
|
1053
|
+
_context10.next = 4;
|
|
978
1054
|
break;
|
|
979
1055
|
}
|
|
980
1056
|
throw new Error('Database is destroyed');
|
|
981
1057
|
case 4:
|
|
982
1058
|
if (this.initialized) {
|
|
983
|
-
|
|
1059
|
+
_context10.next = 7;
|
|
984
1060
|
break;
|
|
985
1061
|
}
|
|
986
|
-
|
|
1062
|
+
_context10.next = 7;
|
|
987
1063
|
return this.init();
|
|
988
1064
|
case 7:
|
|
989
|
-
|
|
990
|
-
if (!
|
|
991
|
-
|
|
1065
|
+
_context10.t0 = this.shouldSave;
|
|
1066
|
+
if (!_context10.t0) {
|
|
1067
|
+
_context10.next = 11;
|
|
992
1068
|
break;
|
|
993
1069
|
}
|
|
994
|
-
|
|
1070
|
+
_context10.next = 11;
|
|
995
1071
|
return this.save()["catch"](console.error);
|
|
996
1072
|
case 11:
|
|
997
|
-
|
|
1073
|
+
_context10.next = 13;
|
|
998
1074
|
return this.indexManager.query(criteria, options);
|
|
999
1075
|
case 13:
|
|
1000
|
-
matchingLines =
|
|
1076
|
+
matchingLines = _context10.sent;
|
|
1001
1077
|
if (!(!matchingLines || !matchingLines.size)) {
|
|
1002
|
-
|
|
1078
|
+
_context10.next = 16;
|
|
1003
1079
|
break;
|
|
1004
1080
|
}
|
|
1005
|
-
return
|
|
1081
|
+
return _context10.abrupt("return", 0);
|
|
1006
1082
|
case 16:
|
|
1007
1083
|
ranges = this.getRanges(_toConsumableArray(matchingLines));
|
|
1008
1084
|
validMatchingLines = new Set(ranges.map(function (r) {
|
|
1009
1085
|
return r.index;
|
|
1010
1086
|
}));
|
|
1011
|
-
|
|
1087
|
+
_context10.next = 20;
|
|
1012
1088
|
return this.fileHandler.replaceLines(ranges, []);
|
|
1013
1089
|
case 20:
|
|
1014
1090
|
offsets = [];
|
|
@@ -1026,14 +1102,14 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
1026
1102
|
this.indexOffset += byteOffset;
|
|
1027
1103
|
this.indexManager.remove(_toConsumableArray(validMatchingLines));
|
|
1028
1104
|
this.shouldSave = true;
|
|
1029
|
-
return
|
|
1105
|
+
return _context10.abrupt("return", ranges.length);
|
|
1030
1106
|
case 28:
|
|
1031
1107
|
case "end":
|
|
1032
|
-
return
|
|
1108
|
+
return _context10.stop();
|
|
1033
1109
|
}
|
|
1034
|
-
},
|
|
1110
|
+
}, _callee9, this);
|
|
1035
1111
|
}));
|
|
1036
|
-
function _delete(
|
|
1112
|
+
function _delete(_x5) {
|
|
1037
1113
|
return _delete2.apply(this, arguments);
|
|
1038
1114
|
}
|
|
1039
1115
|
return _delete;
|
|
@@ -1041,16 +1117,16 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
1041
1117
|
}, {
|
|
1042
1118
|
key: "destroy",
|
|
1043
1119
|
value: function () {
|
|
1044
|
-
var _destroy = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function
|
|
1045
|
-
return _regeneratorRuntime().wrap(function
|
|
1046
|
-
while (1) switch (
|
|
1120
|
+
var _destroy = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10() {
|
|
1121
|
+
return _regeneratorRuntime().wrap(function _callee10$(_context11) {
|
|
1122
|
+
while (1) switch (_context11.prev = _context11.next) {
|
|
1047
1123
|
case 0:
|
|
1048
|
-
|
|
1049
|
-
if (!
|
|
1050
|
-
|
|
1124
|
+
_context11.t0 = this.shouldSave;
|
|
1125
|
+
if (!_context11.t0) {
|
|
1126
|
+
_context11.next = 4;
|
|
1051
1127
|
break;
|
|
1052
1128
|
}
|
|
1053
|
-
|
|
1129
|
+
_context11.next = 4;
|
|
1054
1130
|
return this.save()["catch"](console.error);
|
|
1055
1131
|
case 4:
|
|
1056
1132
|
this.destroyed = true;
|
|
@@ -1061,9 +1137,9 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
|
1061
1137
|
this.fileHandler.destroy();
|
|
1062
1138
|
case 10:
|
|
1063
1139
|
case "end":
|
|
1064
|
-
return
|
|
1140
|
+
return _context11.stop();
|
|
1065
1141
|
}
|
|
1066
|
-
},
|
|
1142
|
+
}, _callee10, this);
|
|
1067
1143
|
}));
|
|
1068
1144
|
function destroy() {
|
|
1069
1145
|
return _destroy.apply(this, arguments);
|
package/package.json
CHANGED
package/src/Database.mjs
CHANGED
|
@@ -10,8 +10,10 @@ export class Database extends EventEmitter {
|
|
|
10
10
|
super()
|
|
11
11
|
this.opts = Object.assign({
|
|
12
12
|
v8: false,
|
|
13
|
-
|
|
13
|
+
create: true,
|
|
14
14
|
indexes: {},
|
|
15
|
+
index: { data: {} },
|
|
16
|
+
includeLinePosition: true,
|
|
15
17
|
compress: false,
|
|
16
18
|
compressIndex: false,
|
|
17
19
|
maxMemoryUsage: 64 * 1024 // 64KB
|
|
@@ -41,13 +43,20 @@ export class Database extends EventEmitter {
|
|
|
41
43
|
await this.fileHandler.truncate(0).catch(console.error)
|
|
42
44
|
throw new Error('Cleared, empty file')
|
|
43
45
|
}
|
|
44
|
-
const lastLine = await this.fileHandler.readLastLine()
|
|
46
|
+
const lastLine = await this.fileHandler.readLastLine().catch(() => 0)
|
|
45
47
|
if (!lastLine || !lastLine.length) {
|
|
46
|
-
|
|
48
|
+
if (this.opts.create) {
|
|
49
|
+
throw new Error('File does not exists or is a empty file')
|
|
50
|
+
} else {
|
|
51
|
+
throw new Error('File is not a valid database file, expected offsets at the end of the file')
|
|
52
|
+
}
|
|
47
53
|
}
|
|
48
54
|
const offsets = await this.serializer.deserialize(lastLine, { compress: this.opts.compressIndex })
|
|
49
55
|
if (!Array.isArray(offsets)) {
|
|
50
|
-
|
|
56
|
+
if(this.opts.create) {
|
|
57
|
+
throw new Error('File does not exists or is a empty file')
|
|
58
|
+
}
|
|
59
|
+
throw new Error('File is not a valid database file, expected offsets at the end of the file to be an array')
|
|
51
60
|
}
|
|
52
61
|
this.indexOffset = offsets[offsets.length - 2]
|
|
53
62
|
this.offsets = offsets
|
|
@@ -62,7 +71,9 @@ export class Database extends EventEmitter {
|
|
|
62
71
|
this.offsets = []
|
|
63
72
|
}
|
|
64
73
|
this.indexOffset = 0
|
|
65
|
-
if
|
|
74
|
+
if(!this.opts.create && !this.opts.clear) {
|
|
75
|
+
throw e
|
|
76
|
+
} else if (!String(e).includes('empty file')) {
|
|
66
77
|
console.error('Error loading database:', e)
|
|
67
78
|
}
|
|
68
79
|
} finally {
|
|
@@ -130,18 +141,6 @@ export class Database extends EventEmitter {
|
|
|
130
141
|
}).filter(n => n !== undefined)
|
|
131
142
|
}
|
|
132
143
|
|
|
133
|
-
async readLines(map, ranges) {
|
|
134
|
-
if (!ranges) ranges = this.getRanges(map)
|
|
135
|
-
const results = await this.fileHandler.readRanges(ranges, this.serializer.deserialize.bind(this.serializer))
|
|
136
|
-
let i = 0
|
|
137
|
-
for (const start in results) {
|
|
138
|
-
if (!results[start] || results[start]._ !== undefined) continue
|
|
139
|
-
while (this.offsets[i] != start && i < map.length) i++ // weak comparison as 'start' is a string
|
|
140
|
-
results[start]._ = map[i++]
|
|
141
|
-
}
|
|
142
|
-
return Object.values(results).filter(r => r !== undefined)
|
|
143
|
-
}
|
|
144
|
-
|
|
145
144
|
async insert(data) {
|
|
146
145
|
if (this.destroyed) throw new Error('Database is destroyed')
|
|
147
146
|
if (!this.initialized) await this.init()
|
|
@@ -232,13 +231,24 @@ export class Database extends EventEmitter {
|
|
|
232
231
|
const groupedRanges = await this.fileHandler.groupedRanges(ranges)
|
|
233
232
|
const fd = await fs.promises.open(this.fileHandler.file, 'r')
|
|
234
233
|
try {
|
|
234
|
+
let count = 0
|
|
235
235
|
for (const groupedRange of groupedRanges) {
|
|
236
|
+
if (options.limit && count >= options.limit) {
|
|
237
|
+
break
|
|
238
|
+
}
|
|
236
239
|
for await (const row of this.fileHandler.readGroupedRange(groupedRange, fd)) {
|
|
240
|
+
if (options.limit && count >= options.limit) {
|
|
241
|
+
break
|
|
242
|
+
}
|
|
237
243
|
const entry = await this.serializer.deserialize(row.line, { compress: this.opts.compress, v8: this.opts.v8 })
|
|
238
244
|
if (entry === null) continue
|
|
245
|
+
count++
|
|
239
246
|
if (options.includeOffsets) {
|
|
240
|
-
yield { entry, start: row.start }
|
|
247
|
+
yield { entry, start: row.start, _: row._ || this.offsets.findIndex(n => n === row.start) }
|
|
241
248
|
} else {
|
|
249
|
+
if (this.opts.includeLinePosition) {
|
|
250
|
+
entry._ = row._ || this.offsets.findIndex(n => n === row.start)
|
|
251
|
+
}
|
|
242
252
|
yield entry
|
|
243
253
|
}
|
|
244
254
|
}
|
|
@@ -252,27 +262,17 @@ export class Database extends EventEmitter {
|
|
|
252
262
|
if (this.destroyed) throw new Error('Database is destroyed')
|
|
253
263
|
if (!this.initialized) await this.init()
|
|
254
264
|
this.shouldSave && await this.save().catch(console.error)
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
}
|
|
265
|
-
if (options.limit) {
|
|
266
|
-
results = results.slice(0, options.limit);
|
|
267
|
-
}
|
|
268
|
-
return results
|
|
269
|
-
} else {
|
|
270
|
-
const matchingLines = await this.indexManager.query(criteria, options)
|
|
271
|
-
if (!matchingLines || !matchingLines.size) {
|
|
272
|
-
return []
|
|
273
|
-
}
|
|
274
|
-
return await this.query([...matchingLines], options)
|
|
265
|
+
let results = []
|
|
266
|
+
for await (const entry of this.walk(criteria, options)) results.push(entry)
|
|
267
|
+
if (options.orderBy) {
|
|
268
|
+
const [field, direction = 'asc'] = options.orderBy.split(' ')
|
|
269
|
+
results.sort((a, b) => {
|
|
270
|
+
if (a[field] > b[field]) return direction === 'asc' ? 1 : -1
|
|
271
|
+
if (a[field] < b[field]) return direction === 'asc' ? -1 : 1
|
|
272
|
+
return 0;
|
|
273
|
+
})
|
|
275
274
|
}
|
|
275
|
+
return results
|
|
276
276
|
}
|
|
277
277
|
|
|
278
278
|
async update(criteria, data, options={}) {
|
|
@@ -292,13 +292,13 @@ export class Database extends EventEmitter {
|
|
|
292
292
|
if (!validMatchingLines.size) {
|
|
293
293
|
return []
|
|
294
294
|
}
|
|
295
|
-
|
|
295
|
+
let entries = []
|
|
296
|
+
for await (const entry of this.walk(criteria, options)) entries.push(entry)
|
|
296
297
|
const lines = []
|
|
297
298
|
for(const entry of entries) {
|
|
298
|
-
let err
|
|
299
299
|
const updated = Object.assign(entry, data)
|
|
300
|
-
const ret = await this.serializer.serialize(updated)
|
|
301
|
-
|
|
300
|
+
const ret = await this.serializer.serialize(updated)
|
|
301
|
+
lines.push(ret)
|
|
302
302
|
}
|
|
303
303
|
const offsets = []
|
|
304
304
|
let byteOffset = 0, k = 0
|
|
Binary file
|
package/test/test-json.jdb
CHANGED
|
Binary file
|
|
Binary file
|
package/test/test.mjs
CHANGED
|
@@ -83,14 +83,19 @@ const runTests = async (id, name, format, opts) => {
|
|
|
83
83
|
console.assert(pass2, `Round 2 - READ: Test failed: ${character.name} seems to have been teleported out of the database!`);
|
|
84
84
|
if(pass1 && pass2) console.log(`Round 2 - READ: Flawless Victory! All characters inserted successfully, led by ${character.name}.`);
|
|
85
85
|
|
|
86
|
-
// 4. Test
|
|
86
|
+
// 4. Test indexes
|
|
87
|
+
const pass3 = await db.indexManager.readColumnIndex('name').has(character.name)
|
|
88
|
+
console.assert(pass3, `Round 3 - INDEX: Test failed: ${character.name} is not in the index.`);
|
|
89
|
+
if(pass3) console.log(`Round 3 - INDEX: Flawless Victory! ${character.name} is in the index.`);
|
|
90
|
+
|
|
91
|
+
// 5. Test data update
|
|
87
92
|
await db.update({ id: 1 }, { name: character.name + ' Updated' });
|
|
88
93
|
results = await db.query({ id: 1 });
|
|
89
94
|
const pass4 = results.length === 1 && results[0].name === character.name + ' Updated';
|
|
90
95
|
console.assert(pass4, `Round 3 - UPDATE: Test failed: ${character.updateMessage}`);
|
|
91
96
|
if(pass4) console.log(`Round 3 - UPDATE: Flawless Victory! ${character.name} has been updated successfully.`);
|
|
92
97
|
|
|
93
|
-
//
|
|
98
|
+
// 6. Test data deletion
|
|
94
99
|
await db.delete({ name: character.name + ' Updated' });
|
|
95
100
|
results = await db.query({ id: { '<=': 2 } });
|
|
96
101
|
const pass5 = results.length === 1;
|
|
@@ -119,7 +124,7 @@ const runTests = async (id, name, format, opts) => {
|
|
|
119
124
|
}
|
|
120
125
|
|
|
121
126
|
async function runAllTests() {
|
|
122
|
-
const depth =
|
|
127
|
+
const depth = 10
|
|
123
128
|
let err, i = 1
|
|
124
129
|
let tests = [
|
|
125
130
|
['json', 'JSON', { indexes: { id: 'number', name: 'string' }, v8: false, compress: false, compressIndex: false }],
|