@aztec/kv-store 0.0.0-test.0 → 0.0.1-commit.001888fc

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 (163) hide show
  1. package/dest/indexeddb/array.d.ts +3 -2
  2. package/dest/indexeddb/array.d.ts.map +1 -1
  3. package/dest/indexeddb/array.js +24 -7
  4. package/dest/indexeddb/index.d.ts +3 -3
  5. package/dest/indexeddb/index.d.ts.map +1 -1
  6. package/dest/indexeddb/index.js +4 -7
  7. package/dest/indexeddb/map.d.ts +18 -6
  8. package/dest/indexeddb/map.d.ts.map +1 -1
  9. package/dest/indexeddb/map.js +51 -63
  10. package/dest/indexeddb/multi_map.d.ts +14 -0
  11. package/dest/indexeddb/multi_map.d.ts.map +1 -0
  12. package/dest/indexeddb/multi_map.js +107 -0
  13. package/dest/indexeddb/set.d.ts +1 -1
  14. package/dest/indexeddb/set.d.ts.map +1 -1
  15. package/dest/indexeddb/singleton.d.ts +3 -2
  16. package/dest/indexeddb/singleton.d.ts.map +1 -1
  17. package/dest/indexeddb/singleton.js +6 -2
  18. package/dest/indexeddb/store.d.ts +15 -16
  19. package/dest/indexeddb/store.d.ts.map +1 -1
  20. package/dest/indexeddb/store.js +51 -46
  21. package/dest/interfaces/array.d.ts +5 -4
  22. package/dest/interfaces/array.d.ts.map +1 -1
  23. package/dest/interfaces/array.js +1 -3
  24. package/dest/interfaces/array_test_suite.d.ts +1 -1
  25. package/dest/interfaces/common.d.ts +11 -9
  26. package/dest/interfaces/common.d.ts.map +1 -1
  27. package/dest/interfaces/common.js +8 -3
  28. package/dest/interfaces/counter.d.ts +1 -1
  29. package/dest/interfaces/index.d.ts +4 -2
  30. package/dest/interfaces/index.d.ts.map +1 -1
  31. package/dest/interfaces/index.js +2 -0
  32. package/dest/interfaces/map.d.ts +21 -49
  33. package/dest/interfaces/map.d.ts.map +1 -1
  34. package/dest/interfaces/map.js +1 -1
  35. package/dest/interfaces/map_test_suite.d.ts +1 -1
  36. package/dest/interfaces/map_test_suite.d.ts.map +1 -1
  37. package/dest/interfaces/map_test_suite.js +181 -70
  38. package/dest/interfaces/multi_map.d.ts +41 -0
  39. package/dest/interfaces/multi_map.d.ts.map +1 -0
  40. package/dest/interfaces/multi_map.js +3 -0
  41. package/dest/interfaces/multi_map_test_suite.d.ts +3 -0
  42. package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -0
  43. package/dest/interfaces/multi_map_test_suite.js +364 -0
  44. package/dest/interfaces/set.d.ts +1 -1
  45. package/dest/interfaces/set_test_suite.d.ts +1 -1
  46. package/dest/interfaces/singleton.d.ts +1 -1
  47. package/dest/interfaces/singleton_test_suite.d.ts +1 -1
  48. package/dest/interfaces/store.d.ts +18 -43
  49. package/dest/interfaces/store.d.ts.map +1 -1
  50. package/dest/interfaces/utils.d.ts +3 -1
  51. package/dest/interfaces/utils.d.ts.map +1 -1
  52. package/dest/interfaces/utils.js +3 -1
  53. package/dest/lmdb/array.d.ts +3 -2
  54. package/dest/lmdb/array.d.ts.map +1 -1
  55. package/dest/lmdb/array.js +4 -2
  56. package/dest/lmdb/counter.d.ts +1 -1
  57. package/dest/lmdb/counter.d.ts.map +1 -1
  58. package/dest/lmdb/index.d.ts +3 -3
  59. package/dest/lmdb/index.d.ts.map +1 -1
  60. package/dest/lmdb/index.js +5 -5
  61. package/dest/lmdb/map.d.ts +11 -23
  62. package/dest/lmdb/map.d.ts.map +1 -1
  63. package/dest/lmdb/map.js +15 -81
  64. package/dest/lmdb/multi_map.d.ts +13 -0
  65. package/dest/lmdb/multi_map.d.ts.map +1 -0
  66. package/dest/lmdb/multi_map.js +44 -0
  67. package/dest/lmdb/set.d.ts +1 -1
  68. package/dest/lmdb/set.d.ts.map +1 -1
  69. package/dest/lmdb/singleton.d.ts +1 -1
  70. package/dest/lmdb/singleton.d.ts.map +1 -1
  71. package/dest/lmdb/store.d.ts +11 -27
  72. package/dest/lmdb/store.d.ts.map +1 -1
  73. package/dest/lmdb/store.js +24 -40
  74. package/dest/lmdb-v2/array.d.ts +4 -3
  75. package/dest/lmdb-v2/array.d.ts.map +1 -1
  76. package/dest/lmdb-v2/array.js +4 -2
  77. package/dest/lmdb-v2/factory.d.ts +7 -10
  78. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  79. package/dest/lmdb-v2/factory.js +26 -12
  80. package/dest/lmdb-v2/index.d.ts +1 -1
  81. package/dest/lmdb-v2/map.d.ts +12 -45
  82. package/dest/lmdb-v2/map.d.ts.map +1 -1
  83. package/dest/lmdb-v2/map.js +17 -104
  84. package/dest/lmdb-v2/message.d.ts +24 -5
  85. package/dest/lmdb-v2/message.d.ts.map +1 -1
  86. package/dest/lmdb-v2/message.js +6 -4
  87. package/dest/lmdb-v2/multi_map.d.ts +52 -0
  88. package/dest/lmdb-v2/multi_map.d.ts.map +1 -0
  89. package/dest/lmdb-v2/multi_map.js +123 -0
  90. package/dest/lmdb-v2/read_transaction.d.ts +3 -1
  91. package/dest/lmdb-v2/read_transaction.d.ts.map +1 -1
  92. package/dest/lmdb-v2/read_transaction.js +34 -0
  93. package/dest/lmdb-v2/set.d.ts +15 -0
  94. package/dest/lmdb-v2/set.d.ts.map +1 -0
  95. package/dest/lmdb-v2/set.js +23 -0
  96. package/dest/lmdb-v2/singleton.d.ts +2 -2
  97. package/dest/lmdb-v2/singleton.d.ts.map +1 -1
  98. package/dest/lmdb-v2/singleton.js +1 -1
  99. package/dest/lmdb-v2/store.d.ts +13 -13
  100. package/dest/lmdb-v2/store.d.ts.map +1 -1
  101. package/dest/lmdb-v2/store.js +19 -29
  102. package/dest/lmdb-v2/tx-helpers.d.ts +6 -0
  103. package/dest/lmdb-v2/tx-helpers.d.ts.map +1 -0
  104. package/dest/lmdb-v2/tx-helpers.js +21 -0
  105. package/dest/lmdb-v2/utils.d.ts +3 -14
  106. package/dest/lmdb-v2/utils.d.ts.map +1 -1
  107. package/dest/lmdb-v2/utils.js +0 -94
  108. package/dest/lmdb-v2/write_transaction.d.ts +3 -5
  109. package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
  110. package/dest/lmdb-v2/write_transaction.js +9 -5
  111. package/dest/stores/index.d.ts +2 -1
  112. package/dest/stores/index.d.ts.map +1 -1
  113. package/dest/stores/index.js +1 -0
  114. package/dest/stores/l2_tips_store.d.ts +24 -8
  115. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  116. package/dest/stores/l2_tips_store.js +63 -45
  117. package/dest/utils.d.ts +9 -6
  118. package/dest/utils.d.ts.map +1 -1
  119. package/dest/utils.js +51 -16
  120. package/package.json +30 -22
  121. package/src/indexeddb/array.ts +9 -5
  122. package/src/indexeddb/index.ts +11 -9
  123. package/src/indexeddb/map.ts +51 -56
  124. package/src/indexeddb/multi_map.ts +105 -0
  125. package/src/indexeddb/singleton.ts +5 -2
  126. package/src/indexeddb/store.ts +77 -60
  127. package/src/interfaces/array.ts +5 -3
  128. package/src/interfaces/common.ts +20 -9
  129. package/src/interfaces/index.ts +3 -1
  130. package/src/interfaces/map.ts +19 -53
  131. package/src/interfaces/map_test_suite.ts +101 -44
  132. package/src/interfaces/multi_map.ts +45 -0
  133. package/src/interfaces/multi_map_test_suite.ts +374 -0
  134. package/src/interfaces/store.ts +18 -53
  135. package/src/interfaces/utils.ts +2 -0
  136. package/src/lmdb/array.ts +2 -1
  137. package/src/lmdb/index.ts +12 -7
  138. package/src/lmdb/map.ts +23 -94
  139. package/src/lmdb/multi_map.ts +51 -0
  140. package/src/lmdb/store.ts +36 -56
  141. package/src/lmdb-v2/array.ts +8 -3
  142. package/src/lmdb-v2/factory.ts +30 -19
  143. package/src/lmdb-v2/map.ts +30 -127
  144. package/src/lmdb-v2/message.ts +23 -0
  145. package/src/lmdb-v2/multi_map.ts +150 -0
  146. package/src/lmdb-v2/read_transaction.ts +40 -0
  147. package/src/lmdb-v2/set.ts +33 -0
  148. package/src/lmdb-v2/singleton.ts +6 -2
  149. package/src/lmdb-v2/store.ts +24 -42
  150. package/src/lmdb-v2/tx-helpers.ts +29 -0
  151. package/src/lmdb-v2/utils.ts +0 -118
  152. package/src/lmdb-v2/write_transaction.ts +11 -10
  153. package/src/stores/index.ts +2 -0
  154. package/src/stores/l2_tips_store.ts +67 -49
  155. package/src/utils.ts +79 -21
  156. package/dest/config.d.ts +0 -17
  157. package/dest/config.d.ts.map +0 -1
  158. package/dest/config.js +0 -24
  159. package/dest/interfaces/store_test_suite.d.ts +0 -3
  160. package/dest/interfaces/store_test_suite.d.ts.map +0 -1
  161. package/dest/interfaces/store_test_suite.js +0 -37
  162. package/src/config.ts +0 -34
  163. package/src/interfaces/store_test_suite.ts +0 -56
package/src/lmdb/map.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { Database, RangeOptions } from 'lmdb';
2
2
 
3
- import type { Key, Range } from '../interfaces/common.js';
4
- import type { AztecAsyncMultiMap, AztecMapWithSize, AztecMultiMap } from '../interfaces/map.js';
3
+ import type { Key, Range, Value } from '../interfaces/common.js';
4
+ import type { AztecAsyncMap, AztecMap } from '../interfaces/map.js';
5
5
 
6
6
  /** The slot where a key-value entry would be stored */
7
7
  type MapValueSlot<K extends Key | Buffer> = ['map', string, 'slot', K];
@@ -9,7 +9,7 @@ type MapValueSlot<K extends Key | Buffer> = ['map', string, 'slot', K];
9
9
  /**
10
10
  * A map backed by LMDB.
11
11
  */
12
- export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, AztecAsyncMultiMap<K, V> {
12
+ export class LmdbAztecMap<K extends Key, V extends Value> implements AztecMap<K, V>, AztecAsyncMap<K, V> {
13
13
  protected db: Database<[K, V], MapValueSlot<K>>;
14
14
  protected name: string;
15
15
 
@@ -39,26 +39,6 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
39
39
  return Promise.resolve(this.get(key));
40
40
  }
41
41
 
42
- *getValues(key: K): IterableIterator<V> {
43
- const transaction = this.db.useReadTransaction();
44
- try {
45
- const values = this.db.getValues(this.slot(key), {
46
- transaction,
47
- });
48
- for (const value of values) {
49
- yield value?.[1];
50
- }
51
- } finally {
52
- transaction.done();
53
- }
54
- }
55
-
56
- async *getValuesAsync(key: K): AsyncIterableIterator<V> {
57
- for (const value of this.getValues(key)) {
58
- yield value;
59
- }
60
- }
61
-
62
42
  has(key: K): boolean {
63
43
  return this.db.doesExist(this.slot(key));
64
44
  }
@@ -71,6 +51,12 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
71
51
  await this.db.put(this.slot(key), [key, val]);
72
52
  }
73
53
 
54
+ async setMany(entries: { key: K; value: V }[]): Promise<void> {
55
+ for (const { key, value } of entries) {
56
+ await this.set(key, value);
57
+ }
58
+ }
59
+
74
60
  swap(key: K, fn: (val: V | undefined) => V): Promise<void> {
75
61
  return this.db.childTransaction(() => {
76
62
  const slot = this.slot(key);
@@ -90,10 +76,6 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
90
76
  await this.db.remove(this.slot(key));
91
77
  }
92
78
 
93
- async deleteValue(key: K, val: V): Promise<void> {
94
- await this.db.remove(this.slot(key), [key, val]);
95
- }
96
-
97
79
  *entries(range: Range<K> = {}): IterableIterator<[K, V]> {
98
80
  const transaction = this.db.useReadTransaction();
99
81
 
@@ -106,16 +88,16 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
106
88
  ? this.slot(range.end)
107
89
  : this.endSentinel
108
90
  : range.start
109
- ? this.slot(range.start)
110
- : this.startSentinel;
91
+ ? this.slot(range.start)
92
+ : this.startSentinel;
111
93
 
112
94
  const end = reverse
113
95
  ? range.start
114
96
  ? this.slot(range.start)
115
97
  : this.startSentinel
116
98
  : range.end
117
- ? this.slot(range.end)
118
- : this.endSentinel;
99
+ ? this.slot(range.end)
100
+ : this.endSentinel;
119
101
 
120
102
  const lmdbRange: RangeOptions = {
121
103
  start,
@@ -137,7 +119,7 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
137
119
  }
138
120
  }
139
121
 
140
- async *entriesAsync(range?: Range<K> | undefined): AsyncIterableIterator<[K, V]> {
122
+ async *entriesAsync(range?: Range<K>): AsyncIterableIterator<[K, V]> {
141
123
  for (const entry of this.entries(range)) {
142
124
  yield entry;
143
125
  }
@@ -155,6 +137,15 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
155
137
  }
156
138
  }
157
139
 
140
+ size(): number {
141
+ const iterator = this.db.getRange({ start: this.startSentinel, end: this.endSentinel });
142
+ return iterator.asArray.length;
143
+ }
144
+
145
+ sizeAsync(): Promise<number> {
146
+ return Promise.resolve(this.size());
147
+ }
148
+
158
149
  *keys(range: Range<K> = {}): IterableIterator<K> {
159
150
  for (const [key, _] of this.entries(range)) {
160
151
  yield key;
@@ -184,65 +175,3 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
184
175
  }
185
176
  }
186
177
  }
187
-
188
- export class LmdbAztecMapWithSize<K extends Key, V>
189
- extends LmdbAztecMap<K, V>
190
- implements AztecMapWithSize<K, V>, AztecAsyncMultiMap<K, V>
191
- {
192
- #sizeCache?: number;
193
-
194
- constructor(rootDb: Database, mapName: string) {
195
- super(rootDb, mapName);
196
- }
197
-
198
- override async set(key: K, val: V): Promise<void> {
199
- await this.db.childTransaction(() => {
200
- const exists = this.db.doesExist(this.slot(key));
201
- this.db.putSync(this.slot(key), [key, val], {
202
- appendDup: true,
203
- });
204
- if (!exists) {
205
- this.#sizeCache = undefined; // Invalidate cache
206
- }
207
- });
208
- }
209
-
210
- override async delete(key: K): Promise<void> {
211
- await this.db.childTransaction(async () => {
212
- const exists = this.db.doesExist(this.slot(key));
213
- if (exists) {
214
- await this.db.remove(this.slot(key));
215
- this.#sizeCache = undefined; // Invalidate cache
216
- }
217
- });
218
- }
219
-
220
- override async deleteValue(key: K, val: V): Promise<void> {
221
- await this.db.childTransaction(async () => {
222
- const exists = this.db.doesExist(this.slot(key));
223
- if (exists) {
224
- await this.db.remove(this.slot(key), [key, val]);
225
- this.#sizeCache = undefined; // Invalidate cache
226
- }
227
- });
228
- }
229
-
230
- /**
231
- * Gets the size of the map by counting entries.
232
- * @returns The number of entries in the map
233
- */
234
- size(): number {
235
- if (this.#sizeCache === undefined) {
236
- this.#sizeCache = this.db.getCount({
237
- start: this.startSentinel,
238
- end: this.endSentinel,
239
- });
240
- }
241
- return this.#sizeCache;
242
- }
243
-
244
- // Reset cache on clear/drop operations
245
- clearCache() {
246
- this.#sizeCache = undefined;
247
- }
248
- }
@@ -0,0 +1,51 @@
1
+ import type { Key, Value } from '../interfaces/common.js';
2
+ import type { AztecAsyncMultiMap, AztecMultiMap } from '../interfaces/multi_map.js';
3
+ import { LmdbAztecMap } from './map.js';
4
+
5
+ /**
6
+ * A map backed by LMDB.
7
+ */
8
+ export class LmdbAztecMultiMap<K extends Key, V extends Value>
9
+ extends LmdbAztecMap<K, V>
10
+ implements AztecMultiMap<K, V>, AztecAsyncMultiMap<K, V>
11
+ {
12
+ *getValues(key: K): IterableIterator<V> {
13
+ const transaction = this.db.useReadTransaction();
14
+ try {
15
+ const values = this.db.getValues(this.slot(key), {
16
+ transaction,
17
+ });
18
+ for (const value of values) {
19
+ yield value?.[1];
20
+ }
21
+ } finally {
22
+ transaction.done();
23
+ }
24
+ }
25
+
26
+ async *getValuesAsync(key: K): AsyncIterableIterator<V> {
27
+ for (const value of this.getValues(key)) {
28
+ yield value;
29
+ }
30
+ }
31
+
32
+ getValueCountAsync(key: K): Promise<number> {
33
+ const transaction = this.db.useReadTransaction();
34
+ try {
35
+ const values = this.db.getValues(this.slot(key), {
36
+ transaction,
37
+ });
38
+ let count = 0;
39
+ for (const _ of values) {
40
+ count++;
41
+ }
42
+ return Promise.resolve(count);
43
+ } finally {
44
+ transaction.done();
45
+ }
46
+ }
47
+
48
+ async deleteValue(key: K, val: V): Promise<void> {
49
+ await this.db.remove(this.slot(key), [key, val]);
50
+ }
51
+ }
package/src/lmdb/store.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { randomBytes } from '@aztec/foundation/crypto';
1
+ import { randomBytes } from '@aztec/foundation/crypto/random';
2
2
  import { createLogger } from '@aztec/foundation/log';
3
3
 
4
4
  import { promises as fs, mkdirSync } from 'fs';
@@ -7,22 +7,17 @@ import { tmpdir } from 'os';
7
7
  import { join } from 'path';
8
8
 
9
9
  import type { AztecArray, AztecAsyncArray } from '../interfaces/array.js';
10
- import type { Key, StoreSize } from '../interfaces/common.js';
10
+ import type { Key, StoreSize, Value } from '../interfaces/common.js';
11
11
  import type { AztecAsyncCounter, AztecCounter } from '../interfaces/counter.js';
12
- import type {
13
- AztecAsyncMap,
14
- AztecAsyncMultiMap,
15
- AztecMap,
16
- AztecMapWithSize,
17
- AztecMultiMap,
18
- AztecMultiMapWithSize,
19
- } from '../interfaces/map.js';
12
+ import type { AztecAsyncMap, AztecMap } from '../interfaces/map.js';
13
+ import type { AztecAsyncMultiMap, AztecMultiMap } from '../interfaces/multi_map.js';
20
14
  import type { AztecAsyncSet, AztecSet } from '../interfaces/set.js';
21
15
  import type { AztecAsyncSingleton, AztecSingleton } from '../interfaces/singleton.js';
22
16
  import type { AztecAsyncKVStore, AztecKVStore } from '../interfaces/store.js';
23
17
  import { LmdbAztecArray } from './array.js';
24
18
  import { LmdbAztecCounter } from './counter.js';
25
- import { LmdbAztecMap, LmdbAztecMapWithSize } from './map.js';
19
+ import { LmdbAztecMap } from './map.js';
20
+ import { LmdbAztecMultiMap } from './multi_map.js';
26
21
  import { LmdbAztecSet } from './set.js';
27
22
  import { LmdbAztecSingleton } from './singleton.js';
28
23
 
@@ -37,7 +32,11 @@ export class AztecLmdbStore implements AztecKVStore, AztecAsyncKVStore {
37
32
  #multiMapData: Database<unknown, Key>;
38
33
  #log = createLogger('kv-store:lmdb');
39
34
 
40
- constructor(rootDb: RootDatabase, public readonly isEphemeral: boolean, private path: string) {
35
+ constructor(
36
+ rootDb: RootDatabase,
37
+ public readonly isEphemeral: boolean,
38
+ private path: string,
39
+ ) {
41
40
  this.#rootDb = rootDb;
42
41
 
43
42
  // big bucket to store all the data
@@ -80,27 +79,12 @@ export class AztecLmdbStore implements AztecKVStore, AztecAsyncKVStore {
80
79
  return new AztecLmdbStore(rootDb, ephemeral, dbPath);
81
80
  }
82
81
 
83
- /**
84
- * Forks the current DB into a new DB by backing it up to a temporary location and opening a new lmdb db.
85
- * @returns A new AztecLmdbStore.
86
- */
87
- async fork() {
88
- const baseDir = this.path;
89
- this.#log.debug(`Forking store with basedir ${baseDir}`);
90
- const forkPath = await fs.mkdtemp(join(baseDir, 'aztec-store-fork-'));
91
- this.#log.verbose(`Forking store to ${forkPath}`);
92
- await this.#rootDb.backup(forkPath, false);
93
- const forkDb = open(forkPath, { noSync: this.isEphemeral });
94
- this.#log.debug(`Forked store at ${forkPath} opened successfully`);
95
- return new AztecLmdbStore(forkDb, this.isEphemeral, forkPath);
96
- }
97
-
98
82
  /**
99
83
  * Creates a new AztecMap in the store.
100
84
  * @param name - Name of the map
101
85
  * @returns A new AztecMap
102
86
  */
103
- openMap<K extends Key, V>(name: string): AztecMap<K, V> & AztecAsyncMap<K, V> {
87
+ openMap<K extends Key, V extends Value>(name: string): AztecMap<K, V> & AztecAsyncMap<K, V> {
104
88
  return new LmdbAztecMap(this.#data, name);
105
89
  }
106
90
 
@@ -118,37 +102,20 @@ export class AztecLmdbStore implements AztecKVStore, AztecAsyncKVStore {
118
102
  * @param name - Name of the map
119
103
  * @returns A new AztecMultiMap
120
104
  */
121
- openMultiMap<K extends Key, V>(name: string): AztecMultiMap<K, V> & AztecAsyncMultiMap<K, V> {
122
- return new LmdbAztecMap(this.#multiMapData, name);
105
+ openMultiMap<K extends Key, V extends Value>(name: string): AztecMultiMap<K, V> & AztecAsyncMultiMap<K, V> {
106
+ return new LmdbAztecMultiMap(this.#multiMapData, name);
123
107
  }
124
108
 
125
109
  openCounter<K extends Key>(name: string): AztecCounter<K> & AztecAsyncCounter<K> {
126
110
  return new LmdbAztecCounter(this.#data, name);
127
111
  }
128
- /**
129
- * Creates a new AztecMultiMapWithSize in the store. A multi-map with size stores multiple values for a single key automatically.
130
- * @param name - Name of the map
131
- * @returns A new AztecMultiMapWithSize
132
- */
133
- openMultiMapWithSize<K extends Key, V>(name: string): AztecMultiMapWithSize<K, V> {
134
- return new LmdbAztecMapWithSize(this.#multiMapData, name);
135
- }
136
-
137
- /**
138
- * Creates a new AztecMapWithSize in the store.
139
- * @param name - Name of the map
140
- * @returns A new AztecMapWithSize
141
- */
142
- openMapWithSize<K extends Key, V>(name: string): AztecMapWithSize<K, V> {
143
- return new LmdbAztecMapWithSize(this.#data, name);
144
- }
145
112
 
146
113
  /**
147
114
  * Creates a new AztecArray in the store.
148
115
  * @param name - Name of the array
149
116
  * @returns A new AztecArray
150
117
  */
151
- openArray<T>(name: string): AztecArray<T> & AztecAsyncArray<T> {
118
+ openArray<T extends Value>(name: string): AztecArray<T> & AztecAsyncArray<T> {
152
119
  return new LmdbAztecArray(this.#data, name);
153
120
  }
154
121
 
@@ -180,21 +147,25 @@ export class AztecLmdbStore implements AztecKVStore, AztecAsyncKVStore {
180
147
  }
181
148
 
182
149
  /**
183
- * Clears all entries in the store & sub DBs.
150
+ * Clears all entries in the store & sub DBs atomically within a single transaction.
184
151
  */
185
152
  async clear() {
186
- await this.#data.clearAsync();
187
- await this.#multiMapData.clearAsync();
188
- await this.#rootDb.clearAsync();
153
+ await this.#rootDb.transaction(async () => {
154
+ await this.#data.clearAsync();
155
+ await this.#multiMapData.clearAsync();
156
+ await this.#rootDb.clearAsync();
157
+ });
189
158
  }
190
159
 
191
160
  /**
192
- * Drops the database & sub DBs.
161
+ * Drops the database & sub DBs atomically within a single transaction.
193
162
  */
194
163
  async drop() {
195
- await this.#data.drop();
196
- await this.#multiMapData.drop();
197
- await this.#rootDb.drop();
164
+ await this.#rootDb.transaction(async () => {
165
+ await this.#data.drop();
166
+ await this.#multiMapData.drop();
167
+ await this.#rootDb.drop();
168
+ });
198
169
  }
199
170
 
200
171
  /**
@@ -224,10 +195,15 @@ export class AztecLmdbStore implements AztecKVStore, AztecAsyncKVStore {
224
195
  if ('mapSize' in stats && typeof stats.mapSize === 'number') {
225
196
  mapSize = stats.mapSize;
226
197
  }
198
+ let physicalFileSize = 0;
199
+ if ('physicalFileSize' in stats && typeof stats.physicalFileSize === 'number') {
200
+ physicalFileSize = stats.physicalFileSize;
201
+ }
227
202
  const dataResult = this.estimateSubDBSize(this.#data);
228
203
  const multiResult = this.estimateSubDBSize(this.#multiMapData);
229
204
  return Promise.resolve({
230
205
  mappingSize: mapSize,
206
+ physicalFileSize: physicalFileSize,
231
207
  actualSize: dataResult.actualSize + multiResult.actualSize,
232
208
  numItems: dataResult.numItems + multiResult.numItems,
233
209
  });
@@ -260,4 +236,8 @@ export class AztecLmdbStore implements AztecKVStore, AztecAsyncKVStore {
260
236
  }
261
237
  return { actualSize, numItems };
262
238
  }
239
+
240
+ backupTo(_dstPath: string, _compact?: boolean): Promise<void> {
241
+ throw new Error('Method not implemented.');
242
+ }
263
243
  }
@@ -1,17 +1,22 @@
1
1
  import { Encoder } from 'msgpackr/pack';
2
2
 
3
3
  import type { AztecAsyncArray } from '../interfaces/array.js';
4
+ import type { Value } from '../interfaces/common.js';
4
5
  import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
5
6
  import type { ReadTransaction } from './read_transaction.js';
6
- import { AztecLMDBStoreV2, execInReadTx, execInWriteTx } from './store.js';
7
+ import type { AztecLMDBStoreV2 } from './store.js';
8
+ import { execInReadTx, execInWriteTx } from './tx-helpers.js';
7
9
  import { deserializeKey, serializeKey } from './utils.js';
8
10
 
9
- export class LMDBArray<T> implements AztecAsyncArray<T> {
11
+ export class LMDBArray<T extends Value> implements AztecAsyncArray<T> {
10
12
  private length: AztecAsyncSingleton<number>;
11
13
  private encoder = new Encoder();
12
14
  private prefix: string;
13
15
 
14
- constructor(private store: AztecLMDBStoreV2, name: string) {
16
+ constructor(
17
+ private store: AztecLMDBStoreV2,
18
+ name: string,
19
+ ) {
15
20
  this.length = store.openSingleton(name + ':length');
16
21
  this.prefix = `array:${name}`;
17
22
  }
@@ -1,12 +1,12 @@
1
1
  import { EthAddress } from '@aztec/foundation/eth-address';
2
- import { type Logger, createLogger } from '@aztec/foundation/log';
3
- import { DatabaseVersionManager } from '@aztec/stdlib/database-version';
2
+ import { type LoggerBindings, createLogger } from '@aztec/foundation/log';
3
+ import { DatabaseVersionManager } from '@aztec/stdlib/database-version/manager';
4
+ import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
4
5
 
5
6
  import { mkdir, mkdtemp, rm } from 'fs/promises';
6
7
  import { tmpdir } from 'os';
7
8
  import { join } from 'path';
8
9
 
9
- import type { DataStoreConfig } from '../config.js';
10
10
  import { AztecLMDBStoreV2 } from './store.js';
11
11
 
12
12
  const MAX_READERS = 16;
@@ -15,8 +15,9 @@ export async function createStore(
15
15
  name: string,
16
16
  schemaVersion: number,
17
17
  config: DataStoreConfig,
18
- log: Logger = createLogger('kv-store:lmdb-v2:' + name),
18
+ bindings?: LoggerBindings,
19
19
  ): Promise<AztecLMDBStoreV2> {
20
+ const log = createLogger('kv-store:lmdb-v2:' + name, bindings);
20
21
  const { dataDirectory, l1Contracts } = config;
21
22
 
22
23
  let store: AztecLMDBStoreV2;
@@ -28,16 +29,20 @@ export async function createStore(
28
29
  const rollupAddress = l1Contracts ? l1Contracts.rollupAddress : EthAddress.ZERO;
29
30
 
30
31
  // Create a version manager
31
- const versionManager = new DatabaseVersionManager(schemaVersion, rollupAddress, subDir, dbDirectory =>
32
- AztecLMDBStoreV2.new(dbDirectory, config.dataStoreMapSizeKB, MAX_READERS, () => Promise.resolve(), log),
33
- );
32
+ const versionManager = new DatabaseVersionManager({
33
+ schemaVersion,
34
+ rollupAddress,
35
+ dataDirectory: subDir,
36
+ onOpen: dbDirectory =>
37
+ AztecLMDBStoreV2.new(dbDirectory, config.dataStoreMapSizeKb, MAX_READERS, () => Promise.resolve(), bindings),
38
+ });
34
39
 
35
40
  log.info(
36
- `Creating ${name} data store at directory ${subDir} with map size ${config.dataStoreMapSizeKB} KB (LMDB v2)`,
41
+ `Creating ${name} data store at directory ${subDir} with map size ${config.dataStoreMapSizeKb} KB (LMDB v2)`,
37
42
  );
38
43
  [store] = await versionManager.open();
39
44
  } else {
40
- store = await openTmpStore(name, true, config.dataStoreMapSizeKB, MAX_READERS, log);
45
+ store = await openTmpStore(name, true, config.dataStoreMapSizeKb, MAX_READERS, bindings);
41
46
  }
42
47
 
43
48
  return store;
@@ -48,8 +53,9 @@ export async function openTmpStore(
48
53
  ephemeral: boolean = true,
49
54
  dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
50
55
  maxReaders = MAX_READERS,
51
- log: Logger = createLogger('kv-store:lmdb-v2:' + name),
56
+ bindings?: LoggerBindings,
52
57
  ): Promise<AztecLMDBStoreV2> {
58
+ const log = createLogger('kv-store:lmdb-v2:' + name, bindings);
53
59
  const dataDir = await mkdtemp(join(tmpdir(), name + '-'));
54
60
  log.debug(`Created temporary data store at: ${dataDir} with size: ${dbMapSizeKb} KB (LMDB v2)`);
55
61
 
@@ -69,30 +75,35 @@ export async function openTmpStore(
69
75
 
70
76
  // For temporary stores, we don't need to worry about versioning
71
77
  // as they are ephemeral and get cleaned up after use
72
- return AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, cleanup, log);
78
+ return AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, cleanup, bindings);
73
79
  }
74
80
 
75
81
  export async function openStoreAt(
76
82
  dataDir: string,
77
83
  dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
78
84
  maxReaders = MAX_READERS,
79
- log: Logger = createLogger('kv-store:lmdb-v2'),
85
+ bindings?: LoggerBindings,
80
86
  ): Promise<AztecLMDBStoreV2> {
87
+ const log = createLogger('kv-store:lmdb-v2', bindings);
81
88
  log.debug(`Opening data store at: ${dataDir} with size: ${dbMapSizeKb} KB (LMDB v2)`);
82
- return await AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, log);
89
+ return await AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, bindings);
83
90
  }
84
91
 
85
92
  export async function openVersionedStoreAt(
86
- dataDir: string,
93
+ dataDirectory: string,
87
94
  schemaVersion: number,
88
95
  rollupAddress: EthAddress,
89
96
  dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
90
97
  maxReaders = MAX_READERS,
91
- log: Logger = createLogger('kv-store:lmdb-v2'),
98
+ bindings?: LoggerBindings,
92
99
  ): Promise<AztecLMDBStoreV2> {
93
- log.debug(`Opening data store at: ${dataDir} with size: ${dbMapSizeKb} KB (LMDB v2)`);
94
- const [store] = await new DatabaseVersionManager(schemaVersion, rollupAddress, dataDir, dataDir =>
95
- AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, log),
96
- ).open();
100
+ const log = createLogger('kv-store:lmdb-v2', bindings);
101
+ log.debug(`Opening data store at: ${dataDirectory} with size: ${dbMapSizeKb} KB (LMDB v2)`);
102
+ const [store] = await new DatabaseVersionManager({
103
+ schemaVersion,
104
+ rollupAddress,
105
+ dataDirectory,
106
+ onOpen: dataDir => AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, bindings),
107
+ }).open();
97
108
  return store;
98
109
  }