zkjson 0.8.0 → 0.8.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.
@@ -20,23 +20,52 @@ var SMTMemDb = exports["default"] = /*#__PURE__*/function () {
20
20
  this.kv = kv;
21
21
  this.nodes = {};
22
22
  this.root = F.zero;
23
- if (this.kv) {
24
- var root = this.kv.get("root");
25
- if (root) this.root = root;else this.kv.put("root", this.root);
26
- }
27
23
  this.F = F;
28
24
  }
29
25
  return _createClass(SMTMemDb, [{
30
- key: "getRoot",
26
+ key: "init",
31
27
  value: function () {
32
- var _getRoot = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
28
+ var _init = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
29
+ var root;
33
30
  return _regenerator().w(function (_context) {
34
31
  while (1) switch (_context.n) {
35
32
  case 0:
36
- return _context.a(2, this.root);
33
+ if (!this.kv) {
34
+ _context.n = 2;
35
+ break;
36
+ }
37
+ root = this.kv.get("root");
38
+ if (!root) {
39
+ _context.n = 1;
40
+ break;
41
+ }
42
+ this.root = root;
43
+ _context.n = 2;
44
+ break;
45
+ case 1:
46
+ _context.n = 2;
47
+ return this.kv.put("root", this.root);
48
+ case 2:
49
+ return _context.a(2);
37
50
  }
38
51
  }, _callee, this);
39
52
  }));
53
+ function init() {
54
+ return _init.apply(this, arguments);
55
+ }
56
+ return init;
57
+ }()
58
+ }, {
59
+ key: "getRoot",
60
+ value: function () {
61
+ var _getRoot = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
62
+ return _regenerator().w(function (_context2) {
63
+ while (1) switch (_context2.n) {
64
+ case 0:
65
+ return _context2.a(2, this.root);
66
+ }
67
+ }, _callee2, this);
68
+ }));
40
69
  function getRoot() {
41
70
  return _getRoot.apply(this, arguments);
42
71
  }
@@ -60,19 +89,19 @@ var SMTMemDb = exports["default"] = /*#__PURE__*/function () {
60
89
  }, {
61
90
  key: "get",
62
91
  value: function () {
63
- var _get = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(key) {
92
+ var _get = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(key) {
64
93
  var keyS, node;
65
- return _regenerator().w(function (_context2) {
66
- while (1) switch (_context2.n) {
94
+ return _regenerator().w(function (_context3) {
95
+ while (1) switch (_context3.n) {
67
96
  case 0:
68
97
  keyS = this._key2str(key);
69
98
  if (!this.nodes[keyS]) {
70
99
  node = this.kv.get("node_".concat(keyS));
71
100
  if (node) this.nodes[keyS] = node;
72
101
  }
73
- return _context2.a(2, this.nodes[keyS]);
102
+ return _context3.a(2, this.nodes[keyS]);
74
103
  }
75
- }, _callee2, this);
104
+ }, _callee3, this);
76
105
  }));
77
106
  function get(_x) {
78
107
  return _get.apply(this, arguments);
@@ -82,21 +111,21 @@ var SMTMemDb = exports["default"] = /*#__PURE__*/function () {
82
111
  }, {
83
112
  key: "multiGet",
84
113
  value: function () {
85
- var _multiGet = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(keys) {
114
+ var _multiGet = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(keys) {
86
115
  var promises, i;
87
- return _regenerator().w(function (_context3) {
88
- while (1) switch (_context3.n) {
116
+ return _regenerator().w(function (_context4) {
117
+ while (1) switch (_context4.n) {
89
118
  case 0:
90
119
  promises = [];
91
120
  for (i = 0; i < keys.length; i++) {
92
121
  promises.push(this.get(keys[i]));
93
122
  }
94
- _context3.n = 1;
123
+ _context4.n = 1;
95
124
  return Promise.all(promises);
96
125
  case 1:
97
- return _context3.a(2, _context3.v);
126
+ return _context4.a(2, _context4.v);
98
127
  }
99
- }, _callee3, this);
128
+ }, _callee4, this);
100
129
  }));
101
130
  function multiGet(_x2) {
102
131
  return _multiGet.apply(this, arguments);
@@ -106,16 +135,21 @@ var SMTMemDb = exports["default"] = /*#__PURE__*/function () {
106
135
  }, {
107
136
  key: "setRoot",
108
137
  value: function () {
109
- var _setRoot = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(rt) {
110
- return _regenerator().w(function (_context4) {
111
- while (1) switch (_context4.n) {
138
+ var _setRoot = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(rt) {
139
+ return _regenerator().w(function (_context5) {
140
+ while (1) switch (_context5.n) {
112
141
  case 0:
113
142
  this.root = rt;
114
- if (this.kv) this.kv.put("root", this.root);
143
+ if (!this.kv) {
144
+ _context5.n = 1;
145
+ break;
146
+ }
147
+ _context5.n = 1;
148
+ return this.kv.put("root", this.root);
115
149
  case 1:
116
- return _context4.a(2);
150
+ return _context5.a(2);
117
151
  }
118
- }, _callee4, this);
152
+ }, _callee5, this);
119
153
  }));
120
154
  function setRoot(_x3) {
121
155
  return _setRoot.apply(this, arguments);
@@ -125,21 +159,34 @@ var SMTMemDb = exports["default"] = /*#__PURE__*/function () {
125
159
  }, {
126
160
  key: "multiIns",
127
161
  value: function () {
128
- var _multiIns = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(inserts) {
162
+ var _multiIns = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(inserts) {
129
163
  var i, keyS;
130
- return _regenerator().w(function (_context5) {
131
- while (1) switch (_context5.n) {
164
+ return _regenerator().w(function (_context6) {
165
+ while (1) switch (_context6.n) {
132
166
  case 0:
133
- for (i = 0; i < inserts.length; i++) {
134
- keyS = this._key2str(inserts[i][0]);
135
- this._normalize(inserts[i][1]);
136
- this.nodes[keyS] = inserts[i][1];
137
- if (this.kv) this.kv.put("node_".concat(keyS), this.nodes[keyS]);
138
- }
167
+ i = 0;
139
168
  case 1:
140
- return _context5.a(2);
169
+ if (!(i < inserts.length)) {
170
+ _context6.n = 3;
171
+ break;
172
+ }
173
+ keyS = this._key2str(inserts[i][0]);
174
+ this._normalize(inserts[i][1]);
175
+ this.nodes[keyS] = inserts[i][1];
176
+ if (!this.kv) {
177
+ _context6.n = 2;
178
+ break;
179
+ }
180
+ _context6.n = 2;
181
+ return this.kv.put("node_".concat(keyS), this.nodes[keyS]);
182
+ case 2:
183
+ i++;
184
+ _context6.n = 1;
185
+ break;
186
+ case 3:
187
+ return _context6.a(2);
141
188
  }
142
- }, _callee5, this);
189
+ }, _callee6, this);
143
190
  }));
144
191
  function multiIns(_x4) {
145
192
  return _multiIns.apply(this, arguments);
@@ -149,20 +196,33 @@ var SMTMemDb = exports["default"] = /*#__PURE__*/function () {
149
196
  }, {
150
197
  key: "multiDel",
151
198
  value: function () {
152
- var _multiDel = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(dels) {
199
+ var _multiDel = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(dels) {
153
200
  var i, keyS;
154
- return _regenerator().w(function (_context6) {
155
- while (1) switch (_context6.n) {
201
+ return _regenerator().w(function (_context7) {
202
+ while (1) switch (_context7.n) {
156
203
  case 0:
157
- for (i = 0; i < dels.length; i++) {
158
- keyS = this._key2str(dels[i]);
159
- delete this.nodes[keyS];
160
- if (this.kv) this.kv.del("node_".concat(keyS));
161
- }
204
+ i = 0;
162
205
  case 1:
163
- return _context6.a(2);
206
+ if (!(i < dels.length)) {
207
+ _context7.n = 3;
208
+ break;
209
+ }
210
+ keyS = this._key2str(dels[i]);
211
+ delete this.nodes[keyS];
212
+ if (!this.kv) {
213
+ _context7.n = 2;
214
+ break;
215
+ }
216
+ _context7.n = 2;
217
+ return this.kv.del("node_".concat(keyS));
218
+ case 2:
219
+ i++;
220
+ _context7.n = 1;
221
+ break;
222
+ case 3:
223
+ return _context7.a(2);
164
224
  }
165
- }, _callee6, this);
225
+ }, _callee7, this);
166
226
  }));
167
227
  function multiDel(_x5) {
168
228
  return _multiDel.apply(this, arguments);
package/cjs/db.js CHANGED
@@ -27,6 +27,13 @@ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o =
27
27
  function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
28
28
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
29
29
  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); }
30
+ var to64 = function to64(hash) {
31
+ var n = BigInt(hash);
32
+ var hex = n.toString(16);
33
+ if (hex.length % 2) hex = "0" + hex;
34
+ var buf = Buffer.from(hex, "hex");
35
+ return buf.toString("base64");
36
+ };
30
37
  var DB = exports["default"] = /*#__PURE__*/function () {
31
38
  function DB(_ref) {
32
39
  var _ref$size_val = _ref.size_val,
@@ -487,6 +494,13 @@ var DB = exports["default"] = /*#__PURE__*/function () {
487
494
  }
488
495
  return getInputs;
489
496
  }()
497
+ }, {
498
+ key: "hash",
499
+ value: function hash(format) {
500
+ var _hash = this.tree.F.toObject(this.tree.root);
501
+ if (format === "base64") return to64(_hash.toString());
502
+ return _hash.toString();
503
+ }
490
504
  }, {
491
505
  key: "getID",
492
506
  value: function getID(num) {
package/cjs/db_tree.js CHANGED
@@ -18,6 +18,19 @@ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o =
18
18
  function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
19
19
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
20
20
  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); }
21
+ var to64 = function to64(hash) {
22
+ var n = BigInt(hash);
23
+ var hex = n.toString(16);
24
+ if (hex.length % 2) hex = "0" + hex;
25
+ var buf = Buffer.from(hex, "hex");
26
+ return buf.toString("base64");
27
+ };
28
+ var from64 = function from64(b64) {
29
+ var buf = Buffer.from(b64, "base64");
30
+ var hex = buf.toString("hex");
31
+ var n = BigInt("0x" + hex);
32
+ return n.toString();
33
+ };
21
34
  var DBTree = exports["default"] = /*#__PURE__*/function () {
22
35
  function DBTree(_ref) {
23
36
  var _ref$size_val = _ref.size_val,
@@ -70,6 +83,13 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
70
83
  }
71
84
  return init;
72
85
  }()
86
+ }, {
87
+ key: "hash",
88
+ value: function hash(format) {
89
+ var _hash = this.tree.F.toObject(this.tree.root);
90
+ if (format === "base64") return to64(_hash.toString());
91
+ return _hash.toString();
92
+ }
73
93
  }, {
74
94
  key: "exists",
75
95
  value: function () {
@@ -147,7 +167,6 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
147
167
  key: "addCollection",
148
168
  value: function () {
149
169
  var _addCollection = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(num) {
150
- var _this$kv2;
151
170
  var id, col, _col, root;
152
171
  return _regenerator().w(function (_context4) {
153
172
  while (1) switch (_context4.n) {
@@ -188,26 +207,26 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
188
207
  case 6:
189
208
  id = _context4.v;
190
209
  case 7:
191
- _col = new _collection_tree["default"]({
192
- size_val: this.size_val,
193
- size_path: this.size_path,
194
- level: this.level,
195
- size_json: this.size_json,
196
- kv: (_this$kv2 = this.kv) === null || _this$kv2 === void 0 ? void 0 : _this$kv2.call(this, "dir_".concat(id))
197
- });
198
210
  _context4.n = 8;
199
- return _col.init();
211
+ return this.loadCol(id);
200
212
  case 8:
201
- this.cols[id] = _col;
202
- this.ids[id] = true;
213
+ _col = _context4.v;
203
214
  root = _col.tree.F.toObject(_col.tree.root).toString();
204
215
  _context4.n = 9;
205
216
  return this.tree.insert(id, [root]);
206
217
  case 9:
207
- if (id === this.count) {
208
- this.count++;
209
- if (this.kvi) this.kvi.put("count", this.count);
218
+ if (!(id === this.count)) {
219
+ _context4.n = 10;
220
+ break;
221
+ }
222
+ this.count++;
223
+ if (!this.kvi) {
224
+ _context4.n = 10;
225
+ break;
210
226
  }
227
+ _context4.n = 10;
228
+ return this.kvi.put("count", this.count);
229
+ case 10:
211
230
  return _context4.a(2, id);
212
231
  }
213
232
  }, _callee4, this);
@@ -219,55 +238,140 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
219
238
  }()
220
239
  }, {
221
240
  key: "getColTree",
222
- value: function getColTree(col) {
223
- var _col = this.cols[col];
224
- if (!_col) throw Error("collection doesn't exist");
225
- return _col;
226
- }
241
+ value: function () {
242
+ var _getColTree = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(col) {
243
+ var _col;
244
+ return _regenerator().w(function (_context5) {
245
+ while (1) switch (_context5.n) {
246
+ case 0:
247
+ _col = this.cols[col];
248
+ if (_col) {
249
+ _context5.n = 2;
250
+ break;
251
+ }
252
+ _context5.n = 1;
253
+ return this.loadCollection(col);
254
+ case 1:
255
+ return _context5.a(2, _context5.v);
256
+ case 2:
257
+ return _context5.a(2, _col);
258
+ case 3:
259
+ return _context5.a(2);
260
+ }
261
+ }, _callee5, this);
262
+ }));
263
+ function getColTree(_x4) {
264
+ return _getColTree.apply(this, arguments);
265
+ }
266
+ return getColTree;
267
+ }()
268
+ }, {
269
+ key: "loadCol",
270
+ value: function () {
271
+ var _loadCol = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(id) {
272
+ var _this$kv2;
273
+ var col;
274
+ return _regenerator().w(function (_context6) {
275
+ while (1) switch (_context6.n) {
276
+ case 0:
277
+ col = new _collection_tree["default"]({
278
+ size_val: this.size_val,
279
+ size_path: this.size_path,
280
+ level: this.level,
281
+ size_json: this.size_json,
282
+ kv: (_this$kv2 = this.kv) === null || _this$kv2 === void 0 ? void 0 : _this$kv2.call(this, "dir_".concat(id))
283
+ });
284
+ _context6.n = 1;
285
+ return col.init();
286
+ case 1:
287
+ this.cols[id] = col;
288
+ this.ids[id] = true;
289
+ return _context6.a(2, col);
290
+ }
291
+ }, _callee6, this);
292
+ }));
293
+ function loadCol(_x5) {
294
+ return _loadCol.apply(this, arguments);
295
+ }
296
+ return loadCol;
297
+ }()
298
+ }, {
299
+ key: "loadCollection",
300
+ value: function () {
301
+ var _loadCollection = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(id) {
302
+ var hit;
303
+ return _regenerator().w(function (_context7) {
304
+ while (1) switch (_context7.n) {
305
+ case 0:
306
+ _context7.n = 1;
307
+ return this.tree.find(id);
308
+ case 1:
309
+ hit = _context7.v;
310
+ if (!(!hit || !hit.found)) {
311
+ _context7.n = 2;
312
+ break;
313
+ }
314
+ throw Error("collection doesn't exist");
315
+ case 2:
316
+ _context7.n = 3;
317
+ return this.loadCol(id);
318
+ case 3:
319
+ return _context7.a(2, _context7.v);
320
+ }
321
+ }, _callee7, this);
322
+ }));
323
+ function loadCollection(_x6) {
324
+ return _loadCollection.apply(this, arguments);
325
+ }
326
+ return loadCollection;
327
+ }()
227
328
  }, {
228
329
  key: "insert",
229
330
  value: function () {
230
- var _insert = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(col, _key, _val) {
331
+ var _insert = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(col, _key, _val) {
231
332
  var _col, update, res_doc, res_col;
232
- return _regenerator().w(function (_context5) {
233
- while (1) switch (_context5.n) {
333
+ return _regenerator().w(function (_context8) {
334
+ while (1) switch (_context8.n) {
234
335
  case 0:
235
- _col = this.getColTree(col);
336
+ _context8.n = 1;
337
+ return this.getColTree(col);
338
+ case 1:
339
+ _col = _context8.v;
236
340
  update = false;
237
- _context5.n = 1;
341
+ _context8.n = 2;
238
342
  return _col.get(_key);
239
- case 1:
240
- if (!_context5.v.found) {
241
- _context5.n = 3;
343
+ case 2:
344
+ if (!_context8.v.found) {
345
+ _context8.n = 4;
242
346
  break;
243
347
  }
244
348
  update = true;
245
- _context5.n = 2;
349
+ _context8.n = 3;
246
350
  return _col.update(_key, _val);
247
- case 2:
248
- res_doc = _context5.v;
249
- _context5.n = 5;
250
- break;
251
351
  case 3:
252
- _context5.n = 4;
253
- return _col.insert(_key, _val);
352
+ res_doc = _context8.v;
353
+ _context8.n = 6;
354
+ break;
254
355
  case 4:
255
- res_doc = _context5.v;
356
+ _context8.n = 5;
357
+ return _col.insert(_key, _val);
256
358
  case 5:
257
- _context5.n = 6;
258
- return this.updateDB(_col, col);
359
+ res_doc = _context8.v;
259
360
  case 6:
260
- res_col = _context5.v;
261
- return _context5.a(2, {
361
+ _context8.n = 7;
362
+ return this.updateDB(_col, col);
363
+ case 7:
364
+ res_col = _context8.v;
365
+ return _context8.a(2, {
262
366
  update: update,
263
367
  doc: res_doc,
264
368
  col: res_col,
265
369
  tree: _col.tree
266
370
  });
267
371
  }
268
- }, _callee5, this);
372
+ }, _callee8, this);
269
373
  }));
270
- function insert(_x4, _x5, _x6) {
374
+ function insert(_x7, _x8, _x9) {
271
375
  return _insert.apply(this, arguments);
272
376
  }
273
377
  return insert;
@@ -275,21 +379,21 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
275
379
  }, {
276
380
  key: "updateDB",
277
381
  value: function () {
278
- var _updateDB = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(_col, col) {
382
+ var _updateDB = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(_col, col) {
279
383
  var root, colD;
280
- return _regenerator().w(function (_context6) {
281
- while (1) switch (_context6.n) {
384
+ return _regenerator().w(function (_context9) {
385
+ while (1) switch (_context9.n) {
282
386
  case 0:
283
387
  root = _col.tree.F.toObject(_col.tree.root).toString();
284
388
  colD = col;
285
- _context6.n = 1;
389
+ _context9.n = 1;
286
390
  return this.tree.update(colD, [root]);
287
391
  case 1:
288
- return _context6.a(2, _context6.v);
392
+ return _context9.a(2, _context9.v);
289
393
  }
290
- }, _callee6, this);
394
+ }, _callee9, this);
291
395
  }));
292
- function updateDB(_x7, _x8) {
396
+ function updateDB(_x0, _x1) {
293
397
  return _updateDB.apply(this, arguments);
294
398
  }
295
399
  return updateDB;
@@ -297,29 +401,32 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
297
401
  }, {
298
402
  key: "update",
299
403
  value: function () {
300
- var _update = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(col, _key, _val) {
404
+ var _update = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0(col, _key, _val) {
301
405
  var _col, res_doc, res_col;
302
- return _regenerator().w(function (_context7) {
303
- while (1) switch (_context7.n) {
406
+ return _regenerator().w(function (_context0) {
407
+ while (1) switch (_context0.n) {
304
408
  case 0:
305
- _col = this.getColTree(col);
306
- _context7.n = 1;
307
- return _col.update(_key, _val);
409
+ _context0.n = 1;
410
+ return this.getColTree(col);
308
411
  case 1:
309
- res_doc = _context7.v;
310
- _context7.n = 2;
311
- return this.updateDB(_col, col);
412
+ _col = _context0.v;
413
+ _context0.n = 2;
414
+ return _col.update(_key, _val);
312
415
  case 2:
313
- res_col = _context7.v;
314
- return _context7.a(2, {
416
+ res_doc = _context0.v;
417
+ _context0.n = 3;
418
+ return this.updateDB(_col, col);
419
+ case 3:
420
+ res_col = _context0.v;
421
+ return _context0.a(2, {
315
422
  doc: res_doc,
316
423
  col: res_col,
317
424
  tree: _col.tree
318
425
  });
319
426
  }
320
- }, _callee7, this);
427
+ }, _callee0, this);
321
428
  }));
322
- function update(_x9, _x0, _x1) {
429
+ function update(_x10, _x11, _x12) {
323
430
  return _update.apply(this, arguments);
324
431
  }
325
432
  return update;
@@ -327,29 +434,32 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
327
434
  }, {
328
435
  key: "delete",
329
436
  value: function () {
330
- var _delete2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(col, _key) {
437
+ var _delete2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1(col, _key) {
331
438
  var _col, res_doc, res_col;
332
- return _regenerator().w(function (_context8) {
333
- while (1) switch (_context8.n) {
439
+ return _regenerator().w(function (_context1) {
440
+ while (1) switch (_context1.n) {
334
441
  case 0:
335
- _col = this.getColTree(col);
336
- _context8.n = 1;
337
- return _col["delete"](_key);
442
+ _context1.n = 1;
443
+ return this.getColTree(col);
338
444
  case 1:
339
- res_doc = _context8.v;
340
- _context8.n = 2;
341
- return this.updateDB(_col, col);
445
+ _col = _context1.v;
446
+ _context1.n = 2;
447
+ return _col["delete"](_key);
342
448
  case 2:
343
- res_col = _context8.v;
344
- return _context8.a(2, {
449
+ res_doc = _context1.v;
450
+ _context1.n = 3;
451
+ return this.updateDB(_col, col);
452
+ case 3:
453
+ res_col = _context1.v;
454
+ return _context1.a(2, {
345
455
  doc: res_doc,
346
456
  col: res_col,
347
457
  tree: _col.tree
348
458
  });
349
459
  }
350
- }, _callee8, this);
460
+ }, _callee1, this);
351
461
  }));
352
- function _delete(_x10, _x11) {
462
+ function _delete(_x13, _x14) {
353
463
  return _delete2.apply(this, arguments);
354
464
  }
355
465
  return _delete;
@@ -357,20 +467,23 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
357
467
  }, {
358
468
  key: "get",
359
469
  value: function () {
360
- var _get = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(col, _key) {
470
+ var _get = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10(col, _key) {
361
471
  var _col;
362
- return _regenerator().w(function (_context9) {
363
- while (1) switch (_context9.n) {
472
+ return _regenerator().w(function (_context10) {
473
+ while (1) switch (_context10.n) {
364
474
  case 0:
365
- _col = this.getColTree(col);
366
- _context9.n = 1;
367
- return _col.get(_key);
475
+ _context10.n = 1;
476
+ return this.getColTree(col);
368
477
  case 1:
369
- return _context9.a(2, _context9.v);
478
+ _col = _context10.v;
479
+ _context10.n = 2;
480
+ return _col.get(_key);
481
+ case 2:
482
+ return _context10.a(2, _context10.v);
370
483
  }
371
- }, _callee9, this);
484
+ }, _callee10, this);
372
485
  }));
373
- function get(_x12, _x13) {
486
+ function get(_x15, _x16) {
374
487
  return _get.apply(this, arguments);
375
488
  }
376
489
  return get;
@@ -378,18 +491,18 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
378
491
  }, {
379
492
  key: "getCol",
380
493
  value: function () {
381
- var _getCol = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0(col) {
382
- return _regenerator().w(function (_context0) {
383
- while (1) switch (_context0.n) {
494
+ var _getCol = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11(col) {
495
+ return _regenerator().w(function (_context11) {
496
+ while (1) switch (_context11.n) {
384
497
  case 0:
385
- _context0.n = 1;
498
+ _context11.n = 1;
386
499
  return this.tree.find(col);
387
500
  case 1:
388
- return _context0.a(2, _context0.v);
501
+ return _context11.a(2, _context11.v);
389
502
  }
390
- }, _callee0, this);
503
+ }, _callee11, this);
391
504
  }));
392
- function getCol(_x14) {
505
+ function getCol(_x17) {
393
506
  return _getCol.apply(this, arguments);
394
507
  }
395
508
  return getCol;
@@ -30,8 +30,11 @@ function _newMemEmptyTrie() {
30
30
  F = _yield$getHashes.F;
31
31
  db = new _SMTMemDb["default"](F, kv);
32
32
  _context.n = 2;
33
- return db.getRoot();
33
+ return db.init();
34
34
  case 2:
35
+ _context.n = 3;
36
+ return db.getRoot();
37
+ case 3:
35
38
  rt = _context.v;
36
39
  smt = new _SMT["default"](db, rt, hash0, hash1, F);
37
40
  return _context.a(2, smt);
@@ -3,14 +3,15 @@ export default class SMTMemDb {
3
3
  this.kv = kv
4
4
  this.nodes = {}
5
5
  this.root = F.zero
6
+ this.F = F
7
+ }
8
+ async init() {
6
9
  if (this.kv) {
7
10
  const root = this.kv.get("root")
8
11
  if (root) this.root = root
9
- else this.kv.put("root", this.root)
12
+ else await this.kv.put("root", this.root)
10
13
  }
11
- this.F = F
12
14
  }
13
-
14
15
  async getRoot() {
15
16
  return this.root
16
17
  }
@@ -47,7 +48,7 @@ export default class SMTMemDb {
47
48
 
48
49
  async setRoot(rt) {
49
50
  this.root = rt
50
- if (this.kv) this.kv.put("root", this.root)
51
+ if (this.kv) await this.kv.put("root", this.root)
51
52
  }
52
53
 
53
54
  async multiIns(inserts) {
@@ -55,7 +56,7 @@ export default class SMTMemDb {
55
56
  const keyS = this._key2str(inserts[i][0])
56
57
  this._normalize(inserts[i][1])
57
58
  this.nodes[keyS] = inserts[i][1]
58
- if (this.kv) this.kv.put(`node_${keyS}`, this.nodes[keyS])
59
+ if (this.kv) await this.kv.put(`node_${keyS}`, this.nodes[keyS])
59
60
  }
60
61
  }
61
62
 
@@ -63,7 +64,7 @@ export default class SMTMemDb {
63
64
  for (let i = 0; i < dels.length; i++) {
64
65
  const keyS = this._key2str(dels[i])
65
66
  delete this.nodes[keyS]
66
- if (this.kv) this.kv.del(`node_${keyS}`)
67
+ if (this.kv) await this.kv.del(`node_${keyS}`)
67
68
  }
68
69
  }
69
70
  }
package/esm/db.js CHANGED
@@ -4,6 +4,14 @@ import { is, indexOf, range, isNil } from "ramda"
4
4
  import { pad, toSignal, encode, toIndex } from "./encoder.js"
5
5
  import Collection from "./collection.js"
6
6
 
7
+ const to64 = hash => {
8
+ const n = BigInt(hash)
9
+ let hex = n.toString(16)
10
+ if (hex.length % 2) hex = "0" + hex
11
+ const buf = Buffer.from(hex, "hex")
12
+ return buf.toString("base64")
13
+ }
14
+
7
15
  export default class DB {
8
16
  constructor({
9
17
  size_val = 8,
@@ -279,6 +287,12 @@ export default class DB {
279
287
  col_root,
280
288
  }
281
289
  }
290
+ hash(format) {
291
+ const _hash = this.tree.F.toObject(this.tree.root)
292
+ if (format === "base64") return to64(_hash.toString())
293
+ return _hash.toString()
294
+ }
295
+
282
296
  getID(num) {
283
297
  if (!isNil(num)) {
284
298
  if (indexOf(num)(this.ids) !== -1) {
package/esm/db_tree.js CHANGED
@@ -2,6 +2,20 @@ import newMemEmptyTrie from "./newMemEmptyTrie.js"
2
2
  import { is, indexOf, isNil } from "ramda"
3
3
  import Collection from "./collection_tree.js"
4
4
 
5
+ const to64 = hash => {
6
+ const n = BigInt(hash)
7
+ let hex = n.toString(16)
8
+ if (hex.length % 2) hex = "0" + hex
9
+ const buf = Buffer.from(hex, "hex")
10
+ return buf.toString("base64")
11
+ }
12
+ const from64 = b64 => {
13
+ const buf = Buffer.from(b64, "base64")
14
+ const hex = buf.toString("hex")
15
+ const n = BigInt("0x" + hex)
16
+ return n.toString()
17
+ }
18
+
5
19
  export default class DBTree {
6
20
  constructor({
7
21
  size_val = 256,
@@ -30,6 +44,11 @@ export default class DBTree {
30
44
  if (!isNil(count)) this.count = count
31
45
  }
32
46
  }
47
+ hash(format) {
48
+ const _hash = this.tree.F.toObject(this.tree.root)
49
+ if (format === "base64") return to64(_hash.toString())
50
+ return _hash.toString()
51
+ }
33
52
  async exists(num) {
34
53
  const ex = this.ids[num] || (await this.tree.find(num)).found
35
54
  if (ex) this.ids[num] = true
@@ -57,31 +76,41 @@ export default class DBTree {
57
76
  } else {
58
77
  id = await this.getID(num)
59
78
  }
60
- const _col = new Collection({
61
- size_val: this.size_val,
62
- size_path: this.size_path,
63
- level: this.level,
64
- size_json: this.size_json,
65
- kv: this.kv?.(`dir_${id}`),
66
- })
67
- await _col.init()
68
- this.cols[id] = _col
69
- this.ids[id] = true
79
+ const _col = await this.loadCol(id)
70
80
  const root = _col.tree.F.toObject(_col.tree.root).toString()
71
81
  await this.tree.insert(id, [root])
72
82
  if (id === this.count) {
73
83
  this.count++
74
- if (this.kvi) this.kvi.put("count", this.count)
84
+ if (this.kvi) await this.kvi.put("count", this.count)
75
85
  }
76
86
  return id
77
87
  }
78
- getColTree(col) {
88
+ async getColTree(col) {
79
89
  const _col = this.cols[col]
80
- if (!_col) throw Error("collection doesn't exist")
81
- return _col
90
+ if (!_col) {
91
+ return await this.loadCollection(col)
92
+ } else return _col
93
+ }
94
+ async loadCol(id) {
95
+ const col = new Collection({
96
+ size_val: this.size_val,
97
+ size_path: this.size_path,
98
+ level: this.level,
99
+ size_json: this.size_json,
100
+ kv: this.kv?.(`dir_${id}`),
101
+ })
102
+ await col.init()
103
+ this.cols[id] = col
104
+ this.ids[id] = true
105
+ return col
106
+ }
107
+ async loadCollection(id) {
108
+ const hit = await this.tree.find(id)
109
+ if (!hit || !hit.found) throw Error("collection doesn't exist")
110
+ return await this.loadCol(id)
82
111
  }
83
112
  async insert(col, _key, _val) {
84
- const _col = this.getColTree(col)
113
+ const _col = await this.getColTree(col)
85
114
  let update = false
86
115
  let res_doc
87
116
  if ((await _col.get(_key)).found) {
@@ -99,21 +128,21 @@ export default class DBTree {
99
128
  return await this.tree.update(colD, [root])
100
129
  }
101
130
  async update(col, _key, _val) {
102
- const _col = this.getColTree(col)
131
+ const _col = await this.getColTree(col)
103
132
  const res_doc = await _col.update(_key, _val)
104
133
  const res_col = await this.updateDB(_col, col)
105
134
  return { doc: res_doc, col: res_col, tree: _col.tree }
106
135
  }
107
136
 
108
137
  async delete(col, _key) {
109
- const _col = this.getColTree(col)
138
+ const _col = await this.getColTree(col)
110
139
  const res_doc = await _col.delete(_key)
111
140
  const res_col = await this.updateDB(_col, col)
112
141
  return { doc: res_doc, col: res_col, tree: _col.tree }
113
142
  }
114
143
 
115
144
  async get(col, _key) {
116
- const _col = this.getColTree(col)
145
+ const _col = await this.getColTree(col)
117
146
  return await _col.get(_key)
118
147
  }
119
148
  async getCol(col) {
@@ -5,6 +5,7 @@ import getHashes from "./circomlibjs/getHashes.js"
5
5
  export default async function newMemEmptyTrie(kv) {
6
6
  const { hash0, hash1, F } = await getHashes()
7
7
  const db = new SMTMemDb(F, kv)
8
+ await db.init()
8
9
  const rt = await db.getRoot()
9
10
  const smt = new SMT(db, rt, hash0, hash1, F)
10
11
  return smt
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zkjson",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "description": "Zero Knowledge Provable JSON",
5
5
  "exports": {
6
6
  ".": {