@aztec/kv-store 0.0.1-commit.c31f2472 → 0.0.1-commit.c52d6e7
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/README.md +10 -1
- 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/deprecated/indexeddb/array.d.ts +22 -0
- package/dest/deprecated/indexeddb/array.d.ts.map +1 -0
- package/dest/{indexeddb → deprecated/indexeddb}/array.js +18 -6
- package/dest/deprecated/indexeddb/index.d.ts +7 -0
- package/dest/deprecated/indexeddb/index.d.ts.map +1 -0
- package/dest/deprecated/indexeddb/index.js +8 -0
- package/dest/deprecated/indexeddb/map.d.ts +38 -0
- package/dest/deprecated/indexeddb/map.d.ts.map +1 -0
- package/dest/{indexeddb → deprecated/indexeddb}/map.js +23 -13
- package/dest/deprecated/indexeddb/multi_map.d.ts +14 -0
- package/dest/deprecated/indexeddb/multi_map.d.ts.map +1 -0
- package/dest/{indexeddb → deprecated/indexeddb}/multi_map.js +16 -1
- package/dest/deprecated/indexeddb/set.d.ts +17 -0
- package/dest/deprecated/indexeddb/set.d.ts.map +1 -0
- package/dest/deprecated/indexeddb/singleton.d.ts +17 -0
- package/dest/deprecated/indexeddb/singleton.d.ts.map +1 -0
- package/dest/{indexeddb → deprecated/indexeddb}/singleton.js +3 -1
- package/dest/deprecated/indexeddb/store.d.ts +98 -0
- package/dest/deprecated/indexeddb/store.d.ts.map +1 -0
- package/dest/{indexeddb → deprecated/indexeddb}/store.js +8 -4
- package/dest/interfaces/array_test_suite.d.ts +1 -1
- package/dest/interfaces/array_test_suite.d.ts.map +1 -1
- package/dest/interfaces/array_test_suite.js +33 -34
- package/dest/interfaces/index.d.ts +2 -2
- package/dest/interfaces/index.d.ts.map +1 -1
- 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 +74 -29
- 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 +84 -60
- package/dest/interfaces/set_test_suite.d.ts +1 -1
- package/dest/interfaces/set_test_suite.d.ts.map +1 -1
- package/dest/interfaces/set_test_suite.js +13 -14
- package/dest/interfaces/singleton_test_suite.d.ts +1 -1
- package/dest/interfaces/singleton_test_suite.d.ts.map +1 -1
- package/dest/interfaces/singleton_test_suite.js +6 -7
- package/dest/lmdb/index.d.ts +3 -3
- package/dest/lmdb/index.d.ts.map +1 -1
- package/dest/lmdb/index.js +4 -4
- package/dest/lmdb/store.d.ts +3 -3
- package/dest/lmdb/store.d.ts.map +1 -1
- package/dest/lmdb/store.js +12 -8
- package/dest/lmdb-v2/factory.d.ts +30 -4
- package/dest/lmdb-v2/factory.d.ts.map +1 -1
- package/dest/lmdb-v2/factory.js +63 -11
- package/dest/lmdb-v2/read_transaction.js +21 -19
- package/dest/lmdb-v2/store.d.ts +2 -2
- package/dest/lmdb-v2/store.d.ts.map +1 -1
- package/dest/lmdb-v2/store.js +4 -4
- 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/errors.d.ts +54 -0
- package/dest/sqlite-opfs/errors.d.ts.map +1 -0
- package/dest/sqlite-opfs/errors.js +77 -0
- package/dest/sqlite-opfs/index.d.ts +14 -0
- package/dest/sqlite-opfs/index.d.ts.map +1 -0
- package/dest/sqlite-opfs/index.js +16 -0
- package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts +32 -0
- package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts.map +1 -0
- package/dest/sqlite-opfs/internal/ordered-binary-browser.js +448 -0
- package/dest/sqlite-opfs/manage.d.ts +22 -0
- package/dest/sqlite-opfs/manage.d.ts.map +1 -0
- package/dest/sqlite-opfs/manage.js +38 -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 +66 -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/pool_integrity.d.ts +20 -0
- package/dest/sqlite-opfs/pool_integrity.d.ts.map +1 -0
- package/dest/sqlite-opfs/pool_integrity.js +282 -0
- package/dest/sqlite-opfs/pool_lock.d.ts +8 -0
- package/dest/sqlite-opfs/pool_lock.d.ts.map +1 -0
- package/dest/sqlite-opfs/pool_lock.js +47 -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 +84 -0
- package/dest/sqlite-opfs/store.d.ts.map +1 -0
- package/dest/sqlite-opfs/store.js +317 -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 +275 -0
- package/dest/stores/l2_tips_store.d.ts +7 -13
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +24 -43
- package/dest/utils.d.ts +9 -6
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +51 -16
- package/package.json +30 -26
- package/src/bench/shared_map_bench.ts +111 -0
- package/src/{indexeddb → deprecated/indexeddb}/array.ts +6 -6
- package/src/deprecated/indexeddb/index.ts +12 -0
- package/src/{indexeddb → deprecated/indexeddb}/map.ts +26 -13
- package/src/{indexeddb → deprecated/indexeddb}/multi_map.ts +17 -3
- package/src/{indexeddb → deprecated/indexeddb}/set.ts +2 -2
- package/src/{indexeddb → deprecated/indexeddb}/singleton.ts +3 -3
- package/src/{indexeddb → deprecated/indexeddb}/store.ts +23 -15
- package/src/interfaces/array_test_suite.ts +33 -35
- package/src/interfaces/index.ts +1 -1
- package/src/interfaces/map_test_suite.ts +56 -30
- package/src/interfaces/multi_map_test_suite.ts +88 -58
- package/src/interfaces/set_test_suite.ts +13 -15
- package/src/interfaces/singleton_test_suite.ts +6 -8
- package/src/lmdb/index.ts +10 -5
- package/src/lmdb/store.ts +12 -8
- package/src/lmdb-v2/factory.ts +86 -10
- package/src/lmdb-v2/read_transaction.ts +23 -23
- package/src/lmdb-v2/store.ts +4 -2
- package/src/sqlite-opfs/array.ts +124 -0
- package/src/sqlite-opfs/errors.ts +94 -0
- package/src/sqlite-opfs/index.ts +27 -0
- package/src/sqlite-opfs/internal/ordered-binary-browser.js +465 -0
- package/src/sqlite-opfs/manage.ts +46 -0
- package/src/sqlite-opfs/map.ts +163 -0
- package/src/sqlite-opfs/messages.ts +39 -0
- package/src/sqlite-opfs/multi_map.ts +74 -0
- package/src/sqlite-opfs/pool_integrity.ts +327 -0
- package/src/sqlite-opfs/pool_lock.ts +62 -0
- package/src/sqlite-opfs/set.ts +29 -0
- package/src/sqlite-opfs/singleton.ts +48 -0
- package/src/sqlite-opfs/store.ts +330 -0
- package/src/sqlite-opfs/worker.ts +259 -0
- package/src/stores/l2_tips_store.ts +20 -41
- 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/dest/indexeddb/array.d.ts +0 -22
- package/dest/indexeddb/array.d.ts.map +0 -1
- package/dest/indexeddb/index.d.ts +0 -7
- package/dest/indexeddb/index.d.ts.map +0 -1
- package/dest/indexeddb/index.js +0 -19
- package/dest/indexeddb/map.d.ts +0 -32
- package/dest/indexeddb/map.d.ts.map +0 -1
- package/dest/indexeddb/multi_map.d.ts +0 -13
- package/dest/indexeddb/multi_map.d.ts.map +0 -1
- package/dest/indexeddb/set.d.ts +0 -17
- package/dest/indexeddb/set.d.ts.map +0 -1
- package/dest/indexeddb/singleton.d.ts +0 -17
- package/dest/indexeddb/singleton.d.ts.map +0 -1
- package/dest/indexeddb/store.d.ts +0 -96
- package/dest/indexeddb/store.d.ts.map +0 -1
- package/src/config.ts +0 -36
- package/src/indexeddb/index.ts +0 -29
- /package/dest/{indexeddb → deprecated/indexeddb}/set.js +0 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { Logger } from '@aztec/foundation/log';
|
|
2
|
+
import { Timer } from '@aztec/foundation/timer';
|
|
3
|
+
|
|
4
|
+
import type { Key } from '../interfaces/common.js';
|
|
5
|
+
import type { AztecAsyncMap } from '../interfaces/map.js';
|
|
6
|
+
import type { AztecAsyncKVStore } from '../interfaces/store.js';
|
|
7
|
+
|
|
8
|
+
/** One benchmark measurement. */
|
|
9
|
+
export type BenchResult = {
|
|
10
|
+
/** Benchmark name (includes the backend prefix for disambiguation). */
|
|
11
|
+
name: string;
|
|
12
|
+
value: number;
|
|
13
|
+
unit: 'ms' | 'us';
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type BenchReporter = (results: BenchResult[]) => void | Promise<void>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Runs the standard Map benchmark suite against any `AztecAsyncKVStore` backend,
|
|
20
|
+
* populates `results`, and calls `reporter` in `afterAll`.
|
|
21
|
+
*
|
|
22
|
+
* Kept free of Node-only deps (`fs`, `path`) so the same runner works under
|
|
23
|
+
* vitest-browser for IndexedDB and SQLite-OPFS.
|
|
24
|
+
*/
|
|
25
|
+
export function describeAztecMapBench(
|
|
26
|
+
backendPrefix: string,
|
|
27
|
+
getStore: () => Promise<AztecAsyncKVStore>,
|
|
28
|
+
logger: Logger,
|
|
29
|
+
reporter: BenchReporter,
|
|
30
|
+
) {
|
|
31
|
+
describe(`${backendPrefix} Map benchmarks`, () => {
|
|
32
|
+
let store: AztecAsyncKVStore;
|
|
33
|
+
let map: AztecAsyncMap<Key, string>;
|
|
34
|
+
|
|
35
|
+
const results: BenchResult[] = [];
|
|
36
|
+
|
|
37
|
+
const generateKeyValuePairs = (count: number, offset = 0) => {
|
|
38
|
+
const keys = Array.from({ length: count }, (_, i) => `key-${i + offset}`);
|
|
39
|
+
const values = Array.from({ length: count }, (_, i) => `value-${i + offset}`);
|
|
40
|
+
return keys.map((key, i) => ({ key, value: values[i] }));
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const record = (name: string, value: number, unit: BenchResult['unit']) => {
|
|
44
|
+
results.push({ name: `${backendPrefix}/Map/${name}`, value, unit });
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
beforeEach(async () => {
|
|
48
|
+
store = await getStore();
|
|
49
|
+
map = store.openMap<Key, string>('test');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterEach(async () => {
|
|
53
|
+
await store.delete();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
afterAll(async () => {
|
|
57
|
+
const pretty = results.map(r => `${r.name}: ${r.value.toFixed(2)} ${r.unit}`).join('\n');
|
|
58
|
+
logger.info(`\n${pretty}\n`);
|
|
59
|
+
await reporter(results);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('adds individual values', async () => {
|
|
63
|
+
const pairs = generateKeyValuePairs(1000);
|
|
64
|
+
const timer = new Timer();
|
|
65
|
+
for (const pair of pairs) {
|
|
66
|
+
await map.set(pair.key, pair.value);
|
|
67
|
+
}
|
|
68
|
+
record('Individual insertion', timer.ms() / pairs.length, 'ms');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('adds batched values', async () => {
|
|
72
|
+
const batches = Array.from({ length: 100 }, (_, i) => generateKeyValuePairs(1000, i * 1000));
|
|
73
|
+
const timer = new Timer();
|
|
74
|
+
for (const batch of batches) {
|
|
75
|
+
await map.setMany(batch);
|
|
76
|
+
}
|
|
77
|
+
record(`Batch insertion of ${batches[0].length} items`, timer.ms() / batches.length, 'ms');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('reads individual values', async () => {
|
|
81
|
+
const pairs = generateKeyValuePairs(10000);
|
|
82
|
+
await map.setMany(pairs);
|
|
83
|
+
const timer = new Timer();
|
|
84
|
+
for (const pair of pairs) {
|
|
85
|
+
await map.getAsync(pair.key);
|
|
86
|
+
}
|
|
87
|
+
record('Individual read', (timer.ms() * 1000) / pairs.length, 'us');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('reads via a cursor', async () => {
|
|
91
|
+
const pairs = generateKeyValuePairs(10000);
|
|
92
|
+
await map.setMany(pairs);
|
|
93
|
+
const timer = new Timer();
|
|
94
|
+
for await (const _ of map.entriesAsync()) {
|
|
95
|
+
// consume
|
|
96
|
+
}
|
|
97
|
+
record(`Iterator per item read of ${pairs.length} items`, (timer.ms() * 1000) / pairs.length, 'us');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('reads the size of the map', async () => {
|
|
101
|
+
const numIterations = 1000;
|
|
102
|
+
const pairs = generateKeyValuePairs(10000);
|
|
103
|
+
await map.setMany(pairs);
|
|
104
|
+
const timer = new Timer();
|
|
105
|
+
for (let i = 0; i < numIterations; i++) {
|
|
106
|
+
await map.sizeAsync();
|
|
107
|
+
}
|
|
108
|
+
record(`Read size of ${pairs.length} items`, (timer.ms() * 1000) / numIterations, 'us');
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { IDBPDatabase, IDBPObjectStore } from 'idb';
|
|
2
2
|
import { hash } from 'ohash';
|
|
3
3
|
|
|
4
|
-
import type { AztecAsyncArray } from '
|
|
5
|
-
import type { Value } from '
|
|
4
|
+
import type { AztecAsyncArray } from '../../interfaces/array.js';
|
|
5
|
+
import type { Value } from '../../interfaces/common.js';
|
|
6
6
|
import type { AztecIDBSchema } from './store.js';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -32,7 +32,7 @@ export class IndexedDBAztecArray<T extends Value> implements AztecAsyncArray<T>
|
|
|
32
32
|
return (
|
|
33
33
|
(await this.db
|
|
34
34
|
.index('key')
|
|
35
|
-
.count(IDBKeyRange.bound([this.#container, this.#name], [this.#container, this.#name]))) ?? 0
|
|
35
|
+
.count(IDBKeyRange.bound([this.#container, [this.#name]], [this.#container, [this.#name]]))) ?? 0
|
|
36
36
|
);
|
|
37
37
|
}
|
|
38
38
|
|
|
@@ -43,7 +43,7 @@ export class IndexedDBAztecArray<T extends Value> implements AztecAsyncArray<T>
|
|
|
43
43
|
value: val,
|
|
44
44
|
hash: hash(val),
|
|
45
45
|
container: this.#container,
|
|
46
|
-
key: this.#name,
|
|
46
|
+
key: [this.#name],
|
|
47
47
|
keyCount: length + 1,
|
|
48
48
|
slot: this.#slot(length),
|
|
49
49
|
});
|
|
@@ -91,7 +91,7 @@ export class IndexedDBAztecArray<T extends Value> implements AztecAsyncArray<T>
|
|
|
91
91
|
value: val,
|
|
92
92
|
hash: hash(val),
|
|
93
93
|
container: this.#container,
|
|
94
|
-
key: this.#name,
|
|
94
|
+
key: [this.#name],
|
|
95
95
|
keyCount: index + 1,
|
|
96
96
|
slot: this.#slot(index),
|
|
97
97
|
});
|
|
@@ -100,7 +100,7 @@ export class IndexedDBAztecArray<T extends Value> implements AztecAsyncArray<T>
|
|
|
100
100
|
|
|
101
101
|
async *entriesAsync(): AsyncIterableIterator<[number, T]> {
|
|
102
102
|
const index = this.db.index('key');
|
|
103
|
-
const rangeQuery = IDBKeyRange.bound([this.#container, this.#name], [this.#container, this.#name]);
|
|
103
|
+
const rangeQuery = IDBKeyRange.bound([this.#container, [this.#name]], [this.#container, [this.#name]]);
|
|
104
104
|
for await (const cursor of index.iterate(rangeQuery)) {
|
|
105
105
|
yield [cursor.value.keyCount - 1, cursor.value.value] as [number, T];
|
|
106
106
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
+
|
|
3
|
+
import { AztecIndexedDBStore } from './store.js';
|
|
4
|
+
|
|
5
|
+
export { AztecIndexedDBStore } from './store.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @deprecated The IndexedDB backend is being retired. Use `@aztec/kv-store/sqlite-opfs` instead.
|
|
9
|
+
*/
|
|
10
|
+
export function openTmpStore(ephemeral: boolean = false): Promise<AztecIndexedDBStore> {
|
|
11
|
+
return AztecIndexedDBStore.open(createLogger('kv-store:indexeddb'), undefined, ephemeral);
|
|
12
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { IDBPDatabase, IDBPObjectStore } from 'idb';
|
|
2
2
|
import { hash } from 'ohash';
|
|
3
3
|
|
|
4
|
-
import type { Key, Range, Value } from '
|
|
5
|
-
import type { AztecAsyncMap } from '
|
|
4
|
+
import type { Key, Range, Value } from '../../interfaces/common.js';
|
|
5
|
+
import type { AztecAsyncMap } from '../../interfaces/map.js';
|
|
6
6
|
import type { AztecIDBSchema } from './store.js';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -31,7 +31,7 @@ export class IndexedDBAztecMap<K extends Key, V extends Value> implements AztecA
|
|
|
31
31
|
|
|
32
32
|
async getAsync(key: K): Promise<V | undefined> {
|
|
33
33
|
const data = await this.db.get(this.slot(key));
|
|
34
|
-
return data
|
|
34
|
+
return data ? this.restoreBuffers(data.value as V) : undefined;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
async hasAsync(key: K): Promise<boolean> {
|
|
@@ -41,7 +41,7 @@ export class IndexedDBAztecMap<K extends Key, V extends Value> implements AztecA
|
|
|
41
41
|
|
|
42
42
|
async sizeAsync(): Promise<number> {
|
|
43
43
|
const index = this.db.index('key');
|
|
44
|
-
const rangeQuery = IDBKeyRange.bound([this.container
|
|
44
|
+
const rangeQuery = IDBKeyRange.bound([this.container], [this.container + '\uffff'], true, true);
|
|
45
45
|
return await index.count(rangeQuery);
|
|
46
46
|
}
|
|
47
47
|
|
|
@@ -80,9 +80,12 @@ export class IndexedDBAztecMap<K extends Key, V extends Value> implements AztecA
|
|
|
80
80
|
|
|
81
81
|
async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<[K, V]> {
|
|
82
82
|
const index = this.db.index('key');
|
|
83
|
+
const startKey = range.start ? this.normalizeKey(range.start) : [];
|
|
84
|
+
const endKey = range.end ? this.normalizeKey(range.end) : ['\uffff'];
|
|
85
|
+
|
|
83
86
|
const rangeQuery = IDBKeyRange.bound(
|
|
84
|
-
[this.container,
|
|
85
|
-
[this.container,
|
|
87
|
+
[this.container, startKey],
|
|
88
|
+
[this.container, endKey],
|
|
86
89
|
!!range.reverse,
|
|
87
90
|
!range.reverse,
|
|
88
91
|
);
|
|
@@ -91,7 +94,7 @@ export class IndexedDBAztecMap<K extends Key, V extends Value> implements AztecA
|
|
|
91
94
|
if (range.limit && count >= range.limit) {
|
|
92
95
|
return;
|
|
93
96
|
}
|
|
94
|
-
yield [this.#denormalizeKey(cursor.value.key), cursor.value.value] as [K, V];
|
|
97
|
+
yield [this.#denormalizeKey(cursor.value.key), this.restoreBuffers(cursor.value.value as V)] as [K, V];
|
|
95
98
|
count++;
|
|
96
99
|
}
|
|
97
100
|
}
|
|
@@ -108,14 +111,24 @@ export class IndexedDBAztecMap<K extends Key, V extends Value> implements AztecA
|
|
|
108
111
|
}
|
|
109
112
|
}
|
|
110
113
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
+
/**
|
|
115
|
+
* IndexedDB's structured clone downcasts Buffer (a Node.js subclass of Uint8Array) to plain Uint8Array.
|
|
116
|
+
* This breaks .toString() behavior: Buffer.toString() returns UTF-8, Uint8Array.toString() returns
|
|
117
|
+
* comma-separated decimal bytes. We restore Buffer identity on read so callers get consistent behavior.
|
|
118
|
+
*/
|
|
119
|
+
protected restoreBuffers(val: V): V {
|
|
120
|
+
if (val instanceof Uint8Array && !Buffer.isBuffer(val)) {
|
|
121
|
+
return Buffer.from(val) as V;
|
|
122
|
+
}
|
|
123
|
+
return val;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
#denormalizeKey(key: (string | number | Uint8Array)[]): K {
|
|
127
|
+
return (key.length > 1 ? key : key[0]) as K;
|
|
114
128
|
}
|
|
115
129
|
|
|
116
|
-
protected normalizeKey(key: K): string {
|
|
117
|
-
|
|
118
|
-
return (arrayKey as K[]).map((element: K) => (typeof element === 'number' ? `n_${element}` : element)).join(',');
|
|
130
|
+
protected normalizeKey(key: K): (string | number | Uint8Array)[] {
|
|
131
|
+
return Array.isArray(key) ? key : [key];
|
|
119
132
|
}
|
|
120
133
|
|
|
121
134
|
protected slot(key: K, index: number = 0): string {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { hash } from 'ohash';
|
|
2
2
|
|
|
3
|
-
import type { Key, Value } from '
|
|
4
|
-
import type { AztecAsyncMultiMap } from '
|
|
3
|
+
import type { Key, Value } from '../../interfaces/common.js';
|
|
4
|
+
import type { AztecAsyncMultiMap } from '../../interfaces/multi_map.js';
|
|
5
5
|
import { IndexedDBAztecMap } from './map.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -57,7 +57,7 @@ export class IndexedDBAztecMultiMap<K extends Key, V extends Value>
|
|
|
57
57
|
false,
|
|
58
58
|
);
|
|
59
59
|
for await (const cursor of index.iterate(rangeQuery)) {
|
|
60
|
-
yield cursor.value.value as V;
|
|
60
|
+
yield this.restoreBuffers(cursor.value.value as V);
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
|
|
@@ -88,4 +88,18 @@ export class IndexedDBAztecMultiMap<K extends Key, V extends Value>
|
|
|
88
88
|
await this.db.delete(fullKey);
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
+
|
|
92
|
+
override async delete(key: K): Promise<void> {
|
|
93
|
+
const index = this.db.index('keyCount');
|
|
94
|
+
const rangeQuery = IDBKeyRange.bound(
|
|
95
|
+
[this.container, this.normalizeKey(key), 0],
|
|
96
|
+
[this.container, this.normalizeKey(key), Number.MAX_SAFE_INTEGER],
|
|
97
|
+
false,
|
|
98
|
+
false,
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
for await (const cursor of index.iterate(rangeQuery)) {
|
|
102
|
+
await cursor.delete();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
91
105
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IDBPDatabase, IDBPObjectStore } from 'idb';
|
|
2
2
|
|
|
3
|
-
import type { Key, Range } from '
|
|
4
|
-
import type { AztecAsyncSet } from '
|
|
3
|
+
import type { Key, Range } from '../../interfaces/common.js';
|
|
4
|
+
import type { AztecAsyncSet } from '../../interfaces/set.js';
|
|
5
5
|
import { IndexedDBAztecMap } from './map.js';
|
|
6
6
|
import type { AztecIDBSchema } from './store.js';
|
|
7
7
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { IDBPDatabase, IDBPObjectStore } from 'idb';
|
|
2
2
|
import { hash } from 'ohash';
|
|
3
3
|
|
|
4
|
-
import type { Value } from '
|
|
5
|
-
import type { AztecAsyncSingleton } from '
|
|
4
|
+
import type { Value } from '../../interfaces/common.js';
|
|
5
|
+
import type { AztecAsyncSingleton } from '../../interfaces/singleton.js';
|
|
6
6
|
import type { AztecIDBSchema } from './store.js';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -37,7 +37,7 @@ export class IndexedDBAztecSingleton<T extends Value> implements AztecAsyncSingl
|
|
|
37
37
|
const result = await this.db.put({
|
|
38
38
|
container: this.#container,
|
|
39
39
|
slot: this.#slot,
|
|
40
|
-
key: this.#slot,
|
|
40
|
+
key: [this.#slot],
|
|
41
41
|
keyCount: 1,
|
|
42
42
|
value: val,
|
|
43
43
|
hash: hash(val),
|
|
@@ -3,14 +3,14 @@ import { SerialQueue } from '@aztec/foundation/queue';
|
|
|
3
3
|
|
|
4
4
|
import { type DBSchema, type IDBPDatabase, deleteDB, openDB } from 'idb';
|
|
5
5
|
|
|
6
|
-
import type { AztecAsyncArray } from '
|
|
7
|
-
import type { Key, StoreSize, Value } from '
|
|
8
|
-
import type { AztecAsyncCounter } from '
|
|
9
|
-
import type { AztecAsyncMap } from '
|
|
10
|
-
import type { AztecAsyncMultiMap } from '
|
|
11
|
-
import type { AztecAsyncSet } from '
|
|
12
|
-
import type { AztecAsyncSingleton } from '
|
|
13
|
-
import type { AztecAsyncKVStore } from '
|
|
6
|
+
import type { AztecAsyncArray } from '../../interfaces/array.js';
|
|
7
|
+
import type { Key, StoreSize, Value } from '../../interfaces/common.js';
|
|
8
|
+
import type { AztecAsyncCounter } from '../../interfaces/counter.js';
|
|
9
|
+
import type { AztecAsyncMap } from '../../interfaces/map.js';
|
|
10
|
+
import type { AztecAsyncMultiMap } from '../../interfaces/multi_map.js';
|
|
11
|
+
import type { AztecAsyncSet } from '../../interfaces/set.js';
|
|
12
|
+
import type { AztecAsyncSingleton } from '../../interfaces/singleton.js';
|
|
13
|
+
import type { AztecAsyncKVStore } from '../../interfaces/store.js';
|
|
14
14
|
import { IndexedDBAztecArray } from './array.js';
|
|
15
15
|
import { IndexedDBAztecMap } from './map.js';
|
|
16
16
|
import { IndexedDBAztecMultiMap } from './multi_map.js';
|
|
@@ -20,7 +20,7 @@ import { IndexedDBAztecSingleton } from './singleton.js';
|
|
|
20
20
|
export type StoredData<V extends Value> = {
|
|
21
21
|
value: V;
|
|
22
22
|
container: string;
|
|
23
|
-
key: string;
|
|
23
|
+
key: (string | number | Uint8Array)[];
|
|
24
24
|
keyCount: number;
|
|
25
25
|
slot: string;
|
|
26
26
|
hash: string;
|
|
@@ -30,14 +30,20 @@ export interface AztecIDBSchema extends DBSchema {
|
|
|
30
30
|
data: {
|
|
31
31
|
value: StoredData<any>;
|
|
32
32
|
key: string;
|
|
33
|
-
indexes: {
|
|
33
|
+
indexes: {
|
|
34
|
+
container: string;
|
|
35
|
+
key: any; // Runtime: (string | number | Uint8Array)[] - idb types don't support arrays
|
|
36
|
+
keyCount: number;
|
|
37
|
+
hash: string;
|
|
38
|
+
};
|
|
34
39
|
};
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
/**
|
|
38
43
|
* A key-value store backed by IndexedDB.
|
|
44
|
+
*
|
|
45
|
+
* @deprecated The IndexedDB backend is being retired. Use `@aztec/kv-store/sqlite-opfs` instead.
|
|
39
46
|
*/
|
|
40
|
-
|
|
41
47
|
export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
42
48
|
#rootDB: IDBPDatabase<AztecIDBSchema>;
|
|
43
49
|
#name: string;
|
|
@@ -187,18 +193,20 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
187
193
|
}
|
|
188
194
|
|
|
189
195
|
/** Deletes this store and removes the database */
|
|
190
|
-
delete() {
|
|
196
|
+
async delete() {
|
|
191
197
|
this.#containers.clear();
|
|
198
|
+
await this.#txQueue.end();
|
|
192
199
|
this.#rootDB.close();
|
|
193
|
-
|
|
200
|
+
await deleteDB(this.#name);
|
|
194
201
|
}
|
|
195
202
|
|
|
196
203
|
estimateSize(): Promise<StoreSize> {
|
|
197
204
|
return Promise.resolve({ mappingSize: 0, physicalFileSize: 0, actualSize: 0, numItems: 0 });
|
|
198
205
|
}
|
|
199
206
|
|
|
200
|
-
close(): Promise<void> {
|
|
201
|
-
|
|
207
|
+
async close(): Promise<void> {
|
|
208
|
+
await this.#txQueue.end();
|
|
209
|
+
this.#rootDB.close();
|
|
202
210
|
}
|
|
203
211
|
|
|
204
212
|
backupTo(_dstPath: string, _compact?: boolean): Promise<void> {
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { toArray } from '@aztec/foundation/iterable';
|
|
2
2
|
|
|
3
|
-
import { expect } from 'chai';
|
|
4
|
-
|
|
5
3
|
import type { AztecArray, AztecAsyncArray } from './array.js';
|
|
6
4
|
import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
|
|
7
5
|
import { isSyncStore } from './utils.js';
|
|
@@ -53,12 +51,12 @@ export function describeAztecArray(
|
|
|
53
51
|
await arr.push(2);
|
|
54
52
|
await arr.push(3);
|
|
55
53
|
|
|
56
|
-
expect(await length()).
|
|
54
|
+
expect(await length()).toBe(3);
|
|
57
55
|
|
|
58
|
-
expect(await arr.pop()).
|
|
59
|
-
expect(await arr.pop()).
|
|
60
|
-
expect(await arr.pop()).
|
|
61
|
-
expect(await arr.pop()).
|
|
56
|
+
expect(await arr.pop()).toBe(3);
|
|
57
|
+
expect(await arr.pop()).toBe(2);
|
|
58
|
+
expect(await arr.pop()).toBe(1);
|
|
59
|
+
expect(await arr.pop()).toBe(undefined);
|
|
62
60
|
});
|
|
63
61
|
|
|
64
62
|
it('should be able to get values by index', async () => {
|
|
@@ -66,14 +64,14 @@ export function describeAztecArray(
|
|
|
66
64
|
await arr.push(2);
|
|
67
65
|
await arr.push(3);
|
|
68
66
|
|
|
69
|
-
expect(await at(0)).
|
|
70
|
-
expect(await at(1)).
|
|
71
|
-
expect(await at(2)).
|
|
72
|
-
expect(await at(3)).
|
|
73
|
-
expect(await at(-1)).
|
|
74
|
-
expect(await at(-2)).
|
|
75
|
-
expect(await at(-3)).
|
|
76
|
-
expect(await at(-4)).
|
|
67
|
+
expect(await at(0)).toBe(1);
|
|
68
|
+
expect(await at(1)).toBe(2);
|
|
69
|
+
expect(await at(2)).toBe(3);
|
|
70
|
+
expect(await at(3)).toBe(undefined);
|
|
71
|
+
expect(await at(-1)).toBe(3);
|
|
72
|
+
expect(await at(-2)).toBe(2);
|
|
73
|
+
expect(await at(-3)).toBe(1);
|
|
74
|
+
expect(await at(-4)).toBe(undefined);
|
|
77
75
|
});
|
|
78
76
|
|
|
79
77
|
it('should be able to set values by index', async () => {
|
|
@@ -81,27 +79,27 @@ export function describeAztecArray(
|
|
|
81
79
|
await arr.push(2);
|
|
82
80
|
await arr.push(3);
|
|
83
81
|
|
|
84
|
-
expect(await arr.setAt(0, 4)).
|
|
85
|
-
expect(await arr.setAt(1, 5)).
|
|
86
|
-
expect(await arr.setAt(2, 6)).
|
|
82
|
+
expect(await arr.setAt(0, 4)).toBe(true);
|
|
83
|
+
expect(await arr.setAt(1, 5)).toBe(true);
|
|
84
|
+
expect(await arr.setAt(2, 6)).toBe(true);
|
|
87
85
|
|
|
88
|
-
expect(await arr.setAt(3, 7)).
|
|
86
|
+
expect(await arr.setAt(3, 7)).toBe(false);
|
|
89
87
|
|
|
90
|
-
expect(await at(0)).
|
|
91
|
-
expect(await at(1)).
|
|
92
|
-
expect(await at(2)).
|
|
93
|
-
expect(await at(3)).
|
|
88
|
+
expect(await at(0)).toBe(4);
|
|
89
|
+
expect(await at(1)).toBe(5);
|
|
90
|
+
expect(await at(2)).toBe(6);
|
|
91
|
+
expect(await at(3)).toBe(undefined);
|
|
94
92
|
|
|
95
|
-
expect(await arr.setAt(-1, 8)).
|
|
96
|
-
expect(await arr.setAt(-2, 9)).
|
|
97
|
-
expect(await arr.setAt(-3, 10)).
|
|
93
|
+
expect(await arr.setAt(-1, 8)).toBe(true);
|
|
94
|
+
expect(await arr.setAt(-2, 9)).toBe(true);
|
|
95
|
+
expect(await arr.setAt(-3, 10)).toBe(true);
|
|
98
96
|
|
|
99
|
-
expect(await arr.setAt(-4, 11)).
|
|
97
|
+
expect(await arr.setAt(-4, 11)).toBe(false);
|
|
100
98
|
|
|
101
|
-
expect(await at(-1)).
|
|
102
|
-
expect(await at(-2)).
|
|
103
|
-
expect(await at(-3)).
|
|
104
|
-
expect(await at(-4)).
|
|
99
|
+
expect(await at(-1)).toBe(8);
|
|
100
|
+
expect(await at(-2)).toBe(9);
|
|
101
|
+
expect(await at(-3)).toBe(10);
|
|
102
|
+
expect(await at(-4)).toBe(undefined);
|
|
105
103
|
});
|
|
106
104
|
|
|
107
105
|
it('should be able to iterate over values', async () => {
|
|
@@ -109,8 +107,8 @@ export function describeAztecArray(
|
|
|
109
107
|
await arr.push(2);
|
|
110
108
|
await arr.push(3);
|
|
111
109
|
|
|
112
|
-
expect(await values()).
|
|
113
|
-
expect(await entries()).
|
|
110
|
+
expect(await values()).toEqual([1, 2, 3]);
|
|
111
|
+
expect(await entries()).toEqual([
|
|
114
112
|
[0, 1],
|
|
115
113
|
[1, 2],
|
|
116
114
|
[2, 3],
|
|
@@ -123,8 +121,8 @@ export function describeAztecArray(
|
|
|
123
121
|
await arr.push(3);
|
|
124
122
|
|
|
125
123
|
const arr2 = store.openArray<number>('test');
|
|
126
|
-
expect(await length(arr2)).
|
|
127
|
-
expect(await values(arr2)).
|
|
124
|
+
expect(await length(arr2)).toBe(3);
|
|
125
|
+
expect(await values(arr2)).toEqual(await values());
|
|
128
126
|
});
|
|
129
127
|
});
|
|
130
128
|
}
|
package/src/interfaces/index.ts
CHANGED