@aztec/kv-store 0.0.0-test.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.
- package/README.md +10 -0
- package/dest/config.d.ts +17 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +24 -0
- package/dest/indexeddb/array.d.ts +21 -0
- package/dest/indexeddb/array.d.ts.map +1 -0
- package/dest/indexeddb/array.js +104 -0
- package/dest/indexeddb/index.d.ts +7 -0
- package/dest/indexeddb/index.d.ts.map +1 -0
- package/dest/indexeddb/index.js +19 -0
- package/dest/indexeddb/map.d.ts +26 -0
- package/dest/indexeddb/map.d.ts.map +1 -0
- package/dest/indexeddb/map.js +133 -0
- package/dest/indexeddb/set.d.ts +17 -0
- package/dest/indexeddb/set.d.ts.map +1 -0
- package/dest/indexeddb/set.js +24 -0
- package/dest/indexeddb/singleton.d.ts +16 -0
- package/dest/indexeddb/singleton.d.ts.map +1 -0
- package/dest/indexeddb/singleton.js +37 -0
- package/dest/indexeddb/store.d.ts +97 -0
- package/dest/indexeddb/store.d.ts.map +1 -0
- package/dest/indexeddb/store.js +164 -0
- package/dest/interfaces/array.d.ts +80 -0
- package/dest/interfaces/array.d.ts.map +1 -0
- package/dest/interfaces/array.js +3 -0
- package/dest/interfaces/array_test_suite.d.ts +3 -0
- package/dest/interfaces/array_test_suite.d.ts.map +1 -0
- package/dest/interfaces/array_test_suite.js +104 -0
- package/dest/interfaces/common.d.ts +23 -0
- package/dest/interfaces/common.d.ts.map +1 -0
- package/dest/interfaces/common.js +3 -0
- package/dest/interfaces/counter.d.ts +59 -0
- package/dest/interfaces/counter.d.ts.map +1 -0
- package/dest/interfaces/counter.js +1 -0
- package/dest/interfaces/index.d.ts +8 -0
- package/dest/interfaces/index.d.ts.map +1 -0
- package/dest/interfaces/index.js +6 -0
- package/dest/interfaces/map.d.ts +134 -0
- package/dest/interfaces/map.d.ts.map +1 -0
- package/dest/interfaces/map.js +3 -0
- package/dest/interfaces/map_test_suite.d.ts +3 -0
- package/dest/interfaces/map_test_suite.d.ts.map +1 -0
- package/dest/interfaces/map_test_suite.js +151 -0
- package/dest/interfaces/set.d.ts +44 -0
- package/dest/interfaces/set.d.ts.map +1 -0
- package/dest/interfaces/set.js +1 -0
- package/dest/interfaces/set_test_suite.d.ts +3 -0
- package/dest/interfaces/set_test_suite.d.ts.map +1 -0
- package/dest/interfaces/set_test_suite.js +96 -0
- package/dest/interfaces/singleton.d.ts +29 -0
- package/dest/interfaces/singleton.d.ts.map +1 -0
- package/dest/interfaces/singleton.js +4 -0
- package/dest/interfaces/singleton_test_suite.d.ts +3 -0
- package/dest/interfaces/singleton_test_suite.d.ts.map +1 -0
- package/dest/interfaces/singleton_test_suite.js +30 -0
- package/dest/interfaces/store.d.ts +145 -0
- package/dest/interfaces/store.d.ts.map +1 -0
- package/dest/interfaces/store.js +1 -0
- package/dest/interfaces/store_test_suite.d.ts +3 -0
- package/dest/interfaces/store_test_suite.d.ts.map +1 -0
- package/dest/interfaces/store_test_suite.js +37 -0
- package/dest/interfaces/utils.d.ts +16 -0
- package/dest/interfaces/utils.d.ts.map +1 -0
- package/dest/interfaces/utils.js +16 -0
- package/dest/lmdb/array.d.ts +23 -0
- package/dest/lmdb/array.d.ts.map +1 -0
- package/dest/lmdb/array.js +117 -0
- package/dest/lmdb/counter.d.ts +19 -0
- package/dest/lmdb/counter.d.ts.map +1 -0
- package/dest/lmdb/counter.js +50 -0
- package/dest/lmdb/index.d.ts +12 -0
- package/dest/lmdb/index.d.ts.map +1 -0
- package/dest/lmdb/index.js +25 -0
- package/dest/lmdb/map.d.ts +52 -0
- package/dest/lmdb/map.d.ts.map +1 -0
- package/dest/lmdb/map.js +222 -0
- package/dest/lmdb/set.d.ts +18 -0
- package/dest/lmdb/set.d.ts.map +1 -0
- package/dest/lmdb/set.js +32 -0
- package/dest/lmdb/singleton.d.ts +14 -0
- package/dest/lmdb/singleton.d.ts.map +1 -0
- package/dest/lmdb/singleton.js +26 -0
- package/dest/lmdb/store.d.ts +110 -0
- package/dest/lmdb/store.d.ts.map +1 -0
- package/dest/lmdb/store.js +216 -0
- package/dest/lmdb-v2/array.d.ts +18 -0
- package/dest/lmdb-v2/array.d.ts.map +1 -0
- package/dest/lmdb-v2/array.js +101 -0
- package/dest/lmdb-v2/factory.d.ts +12 -0
- package/dest/lmdb-v2/factory.d.ts.map +1 -0
- package/dest/lmdb-v2/factory.js +60 -0
- package/dest/lmdb-v2/index.d.ts +3 -0
- package/dest/lmdb-v2/index.d.ts.map +1 -0
- package/dest/lmdb-v2/index.js +2 -0
- package/dest/lmdb-v2/map.d.ts +86 -0
- package/dest/lmdb-v2/map.d.ts.map +1 -0
- package/dest/lmdb-v2/map.js +193 -0
- package/dest/lmdb-v2/message.d.ts +112 -0
- package/dest/lmdb-v2/message.d.ts.map +1 -0
- package/dest/lmdb-v2/message.js +18 -0
- package/dest/lmdb-v2/read_transaction.d.ts +14 -0
- package/dest/lmdb-v2/read_transaction.d.ts.map +1 -0
- package/dest/lmdb-v2/read_transaction.js +101 -0
- package/dest/lmdb-v2/singleton.d.ts +12 -0
- package/dest/lmdb-v2/singleton.d.ts.map +1 -0
- package/dest/lmdb-v2/singleton.js +31 -0
- package/dest/lmdb-v2/store.d.ts +43 -0
- package/dest/lmdb-v2/store.d.ts.map +1 -0
- package/dest/lmdb-v2/store.js +181 -0
- package/dest/lmdb-v2/utils.d.ts +19 -0
- package/dest/lmdb-v2/utils.d.ts.map +1 -0
- package/dest/lmdb-v2/utils.js +126 -0
- package/dest/lmdb-v2/write_transaction.d.ts +19 -0
- package/dest/lmdb-v2/write_transaction.d.ts.map +1 -0
- package/dest/lmdb-v2/write_transaction.js +254 -0
- package/dest/stores/index.d.ts +2 -0
- package/dest/stores/index.d.ts.map +1 -0
- package/dest/stores/index.js +1 -0
- package/dest/stores/l2_tips_store.d.ts +13 -0
- package/dest/stores/l2_tips_store.d.ts.map +1 -0
- package/dest/stores/l2_tips_store.js +65 -0
- package/dest/utils.d.ts +12 -0
- package/dest/utils.d.ts.map +1 -0
- package/dest/utils.js +24 -0
- package/package.json +99 -0
- package/src/config.ts +34 -0
- package/src/indexeddb/array.ts +118 -0
- package/src/indexeddb/index.ts +29 -0
- package/src/indexeddb/map.ts +142 -0
- package/src/indexeddb/set.ts +37 -0
- package/src/indexeddb/singleton.ts +49 -0
- package/src/indexeddb/store.ts +197 -0
- package/src/interfaces/array.ts +90 -0
- package/src/interfaces/array_test_suite.ts +130 -0
- package/src/interfaces/common.ts +20 -0
- package/src/interfaces/counter.ts +65 -0
- package/src/interfaces/index.ts +7 -0
- package/src/interfaces/map.ts +151 -0
- package/src/interfaces/map_test_suite.ts +143 -0
- package/src/interfaces/set.ts +48 -0
- package/src/interfaces/set_test_suite.ts +81 -0
- package/src/interfaces/singleton.ts +29 -0
- package/src/interfaces/singleton_test_suite.ts +46 -0
- package/src/interfaces/store.ts +177 -0
- package/src/interfaces/store_test_suite.ts +56 -0
- package/src/interfaces/utils.ts +21 -0
- package/src/lmdb/array.ts +139 -0
- package/src/lmdb/counter.ts +67 -0
- package/src/lmdb/index.ts +37 -0
- package/src/lmdb/map.ts +248 -0
- package/src/lmdb/set.ts +45 -0
- package/src/lmdb/singleton.ts +35 -0
- package/src/lmdb/store.ts +263 -0
- package/src/lmdb-v2/array.ts +115 -0
- package/src/lmdb-v2/factory.ts +98 -0
- package/src/lmdb-v2/index.ts +2 -0
- package/src/lmdb-v2/map.ts +233 -0
- package/src/lmdb-v2/message.ts +146 -0
- package/src/lmdb-v2/read_transaction.ts +116 -0
- package/src/lmdb-v2/singleton.ts +34 -0
- package/src/lmdb-v2/store.ts +237 -0
- package/src/lmdb-v2/utils.ts +150 -0
- package/src/lmdb-v2/write_transaction.ts +314 -0
- package/src/stores/index.ts +1 -0
- package/src/stores/l2_tips_store.ts +70 -0
- package/src/utils.ts +40 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { randomBytes } from '@aztec/foundation/crypto';
|
|
2
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
3
|
+
|
|
4
|
+
import { promises as fs, mkdirSync } from 'fs';
|
|
5
|
+
import { type Database, type RootDatabase, open } from 'lmdb';
|
|
6
|
+
import { tmpdir } from 'os';
|
|
7
|
+
import { join } from 'path';
|
|
8
|
+
|
|
9
|
+
import type { AztecArray, AztecAsyncArray } from '../interfaces/array.js';
|
|
10
|
+
import type { Key, StoreSize } from '../interfaces/common.js';
|
|
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';
|
|
20
|
+
import type { AztecAsyncSet, AztecSet } from '../interfaces/set.js';
|
|
21
|
+
import type { AztecAsyncSingleton, AztecSingleton } from '../interfaces/singleton.js';
|
|
22
|
+
import type { AztecAsyncKVStore, AztecKVStore } from '../interfaces/store.js';
|
|
23
|
+
import { LmdbAztecArray } from './array.js';
|
|
24
|
+
import { LmdbAztecCounter } from './counter.js';
|
|
25
|
+
import { LmdbAztecMap, LmdbAztecMapWithSize } from './map.js';
|
|
26
|
+
import { LmdbAztecSet } from './set.js';
|
|
27
|
+
import { LmdbAztecSingleton } from './singleton.js';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A key-value store backed by LMDB.
|
|
31
|
+
*/
|
|
32
|
+
export class AztecLmdbStore implements AztecKVStore, AztecAsyncKVStore {
|
|
33
|
+
syncGetters = true as const;
|
|
34
|
+
|
|
35
|
+
#rootDb: RootDatabase;
|
|
36
|
+
#data: Database<unknown, Key>;
|
|
37
|
+
#multiMapData: Database<unknown, Key>;
|
|
38
|
+
#log = createLogger('kv-store:lmdb');
|
|
39
|
+
|
|
40
|
+
constructor(rootDb: RootDatabase, public readonly isEphemeral: boolean, private path: string) {
|
|
41
|
+
this.#rootDb = rootDb;
|
|
42
|
+
|
|
43
|
+
// big bucket to store all the data
|
|
44
|
+
this.#data = rootDb.openDB('data', {
|
|
45
|
+
encoding: 'msgpack',
|
|
46
|
+
keyEncoding: 'ordered-binary',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
this.#multiMapData = rootDb.openDB('data_dup_sort', {
|
|
50
|
+
encoding: 'ordered-binary',
|
|
51
|
+
keyEncoding: 'ordered-binary',
|
|
52
|
+
dupSort: true,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Creates a new AztecKVStore backed by LMDB. The path to the database is optional. If not provided,
|
|
58
|
+
* the database will be stored in a temporary location and be deleted when the process exists.
|
|
59
|
+
*
|
|
60
|
+
* The `rollupAddress` passed is checked against what is stored in the database. If they do not match,
|
|
61
|
+
* the database is cleared before returning the store. This way data is not accidentally shared between
|
|
62
|
+
* different rollup instances.
|
|
63
|
+
*
|
|
64
|
+
* @param path - A path on the disk to store the database. Optional
|
|
65
|
+
* @param ephemeral - true if the store should only exist in memory and not automatically be flushed to disk. Optional
|
|
66
|
+
* @param log - A logger to use. Optional
|
|
67
|
+
* @returns The store
|
|
68
|
+
*/
|
|
69
|
+
static open(
|
|
70
|
+
path?: string,
|
|
71
|
+
mapSizeKb = 1 * 1024 * 1024, // defaults to 1 GB map size
|
|
72
|
+
ephemeral: boolean = false,
|
|
73
|
+
log = createLogger('kv-store:lmdb'),
|
|
74
|
+
): AztecLmdbStore {
|
|
75
|
+
const dbPath = path ?? join(tmpdir(), randomBytes(8).toString('hex'));
|
|
76
|
+
mkdirSync(dbPath, { recursive: true });
|
|
77
|
+
const mapSize = 1024 * mapSizeKb;
|
|
78
|
+
log.debug(`Opening LMDB database at ${path || 'temporary location'} with map size ${mapSize}`);
|
|
79
|
+
const rootDb = open({ path: dbPath, noSync: ephemeral, mapSize });
|
|
80
|
+
return new AztecLmdbStore(rootDb, ephemeral, dbPath);
|
|
81
|
+
}
|
|
82
|
+
|
|
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
|
+
/**
|
|
99
|
+
* Creates a new AztecMap in the store.
|
|
100
|
+
* @param name - Name of the map
|
|
101
|
+
* @returns A new AztecMap
|
|
102
|
+
*/
|
|
103
|
+
openMap<K extends Key, V>(name: string): AztecMap<K, V> & AztecAsyncMap<K, V> {
|
|
104
|
+
return new LmdbAztecMap(this.#data, name);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Creates a new AztecSet in the store.
|
|
109
|
+
* @param name - Name of the set
|
|
110
|
+
* @returns A new AztecSet
|
|
111
|
+
*/
|
|
112
|
+
openSet<K extends Key>(name: string): AztecSet<K> & AztecAsyncSet<K> {
|
|
113
|
+
return new LmdbAztecSet(this.#data, name);
|
|
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): AztecMultiMap<K, V> & AztecAsyncMultiMap<K, V> {
|
|
122
|
+
return new LmdbAztecMap(this.#multiMapData, name);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
openCounter<K extends Key>(name: string): AztecCounter<K> & AztecAsyncCounter<K> {
|
|
126
|
+
return new LmdbAztecCounter(this.#data, name);
|
|
127
|
+
}
|
|
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
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Creates a new AztecArray in the store.
|
|
148
|
+
* @param name - Name of the array
|
|
149
|
+
* @returns A new AztecArray
|
|
150
|
+
*/
|
|
151
|
+
openArray<T>(name: string): AztecArray<T> & AztecAsyncArray<T> {
|
|
152
|
+
return new LmdbAztecArray(this.#data, name);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Creates a new AztecSingleton in the store.
|
|
157
|
+
* @param name - Name of the singleton
|
|
158
|
+
* @returns A new AztecSingleton
|
|
159
|
+
*/
|
|
160
|
+
openSingleton<T>(name: string): AztecSingleton<T> & AztecAsyncSingleton<T> {
|
|
161
|
+
return new LmdbAztecSingleton(this.#data, name);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Runs a callback in a transaction.
|
|
166
|
+
* @param callback - Function to execute in a transaction
|
|
167
|
+
* @returns A promise that resolves to the return value of the callback
|
|
168
|
+
*/
|
|
169
|
+
transaction<T>(callback: () => T): Promise<T> {
|
|
170
|
+
return this.#rootDb.transaction(callback);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Runs a callback in a transaction.
|
|
175
|
+
* @param callback - Function to execute in a transaction
|
|
176
|
+
* @returns A promise that resolves to the return value of the callback
|
|
177
|
+
*/
|
|
178
|
+
async transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
|
|
179
|
+
return await this.#rootDb.transaction(callback);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Clears all entries in the store & sub DBs.
|
|
184
|
+
*/
|
|
185
|
+
async clear() {
|
|
186
|
+
await this.#data.clearAsync();
|
|
187
|
+
await this.#multiMapData.clearAsync();
|
|
188
|
+
await this.#rootDb.clearAsync();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Drops the database & sub DBs.
|
|
193
|
+
*/
|
|
194
|
+
async drop() {
|
|
195
|
+
await this.#data.drop();
|
|
196
|
+
await this.#multiMapData.drop();
|
|
197
|
+
await this.#rootDb.drop();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Close the database. Note, once this is closed we can no longer interact with the DB.
|
|
202
|
+
*/
|
|
203
|
+
async close() {
|
|
204
|
+
await this.#data.close();
|
|
205
|
+
await this.#multiMapData.close();
|
|
206
|
+
await this.#rootDb.close();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Deletes this store and removes the database files from disk */
|
|
210
|
+
async delete() {
|
|
211
|
+
await this.drop();
|
|
212
|
+
await this.close();
|
|
213
|
+
if (this.path) {
|
|
214
|
+
await fs.rm(this.path, { recursive: true, force: true, maxRetries: 3 });
|
|
215
|
+
this.#log.verbose(`Deleted database files at ${this.path}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
estimateSize(): Promise<StoreSize> {
|
|
220
|
+
const stats = this.#rootDb.getStats();
|
|
221
|
+
// The 'mapSize' is the total amount of virtual address space allocated to the DB (effectively the maximum possible size)
|
|
222
|
+
// http://www.lmdb.tech/doc/group__mdb.html#a4bde3c8b676457342cba2fe27aed5fbd
|
|
223
|
+
let mapSize = 0;
|
|
224
|
+
if ('mapSize' in stats && typeof stats.mapSize === 'number') {
|
|
225
|
+
mapSize = stats.mapSize;
|
|
226
|
+
}
|
|
227
|
+
const dataResult = this.estimateSubDBSize(this.#data);
|
|
228
|
+
const multiResult = this.estimateSubDBSize(this.#multiMapData);
|
|
229
|
+
return Promise.resolve({
|
|
230
|
+
mappingSize: mapSize,
|
|
231
|
+
actualSize: dataResult.actualSize + multiResult.actualSize,
|
|
232
|
+
numItems: dataResult.numItems + multiResult.numItems,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private estimateSubDBSize(db: Database<unknown, Key>): { actualSize: number; numItems: number } {
|
|
237
|
+
const stats = db.getStats();
|
|
238
|
+
let actualSize = 0;
|
|
239
|
+
let numItems = 0;
|
|
240
|
+
// This is the total number of key/value pairs present in the DB
|
|
241
|
+
if ('entryCount' in stats && typeof stats.entryCount === 'number') {
|
|
242
|
+
numItems = stats.entryCount;
|
|
243
|
+
}
|
|
244
|
+
// The closest value we can get to the actual size of the database is the number of consumed pages * the page size
|
|
245
|
+
if (
|
|
246
|
+
'treeBranchPageCount' in stats &&
|
|
247
|
+
typeof stats.treeBranchPageCount === 'number' &&
|
|
248
|
+
'treeLeafPageCount' in stats &&
|
|
249
|
+
typeof stats.treeLeafPageCount === 'number' &&
|
|
250
|
+
'overflowPages' in stats &&
|
|
251
|
+
typeof stats.overflowPages === 'number' &&
|
|
252
|
+
'pageSize' in stats &&
|
|
253
|
+
typeof stats.pageSize === 'number'
|
|
254
|
+
) {
|
|
255
|
+
const branchPages = stats.treeBranchPageCount;
|
|
256
|
+
const leafPages = stats.treeLeafPageCount;
|
|
257
|
+
const overflowPages = stats.overflowPages;
|
|
258
|
+
const pageSize = stats.pageSize;
|
|
259
|
+
actualSize = (branchPages + leafPages + overflowPages) * pageSize;
|
|
260
|
+
}
|
|
261
|
+
return { actualSize, numItems };
|
|
262
|
+
}
|
|
263
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Encoder } from 'msgpackr/pack';
|
|
2
|
+
|
|
3
|
+
import type { AztecAsyncArray } from '../interfaces/array.js';
|
|
4
|
+
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
5
|
+
import type { ReadTransaction } from './read_transaction.js';
|
|
6
|
+
import { AztecLMDBStoreV2, execInReadTx, execInWriteTx } from './store.js';
|
|
7
|
+
import { deserializeKey, serializeKey } from './utils.js';
|
|
8
|
+
|
|
9
|
+
export class LMDBArray<T> implements AztecAsyncArray<T> {
|
|
10
|
+
private length: AztecAsyncSingleton<number>;
|
|
11
|
+
private encoder = new Encoder();
|
|
12
|
+
private prefix: string;
|
|
13
|
+
|
|
14
|
+
constructor(private store: AztecLMDBStoreV2, name: string) {
|
|
15
|
+
this.length = store.openSingleton(name + ':length');
|
|
16
|
+
this.prefix = `array:${name}`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
pop(): Promise<T | undefined> {
|
|
20
|
+
return execInWriteTx(this.store, async tx => {
|
|
21
|
+
const length = await this.lengthAsync();
|
|
22
|
+
if (length === 0) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const val = await tx.get(serializeKey(this.prefix, length - 1));
|
|
27
|
+
await tx.remove(serializeKey(this.prefix, length - 1));
|
|
28
|
+
|
|
29
|
+
await this.length.set(length - 1);
|
|
30
|
+
|
|
31
|
+
return val ? this.encoder.unpack(val) : undefined;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
push(...vals: T[]): Promise<number> {
|
|
36
|
+
return execInWriteTx(this.store, async tx => {
|
|
37
|
+
let length = await this.lengthAsync();
|
|
38
|
+
for (const val of vals) {
|
|
39
|
+
await tx.set(serializeKey(this.prefix, length++), this.encoder.pack(val));
|
|
40
|
+
}
|
|
41
|
+
await this.length.set(length);
|
|
42
|
+
return length;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
setAt(index: number, val: T): Promise<boolean> {
|
|
47
|
+
return execInWriteTx(this.store, async tx => {
|
|
48
|
+
const length = await this.lengthAsync();
|
|
49
|
+
if (index < 0) {
|
|
50
|
+
index += length;
|
|
51
|
+
}
|
|
52
|
+
if (index < 0 || index >= length) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
await tx.set(serializeKey(this.prefix, index), this.encoder.pack(val));
|
|
56
|
+
return true;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
atAsync(index: number): Promise<T | undefined> {
|
|
61
|
+
return execInReadTx(this.store, async tx => {
|
|
62
|
+
const length = await this.lengthAsync();
|
|
63
|
+
if (index < 0) {
|
|
64
|
+
index += length;
|
|
65
|
+
}
|
|
66
|
+
if (index < 0 || index >= length) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const val = await tx.get(serializeKey(this.prefix, index));
|
|
71
|
+
return val ? this.encoder.unpack(val) : undefined;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async lengthAsync(): Promise<number> {
|
|
76
|
+
return (await this.length.getAsync()) ?? 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async *entriesAsync(): AsyncIterableIterator<[number, T]> {
|
|
80
|
+
// pin array length so that pushes don't affect iteration
|
|
81
|
+
const length = await this.lengthAsync();
|
|
82
|
+
if (length === 0) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let tx: ReadTransaction | undefined = this.store.getCurrentWriteTx();
|
|
87
|
+
const shouldClose = !tx;
|
|
88
|
+
tx ??= this.store.getReadTx();
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
for await (const [key, val] of tx.iterate(serializeKey(this.prefix, 0), undefined, false, length)) {
|
|
92
|
+
const deserializedKey = deserializeKey<number>(this.prefix, key);
|
|
93
|
+
// if pops happened while iterating we may have read too much. Terminate early
|
|
94
|
+
if (deserializedKey === false) {
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
yield [deserializedKey, this.encoder.unpack(val)];
|
|
98
|
+
}
|
|
99
|
+
} finally {
|
|
100
|
+
if (shouldClose) {
|
|
101
|
+
tx.close();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async *valuesAsync(): AsyncIterableIterator<T> {
|
|
107
|
+
for await (const [_, value] of this.entriesAsync()) {
|
|
108
|
+
yield value;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<T> {
|
|
113
|
+
return this.valuesAsync();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
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';
|
|
4
|
+
|
|
5
|
+
import { mkdir, mkdtemp, rm } from 'fs/promises';
|
|
6
|
+
import { tmpdir } from 'os';
|
|
7
|
+
import { join } from 'path';
|
|
8
|
+
|
|
9
|
+
import type { DataStoreConfig } from '../config.js';
|
|
10
|
+
import { AztecLMDBStoreV2 } from './store.js';
|
|
11
|
+
|
|
12
|
+
const MAX_READERS = 16;
|
|
13
|
+
|
|
14
|
+
export async function createStore(
|
|
15
|
+
name: string,
|
|
16
|
+
schemaVersion: number,
|
|
17
|
+
config: DataStoreConfig,
|
|
18
|
+
log: Logger = createLogger('kv-store:lmdb-v2:' + name),
|
|
19
|
+
): Promise<AztecLMDBStoreV2> {
|
|
20
|
+
const { dataDirectory, l1Contracts } = config;
|
|
21
|
+
|
|
22
|
+
let store: AztecLMDBStoreV2;
|
|
23
|
+
if (typeof dataDirectory !== 'undefined') {
|
|
24
|
+
// Get rollup address from contracts config, or use zero address
|
|
25
|
+
const subDir = join(dataDirectory, name);
|
|
26
|
+
await mkdir(subDir, { recursive: true });
|
|
27
|
+
|
|
28
|
+
const rollupAddress = l1Contracts ? l1Contracts.rollupAddress : EthAddress.ZERO;
|
|
29
|
+
|
|
30
|
+
// 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
|
+
);
|
|
34
|
+
|
|
35
|
+
log.info(
|
|
36
|
+
`Creating ${name} data store at directory ${subDir} with map size ${config.dataStoreMapSizeKB} KB (LMDB v2)`,
|
|
37
|
+
);
|
|
38
|
+
[store] = await versionManager.open();
|
|
39
|
+
} else {
|
|
40
|
+
store = await openTmpStore(name, true, config.dataStoreMapSizeKB, MAX_READERS, log);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return store;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function openTmpStore(
|
|
47
|
+
name: string,
|
|
48
|
+
ephemeral: boolean = true,
|
|
49
|
+
dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
|
|
50
|
+
maxReaders = MAX_READERS,
|
|
51
|
+
log: Logger = createLogger('kv-store:lmdb-v2:' + name),
|
|
52
|
+
): Promise<AztecLMDBStoreV2> {
|
|
53
|
+
const dataDir = await mkdtemp(join(tmpdir(), name + '-'));
|
|
54
|
+
log.debug(`Created temporary data store at: ${dataDir} with size: ${dbMapSizeKb} KB (LMDB v2)`);
|
|
55
|
+
|
|
56
|
+
// pass a cleanup callback because process.on('beforeExit', cleanup) does not work under Jest
|
|
57
|
+
const cleanup = async () => {
|
|
58
|
+
if (ephemeral) {
|
|
59
|
+
try {
|
|
60
|
+
await rm(dataDir, { recursive: true, force: true, maxRetries: 3 });
|
|
61
|
+
log.debug(`Deleted temporary data store: ${dataDir}`);
|
|
62
|
+
} catch (err) {
|
|
63
|
+
log.warn(`Failed to delete temporary data directory (LMDB v2) ${dataDir}: ${err}`);
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
log.debug(`Leaving temporary data store: ${dataDir}`);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// For temporary stores, we don't need to worry about versioning
|
|
71
|
+
// as they are ephemeral and get cleaned up after use
|
|
72
|
+
return AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, cleanup, log);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function openStoreAt(
|
|
76
|
+
dataDir: string,
|
|
77
|
+
dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
|
|
78
|
+
maxReaders = MAX_READERS,
|
|
79
|
+
log: Logger = createLogger('kv-store:lmdb-v2'),
|
|
80
|
+
): Promise<AztecLMDBStoreV2> {
|
|
81
|
+
log.debug(`Opening data store at: ${dataDir} with size: ${dbMapSizeKb} KB (LMDB v2)`);
|
|
82
|
+
return await AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, log);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function openVersionedStoreAt(
|
|
86
|
+
dataDir: string,
|
|
87
|
+
schemaVersion: number,
|
|
88
|
+
rollupAddress: EthAddress,
|
|
89
|
+
dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
|
|
90
|
+
maxReaders = MAX_READERS,
|
|
91
|
+
log: Logger = createLogger('kv-store:lmdb-v2'),
|
|
92
|
+
): 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();
|
|
97
|
+
return store;
|
|
98
|
+
}
|