jexidb 1.0.6 → 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/dist/Database.cjs CHANGED
@@ -8,6 +8,7 @@ var _events = require("events");
8
8
  var _FileHandler = _interopRequireDefault(require("./FileHandler.mjs"));
9
9
  var _IndexManager = _interopRequireDefault(require("./IndexManager.mjs"));
10
10
  var _Serializer = _interopRequireDefault(require("./Serializer.mjs"));
11
+ var _asyncMutex = require("async-mutex");
11
12
  var _fs = _interopRequireDefault(require("fs"));
12
13
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
13
14
  function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
@@ -52,10 +53,12 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
52
53
  _this2 = _callSuper(this, Database);
53
54
  _this2.opts = Object.assign({
54
55
  v8: false,
56
+ create: true,
57
+ indexes: {},
55
58
  index: {
56
59
  data: {}
57
60
  },
58
- indexes: {},
61
+ includeLinePosition: true,
59
62
  compress: false,
60
63
  compressIndex: false,
61
64
  maxMemoryUsage: 64 * 1024 // 64KB
@@ -67,6 +70,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
67
70
  _this2.indexManager = new _IndexManager["default"](_this2.opts);
68
71
  _this2.indexOffset = 0;
69
72
  _this2.writeBuffer = [];
73
+ _this2.mutex = new _asyncMutex.Mutex();
70
74
  return _this2;
71
75
  }
72
76
  _inherits(Database, _EventEmitter);
@@ -120,66 +124,86 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
120
124
  throw new Error('Cleared, empty file');
121
125
  case 14:
122
126
  _context.next = 16;
123
- return this.fileHandler.readLastLine();
127
+ return this.fileHandler.readLastLine()["catch"](function () {
128
+ return 0;
129
+ });
124
130
  case 16:
125
131
  lastLine = _context.sent;
126
132
  if (!(!lastLine || !lastLine.length)) {
127
- _context.next = 19;
133
+ _context.next = 23;
134
+ break;
135
+ }
136
+ if (!this.opts.create) {
137
+ _context.next = 22;
128
138
  break;
129
139
  }
130
140
  throw new Error('File does not exists or is a empty file');
131
- case 19:
132
- _context.next = 21;
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;
133
145
  return this.serializer.deserialize(lastLine, {
134
146
  compress: this.opts.compressIndex
135
147
  });
136
- case 21:
148
+ case 25:
137
149
  offsets = _context.sent;
138
150
  if (Array.isArray(offsets)) {
139
- _context.next = 24;
151
+ _context.next = 30;
140
152
  break;
141
153
  }
142
- throw new Error('File to parse offsets, expected an array');
143
- case 24:
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:
144
162
  this.indexOffset = offsets[offsets.length - 2];
145
163
  this.offsets = offsets;
146
164
  ptr = this.locate(offsets.length - 2);
147
165
  this.offsets = this.offsets.slice(0, -2);
148
166
  this.shouldTruncate = true;
149
- _context.next = 31;
167
+ _context.next = 37;
150
168
  return (_this$fileHandler = this.fileHandler).readRange.apply(_this$fileHandler, _toConsumableArray(ptr));
151
- case 31:
169
+ case 37:
152
170
  indexLine = _context.sent;
153
- _context.next = 34;
171
+ _context.next = 40;
154
172
  return this.serializer.deserialize(indexLine, {
155
173
  compress: this.opts.compressIndex
156
174
  });
157
- case 34:
175
+ case 40:
158
176
  index = _context.sent;
159
177
  index && this.indexManager.load(index);
160
- _context.next = 43;
178
+ _context.next = 53;
161
179
  break;
162
- case 38:
163
- _context.prev = 38;
180
+ case 44:
181
+ _context.prev = 44;
164
182
  _context.t0 = _context["catch"](9);
165
183
  if (Array.isArray(this.offsets)) {
166
184
  this.offsets = [];
167
185
  }
168
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:
169
193
  if (!String(_context.t0).includes('empty file')) {
170
194
  console.error('Error loading database:', _context.t0);
171
195
  }
172
- case 43:
173
- _context.prev = 43;
196
+ case 53:
197
+ _context.prev = 53;
174
198
  this.initializing = false;
175
199
  this.initialized = true;
176
200
  this.emit('init');
177
- return _context.finish(43);
178
- case 48:
201
+ return _context.finish(53);
202
+ case 58:
179
203
  case "end":
180
204
  return _context.stop();
181
205
  }
182
- }, _callee, this, [[9, 38, 43, 48]]);
206
+ }, _callee, this, [[9, 44, 53, 58]]);
183
207
  }));
184
208
  function init() {
185
209
  return _init.apply(this, arguments);
@@ -334,105 +358,60 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
334
358
  return n !== undefined;
335
359
  });
336
360
  }
337
- }, {
338
- key: "readLines",
339
- value: function () {
340
- var _readLines = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(map, ranges) {
341
- var results, i, start;
342
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
343
- while (1) switch (_context4.prev = _context4.next) {
344
- case 0:
345
- if (!ranges) ranges = this.getRanges(map);
346
- _context4.next = 3;
347
- return this.fileHandler.readRanges(ranges, this.serializer.deserialize.bind(this.serializer));
348
- case 3:
349
- results = _context4.sent;
350
- i = 0;
351
- _context4.t0 = _regeneratorRuntime().keys(results);
352
- case 6:
353
- if ((_context4.t1 = _context4.t0()).done) {
354
- _context4.next = 14;
355
- break;
356
- }
357
- start = _context4.t1.value;
358
- if (!(!results[start] || results[start]._ !== undefined)) {
359
- _context4.next = 10;
360
- break;
361
- }
362
- return _context4.abrupt("continue", 6);
363
- case 10:
364
- while (this.offsets[i] != start && i < map.length) i++; // weak comparison as 'start' is a string
365
- results[start]._ = map[i++];
366
- _context4.next = 6;
367
- break;
368
- case 14:
369
- return _context4.abrupt("return", Object.values(results).filter(function (r) {
370
- return r !== undefined;
371
- }));
372
- case 15:
373
- case "end":
374
- return _context4.stop();
375
- }
376
- }, _callee4, this);
377
- }));
378
- function readLines(_x, _x2) {
379
- return _readLines.apply(this, arguments);
380
- }
381
- return readLines;
382
- }()
383
361
  }, {
384
362
  key: "insert",
385
363
  value: function () {
386
- var _insert = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(data) {
364
+ var _insert = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(data) {
387
365
  var line, position;
388
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
389
- while (1) switch (_context5.prev = _context5.next) {
366
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
367
+ while (1) switch (_context4.prev = _context4.next) {
390
368
  case 0:
391
369
  if (!this.destroyed) {
392
- _context5.next = 2;
370
+ _context4.next = 2;
393
371
  break;
394
372
  }
395
373
  throw new Error('Database is destroyed');
396
374
  case 2:
397
375
  if (this.initialized) {
398
- _context5.next = 5;
376
+ _context4.next = 5;
399
377
  break;
400
378
  }
401
- _context5.next = 5;
379
+ _context4.next = 5;
402
380
  return this.init();
403
381
  case 5:
404
382
  if (this.shouldTruncate) {
405
383
  this.writeBuffer.push(this.indexOffset);
406
384
  this.shouldTruncate = false;
407
385
  }
408
- _context5.next = 8;
386
+ _context4.next = 8;
409
387
  return this.serializer.serialize(data, {
410
- compress: this.opts.compress
388
+ compress: this.opts.compress,
389
+ v8: this.opts.v8
411
390
  });
412
391
  case 8:
413
- line = _context5.sent;
392
+ line = _context4.sent;
414
393
  // using Buffer for offsets accuracy
415
394
  position = this.offsets.length;
416
395
  this.offsets.push(this.indexOffset);
417
396
  this.indexOffset += line.length;
418
- this.indexManager.add(data, position);
419
397
  this.emit('insert', data, position);
420
398
  this.writeBuffer.push(line);
421
399
  if (!(!this.flushing && this.currentWriteBufferSize() > this.opts.maxMemoryUsage)) {
422
- _context5.next = 18;
400
+ _context4.next = 17;
423
401
  break;
424
402
  }
425
- _context5.next = 18;
403
+ _context4.next = 17;
426
404
  return this.flush();
427
- case 18:
405
+ case 17:
406
+ this.indexManager.add(data, position);
428
407
  this.shouldSave = true;
429
408
  case 19:
430
409
  case "end":
431
- return _context5.stop();
410
+ return _context4.stop();
432
411
  }
433
- }, _callee5, this);
412
+ }, _callee4, this);
434
413
  }));
435
- function insert(_x3) {
414
+ function insert(_x) {
436
415
  return _insert.apply(this, arguments);
437
416
  }
438
417
  return insert;
@@ -471,19 +450,23 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
471
450
  }, {
472
451
  key: "_flush",
473
452
  value: function () {
474
- var _flush2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
475
- var fd, data, pos;
476
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
477
- while (1) switch (_context6.prev = _context6.next) {
453
+ var _flush2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
454
+ var release, fd, data, pos, err;
455
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
456
+ while (1) switch (_context5.prev = _context5.next) {
478
457
  case 0:
479
- _context6.next = 2;
480
- return _fs["default"].promises.open(this.fileHandler.file, 'a');
458
+ _context5.next = 2;
459
+ return this.mutex.acquire();
481
460
  case 2:
482
- fd = _context6.sent;
483
- _context6.prev = 3;
484
- case 4:
461
+ release = _context5.sent;
462
+ _context5.next = 5;
463
+ return _fs["default"].promises.open(this.fileHandler.file, 'a');
464
+ case 5:
465
+ fd = _context5.sent;
466
+ _context5.prev = 6;
467
+ case 7:
485
468
  if (!this.writeBuffer.length) {
486
- _context6.next = 23;
469
+ _context5.next = 26;
487
470
  break;
488
471
  }
489
472
  data = void 0;
@@ -491,21 +474,21 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
491
474
  return typeof b === 'number';
492
475
  });
493
476
  if (!(pos === 0)) {
494
- _context6.next = 18;
477
+ _context5.next = 21;
495
478
  break;
496
479
  }
497
- _context6.next = 10;
480
+ _context5.next = 13;
498
481
  return fd.close();
499
- case 10:
500
- _context6.next = 12;
482
+ case 13:
483
+ _context5.next = 15;
501
484
  return this.fileHandler.truncate(this.writeBuffer.shift());
502
- case 12:
503
- _context6.next = 14;
485
+ case 15:
486
+ _context5.next = 17;
504
487
  return _fs["default"].promises.open(this.fileHandler.file, 'a');
505
- case 14:
506
- fd = _context6.sent;
507
- return _context6.abrupt("continue", 4);
508
- case 18:
488
+ case 17:
489
+ fd = _context5.sent;
490
+ return _context5.abrupt("continue", 7);
491
+ case 21:
509
492
  if (pos === -1) {
510
493
  data = Buffer.concat(this.writeBuffer);
511
494
  this.writeBuffer.length = 0;
@@ -513,31 +496,35 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
513
496
  data = Buffer.concat(this.writeBuffer.slice(0, pos));
514
497
  this.writeBuffer.splice(0, pos);
515
498
  }
516
- case 19:
517
- _context6.next = 21;
499
+ case 22:
500
+ _context5.next = 24;
518
501
  return fd.write(data);
519
- case 21:
520
- _context6.next = 4;
502
+ case 24:
503
+ _context5.next = 7;
521
504
  break;
522
- case 23:
505
+ case 26:
523
506
  this.shouldSave = true;
524
- _context6.next = 29;
507
+ _context5.next = 32;
525
508
  break;
526
- case 26:
527
- _context6.prev = 26;
528
- _context6.t0 = _context6["catch"](3);
529
- console.error('Error flushing:', _context6.t0);
530
509
  case 29:
531
- _context6.prev = 29;
532
- _context6.next = 32;
533
- return fd.close();
510
+ _context5.prev = 29;
511
+ _context5.t0 = _context5["catch"](6);
512
+ console.error('Error flushing:', _context5.t0);
534
513
  case 32:
535
- return _context6.finish(29);
536
- case 33:
514
+ _context5.prev = 32;
515
+ _context5.next = 35;
516
+ return fd.close()["catch"](function (e) {
517
+ return err = e;
518
+ });
519
+ case 35:
520
+ release();
521
+ err && console.error('Error closing file:', err);
522
+ return _context5.finish(32);
523
+ case 38:
537
524
  case "end":
538
- return _context6.stop();
525
+ return _context5.stop();
539
526
  }
540
- }, _callee6, this, [[3, 26, 29, 33]]);
527
+ }, _callee5, this, [[6, 29, 32, 38]]);
541
528
  }));
542
529
  function _flush() {
543
530
  return _flush2.apply(this, arguments);
@@ -549,9 +536,9 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
549
536
  value: function walk(map) {
550
537
  var _this = this;
551
538
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
552
- return _wrapAsyncGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
553
- var ranges, readSize, groupedRanges, fd, _iterator2, _step2, groupedRange, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, row, entry;
554
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
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) {
555
542
  while (1) switch (_context7.prev = _context7.next) {
556
543
  case 0:
557
544
  if (!_this.destroyed) {
@@ -591,128 +578,190 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
591
578
  }
592
579
  }
593
580
  ranges = _this.getRanges(map);
594
- readSize = 512 * 1024; // 512KB
595
- _context7.next = 16;
581
+ _context7.next = 15;
596
582
  return _awaitAsyncGenerator(_this.fileHandler.groupedRanges(ranges));
597
- case 16:
583
+ case 15:
598
584
  groupedRanges = _context7.sent;
599
- _context7.next = 19;
585
+ _context7.next = 18;
600
586
  return _awaitAsyncGenerator(_fs["default"].promises.open(_this.fileHandler.file, 'r'));
601
- case 19:
587
+ case 18:
602
588
  fd = _context7.sent;
603
- _iterator2 = _createForOfIteratorHelper(groupedRanges);
604
- _context7.prev = 21;
605
- _iterator2.s();
606
- case 23:
607
- if ((_step2 = _iterator2.n()).done) {
608
- _context7.next = 64;
589
+ _context7.prev = 19;
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;
597
+ break;
598
+ }
599
+ groupedRange = _step4.value;
600
+ if (!(options.limit && count >= options.limit)) {
601
+ _context7.next = 28;
609
602
  break;
610
603
  }
611
- groupedRange = _step2.value;
604
+ return _context7.abrupt("break", 63);
605
+ case 28:
612
606
  _iteratorAbruptCompletion = false;
613
607
  _didIteratorError = false;
614
- _context7.prev = 27;
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
+ });
615
664
  _iterator = _asyncIterator(_this.fileHandler.readGroupedRange(groupedRange, fd));
616
- case 29:
617
- _context7.next = 31;
665
+ case 33:
666
+ _context7.next = 35;
618
667
  return _awaitAsyncGenerator(_iterator.next());
619
- case 31:
668
+ case 35:
620
669
  if (!(_iteratorAbruptCompletion = !(_step = _context7.sent).done)) {
621
- _context7.next = 46;
670
+ _context7.next = 45;
622
671
  break;
623
672
  }
624
- row = _step.value;
625
- _context7.next = 35;
626
- return _awaitAsyncGenerator(_this.serializer.deserialize(row.line, {
627
- compress: _this.opts.compress
628
- }));
629
- case 35:
630
- entry = _context7.sent;
631
- if (!options.includeOffsets) {
632
- _context7.next = 41;
673
+ return _context7.delegateYield(_loop(), "t1", 37);
674
+ case 37:
675
+ _ret = _context7.t1;
676
+ if (!(_ret === 0)) {
677
+ _context7.next = 40;
633
678
  break;
634
679
  }
635
- _context7.next = 39;
636
- return {
637
- entry: entry,
638
- start: row.start
639
- };
640
- case 39:
641
- _context7.next = 43;
642
- break;
643
- case 41:
644
- _context7.next = 43;
645
- return entry;
646
- case 43:
680
+ return _context7.abrupt("break", 45);
681
+ case 40:
682
+ if (!(_ret === 1)) {
683
+ _context7.next = 42;
684
+ break;
685
+ }
686
+ return _context7.abrupt("continue", 42);
687
+ case 42:
647
688
  _iteratorAbruptCompletion = false;
648
- _context7.next = 29;
689
+ _context7.next = 33;
649
690
  break;
650
- case 46:
651
- _context7.next = 52;
691
+ case 45:
692
+ _context7.next = 51;
652
693
  break;
653
- case 48:
654
- _context7.prev = 48;
655
- _context7.t1 = _context7["catch"](27);
694
+ case 47:
695
+ _context7.prev = 47;
696
+ _context7.t2 = _context7["catch"](30);
656
697
  _didIteratorError = true;
657
- _iteratorError = _context7.t1;
658
- case 52:
698
+ _iteratorError = _context7.t2;
699
+ case 51:
700
+ _context7.prev = 51;
659
701
  _context7.prev = 52;
660
- _context7.prev = 53;
661
702
  if (!(_iteratorAbruptCompletion && _iterator["return"] != null)) {
662
- _context7.next = 57;
703
+ _context7.next = 56;
663
704
  break;
664
705
  }
665
- _context7.next = 57;
706
+ _context7.next = 56;
666
707
  return _awaitAsyncGenerator(_iterator["return"]());
667
- case 57:
668
- _context7.prev = 57;
708
+ case 56:
709
+ _context7.prev = 56;
669
710
  if (!_didIteratorError) {
670
- _context7.next = 60;
711
+ _context7.next = 59;
671
712
  break;
672
713
  }
673
714
  throw _iteratorError;
715
+ case 59:
716
+ return _context7.finish(56);
674
717
  case 60:
675
- return _context7.finish(57);
718
+ return _context7.finish(51);
676
719
  case 61:
677
- return _context7.finish(52);
678
- case 62:
679
- _context7.next = 23;
720
+ _context7.next = 24;
680
721
  break;
681
- case 64:
682
- _context7.next = 69;
722
+ case 63:
723
+ _context7.next = 68;
683
724
  break;
684
- case 66:
685
- _context7.prev = 66;
686
- _context7.t2 = _context7["catch"](21);
687
- _iterator2.e(_context7.t2);
688
- case 69:
689
- _context7.prev = 69;
690
- _iterator2.f();
691
- return _context7.finish(69);
692
- case 72:
725
+ case 65:
726
+ _context7.prev = 65;
727
+ _context7.t3 = _context7["catch"](22);
728
+ _iterator4.e(_context7.t3);
729
+ case 68:
730
+ _context7.prev = 68;
731
+ _iterator4.f();
732
+ return _context7.finish(68);
733
+ case 71:
734
+ _context7.prev = 71;
693
735
  _context7.next = 74;
694
736
  return _awaitAsyncGenerator(fd.close());
695
737
  case 74:
738
+ return _context7.finish(71);
739
+ case 75:
696
740
  case "end":
697
741
  return _context7.stop();
698
742
  }
699
- }, _callee7, null, [[21, 66, 69, 72], [27, 48, 52, 62], [53,, 57, 61]]);
743
+ }, _callee6, null, [[19,, 71, 75], [22, 65, 68, 71], [30, 47, 51, 61], [52,, 56, 60]]);
700
744
  }))();
701
745
  }
702
746
  }, {
703
747
  key: "query",
704
748
  value: function () {
705
- var _query = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(criteria) {
749
+ var _query = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(criteria) {
706
750
  var options,
707
751
  results,
752
+ _iteratorAbruptCompletion2,
753
+ _didIteratorError2,
754
+ _iteratorError2,
755
+ _iterator2,
756
+ _step2,
757
+ entry,
708
758
  _options$orderBy$spli,
709
759
  _options$orderBy$spli2,
710
760
  field,
711
761
  _options$orderBy$spli3,
712
762
  direction,
713
- matchingLines,
714
763
  _args8 = arguments;
715
- return _regeneratorRuntime().wrap(function _callee8$(_context8) {
764
+ return _regeneratorRuntime().wrap(function _callee7$(_context8) {
716
765
  while (1) switch (_context8.prev = _context8.next) {
717
766
  case 0:
718
767
  options = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {};
@@ -737,14 +786,54 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
737
786
  _context8.next = 10;
738
787
  return this.save()["catch"](console.error);
739
788
  case 10:
740
- if (!Array.isArray(criteria)) {
741
- _context8.next = 19;
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;
742
800
  break;
743
801
  }
744
- _context8.next = 13;
745
- return this.readLines(criteria);
746
- case 13:
747
- results = _context8.sent;
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:
748
837
  if (options.orderBy) {
749
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;
750
839
  results.sort(function (a, b) {
@@ -753,32 +842,14 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
753
842
  return 0;
754
843
  });
755
844
  }
756
- if (options.limit) {
757
- results = results.slice(0, options.limit);
758
- }
759
845
  return _context8.abrupt("return", results);
760
- case 19:
761
- _context8.next = 21;
762
- return this.indexManager.query(criteria, options);
763
- case 21:
764
- matchingLines = _context8.sent;
765
- if (!(!matchingLines || !matchingLines.size)) {
766
- _context8.next = 24;
767
- break;
768
- }
769
- return _context8.abrupt("return", []);
770
- case 24:
771
- _context8.next = 26;
772
- return this.query(_toConsumableArray(matchingLines), options);
773
- case 26:
774
- return _context8.abrupt("return", _context8.sent);
775
- case 27:
846
+ case 41:
776
847
  case "end":
777
848
  return _context8.stop();
778
849
  }
779
- }, _callee8, this);
850
+ }, _callee7, this, [[13, 25, 29, 39], [30,, 34, 38]]);
780
851
  }));
781
- function query(_x4) {
852
+ function query(_x2) {
782
853
  return _query.apply(this, arguments);
783
854
  }
784
855
  return query;
@@ -786,119 +857,145 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
786
857
  }, {
787
858
  key: "update",
788
859
  value: function () {
789
- var _update = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(criteria, data) {
860
+ var _update = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(criteria, data) {
790
861
  var _this8 = this;
791
862
  var options,
792
863
  matchingLines,
793
864
  ranges,
794
865
  validMatchingLines,
795
866
  entries,
796
- lines,
867
+ _iteratorAbruptCompletion3,
868
+ _didIteratorError3,
869
+ _iteratorError3,
797
870
  _iterator3,
798
871
  _step3,
799
- _loop,
872
+ entry,
873
+ lines,
874
+ _i,
875
+ _entries,
876
+ _entry,
877
+ updated,
878
+ ret,
800
879
  offsets,
801
880
  byteOffset,
802
881
  k,
803
- _args10 = arguments;
804
- return _regeneratorRuntime().wrap(function _callee9$(_context10) {
805
- while (1) switch (_context10.prev = _context10.next) {
882
+ _args9 = arguments;
883
+ return _regeneratorRuntime().wrap(function _callee8$(_context9) {
884
+ while (1) switch (_context9.prev = _context9.next) {
806
885
  case 0:
807
- options = _args10.length > 2 && _args10[2] !== undefined ? _args10[2] : {};
886
+ options = _args9.length > 2 && _args9[2] !== undefined ? _args9[2] : {};
808
887
  if (this.shouldTruncate) {
809
888
  this.writeBuffer.push(this.indexOffset);
810
889
  this.shouldTruncate = false;
811
890
  }
812
891
  if (!this.destroyed) {
813
- _context10.next = 4;
892
+ _context9.next = 4;
814
893
  break;
815
894
  }
816
895
  throw new Error('Database is destroyed');
817
896
  case 4:
818
897
  if (this.initialized) {
819
- _context10.next = 7;
898
+ _context9.next = 7;
820
899
  break;
821
900
  }
822
- _context10.next = 7;
901
+ _context9.next = 7;
823
902
  return this.init();
824
903
  case 7:
825
- _context10.t0 = this.shouldSave;
826
- if (!_context10.t0) {
827
- _context10.next = 11;
904
+ _context9.t0 = this.shouldSave;
905
+ if (!_context9.t0) {
906
+ _context9.next = 11;
828
907
  break;
829
908
  }
830
- _context10.next = 11;
909
+ _context9.next = 11;
831
910
  return this.save()["catch"](console.error);
832
911
  case 11:
833
- _context10.next = 13;
912
+ _context9.next = 13;
834
913
  return this.indexManager.query(criteria, options);
835
914
  case 13:
836
- matchingLines = _context10.sent;
915
+ matchingLines = _context9.sent;
837
916
  if (!(!matchingLines || !matchingLines.size)) {
838
- _context10.next = 16;
917
+ _context9.next = 16;
839
918
  break;
840
919
  }
841
- return _context10.abrupt("return", []);
920
+ return _context9.abrupt("return", []);
842
921
  case 16:
843
922
  ranges = this.getRanges(_toConsumableArray(matchingLines));
844
923
  validMatchingLines = new Set(ranges.map(function (r) {
845
924
  return r.index;
846
925
  }));
847
926
  if (validMatchingLines.size) {
848
- _context10.next = 20;
927
+ _context9.next = 20;
849
928
  break;
850
929
  }
851
- return _context10.abrupt("return", []);
930
+ return _context9.abrupt("return", []);
852
931
  case 20:
853
- _context10.next = 22;
854
- return this.readLines(_toConsumableArray(validMatchingLines), ranges);
855
- case 22:
856
- entries = _context10.sent;
857
- lines = [];
858
- _iterator3 = _createForOfIteratorHelper(entries);
859
- _context10.prev = 25;
860
- _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
861
- var entry, err, updated, ret;
862
- return _regeneratorRuntime().wrap(function _loop$(_context9) {
863
- while (1) switch (_context9.prev = _context9.next) {
864
- case 0:
865
- entry = _step3.value;
866
- updated = Object.assign(entry, data);
867
- _context9.next = 4;
868
- return _this8.serializer.serialize(updated)["catch"](function (e) {
869
- return err = e;
870
- });
871
- case 4:
872
- ret = _context9.sent;
873
- err || lines.push(ret);
874
- case 6:
875
- case "end":
876
- return _context9.stop();
877
- }
878
- }, _loop);
879
- });
880
- _iterator3.s();
881
- case 28:
882
- if ((_step3 = _iterator3.n()).done) {
883
- _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;
884
943
  break;
885
944
  }
886
- return _context10.delegateYield(_loop(), "t1", 30);
945
+ entry = _step3.value;
946
+ entries.push(entry);
887
947
  case 30:
888
- _context10.next = 28;
948
+ _iteratorAbruptCompletion3 = false;
949
+ _context9.next = 25;
889
950
  break;
890
- case 32:
891
- _context10.next = 37;
951
+ case 33:
952
+ _context9.next = 39;
892
953
  break;
893
- case 34:
894
- _context10.prev = 34;
895
- _context10.t2 = _context10["catch"](25);
896
- _iterator3.e(_context10.t2);
897
- case 37:
898
- _context10.prev = 37;
899
- _iterator3.f();
900
- return _context10.finish(37);
901
- case 40:
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:
902
999
  offsets = [];
903
1000
  byteOffset = 0, k = 0;
904
1001
  this.offsets.forEach(function (n, i) {
@@ -912,22 +1009,22 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
912
1009
  });
913
1010
  this.offsets = offsets;
914
1011
  this.indexOffset += byteOffset;
915
- _context10.next = 47;
1012
+ _context9.next = 68;
916
1013
  return this.fileHandler.replaceLines(ranges, lines);
917
- case 47:
1014
+ case 68:
918
1015
  _toConsumableArray(validMatchingLines).forEach(function (lineNumber, i) {
919
1016
  _this8.indexManager.dryRemove(lineNumber);
920
1017
  _this8.indexManager.add(entries[i], lineNumber);
921
1018
  });
922
1019
  this.shouldSave = true;
923
- return _context10.abrupt("return", entries);
924
- case 50:
1020
+ return _context9.abrupt("return", entries);
1021
+ case 71:
925
1022
  case "end":
926
- return _context10.stop();
1023
+ return _context9.stop();
927
1024
  }
928
- }, _callee9, this, [[25, 34, 37, 40]]);
1025
+ }, _callee8, this, [[23, 35, 39, 49], [40,, 44, 48]]);
929
1026
  }));
930
- function update(_x5, _x6) {
1027
+ function update(_x3, _x4) {
931
1028
  return _update.apply(this, arguments);
932
1029
  }
933
1030
  return update;
@@ -935,7 +1032,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
935
1032
  }, {
936
1033
  key: "delete",
937
1034
  value: function () {
938
- var _delete2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(criteria) {
1035
+ var _delete2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(criteria) {
939
1036
  var options,
940
1037
  matchingLines,
941
1038
  ranges,
@@ -943,51 +1040,51 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
943
1040
  offsets,
944
1041
  byteOffset,
945
1042
  k,
946
- _args11 = arguments;
947
- return _regeneratorRuntime().wrap(function _callee10$(_context11) {
948
- while (1) switch (_context11.prev = _context11.next) {
1043
+ _args10 = arguments;
1044
+ return _regeneratorRuntime().wrap(function _callee9$(_context10) {
1045
+ while (1) switch (_context10.prev = _context10.next) {
949
1046
  case 0:
950
- options = _args11.length > 1 && _args11[1] !== undefined ? _args11[1] : {};
1047
+ options = _args10.length > 1 && _args10[1] !== undefined ? _args10[1] : {};
951
1048
  if (this.shouldTruncate) {
952
1049
  this.writeBuffer.push(this.indexOffset);
953
1050
  this.shouldTruncate = false;
954
1051
  }
955
1052
  if (!this.destroyed) {
956
- _context11.next = 4;
1053
+ _context10.next = 4;
957
1054
  break;
958
1055
  }
959
1056
  throw new Error('Database is destroyed');
960
1057
  case 4:
961
1058
  if (this.initialized) {
962
- _context11.next = 7;
1059
+ _context10.next = 7;
963
1060
  break;
964
1061
  }
965
- _context11.next = 7;
1062
+ _context10.next = 7;
966
1063
  return this.init();
967
1064
  case 7:
968
- _context11.t0 = this.shouldSave;
969
- if (!_context11.t0) {
970
- _context11.next = 11;
1065
+ _context10.t0 = this.shouldSave;
1066
+ if (!_context10.t0) {
1067
+ _context10.next = 11;
971
1068
  break;
972
1069
  }
973
- _context11.next = 11;
1070
+ _context10.next = 11;
974
1071
  return this.save()["catch"](console.error);
975
1072
  case 11:
976
- _context11.next = 13;
1073
+ _context10.next = 13;
977
1074
  return this.indexManager.query(criteria, options);
978
1075
  case 13:
979
- matchingLines = _context11.sent;
1076
+ matchingLines = _context10.sent;
980
1077
  if (!(!matchingLines || !matchingLines.size)) {
981
- _context11.next = 16;
1078
+ _context10.next = 16;
982
1079
  break;
983
1080
  }
984
- return _context11.abrupt("return", 0);
1081
+ return _context10.abrupt("return", 0);
985
1082
  case 16:
986
1083
  ranges = this.getRanges(_toConsumableArray(matchingLines));
987
1084
  validMatchingLines = new Set(ranges.map(function (r) {
988
1085
  return r.index;
989
1086
  }));
990
- _context11.next = 20;
1087
+ _context10.next = 20;
991
1088
  return this.fileHandler.replaceLines(ranges, []);
992
1089
  case 20:
993
1090
  offsets = [];
@@ -1005,14 +1102,14 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
1005
1102
  this.indexOffset += byteOffset;
1006
1103
  this.indexManager.remove(_toConsumableArray(validMatchingLines));
1007
1104
  this.shouldSave = true;
1008
- return _context11.abrupt("return", ranges.length);
1105
+ return _context10.abrupt("return", ranges.length);
1009
1106
  case 28:
1010
1107
  case "end":
1011
- return _context11.stop();
1108
+ return _context10.stop();
1012
1109
  }
1013
- }, _callee10, this);
1110
+ }, _callee9, this);
1014
1111
  }));
1015
- function _delete(_x7) {
1112
+ function _delete(_x5) {
1016
1113
  return _delete2.apply(this, arguments);
1017
1114
  }
1018
1115
  return _delete;
@@ -1020,16 +1117,16 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
1020
1117
  }, {
1021
1118
  key: "destroy",
1022
1119
  value: function () {
1023
- var _destroy = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11() {
1024
- return _regeneratorRuntime().wrap(function _callee11$(_context12) {
1025
- while (1) switch (_context12.prev = _context12.next) {
1120
+ var _destroy = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10() {
1121
+ return _regeneratorRuntime().wrap(function _callee10$(_context11) {
1122
+ while (1) switch (_context11.prev = _context11.next) {
1026
1123
  case 0:
1027
- _context12.t0 = this.shouldSave;
1028
- if (!_context12.t0) {
1029
- _context12.next = 4;
1124
+ _context11.t0 = this.shouldSave;
1125
+ if (!_context11.t0) {
1126
+ _context11.next = 4;
1030
1127
  break;
1031
1128
  }
1032
- _context12.next = 4;
1129
+ _context11.next = 4;
1033
1130
  return this.save()["catch"](console.error);
1034
1131
  case 4:
1035
1132
  this.destroyed = true;
@@ -1040,9 +1137,9 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
1040
1137
  this.fileHandler.destroy();
1041
1138
  case 10:
1042
1139
  case "end":
1043
- return _context12.stop();
1140
+ return _context11.stop();
1044
1141
  }
1045
- }, _callee11, this);
1142
+ }, _callee10, this);
1046
1143
  }));
1047
1144
  function destroy() {
1048
1145
  return _destroy.apply(this, arguments);