@based/db 0.0.49 → 0.0.50

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.
Binary file
Binary file
@@ -1,7 +1,7 @@
1
1
  import { REFERENCES } from '@based/schema/def';
2
2
  import { ModifyError, ModifyState } from '../ModifyRes.js';
3
3
  import { setCursor } from '../setCursor.js';
4
- import { DELETE, EDGE_INDEX_REALID, EDGE_INDEX_TMPID, EDGE_NOINDEX_REALID, EDGE_NOINDEX_TMPID, NOEDGE_INDEX_REALID, NOEDGE_INDEX_TMPID, NOEDGE_NOINDEX_REALID, NOEDGE_NOINDEX_TMPID, RANGE_ERR, REF_OP_UPDATE, } from '../types.js';
4
+ import { DELETE, EDGE_INDEX_REALID, EDGE_INDEX_TMPID, EDGE_NOINDEX_REALID, EDGE_NOINDEX_TMPID, NOEDGE_INDEX_REALID, NOEDGE_INDEX_TMPID, NOEDGE_NOINDEX_REALID, NOEDGE_NOINDEX_TMPID, RANGE_ERR, REF_OP_OVERWRITE, REF_OP_PUT_ADD, REF_OP_PUT_OVERWRITE, REF_OP_UPDATE, } from '../types.js';
5
5
  import { writeEdges } from './edge.js';
6
6
  export function writeReferences(value, ctx, schema, def, res, mod) {
7
7
  if (typeof value !== 'object') {
@@ -266,8 +266,8 @@ function putRefs(def, ctx, modifyOp, refs, op) {
266
266
  ctx.buf[ctx.len++] = size >>>= 8;
267
267
  ctx.buf[ctx.len++] = size >>>= 8;
268
268
  ctx.buf[ctx.len++] = size >>>= 8;
269
- ctx.buf[ctx.len++] = op === 0 ? 3 : 4;
270
- ctx.len = (ctx.len + 3) & ~3;
269
+ ctx.buf[ctx.len++] =
270
+ op === REF_OP_OVERWRITE ? REF_OP_PUT_OVERWRITE : REF_OP_PUT_ADD;
271
271
  let i = 0;
272
272
  for (; i < refs.length; i++) {
273
273
  let ref = refs[i];
@@ -37,4 +37,6 @@ export declare const EDGE_INDEX_TMPID = 6;
37
37
  export declare const NOEDGE_INDEX_TMPID = 7;
38
38
  export declare const REF_OP_UPDATE = 1;
39
39
  export declare const REF_OP_OVERWRITE = 0;
40
+ export declare const REF_OP_PUT_OVERWRITE = 3;
41
+ export declare const REF_OP_PUT_ADD = 4;
40
42
  export type REF_OP = typeof REF_OP_OVERWRITE | typeof REF_OP_UPDATE;
@@ -25,4 +25,6 @@ export const EDGE_INDEX_TMPID = 6;
25
25
  export const NOEDGE_INDEX_TMPID = 7;
26
26
  export const REF_OP_UPDATE = 1;
27
27
  export const REF_OP_OVERWRITE = 0;
28
+ export const REF_OP_PUT_OVERWRITE = 3;
29
+ export const REF_OP_PUT_ADD = 4;
28
30
  //# sourceMappingURL=types.js.map
@@ -12,6 +12,8 @@ import { convertFilter } from './filter/convertFilter.js';
12
12
  import { validateLocale, validateRange } from './validation.js';
13
13
  import { DEF_RANGE_PROP_LIMIT } from './thresholds.js';
14
14
  import { concatUint8Arr } from '@saulx/utils';
15
+ import { displayTarget } from './display.js';
16
+ import picocolors from 'picocolors';
15
17
  export class QueryBranch {
16
18
  db;
17
19
  def;
@@ -357,7 +359,19 @@ export class BasedDbQuery extends QueryBranch {
357
359
  return this;
358
360
  }
359
361
  subscribe(onData, onError) {
360
- return subscribe(this, onData, onError ??
362
+ return subscribe(this, (res) => {
363
+ try {
364
+ onData(res);
365
+ }
366
+ catch (err) {
367
+ // const t = displayTarget(this.def)
368
+ const def = this.def;
369
+ let name = picocolors.red(`QueryError[${displayTarget(def)}]\n`);
370
+ name += ` Error executing onData handler in subscription\n`;
371
+ name += ` ${err.message}\n`;
372
+ console.error(name);
373
+ }
374
+ }, onError ??
361
375
  ((err) => {
362
376
  console.error(err);
363
377
  }));
@@ -2,13 +2,12 @@ import { DbWorker, SCHEMA_FILE, WRITELOG_FILE } from './index.js';
2
2
  import native from '../native.js';
3
3
  import { rm, mkdir, readFile } from 'node:fs/promises';
4
4
  import { join } from 'node:path';
5
- import { hashEq } from './csmt/index.js';
6
- import { foreachBlock, initCsmt, makeCsmtKey } from './tree.js';
5
+ import { destructureCsmtKey, foreachBlock, initCsmt, makeCsmtKey, specialBlock } from './tree.js';
7
6
  import { availableParallelism } from 'node:os';
8
7
  import exitHook from 'exit-hook';
9
8
  import { save } from './save.js';
10
9
  import { DEFAULT_BLOCK_CAPACITY } from '@based/schema/def';
11
- import { bufToHex, hexToBuf } from '@saulx/utils';
10
+ import { bufToHex } from '@saulx/utils';
12
11
  export async function start(db, opts) {
13
12
  const path = db.fileSystemPath;
14
13
  const noop = () => { };
@@ -74,10 +73,27 @@ export async function start(db, opts) {
74
73
  }, true);
75
74
  }
76
75
  if (writelog?.hash) {
77
- const oldHash = hexToBuf(writelog.hash);
78
- const newHash = db.merkleTree.getRoot()?.hash;
79
- if (!hashEq(oldHash, newHash)) {
80
- console.error(`WARN: CSMT hash mismatch. expected: ${writelog.hash} actual: ${bufToHex(newHash)}`);
76
+ // FIXME FDN-1301
77
+ //const oldHash = hexToBuf(writelog.hash)
78
+ //const newHash = db.merkleTree.getRoot()?.hash
79
+ //if (!hashEq(oldHash, newHash)) {
80
+ // console.error(
81
+ // `WARN: CSMT hash mismatch. expected: ${writelog.hash} actual: ${bufToHex(newHash)}`,
82
+ // )
83
+ //}
84
+ const oldHashSet = new Set();
85
+ const newHashSet = new Set();
86
+ for (let k in writelog.rangeDumps)
87
+ writelog.rangeDumps[k].forEach(({ hash }) => oldHashSet.add(hash));
88
+ db.merkleTree.visitLeafNodes(({ key, hash }) => {
89
+ const [_typeId, start] = destructureCsmtKey(key);
90
+ if (start == specialBlock)
91
+ return; // skip the type specialBlock
92
+ newHashSet.add(bufToHex(hash));
93
+ });
94
+ const setEq = (a, b) => a.size === b.size && [...a].every(value => b.has(value));
95
+ if (!setEq(oldHashSet, newHashSet)) {
96
+ console.error(`WARN: CSMT hash mismatch.`);
81
97
  }
82
98
  }
83
99
  // start workers
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.49",
3
+ "version": "0.0.50",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",