@aztec/kv-store 0.0.0-test.1 → 0.0.1-commit.b655e406
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/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +5 -3
- package/dest/indexeddb/array.d.ts +2 -1
- package/dest/indexeddb/array.d.ts.map +1 -1
- package/dest/indexeddb/array.js +3 -0
- package/dest/indexeddb/index.js +1 -1
- package/dest/indexeddb/map.d.ts +11 -5
- package/dest/indexeddb/map.d.ts.map +1 -1
- package/dest/indexeddb/map.js +38 -60
- package/dest/indexeddb/multi_map.d.ts +12 -0
- package/dest/indexeddb/multi_map.d.ts.map +1 -0
- package/dest/indexeddb/multi_map.js +78 -0
- package/dest/indexeddb/singleton.d.ts +2 -1
- package/dest/indexeddb/singleton.d.ts.map +1 -1
- package/dest/indexeddb/singleton.js +3 -1
- package/dest/indexeddb/store.d.ts +12 -13
- package/dest/indexeddb/store.d.ts.map +1 -1
- package/dest/indexeddb/store.js +45 -42
- package/dest/interfaces/array.d.ts +4 -3
- package/dest/interfaces/array.d.ts.map +1 -1
- package/dest/interfaces/array.js +1 -3
- package/dest/interfaces/common.d.ts +10 -8
- package/dest/interfaces/common.d.ts.map +1 -1
- package/dest/interfaces/common.js +8 -3
- package/dest/interfaces/index.d.ts +3 -1
- package/dest/interfaces/index.d.ts.map +1 -1
- package/dest/interfaces/index.js +2 -0
- package/dest/interfaces/map.d.ts +20 -48
- package/dest/interfaces/map.d.ts.map +1 -1
- package/dest/interfaces/map.js +1 -1
- package/dest/interfaces/map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/map_test_suite.js +135 -70
- package/dest/interfaces/multi_map.d.ts +35 -0
- package/dest/interfaces/multi_map.d.ts.map +1 -0
- package/dest/interfaces/multi_map.js +3 -0
- package/dest/interfaces/multi_map_test_suite.d.ts +3 -0
- package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -0
- package/dest/interfaces/multi_map_test_suite.js +245 -0
- package/dest/interfaces/store.d.ts +17 -42
- package/dest/interfaces/store.d.ts.map +1 -1
- package/dest/interfaces/utils.d.ts +1 -0
- package/dest/interfaces/utils.d.ts.map +1 -1
- package/dest/interfaces/utils.js +2 -1
- package/dest/lmdb/array.d.ts +2 -1
- package/dest/lmdb/array.d.ts.map +1 -1
- package/dest/lmdb/index.js +2 -2
- package/dest/lmdb/map.d.ts +10 -22
- package/dest/lmdb/map.d.ts.map +1 -1
- package/dest/lmdb/map.js +15 -81
- package/dest/lmdb/multi_map.d.ts +12 -0
- package/dest/lmdb/multi_map.d.ts.map +1 -0
- package/dest/lmdb/multi_map.js +29 -0
- package/dest/lmdb/store.d.ts +7 -22
- package/dest/lmdb/store.d.ts.map +1 -1
- package/dest/lmdb/store.js +11 -31
- package/dest/lmdb-v2/array.d.ts +2 -1
- package/dest/lmdb-v2/array.d.ts.map +1 -1
- package/dest/lmdb-v2/array.js +1 -0
- package/dest/lmdb-v2/factory.d.ts +1 -1
- package/dest/lmdb-v2/factory.d.ts.map +1 -1
- package/dest/lmdb-v2/factory.js +16 -6
- package/dest/lmdb-v2/map.d.ts +10 -43
- package/dest/lmdb-v2/map.d.ts.map +1 -1
- package/dest/lmdb-v2/map.js +17 -103
- package/dest/lmdb-v2/message.d.ts +23 -4
- package/dest/lmdb-v2/message.d.ts.map +1 -1
- package/dest/lmdb-v2/message.js +6 -4
- package/dest/lmdb-v2/multi_map.d.ts +51 -0
- package/dest/lmdb-v2/multi_map.d.ts.map +1 -0
- package/dest/lmdb-v2/multi_map.js +113 -0
- package/dest/lmdb-v2/read_transaction.d.ts +2 -0
- package/dest/lmdb-v2/read_transaction.d.ts.map +1 -1
- package/dest/lmdb-v2/read_transaction.js +34 -0
- package/dest/lmdb-v2/set.d.ts +15 -0
- package/dest/lmdb-v2/set.d.ts.map +1 -0
- package/dest/lmdb-v2/set.js +23 -0
- package/dest/lmdb-v2/singleton.d.ts.map +1 -1
- package/dest/lmdb-v2/singleton.js +1 -0
- package/dest/lmdb-v2/store.d.ts +9 -8
- package/dest/lmdb-v2/store.d.ts.map +1 -1
- package/dest/lmdb-v2/store.js +19 -7
- package/dest/lmdb-v2/utils.d.ts +2 -4
- package/dest/lmdb-v2/utils.d.ts.map +1 -1
- package/dest/lmdb-v2/write_transaction.d.ts +2 -4
- package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
- package/dest/stores/index.d.ts +1 -0
- package/dest/stores/index.d.ts.map +1 -1
- package/dest/stores/index.js +1 -0
- package/dest/stores/l2_tips_store.d.ts +2 -1
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +18 -9
- package/package.json +18 -14
- package/src/config.ts +6 -4
- package/src/indexeddb/array.ts +5 -1
- package/src/indexeddb/index.ts +2 -2
- package/src/indexeddb/map.ts +35 -53
- package/src/indexeddb/multi_map.ts +79 -0
- package/src/indexeddb/singleton.ts +4 -1
- package/src/indexeddb/store.ts +66 -56
- package/src/interfaces/array.ts +5 -3
- package/src/interfaces/common.ts +20 -9
- package/src/interfaces/index.ts +3 -1
- package/src/interfaces/map.ts +19 -53
- package/src/interfaces/map_test_suite.ts +73 -44
- package/src/interfaces/multi_map.ts +38 -0
- package/src/interfaces/multi_map_test_suite.ts +242 -0
- package/src/interfaces/store.ts +18 -53
- package/src/interfaces/utils.ts +1 -0
- package/src/lmdb/array.ts +2 -1
- package/src/lmdb/index.ts +3 -3
- package/src/lmdb/map.ts +23 -94
- package/src/lmdb/multi_map.ts +35 -0
- package/src/lmdb/store.ts +23 -47
- package/src/lmdb-v2/array.ts +7 -2
- package/src/lmdb-v2/factory.ts +17 -10
- package/src/lmdb-v2/map.ts +29 -126
- package/src/lmdb-v2/message.ts +23 -0
- package/src/lmdb-v2/multi_map.ts +141 -0
- package/src/lmdb-v2/read_transaction.ts +40 -0
- package/src/lmdb-v2/set.ts +33 -0
- package/src/lmdb-v2/singleton.ts +5 -1
- package/src/lmdb-v2/store.ts +22 -14
- package/src/lmdb-v2/write_transaction.ts +2 -2
- package/src/stores/index.ts +2 -0
- package/src/stores/l2_tips_store.ts +18 -9
- package/dest/interfaces/store_test_suite.d.ts +0 -3
- package/dest/interfaces/store_test_suite.d.ts.map +0 -1
- package/dest/interfaces/store_test_suite.js +0 -37
- package/src/interfaces/store_test_suite.ts +0 -56
package/src/indexeddb/store.ts
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
1
|
import type { Logger } from '@aztec/foundation/log';
|
|
2
|
+
import { SerialQueue } from '@aztec/foundation/queue';
|
|
2
3
|
|
|
3
4
|
import { type DBSchema, type IDBPDatabase, deleteDB, openDB } from 'idb';
|
|
4
5
|
|
|
5
6
|
import type { AztecAsyncArray } from '../interfaces/array.js';
|
|
6
|
-
import type { Key, StoreSize } from '../interfaces/common.js';
|
|
7
|
+
import type { Key, StoreSize, Value } from '../interfaces/common.js';
|
|
7
8
|
import type { AztecAsyncCounter } from '../interfaces/counter.js';
|
|
8
|
-
import type { AztecAsyncMap
|
|
9
|
+
import type { AztecAsyncMap } from '../interfaces/map.js';
|
|
10
|
+
import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
|
|
9
11
|
import type { AztecAsyncSet } from '../interfaces/set.js';
|
|
10
12
|
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
11
13
|
import type { AztecAsyncKVStore } from '../interfaces/store.js';
|
|
12
14
|
import { IndexedDBAztecArray } from './array.js';
|
|
13
15
|
import { IndexedDBAztecMap } from './map.js';
|
|
16
|
+
import { IndexedDBAztecMultiMap } from './multi_map.js';
|
|
14
17
|
import { IndexedDBAztecSet } from './set.js';
|
|
15
18
|
import { IndexedDBAztecSingleton } from './singleton.js';
|
|
16
19
|
|
|
17
|
-
export type StoredData<V> = {
|
|
20
|
+
export type StoredData<V extends Value> = {
|
|
21
|
+
value: V;
|
|
22
|
+
container: string;
|
|
23
|
+
key: string;
|
|
24
|
+
keyCount: number;
|
|
25
|
+
slot: string;
|
|
26
|
+
hash: string;
|
|
27
|
+
};
|
|
18
28
|
|
|
19
29
|
export interface AztecIDBSchema extends DBSchema {
|
|
20
30
|
data: {
|
|
21
31
|
value: StoredData<any>;
|
|
22
32
|
key: string;
|
|
23
|
-
indexes: { container: string; key: string; keyCount: number };
|
|
33
|
+
indexes: { container: string; key: string; keyCount: number; hash: string };
|
|
24
34
|
};
|
|
25
35
|
}
|
|
26
36
|
|
|
@@ -29,18 +39,27 @@ export interface AztecIDBSchema extends DBSchema {
|
|
|
29
39
|
*/
|
|
30
40
|
|
|
31
41
|
export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
32
|
-
#log: Logger;
|
|
33
42
|
#rootDB: IDBPDatabase<AztecIDBSchema>;
|
|
34
43
|
#name: string;
|
|
44
|
+
#txQueue: SerialQueue;
|
|
35
45
|
|
|
36
46
|
#containers = new Set<
|
|
37
|
-
|
|
47
|
+
| IndexedDBAztecArray<any>
|
|
48
|
+
| IndexedDBAztecMap<any, any>
|
|
49
|
+
| IndexedDBAztecMultiMap<any, any>
|
|
50
|
+
| IndexedDBAztecSet<any>
|
|
51
|
+
| IndexedDBAztecSingleton<any>
|
|
38
52
|
>();
|
|
39
53
|
|
|
40
|
-
constructor(
|
|
54
|
+
constructor(
|
|
55
|
+
rootDB: IDBPDatabase<AztecIDBSchema>,
|
|
56
|
+
public readonly isEphemeral: boolean,
|
|
57
|
+
name: string,
|
|
58
|
+
) {
|
|
41
59
|
this.#rootDB = rootDB;
|
|
42
|
-
this.#log = log;
|
|
43
60
|
this.#name = name;
|
|
61
|
+
this.#txQueue = new SerialQueue();
|
|
62
|
+
this.#txQueue.start();
|
|
44
63
|
}
|
|
45
64
|
/**
|
|
46
65
|
* Creates a new AztecKVStore backed by IndexedDB. The path to the database is optional. If not provided,
|
|
@@ -53,50 +72,32 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
53
72
|
* @returns The store
|
|
54
73
|
*/
|
|
55
74
|
static async open(log: Logger, name?: string, ephemeral: boolean = false): Promise<AztecIndexedDBStore> {
|
|
56
|
-
name = name && !ephemeral ? name :
|
|
75
|
+
name = name && !ephemeral ? name : globalThis.crypto.getRandomValues(new Uint8Array(16)).join('');
|
|
57
76
|
log.debug(`Opening IndexedDB ${ephemeral ? 'temp ' : ''}database with name ${name}`);
|
|
58
77
|
const rootDB = await openDB<AztecIDBSchema>(name, 1, {
|
|
59
78
|
upgrade(db) {
|
|
60
79
|
const objectStore = db.createObjectStore('data', { keyPath: 'slot' });
|
|
61
80
|
|
|
62
81
|
objectStore.createIndex('key', ['container', 'key'], { unique: false });
|
|
63
|
-
|
|
82
|
+
// Keep count of the maximum number of keys ever inserted in the container
|
|
83
|
+
// This allows unique slots for repeated keys, which is useful for multi-maps
|
|
84
|
+
objectStore.createIndex('keyCount', ['container', 'key', 'keyCount'], { unique: true });
|
|
85
|
+
// Keep an index on the pair key-hash for a given container, allowing us to efficiently
|
|
86
|
+
// delete unique values from multi-maps
|
|
87
|
+
objectStore.createIndex('hash', ['container', 'key', 'hash'], { unique: true });
|
|
64
88
|
},
|
|
65
89
|
});
|
|
66
90
|
|
|
67
|
-
const kvStore = new AztecIndexedDBStore(rootDB, ephemeral,
|
|
91
|
+
const kvStore = new AztecIndexedDBStore(rootDB, ephemeral, name);
|
|
68
92
|
return kvStore;
|
|
69
93
|
}
|
|
70
94
|
|
|
71
|
-
/**
|
|
72
|
-
* Forks the current DB into a new DB by backing it up to a temporary location and opening a new indexedb.
|
|
73
|
-
* @returns A new AztecIndexedDBStore.
|
|
74
|
-
*/
|
|
75
|
-
async fork(): Promise<AztecAsyncKVStore> {
|
|
76
|
-
const forkedStore = await AztecIndexedDBStore.open(this.#log, undefined, true);
|
|
77
|
-
this.#log.verbose(`Forking store to ${forkedStore.#name}`);
|
|
78
|
-
|
|
79
|
-
// Copy old data to new store
|
|
80
|
-
const oldData = this.#rootDB.transaction('data').store;
|
|
81
|
-
const dataToWrite = [];
|
|
82
|
-
for await (const cursor of oldData.iterate()) {
|
|
83
|
-
dataToWrite.push(cursor.value);
|
|
84
|
-
}
|
|
85
|
-
const tx = forkedStore.#rootDB.transaction('data', 'readwrite').store;
|
|
86
|
-
for (const data of dataToWrite) {
|
|
87
|
-
await tx.add(data);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
this.#log.debug(`Forked store at ${forkedStore.#name} opened successfully`);
|
|
91
|
-
return forkedStore;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
95
|
/**
|
|
95
96
|
* Creates a new AztecMap in the store.
|
|
96
97
|
* @param name - Name of the map
|
|
97
98
|
* @returns A new AztecMap
|
|
98
99
|
*/
|
|
99
|
-
openMap<K extends Key, V>(name: string): AztecAsyncMap<K, V> {
|
|
100
|
+
openMap<K extends Key, V extends Value>(name: string): AztecAsyncMap<K, V> {
|
|
100
101
|
const map = new IndexedDBAztecMap<K, V>(this.#rootDB, name);
|
|
101
102
|
this.#containers.add(map);
|
|
102
103
|
return map;
|
|
@@ -118,8 +119,8 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
118
119
|
* @param name - Name of the map
|
|
119
120
|
* @returns A new AztecMultiMap
|
|
120
121
|
*/
|
|
121
|
-
openMultiMap<K extends Key, V>(name: string): AztecAsyncMultiMap<K, V> {
|
|
122
|
-
const multimap = new
|
|
122
|
+
openMultiMap<K extends Key, V extends Value>(name: string): AztecAsyncMultiMap<K, V> {
|
|
123
|
+
const multimap = new IndexedDBAztecMultiMap<K, V>(this.#rootDB, name);
|
|
123
124
|
this.#containers.add(multimap);
|
|
124
125
|
return multimap;
|
|
125
126
|
}
|
|
@@ -133,7 +134,7 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
133
134
|
* @param name - Name of the array
|
|
134
135
|
* @returns A new AztecArray
|
|
135
136
|
*/
|
|
136
|
-
openArray<T>(name: string): AztecAsyncArray<T> {
|
|
137
|
+
openArray<T extends Value>(name: string): AztecAsyncArray<T> {
|
|
137
138
|
const array = new IndexedDBAztecArray<T>(this.#rootDB, name);
|
|
138
139
|
this.#containers.add(array);
|
|
139
140
|
return array;
|
|
@@ -144,7 +145,7 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
144
145
|
* @param name - Name of the singleton
|
|
145
146
|
* @returns A new AztecSingleton
|
|
146
147
|
*/
|
|
147
|
-
openSingleton<T>(name: string): AztecAsyncSingleton<T> {
|
|
148
|
+
openSingleton<T extends Value>(name: string): AztecAsyncSingleton<T> {
|
|
148
149
|
const singleton = new IndexedDBAztecSingleton<T>(this.#rootDB, name);
|
|
149
150
|
this.#containers.add(singleton);
|
|
150
151
|
return singleton;
|
|
@@ -155,22 +156,27 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
155
156
|
* @param callback - Function to execute in a transaction
|
|
156
157
|
* @returns A promise that resolves to the return value of the callback
|
|
157
158
|
*/
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
for
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
159
|
+
transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
|
|
160
|
+
// We can only have one transaction at a time for the same store
|
|
161
|
+
// So we need to wait for the current one to finish
|
|
162
|
+
return this.#txQueue.put(async () => {
|
|
163
|
+
const tx = this.#rootDB.transaction('data', 'readwrite');
|
|
164
|
+
for (const container of this.#containers) {
|
|
165
|
+
container.db = tx.store;
|
|
166
|
+
}
|
|
167
|
+
// Avoid awaiting this promise so it doesn't get scheduled in the next microtask
|
|
168
|
+
// By then, the tx would be closed
|
|
169
|
+
const runningPromise = callback();
|
|
170
|
+
// Wait for the transaction to finish
|
|
171
|
+
await tx.done;
|
|
172
|
+
for (const container of this.#containers) {
|
|
173
|
+
container.db = undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Return the result of the callback.
|
|
177
|
+
// Tx is guaranteed to already be closed, so the await doesn't hurt anything here
|
|
178
|
+
return await runningPromise;
|
|
179
|
+
});
|
|
174
180
|
}
|
|
175
181
|
|
|
176
182
|
/**
|
|
@@ -188,10 +194,14 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
188
194
|
}
|
|
189
195
|
|
|
190
196
|
estimateSize(): Promise<StoreSize> {
|
|
191
|
-
return Promise.resolve({ mappingSize: 0, actualSize: 0, numItems: 0 });
|
|
197
|
+
return Promise.resolve({ mappingSize: 0, physicalFileSize: 0, actualSize: 0, numItems: 0 });
|
|
192
198
|
}
|
|
193
199
|
|
|
194
200
|
close(): Promise<void> {
|
|
195
201
|
return Promise.resolve();
|
|
196
202
|
}
|
|
203
|
+
|
|
204
|
+
backupTo(_dstPath: string, _compact?: boolean): Promise<void> {
|
|
205
|
+
throw new Error('Method not implemented.');
|
|
206
|
+
}
|
|
197
207
|
}
|
package/src/interfaces/array.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { Value } from './common.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* An array backed by a persistent store. Can not have any holes in it.
|
|
3
5
|
*/
|
|
4
|
-
interface BaseAztecArray<T> {
|
|
6
|
+
interface BaseAztecArray<T extends Value> {
|
|
5
7
|
/**
|
|
6
8
|
* Pushes values to the end of the array
|
|
7
9
|
* @param vals - The values to push to the end of the array
|
|
@@ -27,7 +29,7 @@ interface BaseAztecArray<T> {
|
|
|
27
29
|
/**
|
|
28
30
|
* An array backed by a persistent store. Can not have any holes in it.
|
|
29
31
|
*/
|
|
30
|
-
export interface AztecAsyncArray<T> extends BaseAztecArray<T> {
|
|
32
|
+
export interface AztecAsyncArray<T extends Value> extends BaseAztecArray<T> {
|
|
31
33
|
/**
|
|
32
34
|
* The size of the array
|
|
33
35
|
*/
|
|
@@ -58,7 +60,7 @@ export interface AztecAsyncArray<T> extends BaseAztecArray<T> {
|
|
|
58
60
|
[Symbol.asyncIterator](): AsyncIterableIterator<T>;
|
|
59
61
|
}
|
|
60
62
|
|
|
61
|
-
export interface AztecArray<T> extends BaseAztecArray<T> {
|
|
63
|
+
export interface AztecArray<T extends Value> extends BaseAztecArray<T> {
|
|
62
64
|
/**
|
|
63
65
|
* The size of the array
|
|
64
66
|
*/
|
package/src/interfaces/common.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
export type Key = string | number | Array<string | number>;
|
|
1
|
+
/** The key type for use with the kv-store */
|
|
2
|
+
export type Key = string | number | Uint8Array | Array<string | number>;
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*/
|
|
9
|
-
export type
|
|
4
|
+
export type Value = NonNullable<any>;
|
|
5
|
+
|
|
6
|
+
/** A range of keys of arbitrary type. */
|
|
7
|
+
export type CustomRange<K> = {
|
|
10
8
|
/** The key of the first item to include */
|
|
11
9
|
start?: K;
|
|
12
10
|
/** The key of the last item to include */
|
|
@@ -17,4 +15,17 @@ export type Range<K extends Key = Key> = {
|
|
|
17
15
|
limit?: number;
|
|
18
16
|
};
|
|
19
17
|
|
|
20
|
-
|
|
18
|
+
/** Maps a custom range into a range of valid key types to iterate over. */
|
|
19
|
+
export function mapRange<CK, K extends Key = Key>(range: CustomRange<CK>, mapFn: (key: CK) => K): Range<K> {
|
|
20
|
+
return {
|
|
21
|
+
start: range.start ? mapFn(range.start) : undefined,
|
|
22
|
+
end: range.end ? mapFn(range.end) : undefined,
|
|
23
|
+
reverse: range.reverse,
|
|
24
|
+
limit: range.limit,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A range of keys to iterate over. */
|
|
29
|
+
export type Range<K extends Key = Key> = CustomRange<K>;
|
|
30
|
+
|
|
31
|
+
export type StoreSize = { mappingSize: number; physicalFileSize: number; actualSize: number; numItems: number };
|
package/src/interfaces/index.ts
CHANGED
|
@@ -4,4 +4,6 @@ export * from './counter.js';
|
|
|
4
4
|
export * from './singleton.js';
|
|
5
5
|
export * from './store.js';
|
|
6
6
|
export * from './set.js';
|
|
7
|
-
export
|
|
7
|
+
export * from './multi_map.js';
|
|
8
|
+
export { mapRange } from './common.js';
|
|
9
|
+
export type { CustomRange, Range, StoreSize } from './common.js';
|
package/src/interfaces/map.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { Key, Range } from './common.js';
|
|
1
|
+
import type { Key, Range, Value } from './common.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A map backed by a persistent store.
|
|
5
5
|
*/
|
|
6
|
-
interface AztecBaseMap<K extends Key, V> {
|
|
6
|
+
interface AztecBaseMap<K extends Key, V extends Value> {
|
|
7
7
|
/**
|
|
8
8
|
* Sets the value at the given key.
|
|
9
9
|
* @param key - The key to set the value at
|
|
@@ -11,6 +11,12 @@ interface AztecBaseMap<K extends Key, V> {
|
|
|
11
11
|
*/
|
|
12
12
|
set(key: K, val: V): Promise<void>;
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Sets the values at the given keys.
|
|
16
|
+
* @param entries - The entries to set
|
|
17
|
+
*/
|
|
18
|
+
setMany(entries: { key: K; value: V }[]): Promise<void>;
|
|
19
|
+
|
|
14
20
|
/**
|
|
15
21
|
* Sets the value at the given key if it does not already exist.
|
|
16
22
|
* @param key - The key to set the value at
|
|
@@ -24,13 +30,19 @@ interface AztecBaseMap<K extends Key, V> {
|
|
|
24
30
|
*/
|
|
25
31
|
delete(key: K): Promise<void>;
|
|
26
32
|
}
|
|
27
|
-
export interface AztecMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
33
|
+
export interface AztecMap<K extends Key, V extends Value> extends AztecBaseMap<K, V> {
|
|
28
34
|
/**
|
|
29
35
|
* Gets the value at the given key.
|
|
30
36
|
* @param key - The key to get the value from
|
|
31
37
|
*/
|
|
32
38
|
get(key: K): V | undefined;
|
|
33
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Gets the current size of the map.
|
|
42
|
+
* @returns The size of the map
|
|
43
|
+
*/
|
|
44
|
+
size(): number;
|
|
45
|
+
|
|
34
46
|
/**
|
|
35
47
|
* Checks if a key exists in the map.
|
|
36
48
|
* @param key - The key to check
|
|
@@ -62,44 +74,10 @@ export interface AztecMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
|
62
74
|
clear(): Promise<void>;
|
|
63
75
|
}
|
|
64
76
|
|
|
65
|
-
export interface AztecMapWithSize<K extends Key, V> extends AztecMap<K, V> {
|
|
66
|
-
/**
|
|
67
|
-
* Gets the size of the map.
|
|
68
|
-
* @returns The size of the map
|
|
69
|
-
*/
|
|
70
|
-
size(): number;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* A map backed by a persistent store that can have multiple values for a single key.
|
|
75
|
-
*/
|
|
76
|
-
export interface AztecMultiMap<K extends Key, V> extends AztecMap<K, V> {
|
|
77
|
-
/**
|
|
78
|
-
* Gets all the values at the given key.
|
|
79
|
-
* @param key - The key to get the values from
|
|
80
|
-
*/
|
|
81
|
-
getValues(key: K): IterableIterator<V>;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Deletes a specific value at the given key.
|
|
85
|
-
* @param key - The key to delete the value at
|
|
86
|
-
* @param val - The value to delete
|
|
87
|
-
*/
|
|
88
|
-
deleteValue(key: K, val: V): Promise<void>;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export interface AztecMultiMapWithSize<K extends Key, V> extends AztecMultiMap<K, V> {
|
|
92
|
-
/**
|
|
93
|
-
* Gets the size of the map.
|
|
94
|
-
* @returns The size of the map
|
|
95
|
-
*/
|
|
96
|
-
size(): number;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
77
|
/**
|
|
100
78
|
* A map backed by a persistent store.
|
|
101
79
|
*/
|
|
102
|
-
export interface AztecAsyncMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
80
|
+
export interface AztecAsyncMap<K extends Key, V extends Value> extends AztecBaseMap<K, V> {
|
|
103
81
|
/**
|
|
104
82
|
* Gets the value at the given key.
|
|
105
83
|
* @param key - The key to get the value from
|
|
@@ -130,22 +108,10 @@ export interface AztecAsyncMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
|
130
108
|
* @param range - The range of keys to iterate over
|
|
131
109
|
*/
|
|
132
110
|
keysAsync(range?: Range<K>): AsyncIterableIterator<K>;
|
|
133
|
-
}
|
|
134
111
|
|
|
135
|
-
/**
|
|
136
|
-
* A map backed by a persistent store that can have multiple values for a single key.
|
|
137
|
-
*/
|
|
138
|
-
export interface AztecAsyncMultiMap<K extends Key, V> extends AztecAsyncMap<K, V> {
|
|
139
112
|
/**
|
|
140
|
-
* Gets
|
|
141
|
-
* @
|
|
142
|
-
*/
|
|
143
|
-
getValuesAsync(key: K): AsyncIterableIterator<V>;
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Deletes a specific value at the given key.
|
|
147
|
-
* @param key - The key to delete the value at
|
|
148
|
-
* @param val - The value to delete
|
|
113
|
+
* Gets the current size of the map.
|
|
114
|
+
* @returns The size of the map
|
|
149
115
|
*/
|
|
150
|
-
|
|
116
|
+
sizeAsync(): Promise<number>;
|
|
151
117
|
}
|
|
@@ -3,7 +3,7 @@ import { toArray } from '@aztec/foundation/iterable';
|
|
|
3
3
|
import { expect } from 'chai';
|
|
4
4
|
|
|
5
5
|
import type { Key, Range } from './common.js';
|
|
6
|
-
import type { AztecAsyncMap,
|
|
6
|
+
import type { AztecAsyncMap, AztecMap } from './map.js';
|
|
7
7
|
import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
|
|
8
8
|
import { isSyncStore } from './utils.js';
|
|
9
9
|
|
|
@@ -14,11 +14,11 @@ export function describeAztecMap(
|
|
|
14
14
|
) {
|
|
15
15
|
describe(testName, () => {
|
|
16
16
|
let store: AztecKVStore | AztecAsyncKVStore;
|
|
17
|
-
let map:
|
|
17
|
+
let map: AztecMap<Key, string> | AztecAsyncMap<Key, string>;
|
|
18
18
|
|
|
19
19
|
beforeEach(async () => {
|
|
20
20
|
store = await getStore();
|
|
21
|
-
map = store.
|
|
21
|
+
map = store.openMap<string, string>('test');
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
afterEach(async () => {
|
|
@@ -27,32 +27,32 @@ export function describeAztecMap(
|
|
|
27
27
|
|
|
28
28
|
async function get(key: Key, sut: AztecAsyncMap<any, any> | AztecMap<any, any> = map) {
|
|
29
29
|
return isSyncStore(store) && !forceAsync
|
|
30
|
-
? (sut as
|
|
31
|
-
: await (sut as
|
|
30
|
+
? (sut as AztecMap<any, any>).get(key)
|
|
31
|
+
: await (sut as AztecAsyncMap<any, any>).getAsync(key);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
async function
|
|
34
|
+
async function size(sut: AztecAsyncMap<any, any> | AztecMap<any, any> = map) {
|
|
35
35
|
return isSyncStore(store) && !forceAsync
|
|
36
|
-
?
|
|
37
|
-
: await
|
|
36
|
+
? (sut as AztecMap<any, any>).size()
|
|
37
|
+
: await (sut as AztecAsyncMap<any, any>).sizeAsync();
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
async function
|
|
40
|
+
async function entries() {
|
|
41
41
|
return isSyncStore(store) && !forceAsync
|
|
42
|
-
? await toArray((map as
|
|
43
|
-
: await toArray((map as
|
|
42
|
+
? await toArray((map as AztecMap<any, any>).entries())
|
|
43
|
+
: await toArray((map as AztecAsyncMap<any, any>).entriesAsync());
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
async function
|
|
46
|
+
async function values() {
|
|
47
47
|
return isSyncStore(store) && !forceAsync
|
|
48
|
-
? await toArray((
|
|
49
|
-
: await toArray((
|
|
48
|
+
? await toArray((map as AztecMap<any, any>).values())
|
|
49
|
+
: await toArray((map as AztecAsyncMap<any, any>).valuesAsync());
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
async function
|
|
52
|
+
async function keys(range?: Range<Key>, sut: AztecAsyncMap<any, any> | AztecMap<any, any> = map) {
|
|
53
53
|
return isSyncStore(store) && !forceAsync
|
|
54
|
-
? await toArray((
|
|
55
|
-
: await toArray((
|
|
54
|
+
? await toArray((sut as AztecMap<any, any>).keys(range))
|
|
55
|
+
: await toArray((sut as AztecAsyncMap<any, any>).keysAsync(range));
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
it('should be able to set and get values', async () => {
|
|
@@ -64,6 +64,22 @@ export function describeAztecMap(
|
|
|
64
64
|
expect(await get('quux')).to.equal(undefined);
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
+
it('should be able to set many values', async () => {
|
|
68
|
+
const pairs = Array.from({ length: 100 }, (_, i) => ({ key: `key${i}`, value: `value${i}` }));
|
|
69
|
+
await map.setMany(pairs);
|
|
70
|
+
|
|
71
|
+
for (const { key, value } of pairs) {
|
|
72
|
+
expect(await get(key)).to.equal(value);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should be able to overwrite values', async () => {
|
|
77
|
+
await map.set('foo', 'bar');
|
|
78
|
+
await map.set('foo', 'baz');
|
|
79
|
+
|
|
80
|
+
expect(await get('foo')).to.equal('baz');
|
|
81
|
+
});
|
|
82
|
+
|
|
67
83
|
it('should be able to set values if they do not exist', async () => {
|
|
68
84
|
expect(await map.setIfNotExists('foo', 'bar')).to.equal(true);
|
|
69
85
|
expect(await map.setIfNotExists('foo', 'baz')).to.equal(false);
|
|
@@ -81,6 +97,16 @@ export function describeAztecMap(
|
|
|
81
97
|
expect(await get('baz')).to.equal('qux');
|
|
82
98
|
});
|
|
83
99
|
|
|
100
|
+
it('should be able to return size of the map', async () => {
|
|
101
|
+
await map.set('foo', 'bar');
|
|
102
|
+
expect(await size()).to.equal(1);
|
|
103
|
+
await map.set('baz', 'qux');
|
|
104
|
+
expect(await size()).to.equal(2);
|
|
105
|
+
|
|
106
|
+
await map.delete('foo');
|
|
107
|
+
expect(await size()).to.equal(1);
|
|
108
|
+
});
|
|
109
|
+
|
|
84
110
|
it('should be able to iterate over entries when there are no keys', async () => {
|
|
85
111
|
expect(await entries()).to.deep.equal([]);
|
|
86
112
|
});
|
|
@@ -109,35 +135,38 @@ export function describeAztecMap(
|
|
|
109
135
|
expect(await keys()).to.deep.equal(['baz', 'foo']);
|
|
110
136
|
});
|
|
111
137
|
|
|
112
|
-
it('should be able to
|
|
113
|
-
await map.set('
|
|
114
|
-
await map.set('
|
|
138
|
+
it('should be able to iterate over string keys that represent numbers', async () => {
|
|
139
|
+
await map.set('0x22', 'bar');
|
|
140
|
+
await map.set('0x31', 'qux');
|
|
115
141
|
|
|
116
|
-
expect(await
|
|
142
|
+
expect(await keys()).to.deep.equal(['0x22', '0x31']);
|
|
117
143
|
});
|
|
118
144
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
145
|
+
for (const [name, data] of [
|
|
146
|
+
['chars', ['a', 'b', 'c', 'd']],
|
|
147
|
+
['numbers', [1, 2, 3, 4]],
|
|
148
|
+
// disabled because indexeddb sorts lexigographically
|
|
149
|
+
// ['negative numbers', [-4, -3, -2, -1]],
|
|
150
|
+
['strings', ['aaa', 'bbb', 'ccc', 'ddd']],
|
|
151
|
+
['zero-based numbers', [0, 1, 2, 3]],
|
|
152
|
+
]) {
|
|
153
|
+
it(`supports range queries over ${name} keys`, async () => {
|
|
154
|
+
const [a, b, c, d] = data;
|
|
155
|
+
|
|
156
|
+
await map.set(a, 'a');
|
|
157
|
+
await map.set(b, 'b');
|
|
158
|
+
await map.set(c, 'c');
|
|
159
|
+
await map.set(d, 'd');
|
|
160
|
+
|
|
161
|
+
expect(await keys()).to.deep.equal([a, b, c, d]);
|
|
162
|
+
expect(await keys({ start: b, end: c })).to.deep.equal([b]);
|
|
163
|
+
expect(await keys({ start: b })).to.deep.equal([b, c, d]);
|
|
164
|
+
expect(await keys({ end: c })).to.deep.equal([a, b]);
|
|
165
|
+
expect(await keys({ start: b, end: c, reverse: true })).to.deep.equal([c]);
|
|
166
|
+
expect(await keys({ start: b, limit: 1 })).to.deep.equal([b]);
|
|
167
|
+
expect(await keys({ start: b, reverse: true })).to.deep.equal([d, c]);
|
|
168
|
+
expect(await keys({ end: b, reverse: true })).to.deep.equal([b, a]);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
142
171
|
});
|
|
143
172
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Key, Value } from './common.js';
|
|
2
|
+
import type { AztecAsyncMap, AztecMap } from './map.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A map backed by a persistent store that can have multiple values for a single key.
|
|
6
|
+
*/
|
|
7
|
+
export interface AztecMultiMap<K extends Key, V extends Value> extends AztecMap<K, V> {
|
|
8
|
+
/**
|
|
9
|
+
* Gets all the values at the given key.
|
|
10
|
+
* @param key - The key to get the values from
|
|
11
|
+
*/
|
|
12
|
+
getValues(key: K): IterableIterator<V>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Deletes a specific value at the given key.
|
|
16
|
+
* @param key - The key to delete the value at
|
|
17
|
+
* @param val - The value to delete
|
|
18
|
+
*/
|
|
19
|
+
deleteValue(key: K, val: V): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A map backed by a persistent store that can have multiple values for a single key.
|
|
24
|
+
*/
|
|
25
|
+
export interface AztecAsyncMultiMap<K extends Key, V extends Value> extends AztecAsyncMap<K, V> {
|
|
26
|
+
/**
|
|
27
|
+
* Gets all the values at the given key.
|
|
28
|
+
* @param key - The key to get the values from
|
|
29
|
+
*/
|
|
30
|
+
getValuesAsync(key: K): AsyncIterableIterator<V>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Deletes a specific value at the given key.
|
|
34
|
+
* @param key - The key to delete the value at
|
|
35
|
+
* @param val - The value to delete
|
|
36
|
+
*/
|
|
37
|
+
deleteValue(key: K, val: V): Promise<void>;
|
|
38
|
+
}
|