jexidb 1.0.2
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/.gitattributes +2 -0
- package/LICENSE +21 -0
- package/README.md +125 -0
- package/babel.config.json +5 -0
- package/dist/Database.cjs +983 -0
- package/package.json +58 -0
- package/src/Database.mjs +365 -0
- package/src/FileHandler.mjs +133 -0
- package/src/IndexManager.mjs +186 -0
- package/src/serializers/Advanced.mjs +120 -0
- package/src/serializers/Simple.mjs +21 -0
- package/test/test-json-compressed.jdb +0 -0
- package/test/test-json.jdb +12 -0
- package/test/test-v8-compressed.jdb +0 -0
- package/test/test-v8.jdb +0 -0
- package/test/test.mjs +152 -0
|
@@ -0,0 +1,983 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Database = void 0;
|
|
7
|
+
var _events = require("events");
|
|
8
|
+
var _FileHandler = _interopRequireDefault(require("./FileHandler.mjs"));
|
|
9
|
+
var _IndexManager = _interopRequireDefault(require("./IndexManager.mjs"));
|
|
10
|
+
var _Simple = _interopRequireDefault(require("./serializers/Simple.mjs"));
|
|
11
|
+
var _Advanced = _interopRequireDefault(require("./serializers/Advanced.mjs"));
|
|
12
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
13
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
14
|
+
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; } } }; }
|
|
15
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
16
|
+
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."); }
|
|
17
|
+
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; } }
|
|
18
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
19
|
+
function _readOnlyError(r) { throw new TypeError('"' + r + '" is read-only'); }
|
|
20
|
+
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); }
|
|
21
|
+
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; }
|
|
22
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
23
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
24
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
25
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
26
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
27
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
28
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
29
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
30
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
31
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
32
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
33
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
34
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
35
|
+
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
|
|
36
|
+
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
|
|
37
|
+
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
|
|
38
|
+
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
39
|
+
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
|
|
40
|
+
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
|
|
41
|
+
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
|
|
42
|
+
function _awaitAsyncGenerator(e) { return new _OverloadYield(e, 0); }
|
|
43
|
+
function _wrapAsyncGenerator(e) { return function () { return new AsyncGenerator(e.apply(this, arguments)); }; }
|
|
44
|
+
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); }
|
|
45
|
+
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); };
|
|
46
|
+
function _OverloadYield(e, d) { this.v = e, this.k = d; }
|
|
47
|
+
var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
|
|
48
|
+
function Database(filePath) {
|
|
49
|
+
var _this2;
|
|
50
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
51
|
+
_classCallCheck(this, Database);
|
|
52
|
+
_this2 = _callSuper(this, Database);
|
|
53
|
+
_this2.opts = Object.assign({
|
|
54
|
+
v8: false,
|
|
55
|
+
index: {
|
|
56
|
+
data: {}
|
|
57
|
+
},
|
|
58
|
+
indexes: {},
|
|
59
|
+
compress: false,
|
|
60
|
+
compressIndex: false,
|
|
61
|
+
maxMemoryUsage: 64 * 1024 // 64KB
|
|
62
|
+
}, opts);
|
|
63
|
+
_this2.shouldSave = false;
|
|
64
|
+
if (_this2.opts.v8 || _this2.opts.compress || _this2.opts.compressIndex) {
|
|
65
|
+
_this2.serializer = new _Advanced["default"](_this2.opts);
|
|
66
|
+
} else {
|
|
67
|
+
_this2.serializer = new _Simple["default"](_this2.opts);
|
|
68
|
+
}
|
|
69
|
+
_this2.fileHandler = new _FileHandler["default"](filePath);
|
|
70
|
+
_this2.indexManager = new _IndexManager["default"](_this2.opts);
|
|
71
|
+
_this2.indexOffset = 0;
|
|
72
|
+
_this2.writeBuffer = [];
|
|
73
|
+
return _this2;
|
|
74
|
+
}
|
|
75
|
+
_inherits(Database, _EventEmitter);
|
|
76
|
+
return _createClass(Database, [{
|
|
77
|
+
key: "use",
|
|
78
|
+
value: function use(plugin) {
|
|
79
|
+
if (this.destroyed) throw new Error('Database is destroyed');
|
|
80
|
+
plugin(this);
|
|
81
|
+
}
|
|
82
|
+
}, {
|
|
83
|
+
key: "init",
|
|
84
|
+
value: function () {
|
|
85
|
+
var _init = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
86
|
+
var _this3 = this;
|
|
87
|
+
var _this$fileHandler, lastLine, offsets, ptr, indexLine, index;
|
|
88
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
89
|
+
while (1) switch (_context.prev = _context.next) {
|
|
90
|
+
case 0:
|
|
91
|
+
if (!this.destroyed) {
|
|
92
|
+
_context.next = 2;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
throw new Error('Database is destroyed');
|
|
96
|
+
case 2:
|
|
97
|
+
if (!this.initialized) {
|
|
98
|
+
_context.next = 4;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
return _context.abrupt("return");
|
|
102
|
+
case 4:
|
|
103
|
+
if (!this.initlializing) {
|
|
104
|
+
_context.next = 8;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
_context.next = 7;
|
|
108
|
+
return new Promise(function (resolve) {
|
|
109
|
+
return _this3.once('init', resolve);
|
|
110
|
+
});
|
|
111
|
+
case 7:
|
|
112
|
+
return _context.abrupt("return", _context.sent);
|
|
113
|
+
case 8:
|
|
114
|
+
this.initializing = true;
|
|
115
|
+
_context.prev = 9;
|
|
116
|
+
if (!this.opts.clear) {
|
|
117
|
+
_context.next = 14;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
_context.next = 13;
|
|
121
|
+
return this.fileHandler.truncate(0)["catch"](console.error);
|
|
122
|
+
case 13:
|
|
123
|
+
throw new Error('Cleared, empty file');
|
|
124
|
+
case 14:
|
|
125
|
+
_context.next = 16;
|
|
126
|
+
return this.fileHandler.readLastLine();
|
|
127
|
+
case 16:
|
|
128
|
+
lastLine = _context.sent;
|
|
129
|
+
if (!(!lastLine || !lastLine.length)) {
|
|
130
|
+
_context.next = 19;
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
throw new Error('File does not exists or is a empty file');
|
|
134
|
+
case 19:
|
|
135
|
+
_context.next = 21;
|
|
136
|
+
return this.serializer.deserialize(lastLine, {
|
|
137
|
+
compress: this.opts.compressIndex
|
|
138
|
+
});
|
|
139
|
+
case 21:
|
|
140
|
+
offsets = _context.sent;
|
|
141
|
+
if (Array.isArray(offsets)) {
|
|
142
|
+
_context.next = 24;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
throw new Error('File to parse offsets, expected an array');
|
|
146
|
+
case 24:
|
|
147
|
+
this.indexOffset = offsets[offsets.length - 2];
|
|
148
|
+
this.offsets = offsets;
|
|
149
|
+
ptr = this.locate(offsets.length - 2);
|
|
150
|
+
this.offsets = this.offsets.slice(0, -2);
|
|
151
|
+
this.shouldTruncate = true;
|
|
152
|
+
_context.next = 31;
|
|
153
|
+
return (_this$fileHandler = this.fileHandler).readRange.apply(_this$fileHandler, _toConsumableArray(ptr));
|
|
154
|
+
case 31:
|
|
155
|
+
indexLine = _context.sent;
|
|
156
|
+
_context.next = 34;
|
|
157
|
+
return this.serializer.deserialize(indexLine, {
|
|
158
|
+
compress: this.opts.compressIndex
|
|
159
|
+
});
|
|
160
|
+
case 34:
|
|
161
|
+
index = _context.sent;
|
|
162
|
+
index && this.indexManager.load(index);
|
|
163
|
+
_context.next = 43;
|
|
164
|
+
break;
|
|
165
|
+
case 38:
|
|
166
|
+
_context.prev = 38;
|
|
167
|
+
_context.t0 = _context["catch"](9);
|
|
168
|
+
if (!this.offsets) {
|
|
169
|
+
this.offsets = [];
|
|
170
|
+
}
|
|
171
|
+
this.indexOffset = 0;
|
|
172
|
+
if (!String(_context.t0).includes('empty file')) {
|
|
173
|
+
console.error('Error loading database:', _context.t0);
|
|
174
|
+
}
|
|
175
|
+
case 43:
|
|
176
|
+
_context.prev = 43;
|
|
177
|
+
this.initializing = false;
|
|
178
|
+
this.initialized = true;
|
|
179
|
+
this.emit('init');
|
|
180
|
+
return _context.finish(43);
|
|
181
|
+
case 48:
|
|
182
|
+
case "end":
|
|
183
|
+
return _context.stop();
|
|
184
|
+
}
|
|
185
|
+
}, _callee, this, [[9, 38, 43, 48]]);
|
|
186
|
+
}));
|
|
187
|
+
function init() {
|
|
188
|
+
return _init.apply(this, arguments);
|
|
189
|
+
}
|
|
190
|
+
return init;
|
|
191
|
+
}()
|
|
192
|
+
}, {
|
|
193
|
+
key: "save",
|
|
194
|
+
value: function () {
|
|
195
|
+
var _save = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
196
|
+
var _this4 = this;
|
|
197
|
+
var index, field, term, offsets, indexString, _field, _term, offsetsString;
|
|
198
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
199
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
200
|
+
case 0:
|
|
201
|
+
if (!this.destroyed) {
|
|
202
|
+
_context2.next = 2;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
throw new Error('Database is destroyed');
|
|
206
|
+
case 2:
|
|
207
|
+
if (!this.saving) {
|
|
208
|
+
_context2.next = 4;
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
return _context2.abrupt("return", new Promise(function (resolve) {
|
|
212
|
+
return _this4.once('save', resolve);
|
|
213
|
+
}));
|
|
214
|
+
case 4:
|
|
215
|
+
this.saving = true;
|
|
216
|
+
_context2.next = 7;
|
|
217
|
+
return this.flush();
|
|
218
|
+
case 7:
|
|
219
|
+
this.emit('before-save');
|
|
220
|
+
index = Object.assign({
|
|
221
|
+
data: {}
|
|
222
|
+
}, this.indexManager.index);
|
|
223
|
+
for (field in this.indexManager.index.data) {
|
|
224
|
+
for (term in this.indexManager.index.data[field]) {
|
|
225
|
+
index.data[field][term] = _toConsumableArray(this.indexManager.index.data[field][term]); // set to array
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
offsets = this.offsets.slice(0);
|
|
229
|
+
_context2.next = 13;
|
|
230
|
+
return this.serializer.serialize(index, {
|
|
231
|
+
compress: this.opts.compressIndex
|
|
232
|
+
});
|
|
233
|
+
case 13:
|
|
234
|
+
indexString = _context2.sent;
|
|
235
|
+
for (_field in this.indexManager.index.data) {
|
|
236
|
+
for (_term in this.indexManager.index.data[_field]) {
|
|
237
|
+
this.indexManager.index.data[_field][_term] = new Set(index.data[_field][_term]); // set back to set because of serialization
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
offsets.push(this.indexOffset);
|
|
241
|
+
offsets.push(this.indexOffset + indexString.length);
|
|
242
|
+
_context2.next = 19;
|
|
243
|
+
return this.serializer.serialize(offsets, {
|
|
244
|
+
compress: this.opts.compressIndex,
|
|
245
|
+
linebreak: false
|
|
246
|
+
});
|
|
247
|
+
case 19:
|
|
248
|
+
offsetsString = _context2.sent;
|
|
249
|
+
if (!this.shouldTruncate) {
|
|
250
|
+
_context2.next = 24;
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
_context2.next = 23;
|
|
254
|
+
return this.fileHandler.truncate(this.indexOffset);
|
|
255
|
+
case 23:
|
|
256
|
+
this.shouldTruncate = false;
|
|
257
|
+
case 24:
|
|
258
|
+
this.writeBuffer.push(indexString);
|
|
259
|
+
this.writeBuffer.push(offsetsString);
|
|
260
|
+
_context2.next = 28;
|
|
261
|
+
return this.flush();
|
|
262
|
+
case 28:
|
|
263
|
+
// write the index and offsets
|
|
264
|
+
this.shouldTruncate = true;
|
|
265
|
+
this.shouldSave = false;
|
|
266
|
+
this.saving = false;
|
|
267
|
+
this.emit('save');
|
|
268
|
+
case 32:
|
|
269
|
+
case "end":
|
|
270
|
+
return _context2.stop();
|
|
271
|
+
}
|
|
272
|
+
}, _callee2, this);
|
|
273
|
+
}));
|
|
274
|
+
function save() {
|
|
275
|
+
return _save.apply(this, arguments);
|
|
276
|
+
}
|
|
277
|
+
return save;
|
|
278
|
+
}()
|
|
279
|
+
}, {
|
|
280
|
+
key: "ready",
|
|
281
|
+
value: function () {
|
|
282
|
+
var _ready = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
283
|
+
var _this5 = this;
|
|
284
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
285
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
286
|
+
case 0:
|
|
287
|
+
if (this.initialized) {
|
|
288
|
+
_context3.next = 3;
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
_context3.next = 3;
|
|
292
|
+
return new Promise(function (resolve) {
|
|
293
|
+
return _this5.once('init', resolve);
|
|
294
|
+
});
|
|
295
|
+
case 3:
|
|
296
|
+
case "end":
|
|
297
|
+
return _context3.stop();
|
|
298
|
+
}
|
|
299
|
+
}, _callee3, this);
|
|
300
|
+
}));
|
|
301
|
+
function ready() {
|
|
302
|
+
return _ready.apply(this, arguments);
|
|
303
|
+
}
|
|
304
|
+
return ready;
|
|
305
|
+
}()
|
|
306
|
+
}, {
|
|
307
|
+
key: "locate",
|
|
308
|
+
value: function locate(n) {
|
|
309
|
+
if (this.offsets[n] === undefined) {
|
|
310
|
+
if (this.offsets[n - 1]) {
|
|
311
|
+
return [this.indexOffset, Number.MAX_SAFE_INTEGER];
|
|
312
|
+
}
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
var end = this.offsets[n + 1] || this.indexOffset || Number.MAX_SAFE_INTEGER;
|
|
316
|
+
return [this.offsets[n], end];
|
|
317
|
+
}
|
|
318
|
+
}, {
|
|
319
|
+
key: "getRanges",
|
|
320
|
+
value: function getRanges(map) {
|
|
321
|
+
var _this6 = this;
|
|
322
|
+
return (map || Array.from(this.offsets.keys())).map(function (n) {
|
|
323
|
+
var ret = _this6.locate(n);
|
|
324
|
+
if (ret !== undefined) return {
|
|
325
|
+
start: ret[0],
|
|
326
|
+
end: ret[1],
|
|
327
|
+
index: n
|
|
328
|
+
};
|
|
329
|
+
}).filter(function (n) {
|
|
330
|
+
return n !== undefined;
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
}, {
|
|
334
|
+
key: "readLines",
|
|
335
|
+
value: function () {
|
|
336
|
+
var _readLines = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(map, ranges) {
|
|
337
|
+
var results, i, start;
|
|
338
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
339
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
340
|
+
case 0:
|
|
341
|
+
if (!ranges) ranges = this.getRanges(map);
|
|
342
|
+
_context4.next = 3;
|
|
343
|
+
return this.fileHandler.readRanges(ranges, this.serializer.deserialize.bind(this.serializer));
|
|
344
|
+
case 3:
|
|
345
|
+
results = _context4.sent;
|
|
346
|
+
i = 0;
|
|
347
|
+
_context4.t0 = _regeneratorRuntime().keys(results);
|
|
348
|
+
case 6:
|
|
349
|
+
if ((_context4.t1 = _context4.t0()).done) {
|
|
350
|
+
_context4.next = 14;
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
start = _context4.t1.value;
|
|
354
|
+
if (!(!results[start] || results[start]._ !== undefined)) {
|
|
355
|
+
_context4.next = 10;
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
return _context4.abrupt("continue", 6);
|
|
359
|
+
case 10:
|
|
360
|
+
while (this.offsets[i] != start && i < map.length) i++; // weak comparison as 'start' is a string
|
|
361
|
+
results[start]._ = map[i++];
|
|
362
|
+
_context4.next = 6;
|
|
363
|
+
break;
|
|
364
|
+
case 14:
|
|
365
|
+
return _context4.abrupt("return", Object.values(results).filter(function (r) {
|
|
366
|
+
return r !== undefined;
|
|
367
|
+
}));
|
|
368
|
+
case 15:
|
|
369
|
+
case "end":
|
|
370
|
+
return _context4.stop();
|
|
371
|
+
}
|
|
372
|
+
}, _callee4, this);
|
|
373
|
+
}));
|
|
374
|
+
function readLines(_x, _x2) {
|
|
375
|
+
return _readLines.apply(this, arguments);
|
|
376
|
+
}
|
|
377
|
+
return readLines;
|
|
378
|
+
}()
|
|
379
|
+
}, {
|
|
380
|
+
key: "insert",
|
|
381
|
+
value: function () {
|
|
382
|
+
var _insert = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(data) {
|
|
383
|
+
var line, position;
|
|
384
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
385
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
386
|
+
case 0:
|
|
387
|
+
if (!this.destroyed) {
|
|
388
|
+
_context5.next = 2;
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
throw new Error('Database is destroyed');
|
|
392
|
+
case 2:
|
|
393
|
+
_context5.next = 4;
|
|
394
|
+
return this.serializer.serialize(data, {
|
|
395
|
+
compress: this.opts.compress
|
|
396
|
+
});
|
|
397
|
+
case 4:
|
|
398
|
+
line = _context5.sent;
|
|
399
|
+
// using Buffer for offsets accuracy
|
|
400
|
+
if (this.shouldTruncate) {
|
|
401
|
+
this.writeBuffer.push(this.indexOffset);
|
|
402
|
+
this.shouldTruncate = false;
|
|
403
|
+
}
|
|
404
|
+
position = this.offsets.length;
|
|
405
|
+
this.offsets.push(this.indexOffset);
|
|
406
|
+
this.indexOffset += line.length;
|
|
407
|
+
this.indexManager.add(data, position);
|
|
408
|
+
this.emit('insert', data, position);
|
|
409
|
+
this.writeBuffer.push(line);
|
|
410
|
+
if (!(!this.flushing && this.currentWriteBufferSize() > this.opts.maxMemoryUsage)) {
|
|
411
|
+
_context5.next = 15;
|
|
412
|
+
break;
|
|
413
|
+
}
|
|
414
|
+
_context5.next = 15;
|
|
415
|
+
return this.flush();
|
|
416
|
+
case 15:
|
|
417
|
+
this.shouldSave = true;
|
|
418
|
+
case 16:
|
|
419
|
+
case "end":
|
|
420
|
+
return _context5.stop();
|
|
421
|
+
}
|
|
422
|
+
}, _callee5, this);
|
|
423
|
+
}));
|
|
424
|
+
function insert(_x3) {
|
|
425
|
+
return _insert.apply(this, arguments);
|
|
426
|
+
}
|
|
427
|
+
return insert;
|
|
428
|
+
}()
|
|
429
|
+
}, {
|
|
430
|
+
key: "currentWriteBufferSize",
|
|
431
|
+
value: function currentWriteBufferSize() {
|
|
432
|
+
var lengths = this.writeBuffer.filter(function (b) {
|
|
433
|
+
return Buffer.isBuffer(b);
|
|
434
|
+
}).map(function (b) {
|
|
435
|
+
return b.length;
|
|
436
|
+
});
|
|
437
|
+
return lengths.reduce(function (a, b) {
|
|
438
|
+
return a + b;
|
|
439
|
+
}, 0);
|
|
440
|
+
}
|
|
441
|
+
}, {
|
|
442
|
+
key: "flush",
|
|
443
|
+
value: function flush() {
|
|
444
|
+
var _this7 = this;
|
|
445
|
+
if (this.flushing) return this.flushing;
|
|
446
|
+
return new Promise(function (resolve, reject) {
|
|
447
|
+
if (_this7.destroyed) return reject(new Error('Database is destroyed'));
|
|
448
|
+
if (!_this7.writeBuffer.length) return resolve();
|
|
449
|
+
var err;
|
|
450
|
+
_this7.flushing = _this7._flush()["catch"](function (e) {
|
|
451
|
+
return err = e;
|
|
452
|
+
})["finally"](function () {
|
|
453
|
+
_this7.flushing = false;
|
|
454
|
+
err ? reject(err) : resolve();
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}, {
|
|
459
|
+
key: "_flush",
|
|
460
|
+
value: function () {
|
|
461
|
+
var _flush2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
|
|
462
|
+
var fd, data, pos;
|
|
463
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
464
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
465
|
+
case 0:
|
|
466
|
+
_context6.next = 2;
|
|
467
|
+
return _fs["default"].promises.open(this.fileHandler.filePath, 'a');
|
|
468
|
+
case 2:
|
|
469
|
+
fd = _context6.sent;
|
|
470
|
+
_context6.prev = 3;
|
|
471
|
+
case 4:
|
|
472
|
+
if (!this.writeBuffer.length) {
|
|
473
|
+
_context6.next = 23;
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
data = void 0;
|
|
477
|
+
pos = this.writeBuffer.findIndex(function (b) {
|
|
478
|
+
return typeof b === 'number';
|
|
479
|
+
});
|
|
480
|
+
if (!(pos === 0)) {
|
|
481
|
+
_context6.next = 18;
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
_context6.next = 10;
|
|
485
|
+
return fd.close();
|
|
486
|
+
case 10:
|
|
487
|
+
_context6.next = 12;
|
|
488
|
+
return this.fileHandler.truncate(this.writeBuffer.shift());
|
|
489
|
+
case 12:
|
|
490
|
+
_context6.next = 14;
|
|
491
|
+
return _fs["default"].promises.open(this.fileHandler.filePath, 'a');
|
|
492
|
+
case 14:
|
|
493
|
+
fd = _context6.sent;
|
|
494
|
+
return _context6.abrupt("continue", 4);
|
|
495
|
+
case 18:
|
|
496
|
+
if (pos === -1) {
|
|
497
|
+
data = Buffer.concat(this.writeBuffer);
|
|
498
|
+
this.writeBuffer.length = 0;
|
|
499
|
+
} else {
|
|
500
|
+
data = Buffer.concat(this.writeBuffer.slice(0, pos));
|
|
501
|
+
this.writeBuffer.splice(0, pos);
|
|
502
|
+
}
|
|
503
|
+
case 19:
|
|
504
|
+
_context6.next = 21;
|
|
505
|
+
return fd.write(data);
|
|
506
|
+
case 21:
|
|
507
|
+
_context6.next = 4;
|
|
508
|
+
break;
|
|
509
|
+
case 23:
|
|
510
|
+
this.shouldSave = true;
|
|
511
|
+
_context6.next = 29;
|
|
512
|
+
break;
|
|
513
|
+
case 26:
|
|
514
|
+
_context6.prev = 26;
|
|
515
|
+
_context6.t0 = _context6["catch"](3);
|
|
516
|
+
console.error('Error flushing:', _context6.t0);
|
|
517
|
+
case 29:
|
|
518
|
+
_context6.prev = 29;
|
|
519
|
+
_context6.next = 32;
|
|
520
|
+
return fd.close();
|
|
521
|
+
case 32:
|
|
522
|
+
return _context6.finish(29);
|
|
523
|
+
case 33:
|
|
524
|
+
case "end":
|
|
525
|
+
return _context6.stop();
|
|
526
|
+
}
|
|
527
|
+
}, _callee6, this, [[3, 26, 29, 33]]);
|
|
528
|
+
}));
|
|
529
|
+
function _flush() {
|
|
530
|
+
return _flush2.apply(this, arguments);
|
|
531
|
+
}
|
|
532
|
+
return _flush;
|
|
533
|
+
}()
|
|
534
|
+
}, {
|
|
535
|
+
key: "walk",
|
|
536
|
+
value: function walk(map) {
|
|
537
|
+
var _this = this;
|
|
538
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
539
|
+
return _wrapAsyncGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
|
|
540
|
+
var ranges, partitionedRanges, currentPartition, line, m, _i, _partitionedRanges, _ranges, lines, _loop, _line;
|
|
541
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context8) {
|
|
542
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
543
|
+
case 0:
|
|
544
|
+
if (!_this.destroyed) {
|
|
545
|
+
_context8.next = 2;
|
|
546
|
+
break;
|
|
547
|
+
}
|
|
548
|
+
throw new Error('Database is destroyed');
|
|
549
|
+
case 2:
|
|
550
|
+
_context8.t0 = _this.shouldSave;
|
|
551
|
+
if (!_context8.t0) {
|
|
552
|
+
_context8.next = 6;
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
555
|
+
_context8.next = 6;
|
|
556
|
+
return _awaitAsyncGenerator(_this.save()["catch"](console.error));
|
|
557
|
+
case 6:
|
|
558
|
+
if (!(_this.indexOffset === 0)) {
|
|
559
|
+
_context8.next = 8;
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
return _context8.abrupt("return");
|
|
563
|
+
case 8:
|
|
564
|
+
if (!Array.isArray(map)) {
|
|
565
|
+
if (map && _typeof(map) === 'object') {
|
|
566
|
+
map = _this.indexManager.query(map, options.matchAny);
|
|
567
|
+
} else {
|
|
568
|
+
map = _toConsumableArray(Array(_this.offsets.length).keys());
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
ranges = _this.getRanges(map);
|
|
572
|
+
partitionedRanges = [], currentPartition = 0;
|
|
573
|
+
for (line in ranges) {
|
|
574
|
+
if (partitionedRanges[currentPartition] === undefined) {
|
|
575
|
+
partitionedRanges[currentPartition] = [];
|
|
576
|
+
}
|
|
577
|
+
partitionedRanges[currentPartition].push(ranges[line]);
|
|
578
|
+
if (partitionedRanges[currentPartition].length >= _this.opts.maxMemoryUsage) {
|
|
579
|
+
+currentPartition, _readOnlyError("currentPartition");
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
m = 0;
|
|
583
|
+
_i = 0, _partitionedRanges = partitionedRanges;
|
|
584
|
+
case 14:
|
|
585
|
+
if (!(_i < _partitionedRanges.length)) {
|
|
586
|
+
_context8.next = 31;
|
|
587
|
+
break;
|
|
588
|
+
}
|
|
589
|
+
_ranges = _partitionedRanges[_i];
|
|
590
|
+
_context8.next = 18;
|
|
591
|
+
return _awaitAsyncGenerator(_this.fileHandler.readRanges(_ranges));
|
|
592
|
+
case 18:
|
|
593
|
+
lines = _context8.sent;
|
|
594
|
+
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
|
|
595
|
+
var err, entry;
|
|
596
|
+
return _regeneratorRuntime().wrap(function _loop$(_context7) {
|
|
597
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
598
|
+
case 0:
|
|
599
|
+
_context7.next = 2;
|
|
600
|
+
return _awaitAsyncGenerator(_this.serializer.deserialize(lines[_line])["catch"](function (e) {
|
|
601
|
+
return console.error(err = e);
|
|
602
|
+
}));
|
|
603
|
+
case 2:
|
|
604
|
+
entry = _context7.sent;
|
|
605
|
+
if (!err) {
|
|
606
|
+
_context7.next = 5;
|
|
607
|
+
break;
|
|
608
|
+
}
|
|
609
|
+
return _context7.abrupt("return", 1);
|
|
610
|
+
case 5:
|
|
611
|
+
if (entry._ === undefined) {
|
|
612
|
+
while (_this.offsets[m] != _line && m < map.length) m++; // weak comparison as 'start' is a string
|
|
613
|
+
entry._ = m++;
|
|
614
|
+
}
|
|
615
|
+
_context7.next = 8;
|
|
616
|
+
return entry;
|
|
617
|
+
case 8:
|
|
618
|
+
case "end":
|
|
619
|
+
return _context7.stop();
|
|
620
|
+
}
|
|
621
|
+
}, _loop);
|
|
622
|
+
});
|
|
623
|
+
_context8.t1 = _regeneratorRuntime().keys(lines);
|
|
624
|
+
case 21:
|
|
625
|
+
if ((_context8.t2 = _context8.t1()).done) {
|
|
626
|
+
_context8.next = 28;
|
|
627
|
+
break;
|
|
628
|
+
}
|
|
629
|
+
_line = _context8.t2.value;
|
|
630
|
+
return _context8.delegateYield(_loop(), "t3", 24);
|
|
631
|
+
case 24:
|
|
632
|
+
if (!_context8.t3) {
|
|
633
|
+
_context8.next = 26;
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
return _context8.abrupt("continue", 21);
|
|
637
|
+
case 26:
|
|
638
|
+
_context8.next = 21;
|
|
639
|
+
break;
|
|
640
|
+
case 28:
|
|
641
|
+
_i++;
|
|
642
|
+
_context8.next = 14;
|
|
643
|
+
break;
|
|
644
|
+
case 31:
|
|
645
|
+
case "end":
|
|
646
|
+
return _context8.stop();
|
|
647
|
+
}
|
|
648
|
+
}, _callee7);
|
|
649
|
+
}))();
|
|
650
|
+
}
|
|
651
|
+
}, {
|
|
652
|
+
key: "query",
|
|
653
|
+
value: function () {
|
|
654
|
+
var _query = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(criteria) {
|
|
655
|
+
var options,
|
|
656
|
+
results,
|
|
657
|
+
_options$orderBy$spli,
|
|
658
|
+
_options$orderBy$spli2,
|
|
659
|
+
field,
|
|
660
|
+
_options$orderBy$spli3,
|
|
661
|
+
direction,
|
|
662
|
+
matchingLines,
|
|
663
|
+
_args9 = arguments;
|
|
664
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context9) {
|
|
665
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
666
|
+
case 0:
|
|
667
|
+
options = _args9.length > 1 && _args9[1] !== undefined ? _args9[1] : {};
|
|
668
|
+
if (!this.destroyed) {
|
|
669
|
+
_context9.next = 3;
|
|
670
|
+
break;
|
|
671
|
+
}
|
|
672
|
+
throw new Error('Database is destroyed');
|
|
673
|
+
case 3:
|
|
674
|
+
_context9.t0 = this.shouldSave;
|
|
675
|
+
if (!_context9.t0) {
|
|
676
|
+
_context9.next = 7;
|
|
677
|
+
break;
|
|
678
|
+
}
|
|
679
|
+
_context9.next = 7;
|
|
680
|
+
return this.save()["catch"](console.error);
|
|
681
|
+
case 7:
|
|
682
|
+
if (!Array.isArray(criteria)) {
|
|
683
|
+
_context9.next = 16;
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
_context9.next = 10;
|
|
687
|
+
return this.readLines(criteria);
|
|
688
|
+
case 10:
|
|
689
|
+
results = _context9.sent;
|
|
690
|
+
if (options.orderBy) {
|
|
691
|
+
_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;
|
|
692
|
+
results.sort(function (a, b) {
|
|
693
|
+
if (a[field] > b[field]) return direction === 'asc' ? 1 : -1;
|
|
694
|
+
if (a[field] < b[field]) return direction === 'asc' ? -1 : 1;
|
|
695
|
+
return 0;
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
if (options.limit) {
|
|
699
|
+
results = results.slice(0, options.limit);
|
|
700
|
+
}
|
|
701
|
+
return _context9.abrupt("return", results);
|
|
702
|
+
case 16:
|
|
703
|
+
_context9.next = 18;
|
|
704
|
+
return this.indexManager.query(criteria, options.matchAny);
|
|
705
|
+
case 18:
|
|
706
|
+
matchingLines = _context9.sent;
|
|
707
|
+
if (!(!matchingLines || !matchingLines.size)) {
|
|
708
|
+
_context9.next = 21;
|
|
709
|
+
break;
|
|
710
|
+
}
|
|
711
|
+
return _context9.abrupt("return", []);
|
|
712
|
+
case 21:
|
|
713
|
+
_context9.next = 23;
|
|
714
|
+
return this.query(_toConsumableArray(matchingLines), options);
|
|
715
|
+
case 23:
|
|
716
|
+
return _context9.abrupt("return", _context9.sent);
|
|
717
|
+
case 24:
|
|
718
|
+
case "end":
|
|
719
|
+
return _context9.stop();
|
|
720
|
+
}
|
|
721
|
+
}, _callee8, this);
|
|
722
|
+
}));
|
|
723
|
+
function query(_x4) {
|
|
724
|
+
return _query.apply(this, arguments);
|
|
725
|
+
}
|
|
726
|
+
return query;
|
|
727
|
+
}()
|
|
728
|
+
}, {
|
|
729
|
+
key: "update",
|
|
730
|
+
value: function () {
|
|
731
|
+
var _update = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(criteria, data) {
|
|
732
|
+
var _this8 = this;
|
|
733
|
+
var options,
|
|
734
|
+
matchingLines,
|
|
735
|
+
ranges,
|
|
736
|
+
validMatchingLines,
|
|
737
|
+
entries,
|
|
738
|
+
lines,
|
|
739
|
+
_iterator,
|
|
740
|
+
_step,
|
|
741
|
+
_loop2,
|
|
742
|
+
offsets,
|
|
743
|
+
byteOffset,
|
|
744
|
+
k,
|
|
745
|
+
_args11 = arguments;
|
|
746
|
+
return _regeneratorRuntime().wrap(function _callee9$(_context11) {
|
|
747
|
+
while (1) switch (_context11.prev = _context11.next) {
|
|
748
|
+
case 0:
|
|
749
|
+
options = _args11.length > 2 && _args11[2] !== undefined ? _args11[2] : {};
|
|
750
|
+
if (!this.destroyed) {
|
|
751
|
+
_context11.next = 3;
|
|
752
|
+
break;
|
|
753
|
+
}
|
|
754
|
+
throw new Error('Database is destroyed');
|
|
755
|
+
case 3:
|
|
756
|
+
_context11.t0 = this.shouldSave;
|
|
757
|
+
if (!_context11.t0) {
|
|
758
|
+
_context11.next = 7;
|
|
759
|
+
break;
|
|
760
|
+
}
|
|
761
|
+
_context11.next = 7;
|
|
762
|
+
return this.save()["catch"](console.error);
|
|
763
|
+
case 7:
|
|
764
|
+
_context11.next = 9;
|
|
765
|
+
return this.indexManager.query(criteria, options.matchAny);
|
|
766
|
+
case 9:
|
|
767
|
+
matchingLines = _context11.sent;
|
|
768
|
+
if (!(!matchingLines || !matchingLines.size)) {
|
|
769
|
+
_context11.next = 12;
|
|
770
|
+
break;
|
|
771
|
+
}
|
|
772
|
+
return _context11.abrupt("return", []);
|
|
773
|
+
case 12:
|
|
774
|
+
ranges = this.getRanges(_toConsumableArray(matchingLines));
|
|
775
|
+
validMatchingLines = new Set(ranges.map(function (r) {
|
|
776
|
+
return r.index;
|
|
777
|
+
}));
|
|
778
|
+
if (validMatchingLines.size) {
|
|
779
|
+
_context11.next = 16;
|
|
780
|
+
break;
|
|
781
|
+
}
|
|
782
|
+
return _context11.abrupt("return", []);
|
|
783
|
+
case 16:
|
|
784
|
+
_context11.next = 18;
|
|
785
|
+
return this.readLines(_toConsumableArray(validMatchingLines), ranges);
|
|
786
|
+
case 18:
|
|
787
|
+
entries = _context11.sent;
|
|
788
|
+
lines = [];
|
|
789
|
+
_iterator = _createForOfIteratorHelper(entries);
|
|
790
|
+
_context11.prev = 21;
|
|
791
|
+
_loop2 = /*#__PURE__*/_regeneratorRuntime().mark(function _loop2() {
|
|
792
|
+
var entry, err, updated, ret;
|
|
793
|
+
return _regeneratorRuntime().wrap(function _loop2$(_context10) {
|
|
794
|
+
while (1) switch (_context10.prev = _context10.next) {
|
|
795
|
+
case 0:
|
|
796
|
+
entry = _step.value;
|
|
797
|
+
updated = Object.assign(entry, data);
|
|
798
|
+
_context10.next = 4;
|
|
799
|
+
return _this8.serializer.serialize(updated)["catch"](function (e) {
|
|
800
|
+
return err = e;
|
|
801
|
+
});
|
|
802
|
+
case 4:
|
|
803
|
+
ret = _context10.sent;
|
|
804
|
+
err || lines.push(ret);
|
|
805
|
+
case 6:
|
|
806
|
+
case "end":
|
|
807
|
+
return _context10.stop();
|
|
808
|
+
}
|
|
809
|
+
}, _loop2);
|
|
810
|
+
});
|
|
811
|
+
_iterator.s();
|
|
812
|
+
case 24:
|
|
813
|
+
if ((_step = _iterator.n()).done) {
|
|
814
|
+
_context11.next = 28;
|
|
815
|
+
break;
|
|
816
|
+
}
|
|
817
|
+
return _context11.delegateYield(_loop2(), "t1", 26);
|
|
818
|
+
case 26:
|
|
819
|
+
_context11.next = 24;
|
|
820
|
+
break;
|
|
821
|
+
case 28:
|
|
822
|
+
_context11.next = 33;
|
|
823
|
+
break;
|
|
824
|
+
case 30:
|
|
825
|
+
_context11.prev = 30;
|
|
826
|
+
_context11.t2 = _context11["catch"](21);
|
|
827
|
+
_iterator.e(_context11.t2);
|
|
828
|
+
case 33:
|
|
829
|
+
_context11.prev = 33;
|
|
830
|
+
_iterator.f();
|
|
831
|
+
return _context11.finish(33);
|
|
832
|
+
case 36:
|
|
833
|
+
offsets = [];
|
|
834
|
+
byteOffset = 0, k = 0;
|
|
835
|
+
this.offsets.forEach(function (n, i) {
|
|
836
|
+
var prevByteOffset = byteOffset;
|
|
837
|
+
if (validMatchingLines.has(i) && ranges[k]) {
|
|
838
|
+
var r = ranges[k];
|
|
839
|
+
byteOffset += lines[k].length - (r.end - r.start);
|
|
840
|
+
k++;
|
|
841
|
+
}
|
|
842
|
+
offsets.push(n + prevByteOffset);
|
|
843
|
+
});
|
|
844
|
+
this.offsets = offsets;
|
|
845
|
+
this.indexOffset += byteOffset;
|
|
846
|
+
_context11.next = 43;
|
|
847
|
+
return this.fileHandler.replaceLines(ranges, lines);
|
|
848
|
+
case 43:
|
|
849
|
+
_toConsumableArray(validMatchingLines).forEach(function (lineNumber, i) {
|
|
850
|
+
_this8.indexManager.dryRemove(lineNumber);
|
|
851
|
+
_this8.indexManager.add(entries[i], lineNumber);
|
|
852
|
+
});
|
|
853
|
+
this.shouldSave = true;
|
|
854
|
+
return _context11.abrupt("return", entries);
|
|
855
|
+
case 46:
|
|
856
|
+
case "end":
|
|
857
|
+
return _context11.stop();
|
|
858
|
+
}
|
|
859
|
+
}, _callee9, this, [[21, 30, 33, 36]]);
|
|
860
|
+
}));
|
|
861
|
+
function update(_x5, _x6) {
|
|
862
|
+
return _update.apply(this, arguments);
|
|
863
|
+
}
|
|
864
|
+
return update;
|
|
865
|
+
}()
|
|
866
|
+
}, {
|
|
867
|
+
key: "delete",
|
|
868
|
+
value: function () {
|
|
869
|
+
var _delete2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(criteria) {
|
|
870
|
+
var options,
|
|
871
|
+
matchingLines,
|
|
872
|
+
ranges,
|
|
873
|
+
validMatchingLines,
|
|
874
|
+
offsets,
|
|
875
|
+
byteOffset,
|
|
876
|
+
k,
|
|
877
|
+
_args12 = arguments;
|
|
878
|
+
return _regeneratorRuntime().wrap(function _callee10$(_context12) {
|
|
879
|
+
while (1) switch (_context12.prev = _context12.next) {
|
|
880
|
+
case 0:
|
|
881
|
+
options = _args12.length > 1 && _args12[1] !== undefined ? _args12[1] : {};
|
|
882
|
+
if (!this.destroyed) {
|
|
883
|
+
_context12.next = 3;
|
|
884
|
+
break;
|
|
885
|
+
}
|
|
886
|
+
throw new Error('Database is destroyed');
|
|
887
|
+
case 3:
|
|
888
|
+
_context12.t0 = this.shouldSave;
|
|
889
|
+
if (!_context12.t0) {
|
|
890
|
+
_context12.next = 7;
|
|
891
|
+
break;
|
|
892
|
+
}
|
|
893
|
+
_context12.next = 7;
|
|
894
|
+
return this.save()["catch"](console.error);
|
|
895
|
+
case 7:
|
|
896
|
+
_context12.next = 9;
|
|
897
|
+
return this.indexManager.query(criteria, options.matchAny);
|
|
898
|
+
case 9:
|
|
899
|
+
matchingLines = _context12.sent;
|
|
900
|
+
if (!(!matchingLines || !matchingLines.size)) {
|
|
901
|
+
_context12.next = 12;
|
|
902
|
+
break;
|
|
903
|
+
}
|
|
904
|
+
return _context12.abrupt("return", 0);
|
|
905
|
+
case 12:
|
|
906
|
+
ranges = this.getRanges(_toConsumableArray(matchingLines));
|
|
907
|
+
validMatchingLines = new Set(ranges.map(function (r) {
|
|
908
|
+
return r.index;
|
|
909
|
+
}));
|
|
910
|
+
_context12.next = 16;
|
|
911
|
+
return this.fileHandler.replaceLines(ranges, []);
|
|
912
|
+
case 16:
|
|
913
|
+
offsets = [];
|
|
914
|
+
byteOffset = 0, k = 0;
|
|
915
|
+
this.offsets.forEach(function (n, i) {
|
|
916
|
+
if (validMatchingLines.has(i)) {
|
|
917
|
+
var r = ranges[k];
|
|
918
|
+
byteOffset -= r.end - r.start;
|
|
919
|
+
k++;
|
|
920
|
+
} else {
|
|
921
|
+
offsets.push(n + byteOffset);
|
|
922
|
+
}
|
|
923
|
+
});
|
|
924
|
+
this.offsets = offsets;
|
|
925
|
+
this.indexOffset += byteOffset;
|
|
926
|
+
this.indexManager.remove(_toConsumableArray(validMatchingLines));
|
|
927
|
+
this.shouldSave = true;
|
|
928
|
+
return _context12.abrupt("return", ranges.length);
|
|
929
|
+
case 24:
|
|
930
|
+
case "end":
|
|
931
|
+
return _context12.stop();
|
|
932
|
+
}
|
|
933
|
+
}, _callee10, this);
|
|
934
|
+
}));
|
|
935
|
+
function _delete(_x7) {
|
|
936
|
+
return _delete2.apply(this, arguments);
|
|
937
|
+
}
|
|
938
|
+
return _delete;
|
|
939
|
+
}()
|
|
940
|
+
}, {
|
|
941
|
+
key: "destroy",
|
|
942
|
+
value: function () {
|
|
943
|
+
var _destroy = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11() {
|
|
944
|
+
return _regeneratorRuntime().wrap(function _callee11$(_context13) {
|
|
945
|
+
while (1) switch (_context13.prev = _context13.next) {
|
|
946
|
+
case 0:
|
|
947
|
+
_context13.t0 = this.shouldSave;
|
|
948
|
+
if (!_context13.t0) {
|
|
949
|
+
_context13.next = 4;
|
|
950
|
+
break;
|
|
951
|
+
}
|
|
952
|
+
_context13.next = 4;
|
|
953
|
+
return this.save()["catch"](console.error);
|
|
954
|
+
case 4:
|
|
955
|
+
this.destroyed = true;
|
|
956
|
+
this.indexOffset = 0;
|
|
957
|
+
this.indexManager.index = {};
|
|
958
|
+
this.writeBuffer.length = 0;
|
|
959
|
+
this.initialized = false;
|
|
960
|
+
this.fileHandler.destroy();
|
|
961
|
+
case 10:
|
|
962
|
+
case "end":
|
|
963
|
+
return _context13.stop();
|
|
964
|
+
}
|
|
965
|
+
}, _callee11, this);
|
|
966
|
+
}));
|
|
967
|
+
function destroy() {
|
|
968
|
+
return _destroy.apply(this, arguments);
|
|
969
|
+
}
|
|
970
|
+
return destroy;
|
|
971
|
+
}()
|
|
972
|
+
}, {
|
|
973
|
+
key: "length",
|
|
974
|
+
get: function get() {
|
|
975
|
+
return this.offsets.length;
|
|
976
|
+
}
|
|
977
|
+
}, {
|
|
978
|
+
key: "index",
|
|
979
|
+
get: function get() {
|
|
980
|
+
return this.indexManager.index;
|
|
981
|
+
}
|
|
982
|
+
}]);
|
|
983
|
+
}(_events.EventEmitter);
|