@aztec/kv-store 0.0.0-test.1 → 0.0.1-commit.0208eb9

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/config.d.ts +2 -2
  2. package/dest/config.d.ts.map +1 -1
  3. package/dest/config.js +5 -3
  4. package/dest/indexeddb/array.d.ts +3 -2
  5. package/dest/indexeddb/array.d.ts.map +1 -1
  6. package/dest/indexeddb/array.js +24 -7
  7. package/dest/indexeddb/index.d.ts +2 -2
  8. package/dest/indexeddb/index.d.ts.map +1 -1
  9. package/dest/indexeddb/index.js +4 -7
  10. package/dest/indexeddb/map.d.ts +12 -6
  11. package/dest/indexeddb/map.d.ts.map +1 -1
  12. package/dest/indexeddb/map.js +39 -61
  13. package/dest/indexeddb/multi_map.d.ts +14 -0
  14. package/dest/indexeddb/multi_map.d.ts.map +1 -0
  15. package/dest/indexeddb/multi_map.js +107 -0
  16. package/dest/indexeddb/set.d.ts +1 -1
  17. package/dest/indexeddb/set.d.ts.map +1 -1
  18. package/dest/indexeddb/singleton.d.ts +3 -2
  19. package/dest/indexeddb/singleton.d.ts.map +1 -1
  20. package/dest/indexeddb/singleton.js +6 -2
  21. package/dest/indexeddb/store.d.ts +15 -16
  22. package/dest/indexeddb/store.d.ts.map +1 -1
  23. package/dest/indexeddb/store.js +45 -42
  24. package/dest/interfaces/array.d.ts +5 -4
  25. package/dest/interfaces/array.d.ts.map +1 -1
  26. package/dest/interfaces/array.js +1 -3
  27. package/dest/interfaces/array_test_suite.d.ts +1 -1
  28. package/dest/interfaces/common.d.ts +11 -9
  29. package/dest/interfaces/common.d.ts.map +1 -1
  30. package/dest/interfaces/common.js +8 -3
  31. package/dest/interfaces/counter.d.ts +1 -1
  32. package/dest/interfaces/index.d.ts +4 -2
  33. package/dest/interfaces/index.d.ts.map +1 -1
  34. package/dest/interfaces/index.js +2 -0
  35. package/dest/interfaces/map.d.ts +21 -49
  36. package/dest/interfaces/map.d.ts.map +1 -1
  37. package/dest/interfaces/map.js +1 -1
  38. package/dest/interfaces/map_test_suite.d.ts +1 -1
  39. package/dest/interfaces/map_test_suite.d.ts.map +1 -1
  40. package/dest/interfaces/map_test_suite.js +181 -70
  41. package/dest/interfaces/multi_map.d.ts +41 -0
  42. package/dest/interfaces/multi_map.d.ts.map +1 -0
  43. package/dest/interfaces/multi_map.js +3 -0
  44. package/dest/interfaces/multi_map_test_suite.d.ts +3 -0
  45. package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -0
  46. package/dest/interfaces/multi_map_test_suite.js +364 -0
  47. package/dest/interfaces/set.d.ts +1 -1
  48. package/dest/interfaces/set_test_suite.d.ts +1 -1
  49. package/dest/interfaces/singleton.d.ts +1 -1
  50. package/dest/interfaces/singleton_test_suite.d.ts +1 -1
  51. package/dest/interfaces/store.d.ts +18 -43
  52. package/dest/interfaces/store.d.ts.map +1 -1
  53. package/dest/interfaces/utils.d.ts +3 -1
  54. package/dest/interfaces/utils.d.ts.map +1 -1
  55. package/dest/interfaces/utils.js +3 -1
  56. package/dest/lmdb/array.d.ts +3 -2
  57. package/dest/lmdb/array.d.ts.map +1 -1
  58. package/dest/lmdb/array.js +4 -2
  59. package/dest/lmdb/counter.d.ts +1 -1
  60. package/dest/lmdb/counter.d.ts.map +1 -1
  61. package/dest/lmdb/index.d.ts +2 -2
  62. package/dest/lmdb/index.d.ts.map +1 -1
  63. package/dest/lmdb/index.js +5 -5
  64. package/dest/lmdb/map.d.ts +11 -23
  65. package/dest/lmdb/map.d.ts.map +1 -1
  66. package/dest/lmdb/map.js +15 -81
  67. package/dest/lmdb/multi_map.d.ts +13 -0
  68. package/dest/lmdb/multi_map.d.ts.map +1 -0
  69. package/dest/lmdb/multi_map.js +44 -0
  70. package/dest/lmdb/set.d.ts +1 -1
  71. package/dest/lmdb/set.d.ts.map +1 -1
  72. package/dest/lmdb/singleton.d.ts +1 -1
  73. package/dest/lmdb/singleton.d.ts.map +1 -1
  74. package/dest/lmdb/store.d.ts +9 -25
  75. package/dest/lmdb/store.d.ts.map +1 -1
  76. package/dest/lmdb/store.js +12 -32
  77. package/dest/lmdb-v2/array.d.ts +4 -3
  78. package/dest/lmdb-v2/array.d.ts.map +1 -1
  79. package/dest/lmdb-v2/array.js +4 -2
  80. package/dest/lmdb-v2/factory.d.ts +6 -9
  81. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  82. package/dest/lmdb-v2/factory.js +26 -12
  83. package/dest/lmdb-v2/index.d.ts +1 -1
  84. package/dest/lmdb-v2/map.d.ts +12 -45
  85. package/dest/lmdb-v2/map.d.ts.map +1 -1
  86. package/dest/lmdb-v2/map.js +17 -104
  87. package/dest/lmdb-v2/message.d.ts +24 -5
  88. package/dest/lmdb-v2/message.d.ts.map +1 -1
  89. package/dest/lmdb-v2/message.js +6 -4
  90. package/dest/lmdb-v2/multi_map.d.ts +52 -0
  91. package/dest/lmdb-v2/multi_map.d.ts.map +1 -0
  92. package/dest/lmdb-v2/multi_map.js +123 -0
  93. package/dest/lmdb-v2/read_transaction.d.ts +3 -1
  94. package/dest/lmdb-v2/read_transaction.d.ts.map +1 -1
  95. package/dest/lmdb-v2/read_transaction.js +34 -0
  96. package/dest/lmdb-v2/set.d.ts +15 -0
  97. package/dest/lmdb-v2/set.d.ts.map +1 -0
  98. package/dest/lmdb-v2/set.js +23 -0
  99. package/dest/lmdb-v2/singleton.d.ts +2 -2
  100. package/dest/lmdb-v2/singleton.d.ts.map +1 -1
  101. package/dest/lmdb-v2/singleton.js +1 -1
  102. package/dest/lmdb-v2/store.d.ts +13 -13
  103. package/dest/lmdb-v2/store.d.ts.map +1 -1
  104. package/dest/lmdb-v2/store.js +19 -29
  105. package/dest/lmdb-v2/tx-helpers.d.ts +6 -0
  106. package/dest/lmdb-v2/tx-helpers.d.ts.map +1 -0
  107. package/dest/lmdb-v2/tx-helpers.js +21 -0
  108. package/dest/lmdb-v2/utils.d.ts +3 -14
  109. package/dest/lmdb-v2/utils.d.ts.map +1 -1
  110. package/dest/lmdb-v2/utils.js +0 -94
  111. package/dest/lmdb-v2/write_transaction.d.ts +3 -5
  112. package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
  113. package/dest/lmdb-v2/write_transaction.js +9 -5
  114. package/dest/stores/index.d.ts +2 -1
  115. package/dest/stores/index.d.ts.map +1 -1
  116. package/dest/stores/index.js +1 -0
  117. package/dest/stores/l2_tips_store.d.ts +24 -8
  118. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  119. package/dest/stores/l2_tips_store.js +63 -45
  120. package/dest/utils.d.ts +9 -6
  121. package/dest/utils.d.ts.map +1 -1
  122. package/dest/utils.js +51 -16
  123. package/package.json +25 -16
  124. package/src/config.ts +6 -4
  125. package/src/indexeddb/array.ts +9 -5
  126. package/src/indexeddb/index.ts +10 -8
  127. package/src/indexeddb/map.ts +38 -55
  128. package/src/indexeddb/multi_map.ts +105 -0
  129. package/src/indexeddb/singleton.ts +5 -2
  130. package/src/indexeddb/store.ts +71 -56
  131. package/src/interfaces/array.ts +5 -3
  132. package/src/interfaces/common.ts +20 -9
  133. package/src/interfaces/index.ts +3 -1
  134. package/src/interfaces/map.ts +19 -53
  135. package/src/interfaces/map_test_suite.ts +101 -44
  136. package/src/interfaces/multi_map.ts +45 -0
  137. package/src/interfaces/multi_map_test_suite.ts +374 -0
  138. package/src/interfaces/store.ts +18 -53
  139. package/src/interfaces/utils.ts +2 -0
  140. package/src/lmdb/array.ts +2 -1
  141. package/src/lmdb/index.ts +11 -6
  142. package/src/lmdb/map.ts +23 -94
  143. package/src/lmdb/multi_map.ts +51 -0
  144. package/src/lmdb/store.ts +24 -48
  145. package/src/lmdb-v2/array.ts +8 -3
  146. package/src/lmdb-v2/factory.ts +29 -18
  147. package/src/lmdb-v2/map.ts +30 -127
  148. package/src/lmdb-v2/message.ts +23 -0
  149. package/src/lmdb-v2/multi_map.ts +150 -0
  150. package/src/lmdb-v2/read_transaction.ts +40 -0
  151. package/src/lmdb-v2/set.ts +33 -0
  152. package/src/lmdb-v2/singleton.ts +6 -2
  153. package/src/lmdb-v2/store.ts +24 -42
  154. package/src/lmdb-v2/tx-helpers.ts +29 -0
  155. package/src/lmdb-v2/utils.ts +0 -118
  156. package/src/lmdb-v2/write_transaction.ts +11 -10
  157. package/src/stores/index.ts +2 -0
  158. package/src/stores/l2_tips_store.ts +67 -49
  159. package/src/utils.ts +79 -21
  160. package/dest/interfaces/store_test_suite.d.ts +0 -3
  161. package/dest/interfaces/store_test_suite.d.ts.map +0 -1
  162. package/dest/interfaces/store_test_suite.js +0 -37
  163. package/src/interfaces/store_test_suite.ts +0 -56
@@ -1,14 +1,8 @@
1
1
  import type { AztecArray, AztecAsyncArray } from './array.js';
2
- import type { Key, StoreSize } from './common.js';
2
+ import type { Key, StoreSize, Value } from './common.js';
3
3
  import type { AztecAsyncCounter, AztecCounter } from './counter.js';
4
- import type {
5
- AztecAsyncMap,
6
- AztecAsyncMultiMap,
7
- AztecMap,
8
- AztecMapWithSize,
9
- AztecMultiMap,
10
- AztecMultiMapWithSize,
11
- } from './map.js';
4
+ import type { AztecAsyncMap, AztecMap } from './map.js';
5
+ import type { AztecAsyncMultiMap, AztecMultiMap } from './multi_map.js';
12
6
  import type { AztecAsyncSet, AztecSet } from './set.js';
13
7
  import type { AztecAsyncSingleton, AztecSingleton } from './singleton.js';
14
8
 
@@ -20,7 +14,7 @@ export interface AztecKVStore {
20
14
  * @param name - The name of the map
21
15
  * @returns The map
22
16
  */
23
- openMap<K extends Key, V>(name: string): AztecMap<K, V>;
17
+ openMap<K extends Key, V extends Value>(name: string): AztecMap<K, V>;
24
18
 
25
19
  /**
26
20
  * Creates a new set.
@@ -34,35 +28,21 @@ export interface AztecKVStore {
34
28
  * @param name - The name of the multi-map
35
29
  * @returns The multi-map
36
30
  */
37
- openMultiMap<K extends Key, V>(name: string): AztecMultiMap<K, V>;
38
-
39
- /**
40
- * Creates a new multi-map with size.
41
- * @param name - The name of the multi-map
42
- * @returns The multi-map
43
- */
44
- openMultiMapWithSize<K extends Key, V>(name: string): AztecMultiMapWithSize<K, V>;
45
-
46
- /**
47
- * Creates a new map with size.
48
- * @param name - The name of the map
49
- * @returns The map
50
- */
51
- openMapWithSize<K extends Key, V>(name: string): AztecMapWithSize<K, V>;
31
+ openMultiMap<K extends Key, V extends Value>(name: string): AztecMultiMap<K, V>;
52
32
 
53
33
  /**
54
34
  * Creates a new array.
55
35
  * @param name - The name of the array
56
36
  * @returns The array
57
37
  */
58
- openArray<T>(name: string): AztecArray<T>;
38
+ openArray<T extends Value>(name: string): AztecArray<T>;
59
39
 
60
40
  /**
61
41
  * Creates a new singleton.
62
42
  * @param name - The name of the singleton
63
43
  * @returns The singleton
64
44
  */
65
- openSingleton<T>(name: string): AztecSingleton<T>;
45
+ openSingleton<T extends Value>(name: string): AztecSingleton<T>;
66
46
 
67
47
  /**
68
48
  * Creates a new count map.
@@ -81,11 +61,6 @@ export interface AztecKVStore {
81
61
  */
82
62
  clear(): Promise<void>;
83
63
 
84
- /**
85
- * Forks the store.
86
- */
87
- fork(): Promise<AztecKVStore>;
88
-
89
64
  /**
90
65
  * Deletes the store
91
66
  */
@@ -108,7 +83,7 @@ export interface AztecAsyncKVStore {
108
83
  * @param name - The name of the map
109
84
  * @returns The map
110
85
  */
111
- openMap<K extends Key, V>(name: string): AztecAsyncMap<K, V>;
86
+ openMap<K extends Key, V extends Value>(name: string): AztecAsyncMap<K, V>;
112
87
 
113
88
  /**
114
89
  * Creates a new set.
@@ -122,21 +97,21 @@ export interface AztecAsyncKVStore {
122
97
  * @param name - The name of the multi-map
123
98
  * @returns The multi-map
124
99
  */
125
- openMultiMap<K extends Key, V>(name: string): AztecAsyncMultiMap<K, V>;
100
+ openMultiMap<K extends Key, V extends Value>(name: string): AztecAsyncMultiMap<K, V>;
126
101
 
127
102
  /**
128
103
  * Creates a new array.
129
104
  * @param name - The name of the array
130
105
  * @returns The array
131
106
  */
132
- openArray<T>(name: string): AztecAsyncArray<T>;
107
+ openArray<T extends Value>(name: string): AztecAsyncArray<T>;
133
108
 
134
109
  /**
135
110
  * Creates a new singleton.
136
111
  * @param name - The name of the singleton
137
112
  * @returns The singleton
138
113
  */
139
- openSingleton<T>(name: string): AztecAsyncSingleton<T>;
114
+ openSingleton<T extends Value>(name: string): AztecAsyncSingleton<T>;
140
115
 
141
116
  /**
142
117
  * Creates a new count map.
@@ -150,28 +125,18 @@ export interface AztecAsyncKVStore {
150
125
  */
151
126
  transactionAsync<T extends Exclude<any, Promise<any>>>(callback: () => Promise<T>): Promise<T>;
152
127
 
153
- /**
154
- * Clears all entries in the store
155
- */
128
+ /** Clears all entries in the store */
156
129
  clear(): Promise<void>;
157
130
 
158
- /**
159
- * Forks the store.
160
- */
161
- fork(): Promise<AztecAsyncKVStore>;
162
-
163
- /**
164
- * Deletes the store
165
- */
131
+ /** Deletes the store */
166
132
  delete(): Promise<void>;
167
133
 
168
- /**
169
- * Estimates the size of the store in bytes.
170
- */
134
+ /** Estimates the size of the store in bytes. */
171
135
  estimateSize(): Promise<StoreSize>;
172
136
 
173
- /**
174
- * Closes the store
175
- */
137
+ /** Closes the store */
176
138
  close(): Promise<void>;
139
+
140
+ /** Backups the store to the target folder.*/
141
+ backupTo(dstPath: string, compact?: boolean): Promise<void>;
177
142
  }
@@ -13,6 +13,8 @@ export const mockLogger = {
13
13
  level: 'trace' as const,
14
14
  isLevelEnabled: (_level: string) => true,
15
15
  module: 'kv-store:mock-logger',
16
+ createChild: () => mockLogger,
17
+ getBindings: () => ({}),
16
18
  };
17
19
  /* eslint-enable no-console */
18
20
 
package/src/lmdb/array.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { Database, Key } from 'lmdb';
2
2
 
3
3
  import type { AztecArray, AztecAsyncArray } from '../interfaces/array.js';
4
+ import type { Value } from '../interfaces/common.js';
4
5
  import { LmdbAztecSingleton } from './singleton.js';
5
6
 
6
7
  /** The shape of a key that stores a value in an array */
@@ -9,7 +10,7 @@ type ArrayIndexSlot = ['array', string, 'slot', number];
9
10
  /**
10
11
  * An persistent array backed by LMDB.
11
12
  */
12
- export class LmdbAztecArray<T> implements AztecArray<T>, AztecAsyncArray<T> {
13
+ export class LmdbAztecArray<T extends Value> implements AztecArray<T>, AztecAsyncArray<T> {
13
14
  #db: Database<T, ArrayIndexSlot>;
14
15
  #name: string;
15
16
  #length: LmdbAztecSingleton<number>;
package/src/lmdb/index.ts CHANGED
@@ -3,12 +3,17 @@ import { type Logger, createLogger } from '@aztec/foundation/log';
3
3
  import { join } from 'path';
4
4
 
5
5
  import type { DataStoreConfig } from '../config.js';
6
- import { initStoreForRollup } from '../utils.js';
6
+ import { initStoreForRollupAndSchemaVersion } from '../utils.js';
7
7
  import { AztecLmdbStore } from './store.js';
8
8
 
9
9
  export { AztecLmdbStore } from './store.js';
10
10
 
11
- export function createStore(name: string, config: DataStoreConfig, log: Logger = createLogger('kv-store')) {
11
+ export function createStore(
12
+ name: string,
13
+ config: DataStoreConfig,
14
+ schemaVersion: number | undefined = undefined,
15
+ log: Logger = createLogger('kv-store'),
16
+ ) {
12
17
  let { dataDirectory } = config;
13
18
  if (typeof dataDirectory !== 'undefined') {
14
19
  dataDirectory = join(dataDirectory, name);
@@ -16,13 +21,13 @@ export function createStore(name: string, config: DataStoreConfig, log: Logger =
16
21
 
17
22
  log.info(
18
23
  dataDirectory
19
- ? `Creating ${name} data store at directory ${dataDirectory} with map size ${config.dataStoreMapSizeKB} KB`
20
- : `Creating ${name} ephemeral data store with map size ${config.dataStoreMapSizeKB} KB`,
24
+ ? `Creating ${name} data store at directory ${dataDirectory} with map size ${config.dataStoreMapSizeKb} KB`
25
+ : `Creating ${name} ephemeral data store with map size ${config.dataStoreMapSizeKb} KB`,
21
26
  );
22
27
 
23
- const store = AztecLmdbStore.open(dataDirectory, config.dataStoreMapSizeKB, false);
28
+ const store = AztecLmdbStore.open(dataDirectory, config.dataStoreMapSizeKb, false);
24
29
  if (config.l1Contracts?.rollupAddress) {
25
- return initStoreForRollup(store, config.l1Contracts.rollupAddress, log);
30
+ return initStoreForRollupAndSchemaVersion(store, schemaVersion, config.l1Contracts.rollupAddress, log);
26
31
  }
27
32
  return store;
28
33
  }
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
 
@@ -224,10 +191,15 @@ export class AztecLmdbStore implements AztecKVStore, AztecAsyncKVStore {
224
191
  if ('mapSize' in stats && typeof stats.mapSize === 'number') {
225
192
  mapSize = stats.mapSize;
226
193
  }
194
+ let physicalFileSize = 0;
195
+ if ('physicalFileSize' in stats && typeof stats.physicalFileSize === 'number') {
196
+ physicalFileSize = stats.physicalFileSize;
197
+ }
227
198
  const dataResult = this.estimateSubDBSize(this.#data);
228
199
  const multiResult = this.estimateSubDBSize(this.#multiMapData);
229
200
  return Promise.resolve({
230
201
  mappingSize: mapSize,
202
+ physicalFileSize: physicalFileSize,
231
203
  actualSize: dataResult.actualSize + multiResult.actualSize,
232
204
  numItems: dataResult.numItems + multiResult.numItems,
233
205
  });
@@ -260,4 +232,8 @@ export class AztecLmdbStore implements AztecKVStore, AztecAsyncKVStore {
260
232
  }
261
233
  return { actualSize, numItems };
262
234
  }
235
+
236
+ backupTo(_dstPath: string, _compact?: boolean): Promise<void> {
237
+ throw new Error('Method not implemented.');
238
+ }
263
239
  }
@@ -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
  }