@aztec/kv-store 0.0.1-commit.b655e406 → 0.0.1-commit.b9865e97
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 +8 -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/indexeddb/array.d.ts +1 -1
- package/dest/indexeddb/array.d.ts.map +1 -1
- 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 +3 -1
- package/dest/indexeddb/multi_map.d.ts.map +1 -1
- package/dest/indexeddb/multi_map.js +30 -1
- package/dest/indexeddb/set.d.ts +1 -1
- package/dest/indexeddb/set.d.ts.map +1 -1
- package/dest/indexeddb/singleton.d.ts +1 -1
- package/dest/indexeddb/singleton.d.ts.map +1 -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/array.d.ts +1 -1
- 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/common.d.ts +1 -1
- package/dest/interfaces/counter.d.ts +1 -1
- package/dest/interfaces/index.d.ts +2 -2
- package/dest/interfaces/index.d.ts.map +1 -1
- package/dest/interfaces/map.d.ts +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.d.ts +7 -1
- package/dest/interfaces/multi_map.d.ts.map +1 -1
- 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 +154 -36
- package/dest/interfaces/set.d.ts +1 -1
- 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.d.ts +1 -1
- 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/interfaces/store.d.ts +1 -1
- 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.d.ts +1 -1
- package/dest/lmdb/array.d.ts.map +1 -1
- package/dest/lmdb/array.js +4 -2
- package/dest/lmdb/counter.d.ts +1 -1
- package/dest/lmdb/counter.d.ts.map +1 -1
- 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/map.d.ts +1 -1
- package/dest/lmdb/map.d.ts.map +1 -1
- package/dest/lmdb/multi_map.d.ts +2 -1
- package/dest/lmdb/multi_map.d.ts.map +1 -1
- package/dest/lmdb/multi_map.js +15 -0
- package/dest/lmdb/set.d.ts +1 -1
- package/dest/lmdb/set.d.ts.map +1 -1
- package/dest/lmdb/singleton.d.ts +1 -1
- package/dest/lmdb/singleton.d.ts.map +1 -1
- package/dest/lmdb/store.d.ts +4 -5
- 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 +32 -10
- package/dest/lmdb-v2/factory.d.ts.map +1 -1
- package/dest/lmdb-v2/factory.js +71 -16
- package/dest/lmdb-v2/index.d.ts +1 -1
- 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/message.d.ts +1 -1
- package/dest/lmdb-v2/multi_map.d.ts +3 -2
- package/dest/lmdb-v2/multi_map.d.ts.map +1 -1
- package/dest/lmdb-v2/multi_map.js +11 -1
- package/dest/lmdb-v2/read_transaction.d.ts +1 -1
- package/dest/lmdb-v2/read_transaction.d.ts.map +1 -1
- package/dest/lmdb-v2/read_transaction.js +21 -19
- package/dest/lmdb-v2/set.d.ts +1 -1
- package/dest/lmdb-v2/set.d.ts.map +1 -1
- 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 +6 -28
- 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/errors.d.ts +27 -0
- package/dest/sqlite-opfs/errors.d.ts.map +1 -0
- package/dest/sqlite-opfs/errors.js +34 -0
- package/dest/sqlite-opfs/index.d.ts +16 -0
- package/dest/sqlite-opfs/index.d.ts.map +1 -0
- package/dest/sqlite-opfs/index.js +22 -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/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/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 +79 -0
- package/dest/sqlite-opfs/store.d.ts.map +1 -0
- package/dest/sqlite-opfs/store.js +281 -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 +249 -0
- package/dest/stores/index.d.ts +1 -1
- package/dest/stores/l2_tips_store.d.ts +25 -10
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +62 -53
- package/dest/utils.d.ts +9 -6
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +51 -16
- package/package.json +33 -26
- 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 +27 -1
- package/src/indexeddb/singleton.ts +1 -1
- package/src/indexeddb/store.ts +13 -6
- 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.ts +7 -0
- package/src/interfaces/multi_map_test_suite.ts +167 -37
- package/src/interfaces/set_test_suite.ts +13 -15
- package/src/interfaces/singleton_test_suite.ts +6 -8
- package/src/interfaces/utils.ts +1 -0
- package/src/lmdb/index.ts +10 -5
- package/src/lmdb/multi_map.ts +16 -0
- package/src/lmdb/store.ts +13 -9
- package/src/lmdb-v2/array.ts +2 -2
- package/src/lmdb-v2/factory.ts +93 -19
- package/src/lmdb-v2/map.ts +2 -2
- package/src/lmdb-v2/multi_map.ts +10 -1
- package/src/lmdb-v2/read_transaction.ts +23 -23
- package/src/lmdb-v2/singleton.ts +2 -2
- package/src/lmdb-v2/store.ts +9 -33
- 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/errors.ts +44 -0
- package/src/sqlite-opfs/index.ts +39 -0
- package/src/sqlite-opfs/internal/ordered-binary-browser.js +465 -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/set.ts +29 -0
- package/src/sqlite-opfs/singleton.ts +48 -0
- package/src/sqlite-opfs/store.ts +296 -0
- package/src/sqlite-opfs/worker.ts +231 -0
- package/src/stores/l2_tips_store.ts +66 -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
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
import { L2TipsStoreBase } from '@aztec/stdlib/block';
|
|
2
|
+
import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
3
|
+
/**
|
|
4
|
+
* Persistent implementation of L2 tips store backed by a KV store.
|
|
5
|
+
* Used by nodes that need to persist chain state across restarts.
|
|
6
|
+
*/ export class L2TipsKVStore extends L2TipsStoreBase {
|
|
7
|
+
store;
|
|
2
8
|
l2TipsStore;
|
|
3
9
|
l2BlockHashesStore;
|
|
4
|
-
|
|
10
|
+
l2BlockNumberToCheckpointNumberStore;
|
|
11
|
+
l2CheckpointStore;
|
|
12
|
+
constructor(store, namespace, initialBlockHash){
|
|
13
|
+
super(initialBlockHash), this.store = store;
|
|
5
14
|
this.l2TipsStore = store.openMap([
|
|
6
15
|
namespace,
|
|
7
16
|
'l2_tips'
|
|
@@ -10,65 +19,65 @@
|
|
|
10
19
|
namespace,
|
|
11
20
|
'l2_block_hashes'
|
|
12
21
|
].join('_'));
|
|
22
|
+
this.l2BlockNumberToCheckpointNumberStore = store.openMap([
|
|
23
|
+
namespace,
|
|
24
|
+
'l2_block_number_to_checkpoint_number'
|
|
25
|
+
].join('_'));
|
|
26
|
+
this.l2CheckpointStore = store.openMap([
|
|
27
|
+
namespace,
|
|
28
|
+
'l2_checkpoint_store'
|
|
29
|
+
].join('_'));
|
|
30
|
+
}
|
|
31
|
+
getTip(tag) {
|
|
32
|
+
return this.l2TipsStore.getAsync(tag);
|
|
33
|
+
}
|
|
34
|
+
setTip(tag, blockNumber) {
|
|
35
|
+
return this.l2TipsStore.set(tag, blockNumber);
|
|
13
36
|
}
|
|
14
|
-
|
|
15
|
-
return this.l2BlockHashesStore.getAsync(
|
|
37
|
+
getStoredBlockHash(blockNumber) {
|
|
38
|
+
return this.l2BlockHashesStore.getAsync(blockNumber);
|
|
16
39
|
}
|
|
17
|
-
|
|
18
|
-
return
|
|
19
|
-
latest: await this.getL2Tip('latest'),
|
|
20
|
-
finalized: await this.getL2Tip('finalized'),
|
|
21
|
-
proven: await this.getL2Tip('proven')
|
|
22
|
-
};
|
|
40
|
+
setBlockHash(blockNumber, hash) {
|
|
41
|
+
return this.l2BlockHashesStore.set(blockNumber, hash);
|
|
23
42
|
}
|
|
24
|
-
async
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
hash: undefined
|
|
30
|
-
};
|
|
43
|
+
async deleteBlockHashesBefore(blockNumber) {
|
|
44
|
+
for await (const key of this.l2BlockHashesStore.keysAsync({
|
|
45
|
+
end: blockNumber
|
|
46
|
+
})){
|
|
47
|
+
await this.l2BlockHashesStore.delete(key);
|
|
31
48
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
49
|
+
}
|
|
50
|
+
getCheckpointNumberForBlock(blockNumber) {
|
|
51
|
+
return this.l2BlockNumberToCheckpointNumberStore.getAsync(blockNumber);
|
|
52
|
+
}
|
|
53
|
+
setCheckpointNumberForBlock(blockNumber, checkpointNumber) {
|
|
54
|
+
return this.l2BlockNumberToCheckpointNumberStore.set(blockNumber, checkpointNumber);
|
|
55
|
+
}
|
|
56
|
+
async deleteBlockToCheckpointBefore(blockNumber) {
|
|
57
|
+
for await (const key of this.l2BlockNumberToCheckpointNumberStore.keysAsync({
|
|
58
|
+
end: blockNumber
|
|
59
|
+
})){
|
|
60
|
+
await this.l2BlockNumberToCheckpointNumberStore.delete(key);
|
|
35
61
|
}
|
|
36
|
-
return {
|
|
37
|
-
number: blockNumber,
|
|
38
|
-
hash: blockHash
|
|
39
|
-
};
|
|
40
62
|
}
|
|
41
|
-
async
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const blocks = event.blocks.map((b)=>b.block);
|
|
46
|
-
for (const block of blocks){
|
|
47
|
-
await this.l2BlockHashesStore.set(block.number, (await block.hash()).toString());
|
|
48
|
-
}
|
|
49
|
-
await this.l2TipsStore.set('latest', blocks.at(-1).number);
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
case 'chain-pruned':
|
|
53
|
-
await this.saveTag('latest', event.block);
|
|
54
|
-
break;
|
|
55
|
-
case 'chain-proven':
|
|
56
|
-
await this.saveTag('proven', event.block);
|
|
57
|
-
break;
|
|
58
|
-
case 'chain-finalized':
|
|
59
|
-
await this.saveTag('finalized', event.block);
|
|
60
|
-
for await (const key of this.l2BlockHashesStore.keysAsync({
|
|
61
|
-
end: event.block.number
|
|
62
|
-
})){
|
|
63
|
-
await this.l2BlockHashesStore.delete(key);
|
|
64
|
-
}
|
|
65
|
-
break;
|
|
63
|
+
async getCheckpoint(checkpointNumber) {
|
|
64
|
+
const buffer = await this.l2CheckpointStore.getAsync(checkpointNumber);
|
|
65
|
+
if (!buffer) {
|
|
66
|
+
return undefined;
|
|
66
67
|
}
|
|
68
|
+
return PublishedCheckpoint.fromBuffer(buffer);
|
|
67
69
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
saveCheckpointData(checkpoint) {
|
|
71
|
+
return this.l2CheckpointStore.set(checkpoint.checkpoint.number, checkpoint.toBuffer());
|
|
72
|
+
}
|
|
73
|
+
async deleteCheckpointsBefore(checkpointNumber) {
|
|
74
|
+
for await (const key of this.l2CheckpointStore.keysAsync({
|
|
75
|
+
end: checkpointNumber
|
|
76
|
+
})){
|
|
77
|
+
await this.l2CheckpointStore.delete(key);
|
|
72
78
|
}
|
|
73
79
|
}
|
|
80
|
+
runInTransaction(fn) {
|
|
81
|
+
return this.store.transactionAsync(fn);
|
|
82
|
+
}
|
|
74
83
|
}
|
package/dest/utils.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
2
|
import type { Logger } from '@aztec/foundation/log';
|
|
3
3
|
import type { AztecAsyncKVStore, AztecKVStore } from './interfaces/store.js';
|
|
4
4
|
/**
|
|
5
|
-
* Clears the store if the rollup address does not match the one stored in the database.
|
|
6
|
-
*
|
|
5
|
+
* Clears the store if the schema version or rollup address does not match the one stored in the database.
|
|
6
|
+
* Also clears if migrating from an older store format that didn't track schema version.
|
|
7
|
+
* This is to prevent data from being accidentally mixed up between different rollup instances or schema versions.
|
|
7
8
|
* @param store - The store to check
|
|
9
|
+
* @param targetSchemaVersion - The current schema version
|
|
8
10
|
* @param rollupAddress - The ETH address of the rollup contract
|
|
9
|
-
* @
|
|
11
|
+
* @param log - Optional logger
|
|
12
|
+
* @returns The store (cleared if necessary)
|
|
10
13
|
*/
|
|
11
|
-
export declare function
|
|
12
|
-
//# sourceMappingURL=
|
|
14
|
+
export declare function initStoreForRollupAndSchemaVersion<T extends AztecKVStore | AztecAsyncKVStore>(store: T, schemaVersion: number | undefined, rollupAddress: EthAddress | undefined, log?: Logger): Promise<T>;
|
|
15
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy91dGlscy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sK0JBQStCLENBQUM7QUFDM0QsT0FBTyxLQUFLLEVBQUUsTUFBTSxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFJcEQsT0FBTyxLQUFLLEVBQUUsaUJBQWlCLEVBQUUsWUFBWSxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFHN0U7Ozs7Ozs7OztHQVNHO0FBQ0gsd0JBQXNCLGtDQUFrQyxDQUFDLENBQUMsU0FBUyxZQUFZLEdBQUcsaUJBQWlCLEVBQ2pHLEtBQUssRUFBRSxDQUFDLEVBQ1IsYUFBYSxFQUFFLE1BQU0sR0FBRyxTQUFTLEVBQ2pDLGFBQWEsRUFBRSxVQUFVLEdBQUcsU0FBUyxFQUNyQyxHQUFHLENBQUMsRUFBRSxNQUFNLEdBQ1gsT0FBTyxDQUFDLENBQUMsQ0FBQyxDQXFDWiJ9
|
package/dest/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAC3D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAIpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAG7E;;;;;;;;;GASG;AACH,wBAAsB,kCAAkC,CAAC,CAAC,SAAS,YAAY,GAAG,iBAAiB,EACjG,KAAK,EAAE,CAAC,EACR,aAAa,EAAE,MAAM,GAAG,SAAS,EACjC,aAAa,EAAE,UAAU,GAAG,SAAS,EACrC,GAAG,CAAC,EAAE,MAAM,GACX,OAAO,CAAC,CAAC,CAAC,CAqCZ"}
|
package/dest/utils.js
CHANGED
|
@@ -1,24 +1,59 @@
|
|
|
1
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
|
+
import { DatabaseVersion } from '@aztec/stdlib/database-version/version';
|
|
1
3
|
import { isSyncStore } from './interfaces/utils.js';
|
|
2
4
|
/**
|
|
3
|
-
* Clears the store if the rollup address does not match the one stored in the database.
|
|
4
|
-
*
|
|
5
|
+
* Clears the store if the schema version or rollup address does not match the one stored in the database.
|
|
6
|
+
* Also clears if migrating from an older store format that didn't track schema version.
|
|
7
|
+
* This is to prevent data from being accidentally mixed up between different rollup instances or schema versions.
|
|
5
8
|
* @param store - The store to check
|
|
9
|
+
* @param targetSchemaVersion - The current schema version
|
|
6
10
|
* @param rollupAddress - The ETH address of the rollup contract
|
|
7
|
-
* @
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
* @param log - Optional logger
|
|
12
|
+
* @returns The store (cleared if necessary)
|
|
13
|
+
*/ export async function initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, log) {
|
|
14
|
+
const targetSchemaVersion = schemaVersion ?? 0;
|
|
15
|
+
const targetRollupAddress = rollupAddress ?? EthAddress.ZERO;
|
|
16
|
+
const targetDatabaseVersion = new DatabaseVersion(targetSchemaVersion, targetRollupAddress);
|
|
17
|
+
// DB version: database schema version + rollup address combined)
|
|
18
|
+
const dbVersion = store.openSingleton('dbVersion');
|
|
19
|
+
const storedDatabaseVersion = isSyncStore(store) ? dbVersion.get() : await dbVersion.getAsync();
|
|
20
|
+
// Check if this is an old format store (has rollupAddress singleton but no dbVersion)
|
|
21
|
+
const oldRollupSingleton = store.openSingleton('rollupAddress');
|
|
22
|
+
const hasOldFormat = isSyncStore(store) ? !storedDatabaseVersion && !!oldRollupSingleton.get() : !storedDatabaseVersion && !!await oldRollupSingleton.getAsync();
|
|
23
|
+
if (hasOldFormat || doesStoreNeedToBeCleared(targetDatabaseVersion, storedDatabaseVersion, targetSchemaVersion, targetRollupAddress, log)) {
|
|
24
|
+
if (hasOldFormat) {
|
|
25
|
+
log?.warn('Detected old store format without dbVersion, clearing database');
|
|
26
|
+
}
|
|
20
27
|
await store.clear();
|
|
21
28
|
}
|
|
22
|
-
await
|
|
29
|
+
await dbVersion.set(targetDatabaseVersion.toBuffer().toString('utf-8'));
|
|
23
30
|
return store;
|
|
24
31
|
}
|
|
32
|
+
function doesStoreNeedToBeCleared(targetDatabaseVersion, storedDatabaseVersion, targetSchemaVersion, targetRollupAddress, log) {
|
|
33
|
+
if (storedDatabaseVersion) {
|
|
34
|
+
try {
|
|
35
|
+
const storedVersion = DatabaseVersion.fromBuffer(Buffer.from(storedDatabaseVersion, 'utf-8'));
|
|
36
|
+
const cmp = storedVersion.cmp(targetDatabaseVersion);
|
|
37
|
+
if (cmp === undefined) {
|
|
38
|
+
log?.warn('Rollup address changed, clearing database', {
|
|
39
|
+
stored: storedVersion.rollupAddress.toString(),
|
|
40
|
+
current: targetRollupAddress.toString()
|
|
41
|
+
});
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
if (cmp !== 0) {
|
|
45
|
+
log?.warn('Schema version changed, clearing database', {
|
|
46
|
+
stored: storedVersion.schemaVersion,
|
|
47
|
+
current: targetSchemaVersion
|
|
48
|
+
});
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
} catch (err) {
|
|
52
|
+
log?.warn('Failed to parse stored version, clearing database', {
|
|
53
|
+
err
|
|
54
|
+
});
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
package/package.json
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/kv-store",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.b9865e97",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dest/interfaces/index.js",
|
|
7
|
+
"./interfaces": "./dest/interfaces/index.js",
|
|
7
8
|
"./lmdb": "./dest/lmdb/index.js",
|
|
8
9
|
"./lmdb-v2": "./dest/lmdb-v2/index.js",
|
|
9
10
|
"./indexeddb": "./dest/indexeddb/index.js",
|
|
10
|
-
"./
|
|
11
|
-
"./
|
|
11
|
+
"./sqlite-opfs": "./dest/sqlite-opfs/index.js",
|
|
12
|
+
"./stores": "./dest/stores/index.js"
|
|
13
|
+
},
|
|
14
|
+
"imports": {
|
|
15
|
+
"#msgpackr": {
|
|
16
|
+
"browser": "msgpackr/index-no-eval",
|
|
17
|
+
"default": "msgpackr"
|
|
18
|
+
},
|
|
19
|
+
"#ordered-binary": {
|
|
20
|
+
"browser": "./dest/sqlite-opfs/internal/ordered-binary-browser.js",
|
|
21
|
+
"default": "ordered-binary"
|
|
22
|
+
}
|
|
12
23
|
},
|
|
13
24
|
"scripts": {
|
|
14
|
-
"build": "yarn clean && tsc
|
|
15
|
-
"build:dev": "tsc
|
|
25
|
+
"build": "yarn clean && ../scripts/tsc.sh",
|
|
26
|
+
"build:dev": "../scripts/tsc.sh --watch",
|
|
16
27
|
"clean": "rm -rf ./dest .tsbuildinfo",
|
|
17
|
-
"test:node": "
|
|
18
|
-
"test:browser": "
|
|
19
|
-
"
|
|
28
|
+
"test:node": "vitest run --project node",
|
|
29
|
+
"test:browser": "bash scripts/run-browser-tests.sh",
|
|
30
|
+
"bench:browser": "VITE_BENCH=1 vitest run --project browser src/bench",
|
|
31
|
+
"test": "vitest run",
|
|
20
32
|
"test:jest": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
|
|
21
33
|
},
|
|
22
34
|
"inherits": [
|
|
@@ -24,10 +36,12 @@
|
|
|
24
36
|
"./package.local.json"
|
|
25
37
|
],
|
|
26
38
|
"dependencies": {
|
|
27
|
-
"@aztec/
|
|
28
|
-
"@aztec/
|
|
29
|
-
"@aztec/
|
|
30
|
-
"@aztec/
|
|
39
|
+
"@aztec/constants": "0.0.1-commit.b9865e97",
|
|
40
|
+
"@aztec/ethereum": "0.0.1-commit.b9865e97",
|
|
41
|
+
"@aztec/foundation": "0.0.1-commit.b9865e97",
|
|
42
|
+
"@aztec/native": "0.0.1-commit.b9865e97",
|
|
43
|
+
"@aztec/sqlite3mc-wasm": "0.0.1-commit.b9865e97",
|
|
44
|
+
"@aztec/stdlib": "0.0.1-commit.b9865e97",
|
|
31
45
|
"idb": "^8.0.0",
|
|
32
46
|
"lmdb": "^3.2.0",
|
|
33
47
|
"msgpackr": "^1.11.2",
|
|
@@ -36,24 +50,17 @@
|
|
|
36
50
|
},
|
|
37
51
|
"devDependencies": {
|
|
38
52
|
"@jest/globals": "^30.0.0",
|
|
39
|
-
"@types/chai": "^5.0.1",
|
|
40
|
-
"@types/chai-as-promised": "^8.0.1",
|
|
41
53
|
"@types/jest": "^30.0.0",
|
|
42
|
-
"@types/mocha": "^10.0.10",
|
|
43
|
-
"@types/mocha-each": "^2.0.4",
|
|
44
54
|
"@types/node": "^22.15.17",
|
|
45
|
-
"@
|
|
46
|
-
"@
|
|
47
|
-
"
|
|
48
|
-
"@web/test-runner-playwright": "^0.11.0",
|
|
49
|
-
"chai": "^5.1.2",
|
|
50
|
-
"chai-as-promised": "^8.0.1",
|
|
55
|
+
"@typescript/native-preview": "7.0.0-dev.20260113.1",
|
|
56
|
+
"@vitest/browser-playwright": "^4.0.0",
|
|
57
|
+
"buffer": "^6.0.3",
|
|
51
58
|
"jest": "^30.0.0",
|
|
52
|
-
"
|
|
53
|
-
"mocha-each": "^2.0.1",
|
|
54
|
-
"sinon": "^19.0.2",
|
|
59
|
+
"playwright": "1.49.0",
|
|
55
60
|
"ts-node": "^10.9.1",
|
|
56
|
-
"typescript": "^5.3.3"
|
|
61
|
+
"typescript": "^5.3.3",
|
|
62
|
+
"util": "^0.12.5",
|
|
63
|
+
"vitest": "^4.0.0"
|
|
57
64
|
},
|
|
58
65
|
"files": [
|
|
59
66
|
"dest",
|
|
@@ -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
|
+
}
|
package/src/indexeddb/array.ts
CHANGED
|
@@ -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
|
}
|
package/src/indexeddb/index.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
2
|
+
import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
|
|
2
3
|
|
|
3
|
-
import
|
|
4
|
-
import { initStoreForRollup } from '../utils.js';
|
|
4
|
+
import { initStoreForRollupAndSchemaVersion } from '../utils.js';
|
|
5
5
|
import { AztecIndexedDBStore } from './store.js';
|
|
6
6
|
|
|
7
7
|
export { AztecIndexedDBStore } from './store.js';
|
|
8
8
|
|
|
9
|
-
export async function createStore(
|
|
9
|
+
export async function createStore(
|
|
10
|
+
name: string,
|
|
11
|
+
config: DataStoreConfig,
|
|
12
|
+
schemaVersion: number | undefined = undefined,
|
|
13
|
+
log: Logger = createLogger('kv-store'),
|
|
14
|
+
) {
|
|
10
15
|
let { dataDirectory } = config;
|
|
11
16
|
if (typeof dataDirectory !== 'undefined') {
|
|
12
17
|
dataDirectory = `${dataDirectory}/${name}`;
|
|
@@ -18,10 +23,7 @@ export async function createStore(name: string, config: DataStoreConfig, log: Lo
|
|
|
18
23
|
: `Creating ${name} ephemeral data store with map size ${config.dataStoreMapSizeKb} KB`,
|
|
19
24
|
);
|
|
20
25
|
const store = await AztecIndexedDBStore.open(createLogger('kv-store:indexeddb'), dataDirectory ?? '', false);
|
|
21
|
-
|
|
22
|
-
return initStoreForRollup(store, config.l1Contracts.rollupAddress, log);
|
|
23
|
-
}
|
|
24
|
-
return store;
|
|
26
|
+
return initStoreForRollupAndSchemaVersion(store, schemaVersion, config.rollupAddress, log);
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
export function openTmpStore(ephemeral: boolean = false): Promise<AztecIndexedDBStore> {
|
package/src/indexeddb/map.ts
CHANGED
|
@@ -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 {
|
|
@@ -57,10 +57,22 @@ 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
|
|
|
64
|
+
getValueCountAsync(key: K): Promise<number> {
|
|
65
|
+
// Count entries over the keyCount index range for this key
|
|
66
|
+
const index = this.db.index('keyCount');
|
|
67
|
+
const rangeQuery = IDBKeyRange.bound(
|
|
68
|
+
[this.container, this.normalizeKey(key), 0],
|
|
69
|
+
[this.container, this.normalizeKey(key), Number.MAX_SAFE_INTEGER],
|
|
70
|
+
false,
|
|
71
|
+
false,
|
|
72
|
+
);
|
|
73
|
+
return index.count(rangeQuery);
|
|
74
|
+
}
|
|
75
|
+
|
|
64
76
|
async deleteValue(key: K, val: V): Promise<void> {
|
|
65
77
|
// Since we know the value, we can hash it and directly query the "hash" index
|
|
66
78
|
// to avoid having to iterate over all the values
|
|
@@ -76,4 +88,18 @@ export class IndexedDBAztecMultiMap<K extends Key, V extends Value>
|
|
|
76
88
|
await this.db.delete(fullKey);
|
|
77
89
|
}
|
|
78
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
|
+
}
|
|
79
105
|
}
|
|
@@ -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),
|