@aztec/kv-store 0.65.2 → 0.67.0

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.
Files changed (106) hide show
  1. package/dest/config.d.ts +1 -1
  2. package/dest/config.d.ts.map +1 -1
  3. package/dest/indexeddb/array.d.ts +21 -0
  4. package/dest/indexeddb/array.d.ts.map +1 -0
  5. package/dest/indexeddb/array.js +96 -0
  6. package/dest/indexeddb/index.d.ts +7 -0
  7. package/dest/indexeddb/index.d.ts.map +1 -0
  8. package/dest/indexeddb/index.js +22 -0
  9. package/dest/indexeddb/map.d.ts +26 -0
  10. package/dest/indexeddb/map.d.ts.map +1 -0
  11. package/dest/indexeddb/map.js +104 -0
  12. package/dest/indexeddb/set.d.ts +17 -0
  13. package/dest/indexeddb/set.d.ts.map +1 -0
  14. package/dest/indexeddb/set.js +25 -0
  15. package/dest/indexeddb/singleton.d.ts +16 -0
  16. package/dest/indexeddb/singleton.d.ts.map +1 -0
  17. package/dest/indexeddb/singleton.js +42 -0
  18. package/dest/indexeddb/store.d.ts +100 -0
  19. package/dest/indexeddb/store.d.ts.map +1 -0
  20. package/dest/indexeddb/store.js +156 -0
  21. package/dest/interfaces/array.d.ts +43 -11
  22. package/dest/interfaces/array.d.ts.map +1 -1
  23. package/dest/interfaces/array_test_suite.d.ts +3 -0
  24. package/dest/interfaces/array_test_suite.d.ts.map +1 -0
  25. package/dest/interfaces/array_test_suite.js +97 -0
  26. package/dest/interfaces/counter.d.ts +21 -1
  27. package/dest/interfaces/counter.d.ts.map +1 -1
  28. package/dest/interfaces/map.d.ts +62 -12
  29. package/dest/interfaces/map.d.ts.map +1 -1
  30. package/dest/interfaces/map_test_suite.d.ts +3 -0
  31. package/dest/interfaces/map_test_suite.d.ts.map +1 -0
  32. package/dest/interfaces/map_test_suite.js +114 -0
  33. package/dest/interfaces/set.d.ts +23 -7
  34. package/dest/interfaces/set.d.ts.map +1 -1
  35. package/dest/interfaces/set_test_suite.d.ts +3 -0
  36. package/dest/interfaces/set_test_suite.d.ts.map +1 -0
  37. package/dest/interfaces/set_test_suite.js +56 -0
  38. package/dest/interfaces/singleton.d.ts +14 -5
  39. package/dest/interfaces/singleton.d.ts.map +1 -1
  40. package/dest/interfaces/singleton_test_suite.d.ts +3 -0
  41. package/dest/interfaces/singleton_test_suite.d.ts.map +1 -0
  42. package/dest/interfaces/singleton_test_suite.js +30 -0
  43. package/dest/interfaces/store.d.ts +71 -8
  44. package/dest/interfaces/store.d.ts.map +1 -1
  45. package/dest/interfaces/store_test_suite.d.ts +3 -0
  46. package/dest/interfaces/store_test_suite.d.ts.map +1 -0
  47. package/dest/interfaces/store_test_suite.js +36 -0
  48. package/dest/interfaces/utils.d.ts +16 -0
  49. package/dest/interfaces/utils.d.ts.map +1 -0
  50. package/dest/interfaces/utils.js +19 -0
  51. package/dest/lmdb/array.d.ts +7 -2
  52. package/dest/lmdb/array.d.ts.map +1 -1
  53. package/dest/lmdb/array.js +20 -1
  54. package/dest/lmdb/counter.d.ts +5 -2
  55. package/dest/lmdb/counter.d.ts.map +1 -1
  56. package/dest/lmdb/counter.js +10 -1
  57. package/dest/lmdb/index.d.ts +10 -0
  58. package/dest/lmdb/index.d.ts.map +1 -1
  59. package/dest/lmdb/index.js +28 -1
  60. package/dest/lmdb/map.d.ts +8 -2
  61. package/dest/lmdb/map.d.ts.map +1 -1
  62. package/dest/lmdb/map.js +27 -1
  63. package/dest/lmdb/set.d.ts +4 -2
  64. package/dest/lmdb/set.d.ts.map +1 -1
  65. package/dest/lmdb/set.js +9 -1
  66. package/dest/lmdb/singleton.d.ts +3 -2
  67. package/dest/lmdb/singleton.d.ts.map +1 -1
  68. package/dest/lmdb/singleton.js +4 -1
  69. package/dest/lmdb/store.d.ts +21 -13
  70. package/dest/lmdb/store.d.ts.map +1 -1
  71. package/dest/lmdb/store.js +23 -19
  72. package/dest/stores/l2_tips_store.d.ts +2 -2
  73. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  74. package/dest/stores/l2_tips_store.js +12 -12
  75. package/dest/utils.d.ts +8 -7
  76. package/dest/utils.d.ts.map +1 -1
  77. package/dest/utils.js +6 -25
  78. package/package.json +54 -38
  79. package/src/config.ts +1 -1
  80. package/src/indexeddb/array.ts +118 -0
  81. package/src/indexeddb/index.ts +29 -0
  82. package/src/indexeddb/map.ts +142 -0
  83. package/src/indexeddb/set.ts +37 -0
  84. package/src/indexeddb/singleton.ts +49 -0
  85. package/src/indexeddb/store.ts +192 -0
  86. package/src/interfaces/array.ts +48 -12
  87. package/src/interfaces/array_test_suite.ts +126 -0
  88. package/src/interfaces/counter.ts +23 -1
  89. package/src/interfaces/map.ts +69 -14
  90. package/src/interfaces/map_test_suite.ts +154 -0
  91. package/src/interfaces/set.ts +25 -8
  92. package/src/interfaces/set_test_suite.ts +77 -0
  93. package/src/interfaces/singleton.ts +14 -6
  94. package/src/interfaces/singleton_test_suite.ts +42 -0
  95. package/src/interfaces/store.ts +78 -8
  96. package/src/interfaces/store_test_suite.ts +52 -0
  97. package/src/interfaces/utils.ts +21 -0
  98. package/src/lmdb/array.ts +26 -2
  99. package/src/lmdb/counter.ts +14 -2
  100. package/src/lmdb/index.ts +36 -0
  101. package/src/lmdb/map.ts +34 -2
  102. package/src/lmdb/set.ts +12 -2
  103. package/src/lmdb/singleton.ts +6 -2
  104. package/src/lmdb/store.ts +39 -32
  105. package/src/stores/l2_tips_store.ts +16 -16
  106. package/src/utils.ts +8 -36
@@ -0,0 +1,142 @@
1
+ import { type IDBPDatabase, type IDBPObjectStore } from 'idb';
2
+
3
+ import { type Key, type Range } from '../interfaces/common.js';
4
+ import { type AztecAsyncMultiMap } from '../interfaces/map.js';
5
+ import { type AztecIDBSchema } from './store.js';
6
+
7
+ /**
8
+ * A map backed by IndexedDB.
9
+ */
10
+ export class IndexedDBAztecMap<K extends Key, V> implements AztecAsyncMultiMap<K, V> {
11
+ protected name: string;
12
+ #container: string;
13
+
14
+ #_db?: IDBPObjectStore<AztecIDBSchema, ['data'], 'data', 'readwrite'>;
15
+ #rootDB: IDBPDatabase<AztecIDBSchema>;
16
+
17
+ constructor(rootDB: IDBPDatabase<AztecIDBSchema>, mapName: string) {
18
+ this.name = mapName;
19
+ this.#container = `map:${mapName}`;
20
+ this.#rootDB = rootDB;
21
+ }
22
+
23
+ set db(db: IDBPObjectStore<AztecIDBSchema, ['data'], 'data', 'readwrite'> | undefined) {
24
+ this.#_db = db;
25
+ }
26
+
27
+ get db(): IDBPObjectStore<AztecIDBSchema, ['data'], 'data', 'readwrite'> {
28
+ return this.#_db ? this.#_db : this.#rootDB.transaction('data', 'readwrite').store;
29
+ }
30
+
31
+ async getAsync(key: K): Promise<V | undefined> {
32
+ const data = await this.db.get(this.#slot(key));
33
+ return data?.value as V;
34
+ }
35
+
36
+ async *getValuesAsync(key: K): AsyncIterableIterator<V> {
37
+ const index = this.db.index('keyCount');
38
+ const rangeQuery = IDBKeyRange.bound(
39
+ [this.#container, this.#normalizeKey(key), 0],
40
+ [this.#container, this.#normalizeKey(key), Number.MAX_SAFE_INTEGER],
41
+ false,
42
+ false,
43
+ );
44
+ for await (const cursor of index.iterate(rangeQuery)) {
45
+ yield cursor.value.value as V;
46
+ }
47
+ }
48
+
49
+ async hasAsync(key: K): Promise<boolean> {
50
+ const result = (await this.getAsync(key)) !== undefined;
51
+ return result;
52
+ }
53
+
54
+ async set(key: K, val: V): Promise<void> {
55
+ const count = await this.db
56
+ .index('key')
57
+ .count(IDBKeyRange.bound([this.#container, this.#normalizeKey(key)], [this.#container, this.#normalizeKey(key)]));
58
+ await this.db.put({
59
+ value: val,
60
+ container: this.#container,
61
+ key: this.#normalizeKey(key),
62
+ keyCount: count + 1,
63
+ slot: this.#slot(key, count),
64
+ });
65
+ }
66
+
67
+ swap(_key: K, _fn: (val: V | undefined) => V): Promise<void> {
68
+ throw new Error('Not implemented');
69
+ }
70
+
71
+ async setIfNotExists(key: K, val: V): Promise<boolean> {
72
+ if (!(await this.hasAsync(key))) {
73
+ await this.set(key, val);
74
+ return true;
75
+ }
76
+ return false;
77
+ }
78
+
79
+ async delete(key: K): Promise<void> {
80
+ await this.db.delete(this.#slot(key));
81
+ }
82
+
83
+ async deleteValue(key: K, val: V): Promise<void> {
84
+ const index = this.db.index('keyCount');
85
+ const rangeQuery = IDBKeyRange.bound(
86
+ [this.#container, this.#normalizeKey(key), 0],
87
+ [this.#container, this.#normalizeKey(key), Number.MAX_SAFE_INTEGER],
88
+ false,
89
+ false,
90
+ );
91
+ for await (const cursor of index.iterate(rangeQuery)) {
92
+ if (JSON.stringify(cursor.value.value) === JSON.stringify(val)) {
93
+ await cursor.delete();
94
+ return;
95
+ }
96
+ }
97
+ }
98
+
99
+ async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<[K, V]> {
100
+ const index = this.db.index('key');
101
+ const rangeQuery = IDBKeyRange.bound(
102
+ [this.#container, range.start ?? ''],
103
+ [this.#container, range.end ?? '\uffff'],
104
+ !!range.reverse,
105
+ !range.reverse,
106
+ );
107
+ let count = 0;
108
+ for await (const cursor of index.iterate(rangeQuery, range.reverse ? 'prev' : 'next')) {
109
+ if (range.limit && count >= range.limit) {
110
+ return;
111
+ }
112
+ yield [cursor.value.key, cursor.value.value] as [K, V];
113
+ count++;
114
+ }
115
+ }
116
+
117
+ async *valuesAsync(range: Range<K> = {}): AsyncIterableIterator<V> {
118
+ for await (const [_, value] of this.entriesAsync(range)) {
119
+ yield value;
120
+ }
121
+ }
122
+
123
+ async *keysAsync(range: Range<K> = {}): AsyncIterableIterator<K> {
124
+ for await (const [key, _] of this.entriesAsync(range)) {
125
+ yield this.#denormalizeKey(key as string);
126
+ }
127
+ }
128
+
129
+ #denormalizeKey(key: string): K {
130
+ const denormalizedKey = (key as string).split(',').map(part => (isNaN(parseInt(part)) ? part : parseInt(part)));
131
+ return (denormalizedKey.length > 1 ? denormalizedKey : key) as K;
132
+ }
133
+
134
+ #normalizeKey(key: K): string {
135
+ const arrayKey = Array.isArray(key) ? key : [key];
136
+ return arrayKey.join(',');
137
+ }
138
+
139
+ #slot(key: K, index: number = 0): string {
140
+ return `map:${this.name}:slot:${this.#normalizeKey(key)}:${index}`;
141
+ }
142
+ }
@@ -0,0 +1,37 @@
1
+ import { type IDBPDatabase, type IDBPObjectStore } from 'idb';
2
+
3
+ import { type Key, type Range } from '../interfaces/common.js';
4
+ import { type AztecAsyncSet } from '../interfaces/set.js';
5
+ import { IndexedDBAztecMap } from './map.js';
6
+ import { type AztecIDBSchema } from './store.js';
7
+
8
+ /**
9
+ * A set backed by IndexedDB.
10
+ */
11
+ export class IndexedDBAztecSet<K extends Key> implements AztecAsyncSet<K> {
12
+ private map: IndexedDBAztecMap<K, boolean>;
13
+
14
+ constructor(rootDb: IDBPDatabase<AztecIDBSchema>, mapName: string) {
15
+ this.map = new IndexedDBAztecMap(rootDb, mapName);
16
+ }
17
+
18
+ set db(db: IDBPObjectStore<AztecIDBSchema, ['data'], 'data', 'readwrite'> | undefined) {
19
+ this.map.db = db;
20
+ }
21
+
22
+ hasAsync(key: K): Promise<boolean> {
23
+ return this.map.hasAsync(key);
24
+ }
25
+
26
+ add(key: K): Promise<void> {
27
+ return this.map.set(key, true);
28
+ }
29
+
30
+ delete(key: K): Promise<void> {
31
+ return this.map.delete(key);
32
+ }
33
+
34
+ async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<K> {
35
+ yield* this.map.keysAsync(range);
36
+ }
37
+ }
@@ -0,0 +1,49 @@
1
+ import { type IDBPDatabase, type IDBPObjectStore } from 'idb';
2
+
3
+ import { type AztecAsyncSingleton } from '../interfaces/singleton.js';
4
+ import { type AztecIDBSchema } from './store.js';
5
+
6
+ /**
7
+ * Stores a single value in IndexedDB.
8
+ */
9
+ export class IndexedDBAztecSingleton<T> implements AztecAsyncSingleton<T> {
10
+ #_db?: IDBPObjectStore<AztecIDBSchema, ['data'], 'data', 'readwrite'>;
11
+ #rootDB: IDBPDatabase<AztecIDBSchema>;
12
+ #container: string;
13
+ #slot: string;
14
+
15
+ constructor(rootDB: IDBPDatabase<AztecIDBSchema>, name: string) {
16
+ this.#rootDB = rootDB;
17
+ this.#container = `singleton:${name}`;
18
+ this.#slot = `singleton:${name}:value`;
19
+ }
20
+
21
+ set db(db: IDBPObjectStore<AztecIDBSchema, ['data'], 'data', 'readwrite'> | undefined) {
22
+ this.#_db = db;
23
+ }
24
+
25
+ get db(): IDBPObjectStore<AztecIDBSchema, ['data'], 'data', 'readwrite'> {
26
+ return this.#_db ? this.#_db : this.#rootDB.transaction('data', 'readwrite').store;
27
+ }
28
+
29
+ async getAsync(): Promise<T | undefined> {
30
+ const data = await this.db.get(this.#slot);
31
+ return data?.value as T;
32
+ }
33
+
34
+ async set(val: T): Promise<boolean> {
35
+ const result = await this.db.put({
36
+ container: this.#container,
37
+ slot: this.#slot,
38
+ key: this.#slot,
39
+ keyCount: 1,
40
+ value: val,
41
+ });
42
+ return result !== undefined;
43
+ }
44
+
45
+ async delete(): Promise<boolean> {
46
+ await this.db.delete(this.#slot);
47
+ return true;
48
+ }
49
+ }
@@ -0,0 +1,192 @@
1
+ import { type Logger } from '@aztec/foundation/log';
2
+
3
+ import { type DBSchema, type IDBPDatabase, openDB } from 'idb';
4
+
5
+ import { type AztecAsyncArray } from '../interfaces/array.js';
6
+ import { type Key } from '../interfaces/common.js';
7
+ import { type AztecAsyncCounter } from '../interfaces/counter.js';
8
+ import { type AztecAsyncMap, type AztecAsyncMultiMap } from '../interfaces/map.js';
9
+ import { type AztecAsyncSet } from '../interfaces/set.js';
10
+ import { type AztecAsyncSingleton } from '../interfaces/singleton.js';
11
+ import { type AztecAsyncKVStore } from '../interfaces/store.js';
12
+ import { IndexedDBAztecArray } from './array.js';
13
+ import { IndexedDBAztecMap } from './map.js';
14
+ import { IndexedDBAztecSet } from './set.js';
15
+ import { IndexedDBAztecSingleton } from './singleton.js';
16
+
17
+ export type StoredData<V> = { value: V; container: string; key: string; keyCount: number; slot: string };
18
+
19
+ export interface AztecIDBSchema extends DBSchema {
20
+ data: {
21
+ value: StoredData<any>;
22
+ key: string;
23
+ indexes: { container: string; key: string; keyCount: number };
24
+ };
25
+ }
26
+
27
+ /**
28
+ * A key-value store backed by IndexedDB.
29
+ */
30
+
31
+ export class AztecIndexedDBStore implements AztecAsyncKVStore {
32
+ #log: Logger;
33
+ #rootDB: IDBPDatabase<AztecIDBSchema>;
34
+ #name: string;
35
+
36
+ #containers = new Set<
37
+ IndexedDBAztecArray<any> | IndexedDBAztecMap<any, any> | IndexedDBAztecSet<any> | IndexedDBAztecSingleton<any>
38
+ >();
39
+
40
+ constructor(rootDB: IDBPDatabase<AztecIDBSchema>, public readonly isEphemeral: boolean, log: Logger, name: string) {
41
+ this.#rootDB = rootDB;
42
+ this.#log = log;
43
+ this.#name = name;
44
+ }
45
+ /**
46
+ * Creates a new AztecKVStore backed by IndexedDB. The path to the database is optional. If not provided,
47
+ * the database will be stored in a temporary location and be deleted when the process exists.
48
+ *
49
+ *
50
+ * @param path - A path on the disk to store the database. Optional
51
+ * @param ephemeral - true if the store should only exist in memory and not automatically be flushed to disk. Optional
52
+ * @param log - A logger to use. Optional
53
+ * @returns The store
54
+ */
55
+ static async open(log: Logger, name?: string, ephemeral: boolean = false): Promise<AztecIndexedDBStore> {
56
+ name = name && !ephemeral ? name : self.crypto.getRandomValues(new Uint8Array(16)).join('');
57
+ log.debug(`Opening IndexedDB ${ephemeral ? 'temp ' : ''}database with name ${name}`);
58
+ const rootDB = await openDB<AztecIDBSchema>(name, 1, {
59
+ upgrade(db) {
60
+ const objectStore = db.createObjectStore('data', { keyPath: 'slot' });
61
+
62
+ objectStore.createIndex('key', ['container', 'key'], { unique: false });
63
+ objectStore.createIndex('keyCount', ['container', 'key', 'keyCount'], { unique: false });
64
+ },
65
+ });
66
+
67
+ const kvStore = new AztecIndexedDBStore(rootDB, ephemeral, log, name);
68
+ return kvStore;
69
+ }
70
+
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
+ * Creates a new AztecMap in the store.
96
+ * @param name - Name of the map
97
+ * @returns A new AztecMap
98
+ */
99
+ openMap<K extends Key, V>(name: string): AztecAsyncMap<K, V> {
100
+ const map = new IndexedDBAztecMap<K, V>(this.#rootDB, name);
101
+ this.#containers.add(map);
102
+ return map;
103
+ }
104
+
105
+ /**
106
+ * Creates a new AztecSet in the store.
107
+ * @param name - Name of the set
108
+ * @returns A new AztecSet
109
+ */
110
+ openSet<K extends Key>(name: string): AztecAsyncSet<K> {
111
+ const set = new IndexedDBAztecSet<K>(this.#rootDB, name);
112
+ this.#containers.add(set);
113
+ return set;
114
+ }
115
+
116
+ /**
117
+ * Creates a new AztecMultiMap in the store. A multi-map stores multiple values for a single key automatically.
118
+ * @param name - Name of the map
119
+ * @returns A new AztecMultiMap
120
+ */
121
+ openMultiMap<K extends Key, V>(name: string): AztecAsyncMultiMap<K, V> {
122
+ const multimap = new IndexedDBAztecMap<K, V>(this.#rootDB, name);
123
+ this.#containers.add(multimap);
124
+ return multimap;
125
+ }
126
+
127
+ openCounter<K extends Key | Array<string | number>>(_name: string): AztecAsyncCounter<K> {
128
+ throw new Error('Method not implemented.');
129
+ }
130
+
131
+ /**
132
+ * Creates a new AztecArray in the store.
133
+ * @param name - Name of the array
134
+ * @returns A new AztecArray
135
+ */
136
+ openArray<T>(name: string): AztecAsyncArray<T> {
137
+ const array = new IndexedDBAztecArray<T>(this.#rootDB, name);
138
+ this.#containers.add(array);
139
+ return array;
140
+ }
141
+
142
+ /**
143
+ * Creates a new AztecSingleton in the store.
144
+ * @param name - Name of the singleton
145
+ * @returns A new AztecSingleton
146
+ */
147
+ openSingleton<T>(name: string): AztecAsyncSingleton<T> {
148
+ const singleton = new IndexedDBAztecSingleton<T>(this.#rootDB, name);
149
+ this.#containers.add(singleton);
150
+ return singleton;
151
+ }
152
+
153
+ /**
154
+ * Runs a callback in a transaction.
155
+ * @param callback - Function to execute in a transaction
156
+ * @returns A promise that resolves to the return value of the callback
157
+ */
158
+ async transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
159
+ const tx = this.#rootDB.transaction('data', 'readwrite');
160
+ for (const container of this.#containers) {
161
+ container.db = tx.store;
162
+ }
163
+ // Avoid awaiting this promise so it doesn't get scheduled in the next microtask
164
+ // By then, the tx would be closed
165
+ const runningPromise = callback();
166
+ // Wait for the transaction to finish
167
+ await tx.done;
168
+ for (const container of this.#containers) {
169
+ container.db = undefined;
170
+ }
171
+ // Return the result of the callback.
172
+ // Tx is guaranteed to already be closed, so the await doesn't hurt anything here
173
+ return await runningPromise;
174
+ }
175
+
176
+ /**
177
+ * Clears all entries in the store & sub DBs.
178
+ */
179
+ async clear() {
180
+ await this.#rootDB.transaction('data', 'readwrite').store.clear();
181
+ }
182
+
183
+ /** Deletes this store and removes the database */
184
+ delete() {
185
+ this.#containers.clear();
186
+ return Promise.resolve(this.#rootDB.deleteObjectStore('data'));
187
+ }
188
+
189
+ estimateSize(): { mappingSize: number; actualSize: number; numItems: number } {
190
+ return { mappingSize: 0, actualSize: 0, numItems: 0 };
191
+ }
192
+ }
@@ -1,12 +1,7 @@
1
1
  /**
2
2
  * An array backed by a persistent store. Can not have any holes in it.
3
3
  */
4
- export interface AztecArray<T> {
5
- /**
6
- * The size of the array
7
- */
8
- length: number;
9
-
4
+ interface BaseAztecArray<T> {
10
5
  /**
11
6
  * Pushes values to the end of the array
12
7
  * @param vals - The values to push to the end of the array
@@ -20,6 +15,24 @@ export interface AztecArray<T> {
20
15
  */
21
16
  pop(): Promise<T | undefined>;
22
17
 
18
+ /**
19
+ * Updates the value at the given index. Index can be in the range [-length, length - 1).
20
+ * @param index - The index to set the value at
21
+ * @param val - The value to set
22
+ * @returns Whether the value was set
23
+ */
24
+ setAt(index: number, val: T): Promise<boolean>;
25
+ }
26
+
27
+ /**
28
+ * An array backed by a persistent store. Can not have any holes in it.
29
+ */
30
+ export interface AztecAsyncArray<T> extends BaseAztecArray<T> {
31
+ /**
32
+ * The size of the array
33
+ */
34
+ lengthAsync(): Promise<number>;
35
+
23
36
  /**
24
37
  * Gets the value at the given index. Index can be in the range [-length, length - 1).
25
38
  * If the index is negative, it will be treated as an offset from the end of the array.
@@ -27,15 +40,38 @@ export interface AztecArray<T> {
27
40
  * @param index - The index to get the value from
28
41
  * @returns The value at the given index or undefined if the index is out of bounds
29
42
  */
30
- at(index: number): T | undefined;
43
+ atAsync(index: number): Promise<T | undefined>;
31
44
 
32
45
  /**
33
- * Updates the value at the given index. Index can be in the range [-length, length - 1).
34
- * @param index - The index to set the value at
35
- * @param val - The value to set
36
- * @returns Whether the value was set
46
+ * Iterates over the array with indexes.
37
47
  */
38
- setAt(index: number, val: T): Promise<boolean>;
48
+ entriesAsync(): AsyncIterableIterator<[number, T]>;
49
+
50
+ /**
51
+ * Iterates over the array.
52
+ */
53
+ valuesAsync(): AsyncIterableIterator<T>;
54
+
55
+ /**
56
+ * Iterates over the array.
57
+ */
58
+ [Symbol.asyncIterator](): AsyncIterableIterator<T>;
59
+ }
60
+
61
+ export interface AztecArray<T> extends BaseAztecArray<T> {
62
+ /**
63
+ * The size of the array
64
+ */
65
+ length: number;
66
+
67
+ /**
68
+ * Gets the value at the given index. Index can be in the range [-length, length - 1).
69
+ * If the index is negative, it will be treated as an offset from the end of the array.
70
+ *
71
+ * @param index - The index to get the value from
72
+ * @returns The value at the given index or undefined if the index is out of bounds
73
+ */
74
+ at(index: number): T | undefined;
39
75
 
40
76
  /**
41
77
  * Iterates over the array with indexes.
@@ -0,0 +1,126 @@
1
+ import { toArray } from '@aztec/foundation/iterable';
2
+
3
+ import { expect } from 'chai';
4
+
5
+ import { type AztecArray, type AztecAsyncArray } from './array.js';
6
+ import { type AztecAsyncKVStore, type AztecKVStore } from './store.js';
7
+ import { isSyncStore } from './utils.js';
8
+
9
+ export function describeAztecArray(
10
+ testName: string,
11
+ getStore: () => AztecKVStore | Promise<AztecAsyncKVStore>,
12
+ forceAsync: boolean = false,
13
+ ) {
14
+ describe(testName, () => {
15
+ let store: AztecKVStore | AztecAsyncKVStore;
16
+ let arr: AztecArray<number> | AztecAsyncArray<number>;
17
+
18
+ beforeEach(async () => {
19
+ store = await getStore();
20
+ arr = store.openArray<number>('test');
21
+ });
22
+
23
+ async function length(sut: AztecAsyncArray<number> | AztecArray<number> = arr) {
24
+ return isSyncStore(store) && !forceAsync
25
+ ? (sut as AztecArray<number>).length
26
+ : await (sut as AztecAsyncArray<number>).lengthAsync();
27
+ }
28
+
29
+ async function at(index: number) {
30
+ return isSyncStore(store) && !forceAsync
31
+ ? (arr as AztecArray<number>).at(index)
32
+ : await (arr as AztecAsyncArray<number>).atAsync(index);
33
+ }
34
+
35
+ async function entries() {
36
+ return isSyncStore(store) && !forceAsync
37
+ ? await toArray((arr as AztecArray<number>).entries())
38
+ : await toArray((arr as AztecAsyncArray<number>).entriesAsync());
39
+ }
40
+
41
+ async function values(sut: AztecAsyncArray<number> | AztecArray<number> = arr) {
42
+ return isSyncStore(store) && !forceAsync
43
+ ? await toArray((sut as AztecArray<number>).values())
44
+ : await toArray((sut as AztecAsyncArray<number>).valuesAsync());
45
+ }
46
+
47
+ it('should be able to push and pop values', async () => {
48
+ await arr.push(1);
49
+ await arr.push(2);
50
+ await arr.push(3);
51
+
52
+ expect(await length()).to.equal(3);
53
+
54
+ expect(await arr.pop()).to.equal(3);
55
+ expect(await arr.pop()).to.equal(2);
56
+ expect(await arr.pop()).to.equal(1);
57
+ expect(await arr.pop()).to.equal(undefined);
58
+ });
59
+
60
+ it('should be able to get values by index', async () => {
61
+ await arr.push(1);
62
+ await arr.push(2);
63
+ await arr.push(3);
64
+
65
+ expect(await at(0)).to.equal(1);
66
+ expect(await at(1)).to.equal(2);
67
+ expect(await at(2)).to.equal(3);
68
+ expect(await at(3)).to.equal(undefined);
69
+ expect(await at(-1)).to.equal(3);
70
+ expect(await at(-2)).to.equal(2);
71
+ expect(await at(-3)).to.equal(1);
72
+ expect(await at(-4)).to.equal(undefined);
73
+ });
74
+
75
+ it('should be able to set values by index', async () => {
76
+ await arr.push(1);
77
+ await arr.push(2);
78
+ await arr.push(3);
79
+
80
+ expect(await arr.setAt(0, 4)).to.equal(true);
81
+ expect(await arr.setAt(1, 5)).to.equal(true);
82
+ expect(await arr.setAt(2, 6)).to.equal(true);
83
+
84
+ expect(await arr.setAt(3, 7)).to.equal(false);
85
+
86
+ expect(await at(0)).to.equal(4);
87
+ expect(await at(1)).to.equal(5);
88
+ expect(await at(2)).to.equal(6);
89
+ expect(await at(3)).to.equal(undefined);
90
+
91
+ expect(await arr.setAt(-1, 8)).to.equal(true);
92
+ expect(await arr.setAt(-2, 9)).to.equal(true);
93
+ expect(await arr.setAt(-3, 10)).to.equal(true);
94
+
95
+ expect(await arr.setAt(-4, 11)).to.equal(false);
96
+
97
+ expect(await at(-1)).to.equal(8);
98
+ expect(await at(-2)).to.equal(9);
99
+ expect(await at(-3)).to.equal(10);
100
+ expect(await at(-4)).to.equal(undefined);
101
+ });
102
+
103
+ it('should be able to iterate over values', async () => {
104
+ await arr.push(1);
105
+ await arr.push(2);
106
+ await arr.push(3);
107
+
108
+ expect(await values()).to.deep.equal([1, 2, 3]);
109
+ expect(await entries()).to.deep.equal([
110
+ [0, 1],
111
+ [1, 2],
112
+ [2, 3],
113
+ ]);
114
+ });
115
+
116
+ it('should be able to restore state', async () => {
117
+ await arr.push(1);
118
+ await arr.push(2);
119
+ await arr.push(3);
120
+
121
+ const arr2 = store.openArray<number>('test');
122
+ expect(await length(arr2)).to.equal(3);
123
+ expect(await values(arr2)).to.deep.equal(await values());
124
+ });
125
+ });
126
+ }
@@ -6,7 +6,7 @@ import { type Key, type Range } from './common.js';
6
6
  *
7
7
  * Keys are stored in sorted order
8
8
  */
9
- export interface AztecCounter<K extends Key = Key> {
9
+ interface AztecBaseCounter<K extends Key = Key> {
10
10
  /**
11
11
  * Resets the count of the given key to the given value.
12
12
  * @param key - The key to reset
@@ -22,7 +22,9 @@ export interface AztecCounter<K extends Key = Key> {
22
22
  * @param delta - The amount to modify the key by
23
23
  */
24
24
  update(key: K, delta: number): Promise<void>;
25
+ }
25
26
 
27
+ export interface AztecCounter<K extends Key = Key> extends AztecBaseCounter<K> {
26
28
  /**
27
29
  * Gets the current count.
28
30
  * @param key - The key to get the count of
@@ -41,3 +43,23 @@ export interface AztecCounter<K extends Key = Key> {
41
43
  */
42
44
  entries(range: Range<K>): IterableIterator<[K, number]>;
43
45
  }
46
+
47
+ export interface AztecAsyncCounter<K extends Key = Key> extends AztecBaseCounter<K> {
48
+ /**
49
+ * Gets the current count.
50
+ * @param key - The key to get the count of
51
+ */
52
+ getAsync(key: K): Promise<number>;
53
+
54
+ /**
55
+ * Returns keys in the map in sorted order. Only returns keys that have been seen at least once.
56
+ * @param range - The range of keys to iterate over
57
+ */
58
+ keysAsync(range: Range<K>): AsyncIterableIterator<K>;
59
+
60
+ /**
61
+ * Returns keys and their counts in the map sorted by the key. Only returns keys that have been seen at least once.
62
+ * @param range - The range of keys to iterate over
63
+ */
64
+ entriesAsync(range: Range<K>): AsyncIterableIterator<[K, number]>;
65
+ }