@based/db 0.0.64 → 0.0.67

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 (144) hide show
  1. package/README.md +2 -2
  2. package/dist/lib/darwin_aarch64/include/selva/colvec.h +71 -0
  3. package/dist/lib/darwin_aarch64/include/selva/db.h +33 -4
  4. package/dist/lib/darwin_aarch64/include/selva/fields.h +37 -25
  5. package/dist/lib/darwin_aarch64/include/selva/hll.h +5 -3
  6. package/dist/lib/darwin_aarch64/include/selva/membar.h +23 -0
  7. package/dist/lib/darwin_aarch64/include/selva/types.h +8 -1
  8. package/dist/lib/darwin_aarch64/include/selva/worker_ctx.h +19 -3
  9. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  10. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  11. package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
  12. package/dist/lib/darwin_aarch64/libnode-v21.node +0 -0
  13. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  14. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  15. package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
  16. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  17. package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
  18. package/dist/lib/linux_aarch64/include/selva/colvec.h +71 -0
  19. package/dist/lib/linux_aarch64/include/selva/db.h +33 -4
  20. package/dist/lib/linux_aarch64/include/selva/fields.h +37 -25
  21. package/dist/lib/linux_aarch64/include/selva/hll.h +5 -3
  22. package/dist/lib/linux_aarch64/include/selva/membar.h +23 -0
  23. package/dist/lib/linux_aarch64/include/selva/types.h +8 -1
  24. package/dist/lib/linux_aarch64/include/selva/worker_ctx.h +19 -3
  25. package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
  26. package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
  27. package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
  28. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  29. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  30. package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
  31. package/dist/lib/linux_aarch64/libselva.so +0 -0
  32. package/dist/lib/linux_x86_64/include/selva/colvec.h +71 -0
  33. package/dist/lib/linux_x86_64/include/selva/db.h +33 -4
  34. package/dist/lib/linux_x86_64/include/selva/fields.h +37 -25
  35. package/dist/lib/linux_x86_64/include/selva/hll.h +5 -3
  36. package/dist/lib/linux_x86_64/include/selva/membar.h +23 -0
  37. package/dist/lib/linux_x86_64/include/selva/types.h +8 -1
  38. package/dist/lib/linux_x86_64/include/selva/worker_ctx.h +19 -3
  39. package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
  40. package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
  41. package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
  42. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  43. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  44. package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
  45. package/dist/lib/linux_x86_64/libselva.so +0 -0
  46. package/dist/src/client/flushModify.d.ts +2 -1
  47. package/dist/src/client/flushModify.js +12 -4
  48. package/dist/src/client/modify/create.js +11 -0
  49. package/dist/src/client/modify/delete.js +3 -0
  50. package/dist/src/client/modify/fixed.js +1 -1
  51. package/dist/src/client/modify/modify.js +2 -2
  52. package/dist/src/client/modify/setCursor.d.ts +2 -1
  53. package/dist/src/client/query/BasedDbQuery.d.ts +10 -4
  54. package/dist/src/client/query/BasedDbQuery.js +114 -6
  55. package/dist/src/client/query/aggregates/aggregation.js +24 -11
  56. package/dist/src/client/query/aggregates/types.d.ts +22 -2
  57. package/dist/src/client/query/aggregates/types.js +34 -1
  58. package/dist/src/client/query/display.js +8 -2
  59. package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +2 -3
  60. package/dist/src/client/query/filter/createVariableFilterBuffer.js +20 -7
  61. package/dist/src/client/query/filter/filter.js +13 -3
  62. package/dist/src/client/query/filter/primitiveFilter.d.ts +1 -2
  63. package/dist/src/client/query/include/props.js +18 -2
  64. package/dist/src/client/query/include/toBuffer.js +11 -3
  65. package/dist/src/client/query/include/walk.js +5 -1
  66. package/dist/src/client/query/queryDef.js +4 -1
  67. package/dist/src/client/query/read/read.js +52 -22
  68. package/dist/src/client/query/registerQuery.js +1 -0
  69. package/dist/src/client/query/search/index.d.ts +1 -1
  70. package/dist/src/client/query/search/index.js +21 -7
  71. package/dist/src/client/query/sort.d.ts +1 -1
  72. package/dist/src/client/query/toByteCode/default.d.ts +1 -1
  73. package/dist/src/client/query/toByteCode/default.js +0 -2
  74. package/dist/src/client/query/toByteCode/toBuffer.js +0 -7
  75. package/dist/src/client/query/types.d.ts +16 -5
  76. package/dist/src/client/query/validation.d.ts +3 -0
  77. package/dist/src/client/query/validation.js +34 -2
  78. package/dist/src/client/xxHash64.d.ts +1 -1
  79. package/dist/src/index.d.ts +1 -2
  80. package/dist/src/index.js +0 -1
  81. package/dist/src/native.d.ts +7 -4
  82. package/dist/src/native.js +23 -13
  83. package/dist/src/server/IoWorker.d.ts +8 -0
  84. package/dist/src/server/IoWorker.js +39 -0
  85. package/dist/src/server/QueryWorker.d.ts +8 -0
  86. package/dist/src/server/QueryWorker.js +26 -0
  87. package/dist/src/server/blocks.d.ts +24 -0
  88. package/dist/src/server/blocks.js +112 -0
  89. package/dist/src/server/dbHash.d.ts +1 -1
  90. package/dist/src/server/index.d.ts +10 -16
  91. package/dist/src/server/index.js +39 -15
  92. package/dist/src/server/migrate/index.d.ts +5 -0
  93. package/dist/src/server/migrate/index.js +11 -7
  94. package/dist/src/server/migrate/worker.js +3 -0
  95. package/dist/src/server/save.d.ts +8 -6
  96. package/dist/src/server/save.js +34 -78
  97. package/dist/src/server/schema.js +6 -5
  98. package/dist/src/server/start.js +57 -60
  99. package/dist/src/server/tree.d.ts +24 -13
  100. package/dist/src/server/tree.js +95 -66
  101. package/dist/src/server/workers/DbWorker.d.ts +17 -0
  102. package/dist/src/server/{DbWorker.js → workers/DbWorker.js} +15 -17
  103. package/dist/src/server/workers/io_worker.js +39 -0
  104. package/dist/src/server/workers/io_worker_types.d.ts +12 -0
  105. package/dist/src/server/workers/io_worker_types.js +2 -0
  106. package/dist/src/server/workers/query_worker.d.ts +1 -0
  107. package/dist/src/server/workers/query_worker.js +4 -0
  108. package/dist/src/server/workers/worker.d.ts +1 -0
  109. package/dist/src/server/workers/worker.js +41 -0
  110. package/dist/src/shared/Emitter.d.ts +1 -0
  111. package/dist/src/types.d.ts +1 -1
  112. package/dist/src/types.js +1 -1
  113. package/package.json +3 -3
  114. package/dist/lib/darwin_aarch64/include/selva/find.h +0 -47
  115. package/dist/lib/darwin_aarch64/include/selva/history.h +0 -64
  116. package/dist/lib/darwin_aarch64/include/selva/queue_r.h +0 -190
  117. package/dist/lib/darwin_aarch64/include/selva/traverse.h +0 -65
  118. package/dist/lib/linux_aarch64/include/selva/find.h +0 -47
  119. package/dist/lib/linux_aarch64/include/selva/history.h +0 -64
  120. package/dist/lib/linux_aarch64/include/selva/queue_r.h +0 -190
  121. package/dist/lib/linux_aarch64/include/selva/traverse.h +0 -65
  122. package/dist/lib/linux_x86_64/include/selva/find.h +0 -47
  123. package/dist/lib/linux_x86_64/include/selva/history.h +0 -64
  124. package/dist/lib/linux_x86_64/include/selva/queue_r.h +0 -190
  125. package/dist/lib/linux_x86_64/include/selva/traverse.h +0 -65
  126. package/dist/src/client/query/serialize.d.ts +0 -4
  127. package/dist/src/client/query/serialize.js +0 -26
  128. package/dist/src/server/DbWorker.d.ts +0 -13
  129. package/dist/src/server/csmt/draw-dot.d.ts +0 -4
  130. package/dist/src/server/csmt/draw-dot.js +0 -38
  131. package/dist/src/server/csmt/index.d.ts +0 -4
  132. package/dist/src/server/csmt/index.js +0 -5
  133. package/dist/src/server/csmt/match.d.ts +0 -7
  134. package/dist/src/server/csmt/match.js +0 -10
  135. package/dist/src/server/csmt/memebership-proof.d.ts +0 -7
  136. package/dist/src/server/csmt/memebership-proof.js +0 -122
  137. package/dist/src/server/csmt/tree-utils.d.ts +0 -6
  138. package/dist/src/server/csmt/tree-utils.js +0 -33
  139. package/dist/src/server/csmt/tree.d.ts +0 -3
  140. package/dist/src/server/csmt/tree.js +0 -270
  141. package/dist/src/server/csmt/types.d.ts +0 -46
  142. package/dist/src/server/csmt/types.js +0 -2
  143. package/dist/src/server/worker.js +0 -33
  144. /package/dist/src/server/{worker.d.ts → workers/io_worker.d.ts} +0 -0
@@ -1,26 +0,0 @@
1
- const walk = (q) => {
2
- const obj = {};
3
- for (const key in q) {
4
- if (key === 'schema') {
5
- obj[key] = q[key].type;
6
- }
7
- else {
8
- // if PROPDEF
9
- if (typeof q[key] === 'object') {
10
- obj[key] = walk(q[key]);
11
- }
12
- else {
13
- obj[key] = q[key];
14
- }
15
- }
16
- }
17
- return obj;
18
- };
19
- export const serialize = (q) => {
20
- const obj = walk(q);
21
- return JSON.stringify(obj);
22
- };
23
- export const parse = (str, parsedSchema) => {
24
- //
25
- };
26
- //# sourceMappingURL=serialize.js.map
@@ -1,13 +0,0 @@
1
- import { Worker, MessagePort } from 'node:worker_threads';
2
- import { DbServer } from './index.js';
3
- export declare class DbWorker {
4
- constructor(address: BigInt, db: DbServer, workerIndex: number);
5
- db: DbServer;
6
- channel: MessagePort;
7
- worker: Worker;
8
- resolvers: any[];
9
- readyPromise: Promise<true>;
10
- callback: (resolve: any) => void;
11
- updateCtx(address: BigInt): Promise<void>;
12
- getQueryBuf(buf: Uint8Array): Promise<Uint8Array>;
13
- }
@@ -1,4 +0,0 @@
1
- import { Csmt } from './index.js';
2
- type DataFormatter<T> = (data: T) => string;
3
- export default function draw<T = any>(csmt: Csmt<T>, dataFormatter?: DataFormatter<T>): string;
4
- export {};
@@ -1,38 +0,0 @@
1
- import { encodeBase64 } from '@saulx/utils';
2
- function makeLabel(node) {
3
- return `${node.key}\n${encodeBase64(node.hash).substring(0, 5)}`;
4
- }
5
- function wrapFormatter(dataFormatter, node) {
6
- return `\n${dataFormatter(node.data).replace('"', '\\"')}`;
7
- }
8
- // This can be visualized with Graphviz dot.
9
- // Online: https://dreampuf.github.io/GraphvizOnline/?engine=dot
10
- export default function draw(csmt, dataFormatter) {
11
- const root = csmt.getRoot();
12
- const lines = [];
13
- const nodes = [];
14
- let i = 0;
15
- const walk = (node, prev) => {
16
- const cur = i;
17
- const left = node.left;
18
- const right = node.right;
19
- const isLeaf = !left && !right;
20
- nodes.push(`n${cur} [label="${makeLabel(node) + ((dataFormatter && node.data) ? wrapFormatter(dataFormatter, node) : '')}"${isLeaf ? ' shape=box' : ''}];`);
21
- if (cur > 0) {
22
- lines.push(`n${prev} -- n${cur}`);
23
- }
24
- if (left) {
25
- i++;
26
- walk(left, cur);
27
- }
28
- if (right) {
29
- i++;
30
- walk(right, cur);
31
- }
32
- };
33
- if (root) {
34
- walk(root, i);
35
- }
36
- return `graph ethane {\n${nodes.join('\n')}\n${lines.join('\n')}\n}`;
37
- }
38
- //# sourceMappingURL=draw-dot.js.map
@@ -1,4 +0,0 @@
1
- import drawDot from './draw-dot.js';
2
- export * from './types.js';
3
- export * from './tree.js';
4
- export { drawDot };
@@ -1,5 +0,0 @@
1
- import drawDot from './draw-dot.js';
2
- export * from './types.js';
3
- export * from './tree.js';
4
- export { drawDot };
5
- //# sourceMappingURL=index.js.map
@@ -1,7 +0,0 @@
1
- type Predicate = (x: any) => boolean;
2
- type Func = (x: any) => any;
3
- declare const match: (x: any) => {
4
- on: (pred: Predicate, fn: Func) => any;
5
- otherwise: (fn: Func) => any;
6
- };
7
- export default match;
@@ -1,10 +0,0 @@
1
- const matched = (x) => ({
2
- on: () => matched(x),
3
- otherwise: () => x,
4
- });
5
- const match = (x) => ({
6
- on: (pred, fn) => (pred(x) ? matched(fn(x)) : match(x)),
7
- otherwise: (fn) => fn(x),
8
- });
9
- export default match;
10
- //# sourceMappingURL=match.js.map
@@ -1,7 +0,0 @@
1
- import { TreeKey, TreeNode } from './types.js';
2
- export declare enum Direction {
3
- Left = "L",
4
- Right = "R"
5
- }
6
- export type Proof = [TreeKey | null, TreeKey | null] | [TreeKey | Uint8Array, TreeKey | Direction][];
7
- export default function membershipProof<T = any>(root: TreeNode<T> | null, k: TreeKey): Proof;
@@ -1,122 +0,0 @@
1
- import { minInSubtree, maxInSubtree } from './tree-utils.js';
2
- import match from './match.js';
3
- export var Direction;
4
- (function (Direction) {
5
- Direction["Left"] = "L";
6
- Direction["Right"] = "R";
7
- })(Direction || (Direction = {}));
8
- function reverse(direction) {
9
- switch (direction) {
10
- case Direction.Left:
11
- return Direction.Right;
12
- case Direction.Right:
13
- return Direction.Left;
14
- default:
15
- throw new TypeError('"direction" is not type of Direction');
16
- }
17
- }
18
- function aeq(arr) {
19
- return (x) => x.every((y, i) => y === arr[i]);
20
- }
21
- function isList(x) {
22
- return Array.isArray(x) && x.length > 0 && Array.isArray(x[0]);
23
- }
24
- function nonMembershipProof(k, key, direction, sibling) {
25
- return match([k > key, direction])
26
- .on(aeq([true, Direction.Left]), () => [key, minInSubtree(sibling)])
27
- .on(aeq([true, Direction.Right]), () => [key, null])
28
- .on(aeq([false, Direction.Left]), () => [null, key])
29
- .on(aeq([false, Direction.Right]), () => [maxInSubtree(sibling), key])
30
- .otherwise(() => {
31
- throw new TypeError('"direction" is not type of Direction');
32
- });
33
- }
34
- function membershipProofR(sibling, direction, node, k) {
35
- const left = node.left;
36
- const right = node.right;
37
- if (k === undefined || k === null) {
38
- throw new TypeError('k is not a TreeKey');
39
- }
40
- // && would be more accurate here but there is never a case where only one
41
- // would be set and thus this way we save on error handling later on.
42
- if (!left || !right) {
43
- if (!direction) {
44
- throw new Error('"direction" must be set');
45
- }
46
- if (!sibling) {
47
- throw new Error('"sibling" must be set');
48
- }
49
- // This is a leaf node
50
- if (node.key === k) {
51
- return [
52
- [sibling.hash, reverse(direction)],
53
- [node.hash, node.key],
54
- ];
55
- }
56
- else {
57
- // Find the non-membership proof otherwise
58
- return nonMembershipProof(k, node.key, direction, sibling);
59
- }
60
- }
61
- let result;
62
- if (k <= left.key) {
63
- // Going towards left child
64
- result = membershipProofR(right, Direction.Left, left, k);
65
- }
66
- else if (k <= right.key) {
67
- // Going towards right child
68
- result = membershipProofR(left, Direction.Right, right, k);
69
- }
70
- else {
71
- if (k > right.key) {
72
- // The given key `k` is greater than any key in this tree.
73
- // Trigger a proof for the largest key
74
- return [right.key, null];
75
- }
76
- if (!direction) {
77
- // TODO How should we set direction?
78
- throw new TypeError('"Direction" must be set');
79
- }
80
- if (!sibling) {
81
- throw new Error('"sibling" must be set');
82
- }
83
- // Find the non-membership proof otherwise
84
- return nonMembershipProof(k, node.key, direction, sibling);
85
- }
86
- if (sibling) {
87
- if (isList(result) && direction) {
88
- // @ts-ignore the array thing is confusing for TS
89
- return [[sibling.hash, reverse(direction)], ...result];
90
- }
91
- else if (result[1] === null && direction === Direction.Left) {
92
- return [node.key, minInSubtree(sibling)];
93
- }
94
- else if (result[0] === null && direction === Direction.Right) {
95
- return [maxInSubtree(sibling), node.key];
96
- }
97
- }
98
- return result;
99
- }
100
- export default function membershipProof(root, k) {
101
- if (!root) {
102
- return [];
103
- }
104
- // Root has no sibling or direction so null is used
105
- return (match(membershipProofR(null, null, root, k))
106
- // The key is present in the tree
107
- // Provide the proof in reverse order
108
- .on((x) => isList(x), (r) => r.reverse())
109
- // The key is greater than the largest element in the tree
110
- // Provide a proof for the largest key
111
- .on(([_, y]) => y === null, ([x, _]) => [...membershipProof(root, x), null])
112
- // The key is smaller than the smallest element in the tree
113
- // Provide a proof for the smallest key
114
- .on(([x, _]) => x === null, ([_, y]) => [null, ...membershipProof(root, y)])
115
- // The key is bounded by by two keys in the case of non-membership proff
116
- // provide a proof for the bounding keys to exist
117
- .otherwise(([x, y]) => [
118
- membershipProof(root, x),
119
- membershipProof(root, y),
120
- ]));
121
- }
122
- //# sourceMappingURL=memebership-proof.js.map
@@ -1,6 +0,0 @@
1
- import { TreeKey, TreeNode } from './types.js';
2
- export declare function distance(x: TreeKey, y: TreeKey): TreeKey;
3
- export declare function min(x: TreeNode<any> | null, y: TreeNode<any> | null): TreeKey;
4
- export declare function max(x: TreeNode<any> | null, y: TreeNode<any> | null): TreeKey;
5
- export declare function minInSubtree(node: TreeNode<any>): TreeKey;
6
- export declare function maxInSubtree(node: TreeNode<any>): TreeKey;
@@ -1,33 +0,0 @@
1
- import { TreeKeyNil } from './types.js';
2
- export function distance(x, y) {
3
- let v = x ^ y;
4
- let r = TreeKeyNil;
5
- while ((v >>= 1)) {
6
- r++;
7
- }
8
- return r;
9
- }
10
- export function min(x, y) {
11
- const a = (x && x.key) || TreeKeyNil;
12
- const b = (y && y.key) || TreeKeyNil;
13
- return a < b ? a : b;
14
- }
15
- export function max(x, y) {
16
- const a = (x && x.key) || TreeKeyNil;
17
- const b = (y && y.key) || TreeKeyNil;
18
- return a > b ? a : b;
19
- }
20
- // Find min key in a subtree.
21
- export function minInSubtree(node) {
22
- if (!node.left) {
23
- // We assume that the tree is always full and the last left node we can
24
- // find is the min.
25
- return node.key;
26
- }
27
- return minInSubtree(node.left);
28
- }
29
- // Find max key in a subtree.
30
- export function maxInSubtree(node) {
31
- return node.key;
32
- }
33
- //# sourceMappingURL=tree-utils.js.map
@@ -1,3 +0,0 @@
1
- import { Hash, Csmt } from './types.js';
2
- export declare function hashEq(a: Hash, b: Hash): boolean;
3
- export declare function createTree<T>(createHash: () => any): Csmt<T>;
@@ -1,270 +0,0 @@
1
- import { TreeKeyNil } from './types.js';
2
- import { distance, min, max } from './tree-utils.js';
3
- import membershipProof from './memebership-proof.js';
4
- import { equals } from '@saulx/utils';
5
- export function hashEq(a, b) {
6
- return equals(a, b);
7
- }
8
- export function createTree(createHash) {
9
- let root = null;
10
- const emptyHash = createHash().digest();
11
- function genNodeHash(lHash, rHash) {
12
- return createHash().update(lHash).update(rHash).digest();
13
- }
14
- function createNode(left, right) {
15
- const hash = left && right ? genNodeHash(left.hash, right.hash) : emptyHash;
16
- return {
17
- hash,
18
- key: max(left, right),
19
- left,
20
- right,
21
- };
22
- }
23
- function createLeaf(k, h, data) {
24
- return {
25
- hash: h,
26
- key: k,
27
- data,
28
- left: null,
29
- right: null,
30
- };
31
- }
32
- /**
33
- * Update node properties.
34
- */
35
- function updateNode(node) {
36
- if (node.left || node.right) {
37
- node.key = max(node.left, node.right);
38
- if (node.left && node.right) {
39
- node.hash = genNodeHash(node.left.hash, node.right.hash);
40
- }
41
- }
42
- }
43
- function insert(node, newLeaf) {
44
- const { key: k } = newLeaf;
45
- let left = node.left;
46
- let right = node.right;
47
- // Check if this is a leaf
48
- if (!left && !right) {
49
- const nodeKey = node.key;
50
- if (nodeKey < k) {
51
- return createNode(node, newLeaf);
52
- }
53
- else if (nodeKey > k) {
54
- return createNode(newLeaf, node);
55
- }
56
- else {
57
- throw new Error(`k=${k} exists`);
58
- }
59
- }
60
- const lDist = distance(k, (left && left.key) || TreeKeyNil);
61
- const rDist = distance(k, (right && right.key) || TreeKeyNil);
62
- if (lDist < rDist) {
63
- node.left = left ? insert(left, newLeaf) : newLeaf;
64
- }
65
- else if (lDist > rDist) {
66
- node.right = right ? insert(right, newLeaf) : newLeaf;
67
- }
68
- else {
69
- return k < min(left, right)
70
- ? createNode(newLeaf, node)
71
- : createNode(node, newLeaf);
72
- }
73
- updateNode(node);
74
- return node;
75
- }
76
- function checkForLeaf(node, k) {
77
- return !node.left && !node.right && node.key === k;
78
- }
79
- function deleteNode(node, k) {
80
- const left = node.left;
81
- const right = node.right;
82
- if (!left || !right) {
83
- if (node.data) {
84
- if (node.key === k) {
85
- return null;
86
- }
87
- throw new Error(`k=${k} does not exist`);
88
- }
89
- else {
90
- throw new Error('The tree is broken');
91
- }
92
- }
93
- if (checkForLeaf(left, k) || checkForLeaf(right, k)) {
94
- if (left.key === k) {
95
- // The `left` node is discarded
96
- return right;
97
- }
98
- else {
99
- // The `right` node is discarded
100
- return left;
101
- }
102
- }
103
- else {
104
- const lDist = distance(k, left.key);
105
- const rDist = distance(k, right.key);
106
- if (lDist < rDist) {
107
- node.left = deleteNode(left, k);
108
- updateNode(node);
109
- return node;
110
- }
111
- else if (lDist > rDist) {
112
- node.right = deleteNode(right, k);
113
- updateNode(node);
114
- return node;
115
- }
116
- else {
117
- throw new Error(`k=${k} does not exist`);
118
- }
119
- }
120
- }
121
- function updateNodeHash(node) {
122
- if (node.left && node.right) {
123
- node.hash = genNodeHash(node.left.hash, node.right.hash);
124
- }
125
- }
126
- function updateHash(node, k, hash) {
127
- if (!node)
128
- return;
129
- const { left, right } = node;
130
- if (k === node.key && !left && !right) {
131
- node.hash = hash;
132
- }
133
- else {
134
- if (left && left.key === k) {
135
- updateHash(left, k, hash);
136
- updateNodeHash(left);
137
- updateNodeHash(node);
138
- return;
139
- }
140
- if (right && right.key === k) {
141
- updateHash(right, k, hash);
142
- updateNodeHash(right);
143
- updateNodeHash(node);
144
- return;
145
- }
146
- const lDist = distance(k, (left && left.key) || TreeKeyNil);
147
- const rDist = distance(k, (right && right.key) || TreeKeyNil);
148
- if (left && lDist <= rDist) {
149
- updateHash(left, k, hash);
150
- updateNodeHash(left);
151
- updateNodeHash(node);
152
- return;
153
- }
154
- if (right && rDist <= lDist) {
155
- updateHash(right, k, hash);
156
- updateNodeHash(right);
157
- updateNodeHash(node);
158
- return;
159
- }
160
- }
161
- }
162
- function diffAB(diffA, diffB, nodeA, nodeB) {
163
- if (nodeA &&
164
- nodeB &&
165
- nodeA.key === nodeB.key &&
166
- hashEq(nodeA.hash, nodeB.hash)) {
167
- return;
168
- }
169
- // No hash match
170
- const leftA = nodeA && nodeA.left;
171
- const leftB = nodeB && nodeB.left;
172
- const rightA = nodeA && nodeA.right;
173
- const rightB = nodeB && nodeB.right;
174
- // Check if this is a leaf that is missing from the right tree
175
- if (nodeA && !leftA && !rightA) {
176
- const bHash = diffB.get(nodeA.key);
177
- if (bHash && hashEq(bHash, nodeA.hash)) {
178
- // The same leaf appears to exist in both trees
179
- diffB.delete(nodeA.key);
180
- }
181
- else {
182
- // The leaf doesn't exist or differs from the right tree
183
- diffA.set(nodeA.key, nodeA.hash);
184
- }
185
- }
186
- // Check if this is a leaf that is missing from the left tree
187
- if (nodeB && !leftB && !rightB) {
188
- const aHash = diffA.get(nodeB.key);
189
- if (aHash && hashEq(aHash, nodeB.hash)) {
190
- diffA.delete(nodeB.key);
191
- }
192
- else {
193
- diffB.set(nodeB.key, nodeB.hash);
194
- }
195
- }
196
- if (leftA || leftB) {
197
- // Recurse to the left branch
198
- diffAB(diffA, diffB, leftA, leftB);
199
- }
200
- if (rightA || rightB) {
201
- // Recurse to the right branch
202
- diffAB(diffA, diffB, rightA, rightB);
203
- }
204
- }
205
- function diff(tree) {
206
- const leftMap = new Map();
207
- const rightMap = new Map();
208
- const nodeA = root;
209
- const nodeB = tree.getRoot();
210
- diffAB(leftMap, rightMap, nodeA, nodeB);
211
- return {
212
- left: [...leftMap],
213
- right: [...rightMap],
214
- };
215
- }
216
- function _visitLeafNodes(node, cb) {
217
- if (!node)
218
- return;
219
- if (!node.left && !node.right)
220
- cb(node);
221
- else {
222
- if (node.left)
223
- _visitLeafNodes(node.left, cb);
224
- if (node.right)
225
- _visitLeafNodes(node.right, cb);
226
- }
227
- }
228
- function search(node, k) {
229
- if (!node || (k === node.key && !node.left && !node.right))
230
- return node;
231
- const { left, right } = node;
232
- if (left && left.key === k)
233
- return search(left, k);
234
- if (right && right.key === k)
235
- return search(right, k);
236
- const lDist = distance(k, (left && left.key) || TreeKeyNil);
237
- const rDist = distance(k, (right && right.key) || TreeKeyNil);
238
- if (left && lDist <= rDist)
239
- return search(left, k);
240
- if (right && rDist <= lDist)
241
- return search(right, k);
242
- return null;
243
- }
244
- return {
245
- emptyHash,
246
- getRoot: () => root,
247
- insert: (k, h, data = null) => {
248
- if (!(h instanceof Uint8Array)) {
249
- throw new TypeError('`h` must be a Uint8Array');
250
- }
251
- const newLeaf = createLeaf(k, h, data);
252
- root = root ? insert(root, newLeaf) : newLeaf;
253
- },
254
- update: (k, h) => {
255
- if (root) {
256
- updateHash(root, k, h);
257
- }
258
- },
259
- delete: (k) => {
260
- if (root) {
261
- root = deleteNode(root, k);
262
- }
263
- },
264
- diff,
265
- membershipProof: (k) => membershipProof(root, k),
266
- visitLeafNodes: (cb) => _visitLeafNodes(root, cb),
267
- search: (k) => search(root, k),
268
- };
269
- }
270
- //# sourceMappingURL=tree.js.map
@@ -1,46 +0,0 @@
1
- import { Proof } from './memebership-proof.js';
2
- export type TreeKey = number;
3
- export declare const TreeKeyNil = 0;
4
- export type Hash = Uint8Array;
5
- export interface TreeNode<T> {
6
- hash: Hash;
7
- key: TreeKey | null;
8
- data?: T | null;
9
- left: TreeNode<T> | null;
10
- right: TreeNode<T> | null;
11
- }
12
- export type KeyHashPair = [TreeKey, Hash];
13
- export interface TreeDiff {
14
- left: KeyHashPair[];
15
- right: KeyHashPair[];
16
- }
17
- export interface Csmt<T> {
18
- emptyHash: Uint8Array;
19
- /**
20
- * Get the root node.
21
- */
22
- getRoot: () => TreeNode<T> | null;
23
- /**
24
- * Insert a new key-hash pair.
25
- */
26
- insert: (k: TreeKey, h: Hash, data?: any) => void;
27
- /**
28
- * Delete a key-hash pair from the tree.
29
- */
30
- delete: (k: TreeKey) => void;
31
- /**
32
- * Update node hash.
33
- */
34
- update: (k: TreeKey, h: Hash) => void;
35
- /**
36
- * Compute the diff between this and a given tree.
37
- */
38
- diff: (tree: Csmt<T>) => TreeDiff;
39
- /**
40
- * Provide a proof of membership if a key exist in the three;
41
- * Otherwise a proof of non-membership is returned.
42
- */
43
- membershipProof: (k: TreeKey) => Proof;
44
- visitLeafNodes: (cb: (leaf: TreeNode<T>) => void) => void;
45
- search: (k: TreeKey) => TreeNode<T>;
46
- }
@@ -1,2 +0,0 @@
1
- export const TreeKeyNil = 0;
2
- //# sourceMappingURL=types.js.map
@@ -1,33 +0,0 @@
1
- import { isMainThread, parentPort, workerData } from 'node:worker_threads';
2
- import native from '../native.js';
3
- if (isMainThread) {
4
- console.warn('running query worker.ts in mainthread - incorrect');
5
- }
6
- else if (workerData?.isDbWorker) {
7
- let { address, channel } = workerData;
8
- let dbCtx = native.externalFromInt(address);
9
- native.workerCtxInit();
10
- const handleMsg = (msg) => {
11
- try {
12
- if (typeof msg === 'bigint') {
13
- // it's a ctx address
14
- address = msg;
15
- dbCtx = native.externalFromInt(address);
16
- channel.postMessage(null);
17
- }
18
- else {
19
- const arrayBuf = native.getQueryBuf(msg, dbCtx);
20
- channel.postMessage(arrayBuf, [arrayBuf]);
21
- }
22
- }
23
- catch (e) {
24
- channel.postMessage(e);
25
- }
26
- };
27
- channel.on('message', handleMsg);
28
- parentPort.postMessage('READY');
29
- }
30
- else {
31
- console.info('incorrect worker db query');
32
- }
33
- //# sourceMappingURL=worker.js.map