@aztec/kv-store 0.0.1-commit.5daedc8 → 0.0.1-commit.6201a7b05
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/dest/bench/shared_map_bench.d.ts +19 -0
- package/dest/bench/shared_map_bench.d.ts.map +1 -0
- package/dest/bench/shared_map_bench.js +91 -0
- package/dest/indexeddb/array.js +21 -7
- package/dest/indexeddb/index.d.ts +3 -3
- package/dest/indexeddb/index.d.ts.map +1 -1
- package/dest/indexeddb/index.js +3 -6
- package/dest/indexeddb/map.d.ts +8 -2
- package/dest/indexeddb/map.d.ts.map +1 -1
- package/dest/indexeddb/map.js +23 -13
- package/dest/indexeddb/multi_map.d.ts +2 -1
- package/dest/indexeddb/multi_map.d.ts.map +1 -1
- package/dest/indexeddb/multi_map.js +16 -1
- package/dest/indexeddb/singleton.js +3 -1
- package/dest/indexeddb/store.d.ts +3 -3
- package/dest/indexeddb/store.d.ts.map +1 -1
- package/dest/indexeddb/store.js +6 -4
- package/dest/interfaces/map_test_suite.d.ts +1 -1
- package/dest/interfaces/map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/map_test_suite.js +48 -2
- package/dest/interfaces/multi_map_test_suite.d.ts +1 -1
- package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/multi_map_test_suite.js +25 -0
- package/dest/interfaces/utils.d.ts +2 -1
- package/dest/interfaces/utils.d.ts.map +1 -1
- package/dest/interfaces/utils.js +2 -1
- package/dest/lmdb/array.js +4 -2
- package/dest/lmdb/index.d.ts +3 -3
- package/dest/lmdb/index.d.ts.map +1 -1
- package/dest/lmdb/index.js +3 -3
- package/dest/lmdb/store.d.ts +3 -3
- package/dest/lmdb/store.d.ts.map +1 -1
- package/dest/lmdb/store.js +13 -9
- package/dest/lmdb-v2/array.d.ts +2 -2
- package/dest/lmdb-v2/array.d.ts.map +1 -1
- package/dest/lmdb-v2/array.js +4 -3
- package/dest/lmdb-v2/factory.d.ts +7 -7
- package/dest/lmdb-v2/factory.d.ts.map +1 -1
- package/dest/lmdb-v2/factory.js +14 -10
- package/dest/lmdb-v2/map.d.ts +2 -2
- package/dest/lmdb-v2/map.d.ts.map +1 -1
- package/dest/lmdb-v2/map.js +1 -2
- package/dest/lmdb-v2/multi_map.d.ts +2 -2
- package/dest/lmdb-v2/multi_map.d.ts.map +1 -1
- package/dest/lmdb-v2/multi_map.js +1 -2
- package/dest/lmdb-v2/read_transaction.js +21 -19
- package/dest/lmdb-v2/singleton.d.ts +2 -2
- package/dest/lmdb-v2/singleton.d.ts.map +1 -1
- package/dest/lmdb-v2/singleton.js +1 -2
- package/dest/lmdb-v2/store.d.ts +4 -5
- package/dest/lmdb-v2/store.d.ts.map +1 -1
- package/dest/lmdb-v2/store.js +3 -25
- package/dest/lmdb-v2/tx-helpers.d.ts +6 -0
- package/dest/lmdb-v2/tx-helpers.d.ts.map +1 -0
- package/dest/lmdb-v2/tx-helpers.js +21 -0
- package/dest/lmdb-v2/utils.d.ts +1 -10
- package/dest/lmdb-v2/utils.d.ts.map +1 -1
- package/dest/lmdb-v2/utils.js +0 -94
- package/dest/lmdb-v2/write_transaction.d.ts +1 -1
- package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
- package/dest/lmdb-v2/write_transaction.js +9 -5
- package/dest/sqlite-opfs/array.d.ts +21 -0
- package/dest/sqlite-opfs/array.d.ts.map +1 -0
- package/dest/sqlite-opfs/array.js +128 -0
- package/dest/sqlite-opfs/index.d.ts +7 -0
- package/dest/sqlite-opfs/index.d.ts.map +1 -0
- package/dest/sqlite-opfs/index.js +13 -0
- package/dest/sqlite-opfs/map.d.ts +35 -0
- package/dest/sqlite-opfs/map.d.ts.map +1 -0
- package/dest/sqlite-opfs/map.js +163 -0
- package/dest/sqlite-opfs/messages.d.ts +58 -0
- package/dest/sqlite-opfs/messages.d.ts.map +1 -0
- package/dest/sqlite-opfs/messages.js +5 -0
- package/dest/sqlite-opfs/multi_map.d.ts +16 -0
- package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
- package/dest/sqlite-opfs/multi_map.js +67 -0
- package/dest/sqlite-opfs/set.d.ts +13 -0
- package/dest/sqlite-opfs/set.d.ts.map +1 -0
- package/dest/sqlite-opfs/set.js +19 -0
- package/dest/sqlite-opfs/singleton.d.ts +13 -0
- package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
- package/dest/sqlite-opfs/singleton.js +48 -0
- package/dest/sqlite-opfs/store.d.ts +70 -0
- package/dest/sqlite-opfs/store.d.ts.map +1 -0
- package/dest/sqlite-opfs/store.js +242 -0
- package/dest/sqlite-opfs/worker.d.ts +2 -0
- package/dest/sqlite-opfs/worker.d.ts.map +1 -0
- package/dest/sqlite-opfs/worker.js +194 -0
- package/dest/stores/l2_tips_store.d.ts +24 -9
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +61 -52
- package/dest/utils.d.ts +9 -6
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +51 -16
- package/package.json +21 -15
- package/src/bench/shared_map_bench.ts +111 -0
- package/src/indexeddb/array.ts +4 -4
- package/src/indexeddb/index.ts +9 -7
- package/src/indexeddb/map.ts +24 -11
- package/src/indexeddb/multi_map.ts +15 -1
- package/src/indexeddb/singleton.ts +1 -1
- package/src/indexeddb/store.ts +13 -6
- package/src/interfaces/map_test_suite.ts +30 -2
- package/src/interfaces/multi_map_test_suite.ts +32 -0
- package/src/interfaces/utils.ts +1 -0
- package/src/lmdb/index.ts +9 -4
- package/src/lmdb/store.ts +13 -9
- package/src/lmdb-v2/array.ts +2 -2
- package/src/lmdb-v2/factory.ts +16 -12
- package/src/lmdb-v2/map.ts +2 -2
- package/src/lmdb-v2/multi_map.ts +2 -2
- package/src/lmdb-v2/read_transaction.ts +23 -23
- package/src/lmdb-v2/singleton.ts +2 -2
- package/src/lmdb-v2/store.ts +5 -31
- package/src/lmdb-v2/tx-helpers.ts +29 -0
- package/src/lmdb-v2/utils.ts +0 -118
- package/src/lmdb-v2/write_transaction.ts +9 -8
- package/src/sqlite-opfs/array.ts +124 -0
- package/src/sqlite-opfs/index.ts +27 -0
- package/src/sqlite-opfs/map.ts +163 -0
- package/src/sqlite-opfs/messages.ts +28 -0
- package/src/sqlite-opfs/multi_map.ts +74 -0
- package/src/sqlite-opfs/set.ts +29 -0
- package/src/sqlite-opfs/singleton.ts +48 -0
- package/src/sqlite-opfs/store.ts +248 -0
- package/src/sqlite-opfs/worker.ts +162 -0
- package/src/stores/l2_tips_store.ts +65 -56
- package/src/utils.ts +79 -21
- package/dest/config.d.ts +0 -17
- package/dest/config.d.ts.map +0 -1
- package/dest/config.js +0 -26
- package/src/config.ts +0 -36
|
@@ -66,20 +66,20 @@ export class ReadTransaction {
|
|
|
66
66
|
): AsyncIterable<[Uint8Array, T]> {
|
|
67
67
|
this.assertIsOpen();
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
key: startKey,
|
|
71
|
-
reverse,
|
|
72
|
-
count: typeof limit === 'number' ? Math.min(limit, CURSOR_PAGE_SIZE) : CURSOR_PAGE_SIZE,
|
|
73
|
-
onePage: typeof limit === 'number' && limit < CURSOR_PAGE_SIZE,
|
|
74
|
-
db,
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
const cursor = response.cursor;
|
|
78
|
-
let entries = response.entries;
|
|
79
|
-
let done = typeof cursor !== 'number';
|
|
80
|
-
let count = 0;
|
|
81
|
-
|
|
69
|
+
let cursor: number | undefined;
|
|
82
70
|
try {
|
|
71
|
+
const response = await this.channel.sendMessage(LMDBMessageType.START_CURSOR, {
|
|
72
|
+
key: startKey,
|
|
73
|
+
reverse,
|
|
74
|
+
count: typeof limit === 'number' ? Math.min(limit, CURSOR_PAGE_SIZE) : CURSOR_PAGE_SIZE,
|
|
75
|
+
onePage: typeof limit === 'number' && limit < CURSOR_PAGE_SIZE,
|
|
76
|
+
db,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
cursor = response.cursor ?? undefined;
|
|
80
|
+
let entries = response.entries;
|
|
81
|
+
let done = typeof cursor !== 'number';
|
|
82
|
+
let count = 0;
|
|
83
83
|
// emit the first page and any subsequent pages in a while loop
|
|
84
84
|
// NB: end contition is in the middle of the while loop
|
|
85
85
|
while (entries.length > 0) {
|
|
@@ -125,17 +125,17 @@ export class ReadTransaction {
|
|
|
125
125
|
async #countEntries(db: string, startKey: Uint8Array, endKey: Uint8Array, reverse: boolean): Promise<number> {
|
|
126
126
|
this.assertIsOpen();
|
|
127
127
|
|
|
128
|
-
|
|
129
|
-
key: startKey,
|
|
130
|
-
reverse,
|
|
131
|
-
count: 0,
|
|
132
|
-
onePage: false,
|
|
133
|
-
db,
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
const cursor = response.cursor;
|
|
137
|
-
|
|
128
|
+
let cursor: number | undefined;
|
|
138
129
|
try {
|
|
130
|
+
const response = await this.channel.sendMessage(LMDBMessageType.START_CURSOR, {
|
|
131
|
+
key: startKey,
|
|
132
|
+
reverse,
|
|
133
|
+
count: 0,
|
|
134
|
+
onePage: false,
|
|
135
|
+
db,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
cursor = response.cursor ?? undefined;
|
|
139
139
|
if (!cursor) {
|
|
140
140
|
return 0;
|
|
141
141
|
}
|
package/src/lmdb-v2/singleton.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Encoder } from 'msgpackr';
|
|
2
2
|
|
|
3
3
|
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
4
|
-
|
|
5
|
-
import {
|
|
4
|
+
import type { AztecLMDBStoreV2 } from './store.js';
|
|
5
|
+
import { execInReadTx, execInWriteTx } from './tx-helpers.js';
|
|
6
6
|
import { serializeKey } from './utils.js';
|
|
7
7
|
|
|
8
8
|
export class LMDBSingleValue<T> implements AztecAsyncSingleton<T> {
|
package/src/lmdb-v2/store.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
1
|
+
import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
|
|
2
2
|
import { Semaphore, SerialQueue } from '@aztec/foundation/queue';
|
|
3
3
|
import { MsgpackChannel, NativeLMDBStore } from '@aztec/native';
|
|
4
4
|
|
|
@@ -13,9 +13,7 @@ import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
|
|
|
13
13
|
import type { AztecAsyncSet } from '../interfaces/set.js';
|
|
14
14
|
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
15
15
|
import type { AztecAsyncKVStore } from '../interfaces/store.js';
|
|
16
|
-
// eslint-disable-next-line import/no-cycle
|
|
17
16
|
import { LMDBArray } from './array.js';
|
|
18
|
-
// eslint-disable-next-line import/no-cycle
|
|
19
17
|
import { LMDBMap } from './map.js';
|
|
20
18
|
import {
|
|
21
19
|
Database,
|
|
@@ -27,10 +25,11 @@ import {
|
|
|
27
25
|
import { LMDBMultiMap } from './multi_map.js';
|
|
28
26
|
import { ReadTransaction } from './read_transaction.js';
|
|
29
27
|
import { LMDBSet } from './set.js';
|
|
30
|
-
// eslint-disable-next-line import/no-cycle
|
|
31
28
|
import { LMDBSingleValue } from './singleton.js';
|
|
32
29
|
import { WriteTransaction } from './write_transaction.js';
|
|
33
30
|
|
|
31
|
+
export { execInReadTx, execInWriteTx } from './tx-helpers.js';
|
|
32
|
+
|
|
34
33
|
export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
|
|
35
34
|
private open = false;
|
|
36
35
|
private channel: MsgpackChannel<LMDBMessageType, LMDBRequestBody, LMDBResponseBody>;
|
|
@@ -76,8 +75,9 @@ export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
|
|
|
76
75
|
dbMapSizeKb: number = 10 * 1024 * 1024,
|
|
77
76
|
maxReaders: number = 16,
|
|
78
77
|
cleanup?: () => Promise<void>,
|
|
79
|
-
|
|
78
|
+
bindings?: LoggerBindings,
|
|
80
79
|
) {
|
|
80
|
+
const log = createLogger('kv-store:lmdb-v2', bindings);
|
|
81
81
|
const db = new AztecLMDBStoreV2(dataDir, dbMapSizeKb, maxReaders, log, cleanup);
|
|
82
82
|
await db.start();
|
|
83
83
|
return db;
|
|
@@ -217,29 +217,3 @@ export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
|
|
|
217
217
|
};
|
|
218
218
|
}
|
|
219
219
|
}
|
|
220
|
-
|
|
221
|
-
export function execInWriteTx<T>(store: AztecLMDBStoreV2, fn: (tx: WriteTransaction) => Promise<T>): Promise<T> {
|
|
222
|
-
const currentWrite = store.getCurrentWriteTx();
|
|
223
|
-
if (currentWrite) {
|
|
224
|
-
return fn(currentWrite);
|
|
225
|
-
} else {
|
|
226
|
-
return store.transactionAsync(fn);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export async function execInReadTx<T>(
|
|
231
|
-
store: AztecLMDBStoreV2,
|
|
232
|
-
fn: (tx: ReadTransaction) => T | Promise<T>,
|
|
233
|
-
): Promise<T> {
|
|
234
|
-
const currentWrite = store.getCurrentWriteTx();
|
|
235
|
-
if (currentWrite) {
|
|
236
|
-
return await fn(currentWrite);
|
|
237
|
-
} else {
|
|
238
|
-
const tx = store.getReadTx();
|
|
239
|
-
try {
|
|
240
|
-
return await fn(tx);
|
|
241
|
-
} finally {
|
|
242
|
-
tx.close();
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ReadTransaction } from './read_transaction.js';
|
|
2
|
+
import type { AztecLMDBStoreV2 } from './store.js';
|
|
3
|
+
import type { WriteTransaction } from './write_transaction.js';
|
|
4
|
+
|
|
5
|
+
export function execInWriteTx<T>(store: AztecLMDBStoreV2, fn: (tx: WriteTransaction) => Promise<T>): Promise<T> {
|
|
6
|
+
const currentWrite = store.getCurrentWriteTx();
|
|
7
|
+
if (currentWrite) {
|
|
8
|
+
return fn(currentWrite);
|
|
9
|
+
} else {
|
|
10
|
+
return store.transactionAsync(fn);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function execInReadTx<T>(
|
|
15
|
+
store: AztecLMDBStoreV2,
|
|
16
|
+
fn: (tx: ReadTransaction) => T | Promise<T>,
|
|
17
|
+
): Promise<T> {
|
|
18
|
+
const currentWrite = store.getCurrentWriteTx();
|
|
19
|
+
if (currentWrite) {
|
|
20
|
+
return await fn(currentWrite);
|
|
21
|
+
} else {
|
|
22
|
+
const tx = store.getReadTx();
|
|
23
|
+
try {
|
|
24
|
+
return await fn(tx);
|
|
25
|
+
} finally {
|
|
26
|
+
tx.close();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/lmdb-v2/utils.ts
CHANGED
|
@@ -2,124 +2,6 @@ import { MAXIMUM_KEY, fromBufferKey, toBufferKey } from 'ordered-binary';
|
|
|
2
2
|
|
|
3
3
|
import type { Key } from '../interfaces/common.js';
|
|
4
4
|
|
|
5
|
-
type Cmp<T> = (a: T, b: T) => -1 | 0 | 1;
|
|
6
|
-
|
|
7
|
-
export function dedupeSortedArray<T>(arr: T[], cmp: Cmp<T>): void {
|
|
8
|
-
for (let i = 0; i < arr.length; i++) {
|
|
9
|
-
let j = i + 1;
|
|
10
|
-
for (; j < arr.length; j++) {
|
|
11
|
-
const res = cmp(arr[i], arr[j]);
|
|
12
|
-
if (res === 0) {
|
|
13
|
-
continue;
|
|
14
|
-
} else if (res < 0) {
|
|
15
|
-
break;
|
|
16
|
-
} else {
|
|
17
|
-
throw new Error('Array not sorted');
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (j - i > 1) {
|
|
22
|
-
arr.splice(i + 1, j - i - 1);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function insertIntoSortedArray<T>(arr: T[], item: T, cmp: (a: T, b: T) => number): void {
|
|
28
|
-
let left = 0;
|
|
29
|
-
let right = arr.length;
|
|
30
|
-
|
|
31
|
-
while (left < right) {
|
|
32
|
-
const mid = (left + right) >> 1;
|
|
33
|
-
const comparison = cmp(arr[mid], item);
|
|
34
|
-
|
|
35
|
-
if (comparison < 0) {
|
|
36
|
-
left = mid + 1;
|
|
37
|
-
} else {
|
|
38
|
-
right = mid;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
arr.splice(left, 0, item);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function findIndexInSortedArray<T, N>(values: T[], needle: N, cmp: (a: T, b: N) => number): number {
|
|
46
|
-
let start = 0;
|
|
47
|
-
let end = values.length - 1;
|
|
48
|
-
|
|
49
|
-
while (start <= end) {
|
|
50
|
-
const mid = start + (((end - start) / 2) | 0);
|
|
51
|
-
const res = cmp(values[mid], needle);
|
|
52
|
-
if (res === 0) {
|
|
53
|
-
return mid;
|
|
54
|
-
} else if (res > 0) {
|
|
55
|
-
end = mid - 1;
|
|
56
|
-
} else {
|
|
57
|
-
start = mid + 1;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return -1;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function findInSortedArray<T, N>(values: T[], needle: N, cmp: (a: T, b: N) => number): T | undefined {
|
|
65
|
-
const idx = findIndexInSortedArray(values, needle, cmp);
|
|
66
|
-
return idx > -1 ? values[idx] : undefined;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function removeAnyOf<T, N>(arr: T[], vals: N[], cmp: (a: T, b: N) => -1 | 0 | 1): void {
|
|
70
|
-
let writeIdx = 0;
|
|
71
|
-
let readIdx = 0;
|
|
72
|
-
let valIdx = 0;
|
|
73
|
-
|
|
74
|
-
while (readIdx < arr.length && valIdx < vals.length) {
|
|
75
|
-
const comparison = cmp(arr[readIdx], vals[valIdx]);
|
|
76
|
-
|
|
77
|
-
if (comparison < 0) {
|
|
78
|
-
arr[writeIdx++] = arr[readIdx++];
|
|
79
|
-
} else if (comparison > 0) {
|
|
80
|
-
valIdx++;
|
|
81
|
-
} else {
|
|
82
|
-
readIdx++;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
while (readIdx < arr.length) {
|
|
87
|
-
arr[writeIdx++] = arr[readIdx++];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
arr.length = writeIdx;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function removeFromSortedArray<T, N>(arr: T[], val: N, cmp: (a: T, b: N) => -1 | 0 | 1) {
|
|
94
|
-
const idx = findIndexInSortedArray(arr, val, cmp);
|
|
95
|
-
if (idx > -1) {
|
|
96
|
-
arr.splice(idx, 1);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function merge<T>(arr: T[], toInsert: T[], cmp: (a: T, b: T) => -1 | 0 | 1): void {
|
|
101
|
-
const result = new Array<T>(arr.length + toInsert.length);
|
|
102
|
-
let i = 0,
|
|
103
|
-
j = 0,
|
|
104
|
-
k = 0;
|
|
105
|
-
|
|
106
|
-
while (i < arr.length && j < toInsert.length) {
|
|
107
|
-
result[k++] = cmp(arr[i], toInsert[j]) <= 0 ? arr[i++] : toInsert[j++];
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
while (i < arr.length) {
|
|
111
|
-
result[k++] = arr[i++];
|
|
112
|
-
}
|
|
113
|
-
while (j < toInsert.length) {
|
|
114
|
-
result[k++] = toInsert[j++];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
for (i = 0; i < result.length; i++) {
|
|
118
|
-
arr[i] = result[i];
|
|
119
|
-
}
|
|
120
|
-
arr.length = result.length;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
5
|
export function keyCmp(a: [Uint8Array, Uint8Array[] | null], b: [Uint8Array, Uint8Array[] | null]): -1 | 0 | 1 {
|
|
124
6
|
return Buffer.compare(a[0], b[0]);
|
|
125
7
|
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { type Batch, Database, LMDBMessageType } from './message.js';
|
|
2
|
-
import { ReadTransaction } from './read_transaction.js';
|
|
3
1
|
import {
|
|
4
2
|
dedupeSortedArray,
|
|
5
3
|
findInSortedArray,
|
|
6
4
|
findIndexInSortedArray,
|
|
7
5
|
insertIntoSortedArray,
|
|
8
|
-
keyCmp,
|
|
9
6
|
merge,
|
|
10
7
|
removeAnyOf,
|
|
11
8
|
removeFromSortedArray,
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
} from '@aztec/foundation/array';
|
|
10
|
+
|
|
11
|
+
import { type Batch, Database, LMDBMessageType } from './message.js';
|
|
12
|
+
import { ReadTransaction } from './read_transaction.js';
|
|
13
|
+
import { keyCmp, singleKeyCmp } from './utils.js';
|
|
14
14
|
|
|
15
15
|
export class WriteTransaction extends ReadTransaction {
|
|
16
16
|
// exposed for tests
|
|
@@ -44,7 +44,7 @@ export class WriteTransaction extends ReadTransaction {
|
|
|
44
44
|
remove(key: Uint8Array): Promise<void> {
|
|
45
45
|
const removeEntryIndex = findIndexInSortedArray(this.dataBatch.removeEntries, key, singleKeyCmp);
|
|
46
46
|
if (removeEntryIndex === -1) {
|
|
47
|
-
this.dataBatch.removeEntries
|
|
47
|
+
insertIntoSortedArray(this.dataBatch.removeEntries, [key, null], keyCmp);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
const addEntryIndex = findIndexInSortedArray(this.dataBatch.addEntries, key, singleKeyCmp);
|
|
@@ -218,8 +218,9 @@ export class WriteTransaction extends ReadTransaction {
|
|
|
218
218
|
): AsyncIterable<[Uint8Array, T]> {
|
|
219
219
|
this.assertIsOpen();
|
|
220
220
|
|
|
221
|
-
//
|
|
221
|
+
// Snapshot both add and remove entries at the start of iteration to ensure consistency
|
|
222
222
|
const uncommittedEntries = [...batch.addEntries];
|
|
223
|
+
const removeEntries = [...batch.removeEntries];
|
|
223
224
|
// used to check we're in the right order when comparing between a key and uncommittedEntries
|
|
224
225
|
let cmpDirection = -1;
|
|
225
226
|
if (reverse) {
|
|
@@ -262,7 +263,7 @@ export class WriteTransaction extends ReadTransaction {
|
|
|
262
263
|
break;
|
|
263
264
|
}
|
|
264
265
|
|
|
265
|
-
const toRemove = findInSortedArray(
|
|
266
|
+
const toRemove = findInSortedArray(removeEntries, key, singleKeyCmp);
|
|
266
267
|
|
|
267
268
|
// at this point we've either exhausted all uncommitted entries,
|
|
268
269
|
// we reached a key strictly greater/smaller than `key`
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Encoder } from 'msgpackr';
|
|
2
|
+
import { hash } from 'ohash';
|
|
3
|
+
import { toBufferKey } from 'ordered-binary';
|
|
4
|
+
|
|
5
|
+
import type { AztecAsyncArray } from '../interfaces/array.js';
|
|
6
|
+
import type { Value } from '../interfaces/common.js';
|
|
7
|
+
import type { AztecSQLiteOPFSStore } from './store.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Persistent array backed by SQLite. Entries share a common `key` (the array name)
|
|
11
|
+
* and are ordered by `key_count`, which doubles as the 1-indexed slot number.
|
|
12
|
+
*/
|
|
13
|
+
export class SQLiteOPFSAztecArray<T extends Value> implements AztecAsyncArray<T> {
|
|
14
|
+
readonly #name: string;
|
|
15
|
+
readonly #container: string;
|
|
16
|
+
readonly #encoder = new Encoder();
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
private readonly store: AztecSQLiteOPFSStore,
|
|
20
|
+
name: string,
|
|
21
|
+
) {
|
|
22
|
+
this.#name = name;
|
|
23
|
+
this.#container = `array:${name}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async lengthAsync(): Promise<number> {
|
|
27
|
+
const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ? AND key = ?', [
|
|
28
|
+
this.#container,
|
|
29
|
+
this.#encodedKey(),
|
|
30
|
+
]);
|
|
31
|
+
return Number(rows[0]?.[0] ?? 0);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async push(...vals: T[]): Promise<number> {
|
|
35
|
+
if (vals.length === 0) {
|
|
36
|
+
return this.lengthAsync();
|
|
37
|
+
}
|
|
38
|
+
return await this.store.transactionAsync(async () => {
|
|
39
|
+
let length = await this.lengthAsync();
|
|
40
|
+
for (const val of vals) {
|
|
41
|
+
await this.store.runAsync(
|
|
42
|
+
`INSERT INTO data (slot, container, key, key_count, hash, value)
|
|
43
|
+
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
44
|
+
[this.#slot(length), this.#container, this.#encodedKey(), length + 1, hash(val), this.#encoder.pack(val)],
|
|
45
|
+
);
|
|
46
|
+
length += 1;
|
|
47
|
+
}
|
|
48
|
+
return length;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async pop(): Promise<T | undefined> {
|
|
53
|
+
return await this.store.transactionAsync(async () => {
|
|
54
|
+
const length = await this.lengthAsync();
|
|
55
|
+
if (length === 0) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const slot = this.#slot(length - 1);
|
|
59
|
+
const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [slot]);
|
|
60
|
+
await this.store.runAsync('DELETE FROM data WHERE slot = ?', [slot]);
|
|
61
|
+
const raw = rows[0]?.[0];
|
|
62
|
+
return raw instanceof Uint8Array ? (this.#encoder.unpack(raw) as T) : undefined;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async atAsync(index: number): Promise<T | undefined> {
|
|
67
|
+
const length = await this.lengthAsync();
|
|
68
|
+
const resolved = index < 0 ? length + index : index;
|
|
69
|
+
if (resolved < 0 || resolved >= length) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.#slot(resolved)]);
|
|
73
|
+
const raw = rows[0]?.[0];
|
|
74
|
+
return raw instanceof Uint8Array ? (this.#encoder.unpack(raw) as T) : undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async setAt(index: number, val: T): Promise<boolean> {
|
|
78
|
+
return await this.store.transactionAsync(async () => {
|
|
79
|
+
const length = await this.lengthAsync();
|
|
80
|
+
const resolved = index < 0 ? length + index : index;
|
|
81
|
+
if (resolved < 0 || resolved >= length) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
await this.store.runAsync(
|
|
85
|
+
`INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
|
|
86
|
+
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
87
|
+
[this.#slot(resolved), this.#container, this.#encodedKey(), resolved + 1, hash(val), this.#encoder.pack(val)],
|
|
88
|
+
);
|
|
89
|
+
return true;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async *entriesAsync(): AsyncIterableIterator<[number, T]> {
|
|
94
|
+
const rows = await this.store.allAsync(
|
|
95
|
+
'SELECT key_count, value FROM data WHERE container = ? AND key = ? ORDER BY key_count ASC',
|
|
96
|
+
[this.#container, this.#encodedKey()],
|
|
97
|
+
);
|
|
98
|
+
for (const row of rows) {
|
|
99
|
+
const keyCount = Number(row[0]);
|
|
100
|
+
const raw = row[1];
|
|
101
|
+
if (raw instanceof Uint8Array) {
|
|
102
|
+
yield [keyCount - 1, this.#encoder.unpack(raw) as T];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async *valuesAsync(): AsyncIterableIterator<T> {
|
|
108
|
+
for await (const [, val] of this.entriesAsync()) {
|
|
109
|
+
yield val;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<T> {
|
|
114
|
+
return this.valuesAsync();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
#encodedKey(): Buffer {
|
|
118
|
+
return toBufferKey([this.#name]);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#slot(index: number): string {
|
|
122
|
+
return `array:${this.#name}:slot:${index}`;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
2
|
+
import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
|
|
3
|
+
|
|
4
|
+
import { initStoreForRollupAndSchemaVersion } from '../utils.js';
|
|
5
|
+
import { AztecSQLiteOPFSStore } from './store.js';
|
|
6
|
+
|
|
7
|
+
export { AztecSQLiteOPFSStore } from './store.js';
|
|
8
|
+
|
|
9
|
+
export async function createStore(
|
|
10
|
+
name: string,
|
|
11
|
+
config: DataStoreConfig,
|
|
12
|
+
schemaVersion: number | undefined = undefined,
|
|
13
|
+
log: Logger = createLogger('kv-store'),
|
|
14
|
+
) {
|
|
15
|
+
const { dataDirectory } = config;
|
|
16
|
+
log.info(
|
|
17
|
+
dataDirectory
|
|
18
|
+
? `Creating ${name} SQLite-OPFS data store with map size ${config.dataStoreMapSizeKb} KB`
|
|
19
|
+
: `Creating ${name} ephemeral SQLite-OPFS data store with map size ${config.dataStoreMapSizeKb} KB`,
|
|
20
|
+
);
|
|
21
|
+
const store = await AztecSQLiteOPFSStore.open(createLogger('kv-store:sqlite-opfs'), name, false);
|
|
22
|
+
return initStoreForRollupAndSchemaVersion(store, schemaVersion, config.l1Contracts?.rollupAddress, log);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function openTmpStore(ephemeral: boolean = false): Promise<AztecSQLiteOPFSStore> {
|
|
26
|
+
return AztecSQLiteOPFSStore.open(createLogger('kv-store:sqlite-opfs'), undefined, ephemeral);
|
|
27
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
import { Encoder } from 'msgpackr';
|
|
3
|
+
import { hash } from 'ohash';
|
|
4
|
+
import { fromBufferKey, toBufferKey } from 'ordered-binary';
|
|
5
|
+
|
|
6
|
+
import type { Key, Range, Value } from '../interfaces/common.js';
|
|
7
|
+
import type { AztecAsyncMap } from '../interfaces/map.js';
|
|
8
|
+
import type { SqlValue } from './messages.js';
|
|
9
|
+
import type { AztecSQLiteOPFSStore } from './store.js';
|
|
10
|
+
|
|
11
|
+
/** A map backed by SQLite in OPFS. Mirrors `IndexedDBAztecMap`. */
|
|
12
|
+
export class SQLiteOPFSAztecMap<K extends Key, V extends Value> implements AztecAsyncMap<K, V> {
|
|
13
|
+
protected readonly name: string;
|
|
14
|
+
protected readonly container: string;
|
|
15
|
+
protected readonly encoder = new Encoder();
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
protected readonly store: AztecSQLiteOPFSStore,
|
|
19
|
+
mapName: string,
|
|
20
|
+
) {
|
|
21
|
+
this.name = mapName;
|
|
22
|
+
this.container = `map:${mapName}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async getAsync(key: K): Promise<V | undefined> {
|
|
26
|
+
const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.slot(key)]);
|
|
27
|
+
if (rows.length === 0) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
const raw = rows[0][0];
|
|
31
|
+
return raw == null ? undefined : this.decodeValue(raw);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async hasAsync(key: K): Promise<boolean> {
|
|
35
|
+
const rows = await this.store.allAsync('SELECT 1 FROM data WHERE slot = ? LIMIT 1', [this.slot(key)]);
|
|
36
|
+
return rows.length > 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async sizeAsync(): Promise<number> {
|
|
40
|
+
const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ?', [this.container]);
|
|
41
|
+
return Number(rows[0]?.[0] ?? 0);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async set(key: K, val: V): Promise<void> {
|
|
45
|
+
await this.store.runAsync(
|
|
46
|
+
`INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
|
|
47
|
+
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
48
|
+
[this.slot(key), this.container, this.encodedKey(key), 1, hash(val), this.encoder.pack(val)],
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async setMany(entries: { key: K; value: V }[]): Promise<void> {
|
|
53
|
+
if (entries.length === 0) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
await this.store.transactionAsync(async () => {
|
|
57
|
+
for (const { key, value } of entries) {
|
|
58
|
+
await this.set(key, value);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
swap(_key: K, _fn: (val: V | undefined) => V): Promise<void> {
|
|
64
|
+
throw new Error('Not implemented');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async setIfNotExists(key: K, val: V): Promise<boolean> {
|
|
68
|
+
return await this.store.transactionAsync(async () => {
|
|
69
|
+
if (await this.hasAsync(key)) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
await this.set(key, val);
|
|
73
|
+
return true;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async delete(key: K): Promise<void> {
|
|
78
|
+
await this.store.runAsync('DELETE FROM data WHERE slot = ?', [this.slot(key)]);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<[K, V]> {
|
|
82
|
+
const rows = await this.rangeQuery(range);
|
|
83
|
+
for (const row of rows) {
|
|
84
|
+
const [_slot, keyBlob, value] = row;
|
|
85
|
+
if (keyBlob == null || value == null) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
yield [this.decodeKey(keyBlob), this.decodeValue(value)];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async *valuesAsync(range: Range<K> = {}): AsyncIterableIterator<V> {
|
|
93
|
+
for await (const [, value] of this.entriesAsync(range)) {
|
|
94
|
+
yield value;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async *keysAsync(range: Range<K> = {}): AsyncIterableIterator<K> {
|
|
99
|
+
for await (const [key] of this.entriesAsync(range)) {
|
|
100
|
+
yield key;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
protected async rangeQuery(range: Range<K>): Promise<Array<[string, Uint8Array, Uint8Array | null]>> {
|
|
105
|
+
// Inclusivity flips with direction to match the IndexedDB backend:
|
|
106
|
+
// forward: [start, end) reverse: (start, end]
|
|
107
|
+
// That asymmetry is load-bearing — tests pin the exact inclusivity at boundaries.
|
|
108
|
+
const reverse = !!range.reverse;
|
|
109
|
+
const parts: string[] = ['container = ?'];
|
|
110
|
+
const bind: SqlValue[] = [this.container];
|
|
111
|
+
if (range.start !== undefined) {
|
|
112
|
+
parts.push(reverse ? 'key > ?' : 'key >= ?');
|
|
113
|
+
bind.push(this.encodedKey(range.start));
|
|
114
|
+
}
|
|
115
|
+
if (range.end !== undefined) {
|
|
116
|
+
parts.push(reverse ? 'key <= ?' : 'key < ?');
|
|
117
|
+
bind.push(this.encodedKey(range.end));
|
|
118
|
+
}
|
|
119
|
+
const order = reverse ? 'DESC' : 'ASC';
|
|
120
|
+
let sql = `SELECT slot, key, value FROM data WHERE ${parts.join(' AND ')} ORDER BY key ${order}, key_count ${order}`;
|
|
121
|
+
if (range.limit !== undefined) {
|
|
122
|
+
sql += ' LIMIT ?';
|
|
123
|
+
bind.push(range.limit);
|
|
124
|
+
}
|
|
125
|
+
const rows = await this.store.allAsync(sql, bind);
|
|
126
|
+
return rows.map(r => [String(r[0]), r[1] as Uint8Array, r[2] as Uint8Array | null]);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
protected decodeValue(val: SqlValue): V {
|
|
130
|
+
if (!(val instanceof Uint8Array)) {
|
|
131
|
+
return val as V;
|
|
132
|
+
}
|
|
133
|
+
const unpacked = this.encoder.unpack(val);
|
|
134
|
+
// msgpackr returns plain Uint8Array in browsers for packed Buffers. Callers that
|
|
135
|
+
// stored Buffers (walletDB uses Buffer.from(...).toString('utf8') round-trips)
|
|
136
|
+
// rely on Buffer-flavored behavior — re-wrap at the storage boundary, mirroring
|
|
137
|
+
// IndexedDBAztecMap.restoreBuffers.
|
|
138
|
+
if (unpacked instanceof Uint8Array && !Buffer.isBuffer(unpacked)) {
|
|
139
|
+
return Buffer.from(unpacked) as V;
|
|
140
|
+
}
|
|
141
|
+
return unpacked as V;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
protected decodeKey(raw: Uint8Array): K {
|
|
145
|
+
const parsed = fromBufferKey(Buffer.from(raw));
|
|
146
|
+
if (Array.isArray(parsed)) {
|
|
147
|
+
return (parsed.length > 1 ? parsed : parsed[0]) as K;
|
|
148
|
+
}
|
|
149
|
+
return parsed as K;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
protected encodedKey(key: K): Buffer {
|
|
153
|
+
return toBufferKey(this.normalizeKey(key));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
protected normalizeKey(key: K): (string | number | Uint8Array)[] {
|
|
157
|
+
return Array.isArray(key) ? key : [key];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
protected slot(key: K, index: number = 0): string {
|
|
161
|
+
return `map:${this.name}:slot:${this.normalizeKey(key)}:${index}`;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RPC protocol between the main thread and the SQLite worker.
|
|
3
|
+
* All requests carry a unique `id`; responses echo the same id so the main thread
|
|
4
|
+
* can resolve the right pending promise.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Matches `@sqlite.org/sqlite-wasm`'s internal SqlValue type. Boolean is not a native SQLite type. */
|
|
8
|
+
export type SqlValue = string | number | bigint | null | Uint8Array;
|
|
9
|
+
|
|
10
|
+
/** A row returned in 'array' rowMode — one value per column, in select order. */
|
|
11
|
+
export type ResultRow = SqlValue[];
|
|
12
|
+
|
|
13
|
+
export type WorkerRequest =
|
|
14
|
+
| { type: 'init'; id: number; dbName: string; ephemeral: boolean; poolDirectory?: string }
|
|
15
|
+
| { type: 'close'; id: number }
|
|
16
|
+
| { type: 'deleteDb'; id: number; dbName: string }
|
|
17
|
+
| { type: 'run'; id: number; sql: string; bind?: SqlValue[] }
|
|
18
|
+
| { type: 'all'; id: number; sql: string; bind?: SqlValue[] }
|
|
19
|
+
| { type: 'export'; id: number }
|
|
20
|
+
| { type: 'begin'; id: number }
|
|
21
|
+
| { type: 'commit'; id: number }
|
|
22
|
+
| { type: 'rollback'; id: number };
|
|
23
|
+
|
|
24
|
+
export type WorkerResponse =
|
|
25
|
+
| { type: 'ok'; id: number; rows?: ResultRow[]; changes?: number; bytes?: Uint8Array }
|
|
26
|
+
| { type: 'err'; id: number; message: string };
|
|
27
|
+
|
|
28
|
+
export type WorkerRequestType = WorkerRequest['type'];
|