datafolder 1.0.49 → 1.0.51

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.
Files changed (59) hide show
  1. package/ChangeLog.md +4 -0
  2. package/package.json +1 -1
  3. package/scripts/convertBinFiles.js +4 -2
  4. package/src/QyAcl.d.ts +2 -3
  5. package/src/QyAcl.js +2 -3
  6. package/src/QyAclWriter.d.ts +2 -1
  7. package/src/QyAclWriter.js +20 -9
  8. package/src/QyBatch.d.ts +1 -1
  9. package/src/QyBatch.js +49 -50
  10. package/src/QyBinWriter.d.ts +7 -4
  11. package/src/QyBinWriter.js +77 -62
  12. package/src/QyCD.js +69 -65
  13. package/src/QyCache.js +1 -1
  14. package/src/QyCompressor.js +16 -15
  15. package/src/QyDB.d.ts +3 -3
  16. package/src/QyDir.d.ts +2 -1
  17. package/src/QyDir.js +4 -1
  18. package/src/QyEmbeddingWriter.d.ts +4 -1
  19. package/src/QyEmbeddingWriter.js +6 -6
  20. package/src/QyExpireMonitor.js +3 -3
  21. package/src/QyExpireProcessor_Worker.d.ts +1 -1
  22. package/src/QyExpireProcessor_Worker.js +35 -38
  23. package/src/QyFile.js +9 -9
  24. package/src/QyFileContentUtils.d.ts +1 -0
  25. package/src/QyFileContentUtils.js +78 -65
  26. package/src/QyFileLogger.d.ts +1 -1
  27. package/src/QyFileLogger.js +2 -2
  28. package/src/QyKVData.d.ts +2 -2
  29. package/src/QyKVData.js +42 -37
  30. package/src/QyKVDataLoader.d.ts +1 -1
  31. package/src/QyKVDataLoader.js +13 -13
  32. package/src/QyKVDataSaver_Worker.d.ts +2 -2
  33. package/src/QyLogFileSaver_Worker.d.ts +1 -3
  34. package/src/QyLogFileSaver_Worker.js +7 -13
  35. package/src/QyMessager.d.ts +3 -3
  36. package/src/QyMessager.js +26 -29
  37. package/src/QyOpRunner.d.ts +4 -2
  38. package/src/QyOpRunner.js +32 -22
  39. package/src/QySnapshotCompactor_Worker.d.ts +6 -4
  40. package/src/QySnapshotCompactor_Worker.js +88 -66
  41. package/src/QySnapshotMapSaver.d.ts +4 -0
  42. package/src/QySnapshotMapSaver.js +27 -0
  43. package/src/QySnapshotMapSaver_Worker.d.ts +12 -0
  44. package/src/QySnapshotMapSaver_Worker.js +69 -0
  45. package/src/QySnapshotSaver_Worker.d.ts +4 -2
  46. package/src/QySnapshotSaver_Worker.js +57 -44
  47. package/src/QySnapshots.d.ts +17 -5
  48. package/src/QySnapshots.js +312 -201
  49. package/src/QyStructure.js +25 -24
  50. package/src/QyUtils.d.ts +0 -1
  51. package/src/QyUtils.js +42 -49
  52. package/src/QyWorker.js +19 -18
  53. package/src/hackernews.js +49 -49
  54. package/src/rpc/getFileContent.js +1 -1
  55. package/src/rpc/getSubItems.js +1 -1
  56. package/src/rpc/reloadRpc.js +1 -1
  57. package/src/simulate.js +38 -38
  58. package/src/simulate2.js +5 -5
  59. package/src/soc_pokec.js +7 -7
package/ChangeLog.md CHANGED
@@ -1,3 +1,7 @@
1
+ 1.0.51 Improve buffer allocation performance.
2
+
3
+ 1.0.50 Improve binary file saving performance.
4
+
1
5
  1.0.49 Fastify update.
2
6
 
3
7
  1.0.48 Async runner performance improvement.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datafolder",
3
- "version": "1.0.49",
3
+ "version": "1.0.51",
4
4
  "author": {
5
5
  "name": "lysh",
6
6
  "email": "datafolderdev@outlook.com"
@@ -8,7 +8,7 @@ run();
8
8
  async function loadPosMapFile(posMapFilePath) {
9
9
  const buffer = await readFile(posMapFilePath);
10
10
  const posMap = {};
11
- iterPMBuffer(buffer, (key, nums) => (posMap[key] = nums));
11
+ iterPMBuffer(buffer, (key, start, length) => (posMap[key] = [start, length]));
12
12
  return posMap;
13
13
  }
14
14
  async function run() {
@@ -39,7 +39,9 @@ async function convertFolder(folder, destFolder) {
39
39
  continue;
40
40
  await ensureDir(destFolder);
41
41
  console.log(`Converting ${filePath} -> ${destFilePath}`);
42
- const map = await (match[1] === "pM" ? loadPosMapFile(filePath) : loadPrefixPMFile(filePath));
42
+ const map = match[1] === "pM"
43
+ ? await loadPosMapFile(filePath)
44
+ : Object.fromEntries((await loadPrefixPMFile(filePath)).entries());
43
45
  promises.push(writeFile(destFilePath, JSON.stringify(map, null, 2)));
44
46
  }
45
47
  }
package/src/QyAcl.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare function applyAclCmdObj(aclKeyValueMap: any, cmdArgAsListArray: any): void;
1
+ export declare function applyAclCmdObj(keyValueMap: any, cmdArgAsListArray: any): void;
2
2
  export declare function iterCmdKeys(cmdArgAsListArray: any, fun: any): void;
3
3
  export declare function applyChange(oldContent: any, newValue: any): any;
4
4
  export declare class QyAclCmdGenerator {
@@ -16,8 +16,7 @@ export declare class QyAclCmdGenerator {
16
16
  pushCmdArgAsListArray(cmdArgAsListArray: any): this;
17
17
  takeCmdArgAsListArray(): any[];
18
18
  clear(): void;
19
- toAclBuffer(changeId: any): Buffer<ArrayBuffer> | undefined;
19
+ toAclBuffer(changeId: any): NonSharedBuffer[] | undefined;
20
20
  }
21
21
  export declare function numsToBuffer(nums: any): Buffer<ArrayBuffer>;
22
- export declare function bufferToNums(buffer: any, offset: any, nums: any): any;
23
22
  export declare function readAclBuffer(aclFilePath: any, contentBuffer: any, autoRepairAclFile: any, currentChangeId: any): Promise<any[]>;
package/src/QyAcl.js CHANGED
@@ -21,7 +21,7 @@ function r(t, r) {
21
21
  if (void 0 !== a) for (var i of a.values()) t.has(i) && t.set(i, void 0);
22
22
  if (void 0 !== e) for (var [ s, n ] of e) {
23
23
  let r = t.get(s);
24
- void 0 === r && t.set(s, r = {});
24
+ void 0 === r && t.set(s, r = new Object());
25
25
  for (var [ o, f ] of n) 1 === f ? r[o] = 1 : delete r[o];
26
26
  c(r) && t.set(s, void 0);
27
27
  }
@@ -116,7 +116,7 @@ class e {
116
116
  }
117
117
  toAclBuffer(r) {
118
118
  var t, e = this.batchSize;
119
- if (0 < e) return t = a(this.takeCmdArgAsListArray()), this.clear(), Buffer.concat([ i([ r, e, t.length ]), t ]);
119
+ if (0 < e) return t = a(this.takeCmdArgAsListArray()), this.clear(), [ i([ r, e, t.length ]), t ];
120
120
  }
121
121
  }
122
122
 
@@ -207,6 +207,5 @@ export {
207
207
  m as applyChange,
208
208
  e as QyAclCmdGenerator,
209
209
  i as numsToBuffer,
210
- g as bufferToNums,
211
210
  f as readAclBuffer
212
211
  };
@@ -3,5 +3,6 @@ import { QyAclCmdGenerator } from "./QyAcl.ts";
3
3
  export declare class QyAclWriter extends QyBinWriter {
4
4
  qyAclCmdGenerator: QyAclCmdGenerator;
5
5
  constructor(options: any, filePath?: any);
6
- getBinData(dataQueue: any, changeId: any): Buffer<ArrayBuffer> | undefined;
6
+ save(changeId: any, data: any, needPromise?: any): any;
7
+ _op_save(changeId: any, dataQueue: any): Promise<void> | undefined;
7
8
  }
@@ -3,25 +3,36 @@ import {
3
3
  } from "./QyBinWriter.js";
4
4
 
5
5
  import {
6
- QyAclCmdGenerator as e
6
+ QyAclCmdGenerator as t
7
7
  } from "./QyAcl.js";
8
8
 
9
9
  import {
10
- getDefaultOptions as o
10
+ getDefaultOptions as e
11
11
  } from "./QyDefaultOptions.js";
12
12
 
13
+ import {
14
+ ArgList_Idx as o,
15
+ Promise_Idx as a
16
+ } from "./QyOpRunner.js";
17
+
13
18
  class QyAclWriter extends r {
14
19
  qyAclCmdGenerator;
15
- constructor(r, t = void 0) {
20
+ constructor(r, s = void 0) {
16
21
  super({
17
- ...o("QyAclWriter"),
22
+ ...e("QyAclWriter"),
18
23
  ...r
19
- }, t), this.qyAclCmdGenerator = new e();
24
+ }, s), this.qyAclCmdGenerator = new t();
25
+ }
26
+ save(r, s, t = !1) {
27
+ var e, i = this._getLastSaveOp(t);
28
+ return void 0 === i ? (e = [ this._op_save, [ r, [ s ] ] ], t ? this.pushAndRunWithPromise(e) : this.pushAndRun(e)) : ((e = i[o])[0] = r,
29
+ e[1].push(s), t ? i[a] : void 0);
20
30
  }
21
- getBinData(r, t) {
22
- var e, o = this.qyAclCmdGenerator;
23
- for (e of r) o.pushCmdArgAsListArray(e);
24
- return o.toAclBuffer(t);
31
+ _op_save(r, s) {
32
+ var t, e = this.qyAclCmdGenerator;
33
+ for (t of s) e.pushCmdArgAsListArray(t);
34
+ s = e.toAclBuffer(r);
35
+ if (void 0 !== s) return this._saveBinData(s);
25
36
  }
26
37
  }
27
38
 
package/src/QyBatch.d.ts CHANGED
@@ -50,7 +50,7 @@ export declare class QyBatch extends QyCD {
50
50
  _action_createIndexByQuery(indexHostDir: any, queryObj: any): void;
51
51
  _action_delIndex(indexHostDir: any, indexPropPath: any): void;
52
52
  _action_delFile(file: QyFile, deletingParentDir?: boolean): boolean;
53
- _action_delDir(dir: any): boolean;
53
+ _action_delDir(dir: QyDir): boolean;
54
54
  _action_delContent(file: QyFile): boolean;
55
55
  _action_insertEmbedding(srcFile: QyFile, embedding: any): boolean;
56
56
  _action_delEmbedding(srcFile: any): true | undefined;
package/src/QyBatch.js CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  pathSplit as n,
11
11
  ensurePropNamePathList as a,
12
12
  isString as o,
13
- getSuffix as C,
13
+ getSuffix as v,
14
14
  IndexPropPathsFileName as h,
15
15
  isSimpleType as s,
16
16
  isEmptyObj as _,
@@ -38,7 +38,7 @@ import {
38
38
  } from "./QyCD.js";
39
39
 
40
40
  import {
41
- logger as v
41
+ logger as C
42
42
  } from "./QyLogger.js";
43
43
 
44
44
  let m = {
@@ -198,7 +198,7 @@ class QyBatch extends t {
198
198
  syncList: s
199
199
  } = this;
200
200
  let d;
201
- var h = o.length, a = (0 < h && (l = C(h), 0 < a && (d = "" + new Date().toISOString() + `: ${h} change${l}:
201
+ var h = o.length, a = (0 < h && (l = v(h), 0 < a && (d = "" + new Date().toISOString() + `: ${h} change${l}:
202
202
  ${o.join("\n")}
203
203
  `), o.length = 0), r.takeCmdArgAsListArray()), h = (t => {
204
204
  var i = t.length;
@@ -220,8 +220,8 @@ ${o.join("\n")}
220
220
  0 < g.triggerNodes?.length && _.push(g, c[e + 1]);
221
221
  }
222
222
  for (let e = c.length = 0; e < _.length; e += 2) {
223
- var p, f = _[e], v = _[e + 1];
224
- for (p of f.triggerNodes) p.checkTrigger(f, v);
223
+ var p, f = _[e], C = _[e + 1];
224
+ for (p of f.triggerNodes) p.checkTrigger(f, C);
225
225
  }
226
226
  }
227
227
  return e ? l : n;
@@ -268,36 +268,38 @@ ${o.join("\n")}
268
268
  !0);
269
269
  }
270
270
  _action_delDir(e) {
271
- if (!e.created) return !1;
271
+ if (!e.created || e.isRootDir || e.isHiddenDir) return !1;
272
272
  let {
273
273
  parentDir: t,
274
- subdirMap: i,
275
- fileMap: r,
276
- subdirMapKey: n,
277
- fileMapKey: a
274
+ fileMap: i,
275
+ subdirMapKey: r,
276
+ fileMapKey: n,
277
+ subdirCount: a,
278
+ fileCount: o
278
279
  } = e;
279
- if (r) {
280
- L(this, e, a, "delFileMap");
281
- for (var o of e.fileList) this._action_delFile(o, !0);
280
+ if (0 < o) {
281
+ for (var s of i.values()) s.created && this._action_delFile(s, !0);
282
+ L(this, e, n, "delFileMap");
282
283
  }
283
- if (i) {
284
- L(this, e, n, "delSubdirMap");
285
- for (var s of e.allSubdirs()) {
284
+ if (0 < a) {
285
+ for (var d of e.allSubdirs()) {
286
286
  let {
287
- subdirMap: e,
288
- fileMap: t,
289
- subdirMapKey: i,
290
- fileMapKey: r
291
- } = s;
292
- if (e && L(this, s, i, "delSubdirMap"), t) {
293
- L(this, s, r, "delFileMap");
294
- for (var d of s.fileList) this._action_delFile(d, !0);
287
+ fileMap: e,
288
+ subdirMapKey: t,
289
+ fileMapKey: i,
290
+ subdirCount: r,
291
+ fileCount: n
292
+ } = d;
293
+ if (0 < r && L(this, d, t, "delSubdirMap"), 0 < n) {
294
+ for (var h of e.values()) h.created && this._action_delFile(h, !0);
295
+ L(this, d, i, "delFileMap");
295
296
  }
296
- s.created = !1, this._action_removeItemExpire(s);
297
+ d.created = !1, this._action_removeItemExpire(d);
297
298
  }
299
+ L(this, e, r, "delSubdirMap");
298
300
  }
299
- return void 0 !== t && (e.created = !1, P(this, t, t.subdirMapKey, e), this._action_removeItemExpire(e),
300
- !0);
301
+ return e.created = !1, P(this, t, t.subdirMapKey, e), this._action_removeItemExpire(e),
302
+ !0;
301
303
  }
302
304
  _action_delContent(e) {
303
305
  var t = e.fileContent;
@@ -341,16 +343,19 @@ ${o.join("\n")}
341
343
  F(t, r, a), q(t, "setProp", r, e, n), o;
342
344
  }
343
345
  _action_insert(e, t) {
344
- if (void 0 === t) return v.warn(`Saving undefined to ${e.fullPath}. Not making any change.`),
346
+ if (void 0 === t) return C.warn(`Saving undefined to ${e.fullPath}. Not making any change.`),
345
347
  !1;
346
348
  e.created || this._action_createFile(e);
347
- var i = e.fileContent, [ t, r ] = c(i, t);
348
- if (void 0 === r) return !1;
349
- e.fileContent = t;
350
- var n, a, o, [ s, d ] = r, h = e.fileContentKey;
351
- return void 0 === t ? L(this, e, h, "delContent", i) : (i = e, h = h, a = t,
352
- o = r, (n = this).qyAclCmdGenerator.pushMCmd(h, d), F(n, i, h), q(n, "insert", i, a, o)),
353
- this.fileDeltaList.push(e, r), O(this, e, s, d, t), !0;
349
+ var {
350
+ fileContent: i,
351
+ fileContentKey: r
352
+ } = e, [ i, t ] = c(i, t);
353
+ if (void 0 === t) return !1;
354
+ e.fileContent = i;
355
+ var n, a, o, s, [ d, h ] = t;
356
+ return a = e, r = r, o = i, s = t, (n = this).qyAclCmdGenerator.pushMCmd(r, h),
357
+ F(n, a, r), q(n, "insert", a, o, s), this.fileDeltaList.push(e, t), O(this, e, d, h, i),
358
+ !0;
354
359
  }
355
360
  _action_createFile(e) {
356
361
  var t;
@@ -368,20 +373,14 @@ ${o.join("\n")}
368
373
  return !0;
369
374
  }
370
375
  _action_expireItem(e, t) {
371
- var {
372
- parentDir: i,
373
- underHiddenDir: r
374
- } = e;
375
- return void 0 !== i && !r && void 0 !== (i = M(t, !0)) && (r = this.qyCache,
376
- t = r.getOrCreateInfoFile(e), this._action_setProp(t, T(e), i) !== i) && (r.monitorItemExpire(e, i),
376
+ var i, r;
377
+ return !e.isRootDir && !e.underHiddenDir && void 0 !== (t = M(t, !0)) && (i = this.qyCache,
378
+ r = i.getOrCreateInfoFile(e), this._action_setProp(r, T(e), t) !== t) && (i.monitorItemExpire(e, t),
377
379
  !0);
378
380
  }
379
381
  _action_removeItemExpire(e) {
380
- var {
381
- parentDir: t,
382
- underHiddenDir: i
383
- } = e;
384
- return void 0 !== t && !i && (t = this.qyCache, void 0 !== (i = t.getInfoFile(e))) && void 0 !== this._action_setProp(i, T(e), null) && (t.unmonitorItemExpire(e),
382
+ var t, i;
383
+ return !e.isRootDir && !e.underHiddenDir && (t = this.qyCache, void 0 !== (i = t.getInfoFile(e))) && void 0 !== this._action_setProp(i, T(e), null) && (t.unmonitorItemExpire(e),
385
384
  !0);
386
385
  }
387
386
  _action_expireContent(e, i, r) {
@@ -411,9 +410,9 @@ function y(e, t) {
411
410
  }
412
411
 
413
412
  function D(t, i, e, r, n) {
414
- var a, o = {};
413
+ var a, o = new Object();
415
414
  for (a of n) o[JSON.stringify(a.indexPropPath)] = 1;
416
- v.log(`Creating index ${n.map(e => e.indexPropPath)} for ` + i.fullPath), t._action_insert(r, o);
415
+ C.log(`Creating index ${n.map(e => e.indexPropPath)} for ` + i.fullPath), t._action_insert(r, o);
417
416
  {
418
417
  var s = t, d = i, h;
419
418
  let e;
@@ -483,7 +482,7 @@ function P(e, t, i, r) {
483
482
  }
484
483
 
485
484
  function L(e, t, i, r, n = void 0) {
486
- e.qyAclCmdGenerator.pushDCmd(i), F(e, t, i), void 0 !== n && q(e, r, t, n);
485
+ e.qyAclCmdGenerator.pushDCmd(i), F(e, t, i), q(e, r, t, n);
487
486
  }
488
487
 
489
488
  function O(e, t, i, r = void 0, n = void 0) {
@@ -525,7 +524,7 @@ function b(t, e, i, r, n, a, o = void 0) {
525
524
  }
526
525
  }
527
526
 
528
- function E(e, t = void 0, i = [], r = {}) {
527
+ function E(e, t = void 0, i = [], r = new Object()) {
529
528
  if (s(e)) {
530
529
  var n = t ? t(e) : e;
531
530
  void 0 !== n && (r[JSON.stringify(i)] = n);
@@ -1,16 +1,19 @@
1
+ import { type FileHandle } from "node:fs/promises";
1
2
  import { QyOpRunner } from "./QyOpRunner.ts";
2
3
  export declare class QyBinWriter extends QyOpRunner {
3
4
  filePath: any;
4
5
  totalSavedSize: number;
5
- fileHandle: any;
6
+ fileHandle: FileHandle | undefined;
6
7
  constructor(options?: any, filePath?: any);
7
- save(changeId: any, data?: any, needPromise?: any): any;
8
+ save(data: any, needPromise?: any): any;
9
+ savev(data: any, needPromise?: any): any;
8
10
  truncate(): any;
9
11
  switch(newFilePath: any): any;
10
- getBinData(dataQueue: any[], changeId?: undefined): any;
11
12
  _op_start(filePath?: any): Promise<this>;
12
13
  _op_stop(): Promise<void>;
13
- _op_save(changeId: any, dataQueue: any): Promise<void> | undefined;
14
+ _op_save(dataQueue: any): Promise<void> | undefined;
14
15
  _op_truncate(): Promise<void>;
15
16
  _op_switch(newFilePath: any): Promise<void>;
17
+ _saveBinData(binData: any[]): Promise<void>;
18
+ _getLastSaveOp(needPromise: any): any;
16
19
  }
@@ -4,28 +4,27 @@ import {
4
4
 
5
5
  import {
6
6
  ArgList_Idx as s,
7
- OpFunName_Idx as c,
8
- Promise_Idx as o,
7
+ OpFun_Idx as d,
8
+ Promise_Idx as r,
9
9
  QyOpRunner as t,
10
- setOpPromise as l
10
+ setOpPromise as e
11
11
  } from "./QyOpRunner.js";
12
12
 
13
13
  import {
14
- ensureParentDir as f,
15
- getSize as d,
16
- getSizeStr as u,
14
+ ensureParentDir as h,
15
+ getSize as l,
17
16
  sleep as v,
18
- toBuffer as _,
19
- folderOrFileExists as h,
20
- isString as e
17
+ toBuffer as o,
18
+ folderOrFileExists as p,
19
+ isString as i
21
20
  } from "./QyUtils.js";
22
21
 
23
22
  import {
24
- getDefaultOptions as i
23
+ getDefaultOptions as f
25
24
  } from "./QyDefaultOptions.js";
26
25
 
27
26
  import {
28
- logger as m
27
+ logger as u
29
28
  } from "./QyLogger.js";
30
29
 
31
30
  class QyBinWriter extends t {
@@ -33,32 +32,38 @@ class QyBinWriter extends t {
33
32
  totalSavedSize = 0;
34
33
  fileHandle;
35
34
  constructor(t = void 0, a = void 0) {
36
- e(t) && ([ a, t ] = [ t, void 0 ]), super({
37
- ...i("QyBinWriter"),
35
+ i(t) && ([ a, t ] = [ t, void 0 ]), super({
36
+ ...f("QyBinWriter"),
38
37
  ...t
39
38
  }), this.filePath = a;
40
39
  }
41
- save(t, a = void 0, e = !1) {
42
- void 0 === a && ([ a, t ] = [ t, 0 ]);
43
- var i, r = this.lastOp;
44
- return void 0 === r || "_op_save" !== r[c] ? (i = [ "_op_save", [ t, [ a ] ] ],
45
- e ? this.pushAndRunWithPromise(i) : this.pushAndRun(i)) : ((i = r[s])[0] = t,
46
- i[1].push(a), e ? (l(r), r[o]) : void 0);
40
+ save(t, a = !1) {
41
+ t = o(t);
42
+ var i, e = this._getLastSaveOp(a);
43
+ return void 0 === e ? (i = [ this._op_save, [ [ t ] ] ], a ? this.pushAndRunWithPromise(i) : this.pushAndRun(i)) : ([ i ] = e[s],
44
+ i.push(t), a ? e[r] : void 0);
45
+ }
46
+ savev(t, a = !1) {
47
+ var i, e = this._getLastSaveOp(a);
48
+ return void 0 === e ? (i = [ this._op_save, [ t ] ], a ? this.pushAndRunWithPromise(i) : this.pushAndRun(i)) : ([ i ] = e[s],
49
+ i.push(...t), a ? e[r] : void 0);
47
50
  }
48
51
  truncate() {
49
- var t = this.lastOp;
50
- return void 0 !== t && "_op_truncate" === t[c] ? t[o] : this.pushAndRunWithPromise([ "_op_truncate" ]);
52
+ var {
53
+ lastOp: t,
54
+ _op_truncate: a
55
+ } = this;
56
+ return void 0 !== t && t[d] === a ? t[r] : this.pushAndRunWithPromise([ a ]);
51
57
  }
52
58
  switch(t) {
53
- var a = this.lastOp;
54
- return void 0 !== a && "_op_switch" === a[c] ? (a[s][0] = t, a[o]) : this.pushAndRunWithPromise([ "_op_switch", [ t ] ]);
55
- }
56
- getBinData(t, a = 0) {
57
- var e = (t = t.flat()).length;
58
- return 0 === e ? "" : 1 === e ? t[0] : Buffer.isBuffer(t[0]) ? Buffer.concat(t) : Buffer.concat(t.map(t => Buffer.from(t)));
59
+ var {
60
+ lastOp: a,
61
+ _op_switch: i
62
+ } = this;
63
+ return void 0 !== a && a[d] === i ? (a[s][0] = t, a[r]) : this.pushAndRunWithPromise([ i, [ t ] ]);
59
64
  }
60
65
  async _op_start(t = void 0) {
61
- return await r(this, t = void 0 === t ? this.filePath : t), this;
66
+ return await c(this, t = void 0 === t ? this.filePath : t), this;
62
67
  }
63
68
  async _op_stop() {
64
69
  var {
@@ -68,30 +73,12 @@ class QyBinWriter extends t {
68
73
  if (t) try {
69
74
  await t.close(), this.fileHandle = void 0;
70
75
  } catch (t) {
71
- m.error(`Close ${a} failed:`, t);
76
+ u.error(`Close ${a} failed:`, t);
72
77
  }
73
78
  this.totalSavedSize = 0;
74
79
  }
75
- _op_save(t, a) {
76
- a = this.getBinData(a, t);
77
- if (a) return (async (t, a, e) => {
78
- let {
79
- fileHandle: i,
80
- totalSavedSize: r,
81
- filePath: s,
82
- options: o
83
- } = t, {
84
- maxRetryInterval: n,
85
- retryIntervalFactor: l
86
- } = o, f = _(e), d = f.length, h = `size ${u(d)} (MaxId:${a}) to ` + s, p = 0;
87
- for (;void 0 === t.firstOp || "_op_truncate" !== t.firstOp[c]; ) try {
88
- return await i.write(f, 0, f.byteLength, r), void (t.totalSavedSize = r + d);
89
- } catch (t) {
90
- m.error(`Saving ${h} failed:`, t, "Truncated back to " + r), await i.truncate(r),
91
- p = 0 === p ? 1 : Math.min(n, p * l), m.log(`Retry in ${p} seconds`),
92
- await v(p);
93
- }
94
- })(this, t, a);
80
+ _op_save(t) {
81
+ if (0 < t.length) return this._saveBinData(t);
95
82
  }
96
83
  async _op_truncate() {
97
84
  var {
@@ -99,36 +86,64 @@ class QyBinWriter extends t {
99
86
  filePath: a
100
87
  } = this;
101
88
  if (t) try {
102
- await t.truncate(0), m.info("Truncated " + a);
89
+ await t.truncate(0), u.info("Truncated " + a);
103
90
  } catch (t) {
104
- m.error(`Truncate ${a} failed:`, t);
91
+ u.error(`Truncate ${a} failed:`, t);
105
92
  }
106
93
  this.totalSavedSize = 0;
107
94
  }
108
95
  async _op_switch(t) {
109
96
  var {
110
97
  fileHandle: a,
111
- filePath: e
98
+ filePath: i
99
+ } = this;
100
+ void 0 !== a && i === t || (await c(this, t), u.info(`Switched from ${i} to ` + t));
101
+ }
102
+ async _saveBinData(t) {
103
+ var {
104
+ fileHandle: a,
105
+ totalSavedSize: i,
106
+ filePath: e,
107
+ options: s,
108
+ _op_truncate: r
109
+ } = this, {
110
+ maxRetryInterval: o,
111
+ retryIntervalFactor: n
112
+ } = s;
113
+ let h = 0;
114
+ for (;void 0 === this.firstOp || this.firstOp[d] !== r; ) try {
115
+ var l = (await a.writev(t, i)).bytesWritten;
116
+ return void (this.totalSavedSize += l);
117
+ } catch (t) {
118
+ u.error(`Saving to ${e} failed:`, t, "Truncated back to " + i), await a.truncate(i),
119
+ h = 0 === h ? 1 : Math.min(o, h * n), u.log(`Retry in ${h} seconds`),
120
+ await v(h);
121
+ }
122
+ }
123
+ _getLastSaveOp(t) {
124
+ var {
125
+ lastOp: a,
126
+ _op_save: i
112
127
  } = this;
113
- void 0 !== a && e === t || (await r(this, t), m.info(`Switched from ${e} to ` + t));
128
+ if (void 0 !== a && a[d] === i) return t && e(a), a;
114
129
  }
115
130
  }
116
131
 
117
- async function r(t, a) {
132
+ async function c(t, a) {
118
133
  var {
119
- fileHandle: e,
120
- options: i
134
+ fileHandle: i,
135
+ options: e
121
136
  } = t;
122
137
  t.totalSavedSize = 0;
123
138
  try {
124
- e && (e.close(), t.fileHandle = void 0), await f(a), t.filePath = a;
139
+ i && (i.close(), t.fileHandle = void 0), await h(a), t.filePath = a;
125
140
  var {
126
- append: r,
127
- read: s
128
- } = i, o = r && await h(a) ? "r+" : s ? "w+" : "w";
129
- t.fileHandle = await n(a, o), r && (t.totalSavedSize = await d(a));
141
+ append: s,
142
+ read: r
143
+ } = e, o = s && await p(a) ? "r+" : r ? "w+" : "w";
144
+ t.fileHandle = await n(a, o), s && (t.totalSavedSize = await l(a));
130
145
  } catch (t) {
131
- m.error(`Open ${a} failed:`, t);
146
+ u.error(`Open ${a} failed:`, t);
132
147
  }
133
148
  }
134
149