@aztec/kv-store 0.0.1-commit.96bb3f7 → 0.0.1-commit.993d240
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/bench/shared_map_bench.d.ts +19 -0
- package/dest/bench/shared_map_bench.d.ts.map +1 -0
- package/dest/bench/shared_map_bench.js +91 -0
- package/dest/indexeddb/array.js +18 -6
- package/dest/indexeddb/index.d.ts +3 -3
- package/dest/indexeddb/index.d.ts.map +1 -1
- package/dest/indexeddb/index.js +3 -6
- package/dest/indexeddb/map.d.ts +8 -2
- package/dest/indexeddb/map.d.ts.map +1 -1
- package/dest/indexeddb/map.js +23 -13
- package/dest/indexeddb/multi_map.d.ts +2 -1
- package/dest/indexeddb/multi_map.d.ts.map +1 -1
- package/dest/indexeddb/multi_map.js +16 -1
- package/dest/indexeddb/singleton.js +3 -1
- package/dest/indexeddb/store.d.ts +3 -3
- package/dest/indexeddb/store.d.ts.map +1 -1
- package/dest/indexeddb/store.js +6 -4
- package/dest/interfaces/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/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/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 +32 -7
- package/dest/lmdb-v2/factory.d.ts.map +1 -1
- package/dest/lmdb-v2/factory.js +71 -16
- package/dest/lmdb-v2/read_transaction.js +21 -19
- package/dest/lmdb-v2/store.d.ts +3 -3
- package/dest/lmdb-v2/store.d.ts.map +1 -1
- package/dest/lmdb-v2/store.js +5 -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 +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/l2_tips_store.d.ts +3 -3
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +2 -2
- 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/array.ts +4 -4
- package/src/indexeddb/index.ts +9 -7
- package/src/indexeddb/map.ts +24 -11
- package/src/indexeddb/multi_map.ts +15 -1
- package/src/indexeddb/singleton.ts +1 -1
- package/src/indexeddb/store.ts +13 -6
- package/src/interfaces/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/interfaces/utils.ts +1 -0
- package/src/lmdb/index.ts +10 -5
- package/src/lmdb/store.ts +12 -8
- package/src/lmdb-v2/factory.ts +93 -19
- package/src/lmdb-v2/read_transaction.ts +23 -23
- package/src/lmdb-v2/store.ts +7 -4
- 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 +3 -2
- 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
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,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
|
}
|
|
@@ -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),
|
package/src/indexeddb/store.ts
CHANGED
|
@@ -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,7 +30,12 @@ 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
|
|
|
@@ -187,18 +192,20 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
187
192
|
}
|
|
188
193
|
|
|
189
194
|
/** Deletes this store and removes the database */
|
|
190
|
-
delete() {
|
|
195
|
+
async delete() {
|
|
191
196
|
this.#containers.clear();
|
|
197
|
+
await this.#txQueue.end();
|
|
192
198
|
this.#rootDB.close();
|
|
193
|
-
|
|
199
|
+
await deleteDB(this.#name);
|
|
194
200
|
}
|
|
195
201
|
|
|
196
202
|
estimateSize(): Promise<StoreSize> {
|
|
197
203
|
return Promise.resolve({ mappingSize: 0, physicalFileSize: 0, actualSize: 0, numItems: 0 });
|
|
198
204
|
}
|
|
199
205
|
|
|
200
|
-
close(): Promise<void> {
|
|
201
|
-
|
|
206
|
+
async close(): Promise<void> {
|
|
207
|
+
await this.#txQueue.end();
|
|
208
|
+
this.#rootDB.close();
|
|
202
209
|
}
|
|
203
210
|
|
|
204
211
|
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
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { toArray } from '@aztec/foundation/iterable';
|
|
2
2
|
|
|
3
|
-
import { expect } from 'chai';
|
|
4
|
-
|
|
5
3
|
import type { Key, Range } from './common.js';
|
|
6
4
|
import type { AztecAsyncMap, AztecMap } from './map.js';
|
|
7
5
|
import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
|
|
@@ -59,9 +57,9 @@ export function describeAztecMap(
|
|
|
59
57
|
await map.set('foo', 'bar');
|
|
60
58
|
await map.set('baz', 'qux');
|
|
61
59
|
|
|
62
|
-
expect(await get('foo')).
|
|
63
|
-
expect(await get('baz')).
|
|
64
|
-
expect(await get('quux')).
|
|
60
|
+
expect(await get('foo')).toBe('bar');
|
|
61
|
+
expect(await get('baz')).toBe('qux');
|
|
62
|
+
expect(await get('quux')).toBe(undefined);
|
|
65
63
|
});
|
|
66
64
|
|
|
67
65
|
it('should be able to set many values', async () => {
|
|
@@ -69,7 +67,7 @@ export function describeAztecMap(
|
|
|
69
67
|
await map.setMany(pairs);
|
|
70
68
|
|
|
71
69
|
for (const { key, value } of pairs) {
|
|
72
|
-
expect(await get(key)).
|
|
70
|
+
expect(await get(key)).toBe(value);
|
|
73
71
|
}
|
|
74
72
|
});
|
|
75
73
|
|
|
@@ -77,14 +75,14 @@ export function describeAztecMap(
|
|
|
77
75
|
await map.set('foo', 'bar');
|
|
78
76
|
await map.set('foo', 'baz');
|
|
79
77
|
|
|
80
|
-
expect(await get('foo')).
|
|
78
|
+
expect(await get('foo')).toBe('baz');
|
|
81
79
|
});
|
|
82
80
|
|
|
83
81
|
it('should be able to set values if they do not exist', async () => {
|
|
84
|
-
expect(await map.setIfNotExists('foo', 'bar')).
|
|
85
|
-
expect(await map.setIfNotExists('foo', 'baz')).
|
|
82
|
+
expect(await map.setIfNotExists('foo', 'bar')).toBe(true);
|
|
83
|
+
expect(await map.setIfNotExists('foo', 'baz')).toBe(false);
|
|
86
84
|
|
|
87
|
-
expect(await get('foo')).
|
|
85
|
+
expect(await get('foo')).toBe('bar');
|
|
88
86
|
});
|
|
89
87
|
|
|
90
88
|
it('should be able to delete values', async () => {
|
|
@@ -93,29 +91,56 @@ export function describeAztecMap(
|
|
|
93
91
|
|
|
94
92
|
await map.delete('foo');
|
|
95
93
|
|
|
96
|
-
expect(await get('foo')).
|
|
97
|
-
expect(await get('baz')).
|
|
94
|
+
expect(await get('foo')).toBe(undefined);
|
|
95
|
+
expect(await get('baz')).toBe('qux');
|
|
98
96
|
});
|
|
99
97
|
|
|
100
98
|
it('should be able to return size of the map', async () => {
|
|
101
99
|
await map.set('foo', 'bar');
|
|
102
|
-
expect(await size()).
|
|
100
|
+
expect(await size()).toBe(1);
|
|
103
101
|
await map.set('baz', 'qux');
|
|
104
|
-
expect(await size()).
|
|
102
|
+
expect(await size()).toBe(2);
|
|
105
103
|
|
|
106
104
|
await map.delete('foo');
|
|
107
|
-
expect(await size()).
|
|
105
|
+
expect(await size()).toBe(1);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('returns 0 for empty map size', async () => {
|
|
109
|
+
expect(await size()).toBe(0);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('calculates size correctly across multiple operations', async () => {
|
|
113
|
+
expect(await size()).toBe(0);
|
|
114
|
+
|
|
115
|
+
// Add items
|
|
116
|
+
await map.set('a', 'value1');
|
|
117
|
+
await map.set('b', 'value2');
|
|
118
|
+
await map.set('c', 'value3');
|
|
119
|
+
expect(await size()).toBe(3);
|
|
120
|
+
|
|
121
|
+
// Update existing (size should not change)
|
|
122
|
+
await map.set('b', 'updated');
|
|
123
|
+
expect(await size()).toBe(3);
|
|
124
|
+
|
|
125
|
+
// Delete some
|
|
126
|
+
await map.delete('a');
|
|
127
|
+
expect(await size()).toBe(2);
|
|
128
|
+
|
|
129
|
+
// Delete all
|
|
130
|
+
await map.delete('b');
|
|
131
|
+
await map.delete('c');
|
|
132
|
+
expect(await size()).toBe(0);
|
|
108
133
|
});
|
|
109
134
|
|
|
110
135
|
it('should be able to iterate over entries when there are no keys', async () => {
|
|
111
|
-
expect(await entries()).
|
|
136
|
+
expect(await entries()).toEqual([]);
|
|
112
137
|
});
|
|
113
138
|
|
|
114
139
|
it('should be able to iterate over entries', async () => {
|
|
115
140
|
await map.set('foo', 'bar');
|
|
116
141
|
await map.set('baz', 'qux');
|
|
117
142
|
|
|
118
|
-
expect(await entries()).
|
|
143
|
+
expect(await entries()).toEqual([
|
|
119
144
|
['baz', 'qux'],
|
|
120
145
|
['foo', 'bar'],
|
|
121
146
|
]);
|
|
@@ -125,30 +150,31 @@ export function describeAztecMap(
|
|
|
125
150
|
await map.set('foo', 'bar');
|
|
126
151
|
await map.set('baz', 'quux');
|
|
127
152
|
|
|
128
|
-
expect(await values()).
|
|
153
|
+
expect(await values()).toEqual(['quux', 'bar']);
|
|
129
154
|
});
|
|
130
155
|
|
|
131
156
|
it('should be able to iterate over keys', async () => {
|
|
132
157
|
await map.set('foo', 'bar');
|
|
133
158
|
await map.set('baz', 'qux');
|
|
134
159
|
|
|
135
|
-
expect(await keys()).
|
|
160
|
+
expect(await keys()).toEqual(['baz', 'foo']);
|
|
136
161
|
});
|
|
137
162
|
|
|
138
163
|
it('should be able to iterate over string keys that represent numbers', async () => {
|
|
139
164
|
await map.set('0x22', 'bar');
|
|
140
165
|
await map.set('0x31', 'qux');
|
|
141
166
|
|
|
142
|
-
expect(await keys()).
|
|
167
|
+
expect(await keys()).toEqual(['0x22', '0x31']);
|
|
143
168
|
});
|
|
144
169
|
|
|
145
170
|
for (const [name, data] of [
|
|
146
171
|
['chars', ['a', 'b', 'c', 'd']],
|
|
147
172
|
['numbers', [1, 2, 3, 4]],
|
|
148
|
-
|
|
149
|
-
// ['negative numbers', [-4, -3, -2, -1]],
|
|
173
|
+
['negative numbers', [-4, -3, -2, -1]],
|
|
150
174
|
['strings', ['aaa', 'bbb', 'ccc', 'ddd']],
|
|
151
175
|
['zero-based numbers', [0, 1, 2, 3]],
|
|
176
|
+
['large numbers', [100, 999, 1000, 1001]],
|
|
177
|
+
['mixed negative and positive', [-1000, -1, 1, 1000]],
|
|
152
178
|
]) {
|
|
153
179
|
it(`supports range queries over ${name} keys`, async () => {
|
|
154
180
|
const [a, b, c, d] = data;
|
|
@@ -158,14 +184,14 @@ export function describeAztecMap(
|
|
|
158
184
|
await map.set(c, 'c');
|
|
159
185
|
await map.set(d, 'd');
|
|
160
186
|
|
|
161
|
-
expect(await keys()).
|
|
162
|
-
expect(await keys({ start: b, end: c })).
|
|
163
|
-
expect(await keys({ start: b })).
|
|
164
|
-
expect(await keys({ end: c })).
|
|
165
|
-
expect(await keys({ start: b, end: c, reverse: true })).
|
|
166
|
-
expect(await keys({ start: b, limit: 1 })).
|
|
167
|
-
expect(await keys({ start: b, reverse: true })).
|
|
168
|
-
expect(await keys({ end: b, reverse: true })).
|
|
187
|
+
expect(await keys()).toEqual([a, b, c, d]);
|
|
188
|
+
expect(await keys({ start: b, end: c })).toEqual([b]);
|
|
189
|
+
expect(await keys({ start: b })).toEqual([b, c, d]);
|
|
190
|
+
expect(await keys({ end: c })).toEqual([a, b]);
|
|
191
|
+
expect(await keys({ start: b, end: c, reverse: true })).toEqual([c]);
|
|
192
|
+
expect(await keys({ start: b, limit: 1 })).toEqual([b]);
|
|
193
|
+
expect(await keys({ start: b, reverse: true })).toEqual([d, c]);
|
|
194
|
+
expect(await keys({ end: b, reverse: true })).toEqual([b, a]);
|
|
169
195
|
});
|
|
170
196
|
}
|
|
171
197
|
});
|