jexidb 1.0.5 → 1.0.8

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,14 +8,14 @@ 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
- function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
14
14
  function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
15
15
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
16
16
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
17
17
  function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
18
- function _readOnlyError(r) { throw new TypeError('"' + r + '" is read-only'); }
18
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
19
19
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
20
20
  function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
21
21
  function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
@@ -43,6 +43,8 @@ function _wrapAsyncGenerator(e) { return function () { return new AsyncGenerator
43
43
  function AsyncGenerator(e) { var r, t; function resume(r, t) { try { var n = e[r](t), o = n.value, u = o instanceof _OverloadYield; Promise.resolve(u ? o.v : o).then(function (t) { if (u) { var i = "return" === r ? "return" : "next"; if (!o.k || t.done) return resume(i, t); t = e[i](t).value; } settle(n.done ? "return" : "normal", t); }, function (e) { resume("throw", e); }); } catch (e) { settle("throw", e); } } function settle(e, n) { switch (e) { case "return": r.resolve({ value: n, done: !0 }); break; case "throw": r.reject(n); break; default: r.resolve({ value: n, done: !1 }); } (r = r.next) ? resume(r.key, r.arg) : t = null; } this._invoke = function (e, n) { return new Promise(function (o, u) { var i = { key: e, arg: n, resolve: o, reject: u, next: null }; t ? t = t.next = i : (r = t = i, resume(e, n)); }); }, "function" != typeof e["return"] && (this["return"] = void 0); }
44
44
  AsyncGenerator.prototype["function" == typeof Symbol && Symbol.asyncIterator || "@@asyncIterator"] = function () { return this; }, AsyncGenerator.prototype.next = function (e) { return this._invoke("next", e); }, AsyncGenerator.prototype["throw"] = function (e) { return this._invoke("throw", e); }, AsyncGenerator.prototype["return"] = function (e) { return this._invoke("return", e); };
45
45
  function _OverloadYield(e, d) { this.v = e, this.k = d; }
46
+ function _asyncIterator(r) { var n, t, o, e = 2; for ("undefined" != typeof Symbol && (t = Symbol.asyncIterator, o = Symbol.iterator); e--;) { if (t && null != (n = r[t])) return n.call(r); if (o && null != (n = r[o])) return new AsyncFromSyncIterator(n.call(r)); t = "@@asyncIterator", o = "@@iterator"; } throw new TypeError("Object is not async iterable"); }
47
+ function AsyncFromSyncIterator(r) { function AsyncFromSyncIteratorContinuation(r) { if (Object(r) !== r) return Promise.reject(new TypeError(r + " is not an object.")); var n = r.done; return Promise.resolve(r.value).then(function (r) { return { value: r, done: n }; }); } return AsyncFromSyncIterator = function AsyncFromSyncIterator(r) { this.s = r, this.n = r.next; }, AsyncFromSyncIterator.prototype = { s: null, n: null, next: function next() { return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments)); }, "return": function _return(r) { var n = this.s["return"]; return void 0 === n ? Promise.resolve({ value: r, done: !0 }) : AsyncFromSyncIteratorContinuation(n.apply(this.s, arguments)); }, "throw": function _throw(r) { var n = this.s["return"]; return void 0 === n ? Promise.reject(r) : AsyncFromSyncIteratorContinuation(n.apply(this.s, arguments)); } }, new AsyncFromSyncIterator(r); }
46
48
  var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
47
49
  function Database(file) {
48
50
  var _this2;
@@ -66,6 +68,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
66
68
  _this2.indexManager = new _IndexManager["default"](_this2.opts);
67
69
  _this2.indexOffset = 0;
68
70
  _this2.writeBuffer = [];
71
+ _this2.mutex = new _asyncMutex.Mutex();
69
72
  return _this2;
70
73
  }
71
74
  _inherits(Database, _EventEmitter);
@@ -406,7 +409,8 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
406
409
  }
407
410
  _context5.next = 8;
408
411
  return this.serializer.serialize(data, {
409
- compress: this.opts.compress
412
+ compress: this.opts.compress,
413
+ v8: this.opts.v8
410
414
  });
411
415
  case 8:
412
416
  line = _context5.sent;
@@ -414,16 +418,16 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
414
418
  position = this.offsets.length;
415
419
  this.offsets.push(this.indexOffset);
416
420
  this.indexOffset += line.length;
417
- this.indexManager.add(data, position);
418
421
  this.emit('insert', data, position);
419
422
  this.writeBuffer.push(line);
420
423
  if (!(!this.flushing && this.currentWriteBufferSize() > this.opts.maxMemoryUsage)) {
421
- _context5.next = 18;
424
+ _context5.next = 17;
422
425
  break;
423
426
  }
424
- _context5.next = 18;
427
+ _context5.next = 17;
425
428
  return this.flush();
426
- case 18:
429
+ case 17:
430
+ this.indexManager.add(data, position);
427
431
  this.shouldSave = true;
428
432
  case 19:
429
433
  case "end":
@@ -471,18 +475,22 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
471
475
  key: "_flush",
472
476
  value: function () {
473
477
  var _flush2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
474
- var fd, data, pos;
478
+ var release, fd, data, pos, err;
475
479
  return _regeneratorRuntime().wrap(function _callee6$(_context6) {
476
480
  while (1) switch (_context6.prev = _context6.next) {
477
481
  case 0:
478
482
  _context6.next = 2;
479
- return _fs["default"].promises.open(this.fileHandler.file, 'a');
483
+ return this.mutex.acquire();
480
484
  case 2:
485
+ release = _context6.sent;
486
+ _context6.next = 5;
487
+ return _fs["default"].promises.open(this.fileHandler.file, 'a');
488
+ case 5:
481
489
  fd = _context6.sent;
482
- _context6.prev = 3;
483
- case 4:
490
+ _context6.prev = 6;
491
+ case 7:
484
492
  if (!this.writeBuffer.length) {
485
- _context6.next = 23;
493
+ _context6.next = 26;
486
494
  break;
487
495
  }
488
496
  data = void 0;
@@ -490,21 +498,21 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
490
498
  return typeof b === 'number';
491
499
  });
492
500
  if (!(pos === 0)) {
493
- _context6.next = 18;
501
+ _context6.next = 21;
494
502
  break;
495
503
  }
496
- _context6.next = 10;
504
+ _context6.next = 13;
497
505
  return fd.close();
498
- case 10:
499
- _context6.next = 12;
506
+ case 13:
507
+ _context6.next = 15;
500
508
  return this.fileHandler.truncate(this.writeBuffer.shift());
501
- case 12:
502
- _context6.next = 14;
509
+ case 15:
510
+ _context6.next = 17;
503
511
  return _fs["default"].promises.open(this.fileHandler.file, 'a');
504
- case 14:
512
+ case 17:
505
513
  fd = _context6.sent;
506
- return _context6.abrupt("continue", 4);
507
- case 18:
514
+ return _context6.abrupt("continue", 7);
515
+ case 21:
508
516
  if (pos === -1) {
509
517
  data = Buffer.concat(this.writeBuffer);
510
518
  this.writeBuffer.length = 0;
@@ -512,31 +520,35 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
512
520
  data = Buffer.concat(this.writeBuffer.slice(0, pos));
513
521
  this.writeBuffer.splice(0, pos);
514
522
  }
515
- case 19:
516
- _context6.next = 21;
523
+ case 22:
524
+ _context6.next = 24;
517
525
  return fd.write(data);
518
- case 21:
519
- _context6.next = 4;
526
+ case 24:
527
+ _context6.next = 7;
520
528
  break;
521
- case 23:
529
+ case 26:
522
530
  this.shouldSave = true;
523
- _context6.next = 29;
531
+ _context6.next = 32;
524
532
  break;
525
- case 26:
526
- _context6.prev = 26;
527
- _context6.t0 = _context6["catch"](3);
528
- console.error('Error flushing:', _context6.t0);
529
533
  case 29:
530
534
  _context6.prev = 29;
531
- _context6.next = 32;
532
- return fd.close();
535
+ _context6.t0 = _context6["catch"](6);
536
+ console.error('Error flushing:', _context6.t0);
533
537
  case 32:
534
- return _context6.finish(29);
535
- case 33:
538
+ _context6.prev = 32;
539
+ _context6.next = 35;
540
+ return fd.close()["catch"](function (e) {
541
+ return err = e;
542
+ });
543
+ case 35:
544
+ release();
545
+ err && console.error('Error closing file:', err);
546
+ return _context6.finish(32);
547
+ case 38:
536
548
  case "end":
537
549
  return _context6.stop();
538
550
  }
539
- }, _callee6, this, [[3, 26, 29, 33]]);
551
+ }, _callee6, this, [[6, 29, 32, 38]]);
540
552
  }));
541
553
  function _flush() {
542
554
  return _flush2.apply(this, arguments);
@@ -549,36 +561,36 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
549
561
  var _this = this;
550
562
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
551
563
  return _wrapAsyncGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
552
- var ranges, partitionedRanges, currentPartition, line, m, _i, _partitionedRanges, _ranges, lines, _loop, _line;
553
- return _regeneratorRuntime().wrap(function _callee7$(_context8) {
554
- while (1) switch (_context8.prev = _context8.next) {
564
+ var ranges, groupedRanges, fd, _iterator2, _step2, groupedRange, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, row, entry;
565
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
566
+ while (1) switch (_context7.prev = _context7.next) {
555
567
  case 0:
556
568
  if (!_this.destroyed) {
557
- _context8.next = 2;
569
+ _context7.next = 2;
558
570
  break;
559
571
  }
560
572
  throw new Error('Database is destroyed');
561
573
  case 2:
562
574
  if (_this.initialized) {
563
- _context8.next = 5;
575
+ _context7.next = 5;
564
576
  break;
565
577
  }
566
- _context8.next = 5;
578
+ _context7.next = 5;
567
579
  return _awaitAsyncGenerator(_this.init());
568
580
  case 5:
569
- _context8.t0 = _this.shouldSave;
570
- if (!_context8.t0) {
571
- _context8.next = 9;
581
+ _context7.t0 = _this.shouldSave;
582
+ if (!_context7.t0) {
583
+ _context7.next = 9;
572
584
  break;
573
585
  }
574
- _context8.next = 9;
586
+ _context7.next = 9;
575
587
  return _awaitAsyncGenerator(_this.save()["catch"](console.error));
576
588
  case 9:
577
589
  if (!(_this.indexOffset === 0)) {
578
- _context8.next = 11;
590
+ _context7.next = 11;
579
591
  break;
580
592
  }
581
- return _context8.abrupt("return");
593
+ return _context7.abrupt("return");
582
594
  case 11:
583
595
  if (!Array.isArray(map)) {
584
596
  if (map instanceof Set) {
@@ -590,83 +602,122 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
590
602
  }
591
603
  }
592
604
  ranges = _this.getRanges(map);
593
- partitionedRanges = [], currentPartition = 0;
594
- for (line in ranges) {
595
- if (partitionedRanges[currentPartition] === undefined) {
596
- partitionedRanges[currentPartition] = [];
597
- }
598
- partitionedRanges[currentPartition].push(ranges[line]);
599
- if (partitionedRanges[currentPartition].length >= _this.opts.maxMemoryUsage) {
600
- +currentPartition, _readOnlyError("currentPartition");
601
- }
605
+ _context7.next = 15;
606
+ return _awaitAsyncGenerator(_this.fileHandler.groupedRanges(ranges));
607
+ case 15:
608
+ groupedRanges = _context7.sent;
609
+ _context7.next = 18;
610
+ return _awaitAsyncGenerator(_fs["default"].promises.open(_this.fileHandler.file, 'r'));
611
+ case 18:
612
+ fd = _context7.sent;
613
+ _context7.prev = 19;
614
+ _iterator2 = _createForOfIteratorHelper(groupedRanges);
615
+ _context7.prev = 21;
616
+ _iterator2.s();
617
+ case 23:
618
+ if ((_step2 = _iterator2.n()).done) {
619
+ _context7.next = 66;
620
+ break;
602
621
  }
603
- m = 0;
604
- _i = 0, _partitionedRanges = partitionedRanges;
605
- case 17:
606
- if (!(_i < _partitionedRanges.length)) {
607
- _context8.next = 34;
622
+ groupedRange = _step2.value;
623
+ _iteratorAbruptCompletion = false;
624
+ _didIteratorError = false;
625
+ _context7.prev = 27;
626
+ _iterator = _asyncIterator(_this.fileHandler.readGroupedRange(groupedRange, fd));
627
+ case 29:
628
+ _context7.next = 31;
629
+ return _awaitAsyncGenerator(_iterator.next());
630
+ case 31:
631
+ if (!(_iteratorAbruptCompletion = !(_step = _context7.sent).done)) {
632
+ _context7.next = 48;
608
633
  break;
609
634
  }
610
- _ranges = _partitionedRanges[_i];
611
- _context8.next = 21;
612
- return _awaitAsyncGenerator(_this.fileHandler.readRanges(_ranges));
613
- case 21:
614
- lines = _context8.sent;
615
- _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
616
- var err, entry;
617
- return _regeneratorRuntime().wrap(function _loop$(_context7) {
618
- while (1) switch (_context7.prev = _context7.next) {
619
- case 0:
620
- _context7.next = 2;
621
- return _awaitAsyncGenerator(_this.serializer.deserialize(lines[_line])["catch"](function (e) {
622
- return console.error(err = e);
623
- }));
624
- case 2:
625
- entry = _context7.sent;
626
- if (!err) {
627
- _context7.next = 5;
628
- break;
629
- }
630
- return _context7.abrupt("return", 1);
631
- case 5:
632
- if (entry._ === undefined) {
633
- while (_this.offsets[m] != _line && m < map.length) m++; // weak comparison as 'start' is a string
634
- entry._ = m++;
635
- }
636
- _context7.next = 8;
637
- return entry;
638
- case 8:
639
- case "end":
640
- return _context7.stop();
641
- }
642
- }, _loop);
643
- });
644
- _context8.t1 = _regeneratorRuntime().keys(lines);
645
- case 24:
646
- if ((_context8.t2 = _context8.t1()).done) {
647
- _context8.next = 31;
635
+ row = _step.value;
636
+ _context7.next = 35;
637
+ return _awaitAsyncGenerator(_this.serializer.deserialize(row.line, {
638
+ compress: _this.opts.compress,
639
+ v8: _this.opts.v8
640
+ }));
641
+ case 35:
642
+ entry = _context7.sent;
643
+ if (!(entry === null)) {
644
+ _context7.next = 38;
648
645
  break;
649
646
  }
650
- _line = _context8.t2.value;
651
- return _context8.delegateYield(_loop(), "t3", 27);
652
- case 27:
653
- if (!_context8.t3) {
654
- _context8.next = 29;
647
+ return _context7.abrupt("continue", 45);
648
+ case 38:
649
+ if (!options.includeOffsets) {
650
+ _context7.next = 43;
655
651
  break;
656
652
  }
657
- return _context8.abrupt("continue", 24);
658
- case 29:
659
- _context8.next = 24;
653
+ _context7.next = 41;
654
+ return {
655
+ entry: entry,
656
+ start: row.start
657
+ };
658
+ case 41:
659
+ _context7.next = 45;
660
660
  break;
661
- case 31:
662
- _i++;
663
- _context8.next = 17;
661
+ case 43:
662
+ _context7.next = 45;
663
+ return entry;
664
+ case 45:
665
+ _iteratorAbruptCompletion = false;
666
+ _context7.next = 29;
664
667
  break;
665
- case 34:
668
+ case 48:
669
+ _context7.next = 54;
670
+ break;
671
+ case 50:
672
+ _context7.prev = 50;
673
+ _context7.t1 = _context7["catch"](27);
674
+ _didIteratorError = true;
675
+ _iteratorError = _context7.t1;
676
+ case 54:
677
+ _context7.prev = 54;
678
+ _context7.prev = 55;
679
+ if (!(_iteratorAbruptCompletion && _iterator["return"] != null)) {
680
+ _context7.next = 59;
681
+ break;
682
+ }
683
+ _context7.next = 59;
684
+ return _awaitAsyncGenerator(_iterator["return"]());
685
+ case 59:
686
+ _context7.prev = 59;
687
+ if (!_didIteratorError) {
688
+ _context7.next = 62;
689
+ break;
690
+ }
691
+ throw _iteratorError;
692
+ case 62:
693
+ return _context7.finish(59);
694
+ case 63:
695
+ return _context7.finish(54);
696
+ case 64:
697
+ _context7.next = 23;
698
+ break;
699
+ case 66:
700
+ _context7.next = 71;
701
+ break;
702
+ case 68:
703
+ _context7.prev = 68;
704
+ _context7.t2 = _context7["catch"](21);
705
+ _iterator2.e(_context7.t2);
706
+ case 71:
707
+ _context7.prev = 71;
708
+ _iterator2.f();
709
+ return _context7.finish(71);
710
+ case 74:
711
+ _context7.prev = 74;
712
+ _context7.next = 77;
713
+ return _awaitAsyncGenerator(fd.close());
714
+ case 77:
715
+ return _context7.finish(74);
716
+ case 78:
666
717
  case "end":
667
- return _context8.stop();
718
+ return _context7.stop();
668
719
  }
669
- }, _callee7);
720
+ }, _callee7, null, [[19,, 74, 78], [21, 68, 71, 74], [27, 50, 54, 64], [55,, 59, 63]]);
670
721
  }))();
671
722
  }
672
723
  }, {
@@ -681,40 +732,40 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
681
732
  _options$orderBy$spli3,
682
733
  direction,
683
734
  matchingLines,
684
- _args9 = arguments;
685
- return _regeneratorRuntime().wrap(function _callee8$(_context9) {
686
- while (1) switch (_context9.prev = _context9.next) {
735
+ _args8 = arguments;
736
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
737
+ while (1) switch (_context8.prev = _context8.next) {
687
738
  case 0:
688
- options = _args9.length > 1 && _args9[1] !== undefined ? _args9[1] : {};
739
+ options = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {};
689
740
  if (!this.destroyed) {
690
- _context9.next = 3;
741
+ _context8.next = 3;
691
742
  break;
692
743
  }
693
744
  throw new Error('Database is destroyed');
694
745
  case 3:
695
746
  if (this.initialized) {
696
- _context9.next = 6;
747
+ _context8.next = 6;
697
748
  break;
698
749
  }
699
- _context9.next = 6;
750
+ _context8.next = 6;
700
751
  return this.init();
701
752
  case 6:
702
- _context9.t0 = this.shouldSave;
703
- if (!_context9.t0) {
704
- _context9.next = 10;
753
+ _context8.t0 = this.shouldSave;
754
+ if (!_context8.t0) {
755
+ _context8.next = 10;
705
756
  break;
706
757
  }
707
- _context9.next = 10;
758
+ _context8.next = 10;
708
759
  return this.save()["catch"](console.error);
709
760
  case 10:
710
761
  if (!Array.isArray(criteria)) {
711
- _context9.next = 19;
762
+ _context8.next = 19;
712
763
  break;
713
764
  }
714
- _context9.next = 13;
765
+ _context8.next = 13;
715
766
  return this.readLines(criteria);
716
767
  case 13:
717
- results = _context9.sent;
768
+ results = _context8.sent;
718
769
  if (options.orderBy) {
719
770
  _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;
720
771
  results.sort(function (a, b) {
@@ -726,25 +777,25 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
726
777
  if (options.limit) {
727
778
  results = results.slice(0, options.limit);
728
779
  }
729
- return _context9.abrupt("return", results);
780
+ return _context8.abrupt("return", results);
730
781
  case 19:
731
- _context9.next = 21;
782
+ _context8.next = 21;
732
783
  return this.indexManager.query(criteria, options);
733
784
  case 21:
734
- matchingLines = _context9.sent;
785
+ matchingLines = _context8.sent;
735
786
  if (!(!matchingLines || !matchingLines.size)) {
736
- _context9.next = 24;
787
+ _context8.next = 24;
737
788
  break;
738
789
  }
739
- return _context9.abrupt("return", []);
790
+ return _context8.abrupt("return", []);
740
791
  case 24:
741
- _context9.next = 26;
792
+ _context8.next = 26;
742
793
  return this.query(_toConsumableArray(matchingLines), options);
743
794
  case 26:
744
- return _context9.abrupt("return", _context9.sent);
795
+ return _context8.abrupt("return", _context8.sent);
745
796
  case 27:
746
797
  case "end":
747
- return _context9.stop();
798
+ return _context8.stop();
748
799
  }
749
800
  }, _callee8, this);
750
801
  }));
@@ -764,110 +815,110 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
764
815
  validMatchingLines,
765
816
  entries,
766
817
  lines,
767
- _iterator,
768
- _step,
769
- _loop2,
818
+ _iterator3,
819
+ _step3,
820
+ _loop,
770
821
  offsets,
771
822
  byteOffset,
772
823
  k,
773
- _args11 = arguments;
774
- return _regeneratorRuntime().wrap(function _callee9$(_context11) {
775
- while (1) switch (_context11.prev = _context11.next) {
824
+ _args10 = arguments;
825
+ return _regeneratorRuntime().wrap(function _callee9$(_context10) {
826
+ while (1) switch (_context10.prev = _context10.next) {
776
827
  case 0:
777
- options = _args11.length > 2 && _args11[2] !== undefined ? _args11[2] : {};
828
+ options = _args10.length > 2 && _args10[2] !== undefined ? _args10[2] : {};
778
829
  if (this.shouldTruncate) {
779
830
  this.writeBuffer.push(this.indexOffset);
780
831
  this.shouldTruncate = false;
781
832
  }
782
833
  if (!this.destroyed) {
783
- _context11.next = 4;
834
+ _context10.next = 4;
784
835
  break;
785
836
  }
786
837
  throw new Error('Database is destroyed');
787
838
  case 4:
788
839
  if (this.initialized) {
789
- _context11.next = 7;
840
+ _context10.next = 7;
790
841
  break;
791
842
  }
792
- _context11.next = 7;
843
+ _context10.next = 7;
793
844
  return this.init();
794
845
  case 7:
795
- _context11.t0 = this.shouldSave;
796
- if (!_context11.t0) {
797
- _context11.next = 11;
846
+ _context10.t0 = this.shouldSave;
847
+ if (!_context10.t0) {
848
+ _context10.next = 11;
798
849
  break;
799
850
  }
800
- _context11.next = 11;
851
+ _context10.next = 11;
801
852
  return this.save()["catch"](console.error);
802
853
  case 11:
803
- _context11.next = 13;
854
+ _context10.next = 13;
804
855
  return this.indexManager.query(criteria, options);
805
856
  case 13:
806
- matchingLines = _context11.sent;
857
+ matchingLines = _context10.sent;
807
858
  if (!(!matchingLines || !matchingLines.size)) {
808
- _context11.next = 16;
859
+ _context10.next = 16;
809
860
  break;
810
861
  }
811
- return _context11.abrupt("return", []);
862
+ return _context10.abrupt("return", []);
812
863
  case 16:
813
864
  ranges = this.getRanges(_toConsumableArray(matchingLines));
814
865
  validMatchingLines = new Set(ranges.map(function (r) {
815
866
  return r.index;
816
867
  }));
817
868
  if (validMatchingLines.size) {
818
- _context11.next = 20;
869
+ _context10.next = 20;
819
870
  break;
820
871
  }
821
- return _context11.abrupt("return", []);
872
+ return _context10.abrupt("return", []);
822
873
  case 20:
823
- _context11.next = 22;
874
+ _context10.next = 22;
824
875
  return this.readLines(_toConsumableArray(validMatchingLines), ranges);
825
876
  case 22:
826
- entries = _context11.sent;
877
+ entries = _context10.sent;
827
878
  lines = [];
828
- _iterator = _createForOfIteratorHelper(entries);
829
- _context11.prev = 25;
830
- _loop2 = /*#__PURE__*/_regeneratorRuntime().mark(function _loop2() {
879
+ _iterator3 = _createForOfIteratorHelper(entries);
880
+ _context10.prev = 25;
881
+ _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
831
882
  var entry, err, updated, ret;
832
- return _regeneratorRuntime().wrap(function _loop2$(_context10) {
833
- while (1) switch (_context10.prev = _context10.next) {
883
+ return _regeneratorRuntime().wrap(function _loop$(_context9) {
884
+ while (1) switch (_context9.prev = _context9.next) {
834
885
  case 0:
835
- entry = _step.value;
886
+ entry = _step3.value;
836
887
  updated = Object.assign(entry, data);
837
- _context10.next = 4;
888
+ _context9.next = 4;
838
889
  return _this8.serializer.serialize(updated)["catch"](function (e) {
839
890
  return err = e;
840
891
  });
841
892
  case 4:
842
- ret = _context10.sent;
893
+ ret = _context9.sent;
843
894
  err || lines.push(ret);
844
895
  case 6:
845
896
  case "end":
846
- return _context10.stop();
897
+ return _context9.stop();
847
898
  }
848
- }, _loop2);
899
+ }, _loop);
849
900
  });
850
- _iterator.s();
901
+ _iterator3.s();
851
902
  case 28:
852
- if ((_step = _iterator.n()).done) {
853
- _context11.next = 32;
903
+ if ((_step3 = _iterator3.n()).done) {
904
+ _context10.next = 32;
854
905
  break;
855
906
  }
856
- return _context11.delegateYield(_loop2(), "t1", 30);
907
+ return _context10.delegateYield(_loop(), "t1", 30);
857
908
  case 30:
858
- _context11.next = 28;
909
+ _context10.next = 28;
859
910
  break;
860
911
  case 32:
861
- _context11.next = 37;
912
+ _context10.next = 37;
862
913
  break;
863
914
  case 34:
864
- _context11.prev = 34;
865
- _context11.t2 = _context11["catch"](25);
866
- _iterator.e(_context11.t2);
915
+ _context10.prev = 34;
916
+ _context10.t2 = _context10["catch"](25);
917
+ _iterator3.e(_context10.t2);
867
918
  case 37:
868
- _context11.prev = 37;
869
- _iterator.f();
870
- return _context11.finish(37);
919
+ _context10.prev = 37;
920
+ _iterator3.f();
921
+ return _context10.finish(37);
871
922
  case 40:
872
923
  offsets = [];
873
924
  byteOffset = 0, k = 0;
@@ -882,7 +933,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
882
933
  });
883
934
  this.offsets = offsets;
884
935
  this.indexOffset += byteOffset;
885
- _context11.next = 47;
936
+ _context10.next = 47;
886
937
  return this.fileHandler.replaceLines(ranges, lines);
887
938
  case 47:
888
939
  _toConsumableArray(validMatchingLines).forEach(function (lineNumber, i) {
@@ -890,10 +941,10 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
890
941
  _this8.indexManager.add(entries[i], lineNumber);
891
942
  });
892
943
  this.shouldSave = true;
893
- return _context11.abrupt("return", entries);
944
+ return _context10.abrupt("return", entries);
894
945
  case 50:
895
946
  case "end":
896
- return _context11.stop();
947
+ return _context10.stop();
897
948
  }
898
949
  }, _callee9, this, [[25, 34, 37, 40]]);
899
950
  }));
@@ -913,51 +964,51 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
913
964
  offsets,
914
965
  byteOffset,
915
966
  k,
916
- _args12 = arguments;
917
- return _regeneratorRuntime().wrap(function _callee10$(_context12) {
918
- while (1) switch (_context12.prev = _context12.next) {
967
+ _args11 = arguments;
968
+ return _regeneratorRuntime().wrap(function _callee10$(_context11) {
969
+ while (1) switch (_context11.prev = _context11.next) {
919
970
  case 0:
920
- options = _args12.length > 1 && _args12[1] !== undefined ? _args12[1] : {};
971
+ options = _args11.length > 1 && _args11[1] !== undefined ? _args11[1] : {};
921
972
  if (this.shouldTruncate) {
922
973
  this.writeBuffer.push(this.indexOffset);
923
974
  this.shouldTruncate = false;
924
975
  }
925
976
  if (!this.destroyed) {
926
- _context12.next = 4;
977
+ _context11.next = 4;
927
978
  break;
928
979
  }
929
980
  throw new Error('Database is destroyed');
930
981
  case 4:
931
982
  if (this.initialized) {
932
- _context12.next = 7;
983
+ _context11.next = 7;
933
984
  break;
934
985
  }
935
- _context12.next = 7;
986
+ _context11.next = 7;
936
987
  return this.init();
937
988
  case 7:
938
- _context12.t0 = this.shouldSave;
939
- if (!_context12.t0) {
940
- _context12.next = 11;
989
+ _context11.t0 = this.shouldSave;
990
+ if (!_context11.t0) {
991
+ _context11.next = 11;
941
992
  break;
942
993
  }
943
- _context12.next = 11;
994
+ _context11.next = 11;
944
995
  return this.save()["catch"](console.error);
945
996
  case 11:
946
- _context12.next = 13;
997
+ _context11.next = 13;
947
998
  return this.indexManager.query(criteria, options);
948
999
  case 13:
949
- matchingLines = _context12.sent;
1000
+ matchingLines = _context11.sent;
950
1001
  if (!(!matchingLines || !matchingLines.size)) {
951
- _context12.next = 16;
1002
+ _context11.next = 16;
952
1003
  break;
953
1004
  }
954
- return _context12.abrupt("return", 0);
1005
+ return _context11.abrupt("return", 0);
955
1006
  case 16:
956
1007
  ranges = this.getRanges(_toConsumableArray(matchingLines));
957
1008
  validMatchingLines = new Set(ranges.map(function (r) {
958
1009
  return r.index;
959
1010
  }));
960
- _context12.next = 20;
1011
+ _context11.next = 20;
961
1012
  return this.fileHandler.replaceLines(ranges, []);
962
1013
  case 20:
963
1014
  offsets = [];
@@ -975,10 +1026,10 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
975
1026
  this.indexOffset += byteOffset;
976
1027
  this.indexManager.remove(_toConsumableArray(validMatchingLines));
977
1028
  this.shouldSave = true;
978
- return _context12.abrupt("return", ranges.length);
1029
+ return _context11.abrupt("return", ranges.length);
979
1030
  case 28:
980
1031
  case "end":
981
- return _context12.stop();
1032
+ return _context11.stop();
982
1033
  }
983
1034
  }, _callee10, this);
984
1035
  }));
@@ -991,15 +1042,15 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
991
1042
  key: "destroy",
992
1043
  value: function () {
993
1044
  var _destroy = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11() {
994
- return _regeneratorRuntime().wrap(function _callee11$(_context13) {
995
- while (1) switch (_context13.prev = _context13.next) {
1045
+ return _regeneratorRuntime().wrap(function _callee11$(_context12) {
1046
+ while (1) switch (_context12.prev = _context12.next) {
996
1047
  case 0:
997
- _context13.t0 = this.shouldSave;
998
- if (!_context13.t0) {
999
- _context13.next = 4;
1048
+ _context12.t0 = this.shouldSave;
1049
+ if (!_context12.t0) {
1050
+ _context12.next = 4;
1000
1051
  break;
1001
1052
  }
1002
- _context13.next = 4;
1053
+ _context12.next = 4;
1003
1054
  return this.save()["catch"](console.error);
1004
1055
  case 4:
1005
1056
  this.destroyed = true;
@@ -1010,7 +1061,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
1010
1061
  this.fileHandler.destroy();
1011
1062
  case 10:
1012
1063
  case "end":
1013
- return _context13.stop();
1064
+ return _context12.stop();
1014
1065
  }
1015
1066
  }, _callee11, this);
1016
1067
  }));