@aztec/kv-store 0.0.1-commit.b655e406 → 0.0.1-commit.b9865e97
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 +8 -1
- package/dest/bench/shared_map_bench.d.ts +19 -0
- package/dest/bench/shared_map_bench.d.ts.map +1 -0
- package/dest/bench/shared_map_bench.js +91 -0
- package/dest/indexeddb/array.d.ts +1 -1
- package/dest/indexeddb/array.d.ts.map +1 -1
- package/dest/indexeddb/array.js +21 -7
- package/dest/indexeddb/index.d.ts +3 -3
- package/dest/indexeddb/index.d.ts.map +1 -1
- package/dest/indexeddb/index.js +3 -6
- package/dest/indexeddb/map.d.ts +8 -2
- package/dest/indexeddb/map.d.ts.map +1 -1
- package/dest/indexeddb/map.js +23 -13
- package/dest/indexeddb/multi_map.d.ts +3 -1
- package/dest/indexeddb/multi_map.d.ts.map +1 -1
- package/dest/indexeddb/multi_map.js +30 -1
- package/dest/indexeddb/set.d.ts +1 -1
- package/dest/indexeddb/set.d.ts.map +1 -1
- package/dest/indexeddb/singleton.d.ts +1 -1
- package/dest/indexeddb/singleton.d.ts.map +1 -1
- package/dest/indexeddb/singleton.js +3 -1
- package/dest/indexeddb/store.d.ts +3 -3
- package/dest/indexeddb/store.d.ts.map +1 -1
- package/dest/indexeddb/store.js +6 -4
- package/dest/interfaces/array.d.ts +1 -1
- package/dest/interfaces/array_test_suite.d.ts +1 -1
- package/dest/interfaces/array_test_suite.d.ts.map +1 -1
- package/dest/interfaces/array_test_suite.js +33 -34
- package/dest/interfaces/common.d.ts +1 -1
- package/dest/interfaces/counter.d.ts +1 -1
- package/dest/interfaces/index.d.ts +2 -2
- package/dest/interfaces/index.d.ts.map +1 -1
- package/dest/interfaces/map.d.ts +1 -1
- package/dest/interfaces/map_test_suite.d.ts +1 -1
- package/dest/interfaces/map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/map_test_suite.js +74 -29
- package/dest/interfaces/multi_map.d.ts +7 -1
- package/dest/interfaces/multi_map.d.ts.map +1 -1
- package/dest/interfaces/multi_map_test_suite.d.ts +1 -1
- package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/multi_map_test_suite.js +154 -36
- package/dest/interfaces/set.d.ts +1 -1
- package/dest/interfaces/set_test_suite.d.ts +1 -1
- package/dest/interfaces/set_test_suite.d.ts.map +1 -1
- package/dest/interfaces/set_test_suite.js +13 -14
- package/dest/interfaces/singleton.d.ts +1 -1
- package/dest/interfaces/singleton_test_suite.d.ts +1 -1
- package/dest/interfaces/singleton_test_suite.d.ts.map +1 -1
- package/dest/interfaces/singleton_test_suite.js +6 -7
- package/dest/interfaces/store.d.ts +1 -1
- package/dest/interfaces/utils.d.ts +2 -1
- package/dest/interfaces/utils.d.ts.map +1 -1
- package/dest/interfaces/utils.js +2 -1
- package/dest/lmdb/array.d.ts +1 -1
- package/dest/lmdb/array.d.ts.map +1 -1
- package/dest/lmdb/array.js +4 -2
- package/dest/lmdb/counter.d.ts +1 -1
- package/dest/lmdb/counter.d.ts.map +1 -1
- package/dest/lmdb/index.d.ts +3 -3
- package/dest/lmdb/index.d.ts.map +1 -1
- package/dest/lmdb/index.js +4 -4
- package/dest/lmdb/map.d.ts +1 -1
- package/dest/lmdb/map.d.ts.map +1 -1
- package/dest/lmdb/multi_map.d.ts +2 -1
- package/dest/lmdb/multi_map.d.ts.map +1 -1
- package/dest/lmdb/multi_map.js +15 -0
- package/dest/lmdb/set.d.ts +1 -1
- package/dest/lmdb/set.d.ts.map +1 -1
- package/dest/lmdb/singleton.d.ts +1 -1
- package/dest/lmdb/singleton.d.ts.map +1 -1
- package/dest/lmdb/store.d.ts +4 -5
- package/dest/lmdb/store.d.ts.map +1 -1
- package/dest/lmdb/store.js +13 -9
- package/dest/lmdb-v2/array.d.ts +2 -2
- package/dest/lmdb-v2/array.d.ts.map +1 -1
- package/dest/lmdb-v2/array.js +4 -3
- package/dest/lmdb-v2/factory.d.ts +32 -10
- package/dest/lmdb-v2/factory.d.ts.map +1 -1
- package/dest/lmdb-v2/factory.js +71 -16
- package/dest/lmdb-v2/index.d.ts +1 -1
- package/dest/lmdb-v2/map.d.ts +2 -2
- package/dest/lmdb-v2/map.d.ts.map +1 -1
- package/dest/lmdb-v2/map.js +1 -2
- package/dest/lmdb-v2/message.d.ts +1 -1
- package/dest/lmdb-v2/multi_map.d.ts +3 -2
- package/dest/lmdb-v2/multi_map.d.ts.map +1 -1
- package/dest/lmdb-v2/multi_map.js +11 -1
- package/dest/lmdb-v2/read_transaction.d.ts +1 -1
- package/dest/lmdb-v2/read_transaction.d.ts.map +1 -1
- package/dest/lmdb-v2/read_transaction.js +21 -19
- package/dest/lmdb-v2/set.d.ts +1 -1
- package/dest/lmdb-v2/set.d.ts.map +1 -1
- package/dest/lmdb-v2/singleton.d.ts +2 -2
- package/dest/lmdb-v2/singleton.d.ts.map +1 -1
- package/dest/lmdb-v2/singleton.js +1 -2
- package/dest/lmdb-v2/store.d.ts +4 -5
- package/dest/lmdb-v2/store.d.ts.map +1 -1
- package/dest/lmdb-v2/store.js +6 -28
- package/dest/lmdb-v2/tx-helpers.d.ts +6 -0
- package/dest/lmdb-v2/tx-helpers.d.ts.map +1 -0
- package/dest/lmdb-v2/tx-helpers.js +21 -0
- package/dest/lmdb-v2/utils.d.ts +1 -10
- package/dest/lmdb-v2/utils.d.ts.map +1 -1
- package/dest/lmdb-v2/utils.js +0 -94
- package/dest/lmdb-v2/write_transaction.d.ts +1 -1
- package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
- package/dest/lmdb-v2/write_transaction.js +9 -5
- package/dest/sqlite-opfs/array.d.ts +21 -0
- package/dest/sqlite-opfs/array.d.ts.map +1 -0
- package/dest/sqlite-opfs/array.js +128 -0
- package/dest/sqlite-opfs/errors.d.ts +27 -0
- package/dest/sqlite-opfs/errors.d.ts.map +1 -0
- package/dest/sqlite-opfs/errors.js +34 -0
- package/dest/sqlite-opfs/index.d.ts +16 -0
- package/dest/sqlite-opfs/index.d.ts.map +1 -0
- package/dest/sqlite-opfs/index.js +22 -0
- package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts +32 -0
- package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts.map +1 -0
- package/dest/sqlite-opfs/internal/ordered-binary-browser.js +448 -0
- package/dest/sqlite-opfs/map.d.ts +35 -0
- package/dest/sqlite-opfs/map.d.ts.map +1 -0
- package/dest/sqlite-opfs/map.js +163 -0
- package/dest/sqlite-opfs/messages.d.ts +66 -0
- package/dest/sqlite-opfs/messages.d.ts.map +1 -0
- package/dest/sqlite-opfs/messages.js +5 -0
- package/dest/sqlite-opfs/multi_map.d.ts +16 -0
- package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
- package/dest/sqlite-opfs/multi_map.js +67 -0
- package/dest/sqlite-opfs/set.d.ts +13 -0
- package/dest/sqlite-opfs/set.d.ts.map +1 -0
- package/dest/sqlite-opfs/set.js +19 -0
- package/dest/sqlite-opfs/singleton.d.ts +13 -0
- package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
- package/dest/sqlite-opfs/singleton.js +48 -0
- package/dest/sqlite-opfs/store.d.ts +79 -0
- package/dest/sqlite-opfs/store.d.ts.map +1 -0
- package/dest/sqlite-opfs/store.js +281 -0
- package/dest/sqlite-opfs/worker.d.ts +2 -0
- package/dest/sqlite-opfs/worker.d.ts.map +1 -0
- package/dest/sqlite-opfs/worker.js +249 -0
- package/dest/stores/index.d.ts +1 -1
- package/dest/stores/l2_tips_store.d.ts +25 -10
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +62 -53
- package/dest/utils.d.ts +9 -6
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +51 -16
- package/package.json +33 -26
- package/src/bench/shared_map_bench.ts +111 -0
- package/src/indexeddb/array.ts +4 -4
- package/src/indexeddb/index.ts +9 -7
- package/src/indexeddb/map.ts +24 -11
- package/src/indexeddb/multi_map.ts +27 -1
- package/src/indexeddb/singleton.ts +1 -1
- package/src/indexeddb/store.ts +13 -6
- package/src/interfaces/array_test_suite.ts +33 -35
- package/src/interfaces/index.ts +1 -1
- package/src/interfaces/map_test_suite.ts +56 -30
- package/src/interfaces/multi_map.ts +7 -0
- package/src/interfaces/multi_map_test_suite.ts +167 -37
- package/src/interfaces/set_test_suite.ts +13 -15
- package/src/interfaces/singleton_test_suite.ts +6 -8
- package/src/interfaces/utils.ts +1 -0
- package/src/lmdb/index.ts +10 -5
- package/src/lmdb/multi_map.ts +16 -0
- package/src/lmdb/store.ts +13 -9
- package/src/lmdb-v2/array.ts +2 -2
- package/src/lmdb-v2/factory.ts +93 -19
- package/src/lmdb-v2/map.ts +2 -2
- package/src/lmdb-v2/multi_map.ts +10 -1
- package/src/lmdb-v2/read_transaction.ts +23 -23
- package/src/lmdb-v2/singleton.ts +2 -2
- package/src/lmdb-v2/store.ts +9 -33
- package/src/lmdb-v2/tx-helpers.ts +29 -0
- package/src/lmdb-v2/utils.ts +0 -118
- package/src/lmdb-v2/write_transaction.ts +9 -8
- package/src/sqlite-opfs/array.ts +124 -0
- package/src/sqlite-opfs/errors.ts +44 -0
- package/src/sqlite-opfs/index.ts +39 -0
- package/src/sqlite-opfs/internal/ordered-binary-browser.js +465 -0
- package/src/sqlite-opfs/map.ts +163 -0
- package/src/sqlite-opfs/messages.ts +39 -0
- package/src/sqlite-opfs/multi_map.ts +74 -0
- package/src/sqlite-opfs/set.ts +29 -0
- package/src/sqlite-opfs/singleton.ts +48 -0
- package/src/sqlite-opfs/store.ts +296 -0
- package/src/sqlite-opfs/worker.ts +231 -0
- package/src/stores/l2_tips_store.ts +66 -56
- package/src/utils.ts +79 -21
- package/dest/config.d.ts +0 -17
- package/dest/config.d.ts.map +0 -1
- package/dest/config.js +0 -26
- package/src/config.ts +0 -36
package/src/lmdb-v2/factory.ts
CHANGED
|
@@ -1,23 +1,31 @@
|
|
|
1
1
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
|
-
import { type
|
|
3
|
-
import { DatabaseVersionManager } from '@aztec/stdlib/database-version';
|
|
2
|
+
import { type LoggerBindings, createLogger } from '@aztec/foundation/log';
|
|
3
|
+
import { DatabaseVersionManager, type SchemaVersionMismatchPolicy } from '@aztec/stdlib/database-version/manager';
|
|
4
|
+
import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
|
|
4
5
|
|
|
5
|
-
import { mkdir, mkdtemp, rm } from 'fs/promises';
|
|
6
|
+
import { copyFile, 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;
|
|
13
13
|
|
|
14
|
+
/** Optional versioning hooks for persistent LMDB stores. */
|
|
15
|
+
export type CreateStoreOptions = {
|
|
16
|
+
onUpgrade?: (dataDir: string, currentVersion: number, latestVersion: number) => Promise<void>;
|
|
17
|
+
schemaVersionMismatchPolicy?: SchemaVersionMismatchPolicy;
|
|
18
|
+
};
|
|
19
|
+
|
|
14
20
|
export async function createStore(
|
|
15
21
|
name: string,
|
|
16
22
|
schemaVersion: number,
|
|
17
23
|
config: DataStoreConfig,
|
|
18
|
-
|
|
24
|
+
bindings?: LoggerBindings,
|
|
25
|
+
options: CreateStoreOptions = {},
|
|
19
26
|
): Promise<AztecLMDBStoreV2> {
|
|
20
|
-
const
|
|
27
|
+
const log = createLogger('kv-store:lmdb-v2:' + name, bindings);
|
|
28
|
+
const { dataDirectory, rollupAddress: rollupFromConfig } = config;
|
|
21
29
|
|
|
22
30
|
let store: AztecLMDBStoreV2;
|
|
23
31
|
if (typeof dataDirectory !== 'undefined') {
|
|
@@ -25,7 +33,7 @@ export async function createStore(
|
|
|
25
33
|
const subDir = join(dataDirectory, name);
|
|
26
34
|
await mkdir(subDir, { recursive: true });
|
|
27
35
|
|
|
28
|
-
const rollupAddress =
|
|
36
|
+
const rollupAddress = rollupFromConfig ?? EthAddress.ZERO;
|
|
29
37
|
|
|
30
38
|
// Create a version manager
|
|
31
39
|
const versionManager = new DatabaseVersionManager({
|
|
@@ -33,7 +41,9 @@ export async function createStore(
|
|
|
33
41
|
rollupAddress,
|
|
34
42
|
dataDirectory: subDir,
|
|
35
43
|
onOpen: dbDirectory =>
|
|
36
|
-
AztecLMDBStoreV2.new(dbDirectory, config.dataStoreMapSizeKb, MAX_READERS, () => Promise.resolve(),
|
|
44
|
+
AztecLMDBStoreV2.new(dbDirectory, config.dataStoreMapSizeKb, MAX_READERS, () => Promise.resolve(), bindings),
|
|
45
|
+
onUpgrade: options.onUpgrade,
|
|
46
|
+
schemaVersionMismatchPolicy: options.schemaVersionMismatchPolicy,
|
|
37
47
|
});
|
|
38
48
|
|
|
39
49
|
log.info(
|
|
@@ -41,25 +51,31 @@ export async function createStore(
|
|
|
41
51
|
);
|
|
42
52
|
[store] = await versionManager.open();
|
|
43
53
|
} else {
|
|
44
|
-
store = await openTmpStore(name, true, config.dataStoreMapSizeKb, MAX_READERS,
|
|
54
|
+
store = await openTmpStore(name, true, config.dataStoreMapSizeKb, MAX_READERS, bindings);
|
|
45
55
|
}
|
|
46
56
|
|
|
47
57
|
return store;
|
|
48
58
|
}
|
|
49
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Open a persistent on-disk store rooted in OS tmpdir. Caller chooses whether to
|
|
62
|
+
* auto-remove the directory on close. Uses standard durable LMDB flags (fsync on
|
|
63
|
+
* every commit) — for full in-memory / NOSYNC semantics use `openEphemeralStore`.
|
|
64
|
+
*/
|
|
50
65
|
export async function openTmpStore(
|
|
51
66
|
name: string,
|
|
52
|
-
|
|
67
|
+
cleanupTmpDir: boolean = true,
|
|
53
68
|
dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
|
|
54
69
|
maxReaders = MAX_READERS,
|
|
55
|
-
|
|
70
|
+
bindings?: LoggerBindings,
|
|
56
71
|
): Promise<AztecLMDBStoreV2> {
|
|
72
|
+
const log = createLogger('kv-store:lmdb-v2:' + name, bindings);
|
|
57
73
|
const dataDir = await mkdtemp(join(tmpdir(), name + '-'));
|
|
58
74
|
log.debug(`Created temporary data store at: ${dataDir} with size: ${dbMapSizeKb} KB (LMDB v2)`);
|
|
59
75
|
|
|
60
76
|
// pass a cleanup callback because process.on('beforeExit', cleanup) does not work under Jest
|
|
61
77
|
const cleanup = async () => {
|
|
62
|
-
if (
|
|
78
|
+
if (cleanupTmpDir) {
|
|
63
79
|
try {
|
|
64
80
|
await rm(dataDir, { recursive: true, force: true, maxRetries: 3 });
|
|
65
81
|
log.debug(`Deleted temporary data store: ${dataDir}`);
|
|
@@ -71,19 +87,76 @@ export async function openTmpStore(
|
|
|
71
87
|
}
|
|
72
88
|
};
|
|
73
89
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
90
|
+
return AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, cleanup, bindings, false);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Open a fully-ephemeral store: a fresh tmpdir backing file, LMDB opened with
|
|
95
|
+
* MDB_NOSYNC | MDB_NOMETASYNC (no fsync, kernel-only writeback), and the directory
|
|
96
|
+
* unconditionally removed on close. Intended for tests and worker-local scratch
|
|
97
|
+
* stores where durability is explicitly not desired.
|
|
98
|
+
*/
|
|
99
|
+
export async function openEphemeralStore(
|
|
100
|
+
name: string,
|
|
101
|
+
dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
|
|
102
|
+
maxReaders = MAX_READERS,
|
|
103
|
+
bindings?: LoggerBindings,
|
|
104
|
+
): Promise<AztecLMDBStoreV2> {
|
|
105
|
+
const log = createLogger('kv-store:lmdb-v2:' + name, bindings);
|
|
106
|
+
const dataDir = await mkdtemp(join(tmpdir(), name + '-'));
|
|
107
|
+
log.debug(`Created ephemeral data store at: ${dataDir} with size: ${dbMapSizeKb} KB (LMDB v2)`);
|
|
108
|
+
|
|
109
|
+
const cleanup = async () => {
|
|
110
|
+
try {
|
|
111
|
+
await rm(dataDir, { recursive: true, force: true, maxRetries: 3 });
|
|
112
|
+
log.debug(`Deleted ephemeral data store: ${dataDir}`);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
log.warn(`Failed to delete ephemeral data directory (LMDB v2) ${dataDir}: ${err}`);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
return AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, cleanup, bindings, true);
|
|
77
119
|
}
|
|
78
120
|
|
|
79
121
|
export async function openStoreAt(
|
|
80
122
|
dataDir: string,
|
|
81
123
|
dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
|
|
82
124
|
maxReaders = MAX_READERS,
|
|
83
|
-
|
|
125
|
+
bindings?: LoggerBindings,
|
|
84
126
|
): Promise<AztecLMDBStoreV2> {
|
|
127
|
+
const log = createLogger('kv-store:lmdb-v2', bindings);
|
|
85
128
|
log.debug(`Opening data store at: ${dataDir} with size: ${dbMapSizeKb} KB (LMDB v2)`);
|
|
86
|
-
return await AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined,
|
|
129
|
+
return await AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, bindings);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Open a fully-ephemeral store seeded from an existing `data.mdb` file. Creates a fresh tmpdir,
|
|
134
|
+
* copies `srcDataMdbPath` into it as `data.mdb`, opens with MDB_NOSYNC | MDB_NOMETASYNC, and
|
|
135
|
+
* removes the tmpdir on close. Intended for worker clones of a main-thread-built store — the
|
|
136
|
+
* source file is never written to.
|
|
137
|
+
*/
|
|
138
|
+
export async function cloneEphemeralStoreFrom(
|
|
139
|
+
srcDataMdbPath: string,
|
|
140
|
+
name: string,
|
|
141
|
+
dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
|
|
142
|
+
maxReaders = MAX_READERS,
|
|
143
|
+
bindings?: LoggerBindings,
|
|
144
|
+
): Promise<AztecLMDBStoreV2> {
|
|
145
|
+
const log = createLogger('kv-store:lmdb-v2:' + name, bindings);
|
|
146
|
+
const dataDir = await mkdtemp(join(tmpdir(), name + '-'));
|
|
147
|
+
await copyFile(srcDataMdbPath, join(dataDir, 'data.mdb'));
|
|
148
|
+
log.debug(`Cloned ephemeral data store at: ${dataDir} from ${srcDataMdbPath} (LMDB v2)`);
|
|
149
|
+
|
|
150
|
+
const cleanup = async () => {
|
|
151
|
+
try {
|
|
152
|
+
await rm(dataDir, { recursive: true, force: true, maxRetries: 3 });
|
|
153
|
+
log.debug(`Deleted ephemeral data store: ${dataDir}`);
|
|
154
|
+
} catch (err) {
|
|
155
|
+
log.warn(`Failed to delete ephemeral data directory (LMDB v2) ${dataDir}: ${err}`);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
return AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, cleanup, bindings, true);
|
|
87
160
|
}
|
|
88
161
|
|
|
89
162
|
export async function openVersionedStoreAt(
|
|
@@ -92,14 +165,15 @@ export async function openVersionedStoreAt(
|
|
|
92
165
|
rollupAddress: EthAddress,
|
|
93
166
|
dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
|
|
94
167
|
maxReaders = MAX_READERS,
|
|
95
|
-
|
|
168
|
+
bindings?: LoggerBindings,
|
|
96
169
|
): Promise<AztecLMDBStoreV2> {
|
|
170
|
+
const log = createLogger('kv-store:lmdb-v2', bindings);
|
|
97
171
|
log.debug(`Opening data store at: ${dataDirectory} with size: ${dbMapSizeKb} KB (LMDB v2)`);
|
|
98
172
|
const [store] = await new DatabaseVersionManager({
|
|
99
173
|
schemaVersion,
|
|
100
174
|
rollupAddress,
|
|
101
175
|
dataDirectory,
|
|
102
|
-
onOpen: dataDir => AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined,
|
|
176
|
+
onOpen: dataDir => AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, bindings),
|
|
103
177
|
}).open();
|
|
104
178
|
return store;
|
|
105
179
|
}
|
package/src/lmdb-v2/map.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { Encoder } from 'msgpackr';
|
|
|
3
3
|
import type { Key, Range, Value } from '../interfaces/common.js';
|
|
4
4
|
import type { AztecAsyncMap } from '../interfaces/map.js';
|
|
5
5
|
import type { ReadTransaction } from './read_transaction.js';
|
|
6
|
-
|
|
7
|
-
import {
|
|
6
|
+
import type { AztecLMDBStoreV2 } from './store.js';
|
|
7
|
+
import { execInReadTx, execInWriteTx } from './tx-helpers.js';
|
|
8
8
|
import { deserializeKey, maxKey, minKey, serializeKey } from './utils.js';
|
|
9
9
|
|
|
10
10
|
export class LMDBMap<K extends Key, V extends Value> implements AztecAsyncMap<K, V> {
|
package/src/lmdb-v2/multi_map.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { Encoder } from 'msgpackr/pack';
|
|
2
|
+
import { MAXIMUM_KEY, toBufferKey } from 'ordered-binary';
|
|
2
3
|
|
|
3
4
|
import type { Key, Range, Value } from '../interfaces/common.js';
|
|
4
5
|
import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
|
|
5
6
|
import type { ReadTransaction } from './read_transaction.js';
|
|
6
|
-
import {
|
|
7
|
+
import type { AztecLMDBStoreV2 } from './store.js';
|
|
8
|
+
import { execInReadTx, execInWriteTx } from './tx-helpers.js';
|
|
7
9
|
import { deserializeKey, maxKey, minKey, serializeKey } from './utils.js';
|
|
8
10
|
|
|
9
11
|
export class LMDBMultiMap<K extends Key, V extends Value> implements AztecAsyncMultiMap<K, V> {
|
|
@@ -138,4 +140,11 @@ export class LMDBMultiMap<K extends Key, V extends Value> implements AztecAsyncM
|
|
|
138
140
|
yield this.encoder.unpack(value);
|
|
139
141
|
}
|
|
140
142
|
}
|
|
143
|
+
|
|
144
|
+
getValueCountAsync(key: K): Promise<number> {
|
|
145
|
+
const startKey = serializeKey(this.prefix, key);
|
|
146
|
+
const endKey = toBufferKey([this.prefix, key, MAXIMUM_KEY]);
|
|
147
|
+
|
|
148
|
+
return execInReadTx(this.store, tx => tx.countEntriesIndex(startKey, endKey, false));
|
|
149
|
+
}
|
|
141
150
|
}
|
|
@@ -66,20 +66,20 @@ export class ReadTransaction {
|
|
|
66
66
|
): AsyncIterable<[Uint8Array, T]> {
|
|
67
67
|
this.assertIsOpen();
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
key: startKey,
|
|
71
|
-
reverse,
|
|
72
|
-
count: typeof limit === 'number' ? Math.min(limit, CURSOR_PAGE_SIZE) : CURSOR_PAGE_SIZE,
|
|
73
|
-
onePage: typeof limit === 'number' && limit < CURSOR_PAGE_SIZE,
|
|
74
|
-
db,
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
const cursor = response.cursor;
|
|
78
|
-
let entries = response.entries;
|
|
79
|
-
let done = typeof cursor !== 'number';
|
|
80
|
-
let count = 0;
|
|
81
|
-
|
|
69
|
+
let cursor: number | undefined;
|
|
82
70
|
try {
|
|
71
|
+
const response = await this.channel.sendMessage(LMDBMessageType.START_CURSOR, {
|
|
72
|
+
key: startKey,
|
|
73
|
+
reverse,
|
|
74
|
+
count: typeof limit === 'number' ? Math.min(limit, CURSOR_PAGE_SIZE) : CURSOR_PAGE_SIZE,
|
|
75
|
+
onePage: typeof limit === 'number' && limit < CURSOR_PAGE_SIZE,
|
|
76
|
+
db,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
cursor = response.cursor ?? undefined;
|
|
80
|
+
let entries = response.entries;
|
|
81
|
+
let done = typeof cursor !== 'number';
|
|
82
|
+
let count = 0;
|
|
83
83
|
// emit the first page and any subsequent pages in a while loop
|
|
84
84
|
// NB: end contition is in the middle of the while loop
|
|
85
85
|
while (entries.length > 0) {
|
|
@@ -125,17 +125,17 @@ export class ReadTransaction {
|
|
|
125
125
|
async #countEntries(db: string, startKey: Uint8Array, endKey: Uint8Array, reverse: boolean): Promise<number> {
|
|
126
126
|
this.assertIsOpen();
|
|
127
127
|
|
|
128
|
-
|
|
129
|
-
key: startKey,
|
|
130
|
-
reverse,
|
|
131
|
-
count: 0,
|
|
132
|
-
onePage: false,
|
|
133
|
-
db,
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
const cursor = response.cursor;
|
|
137
|
-
|
|
128
|
+
let cursor: number | undefined;
|
|
138
129
|
try {
|
|
130
|
+
const response = await this.channel.sendMessage(LMDBMessageType.START_CURSOR, {
|
|
131
|
+
key: startKey,
|
|
132
|
+
reverse,
|
|
133
|
+
count: 0,
|
|
134
|
+
onePage: false,
|
|
135
|
+
db,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
cursor = response.cursor ?? undefined;
|
|
139
139
|
if (!cursor) {
|
|
140
140
|
return 0;
|
|
141
141
|
}
|
package/src/lmdb-v2/singleton.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Encoder } from 'msgpackr';
|
|
2
2
|
|
|
3
3
|
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
4
|
-
|
|
5
|
-
import {
|
|
4
|
+
import type { AztecLMDBStoreV2 } from './store.js';
|
|
5
|
+
import { execInReadTx, execInWriteTx } from './tx-helpers.js';
|
|
6
6
|
import { serializeKey } from './utils.js';
|
|
7
7
|
|
|
8
8
|
export class LMDBSingleValue<T> implements AztecAsyncSingleton<T> {
|
package/src/lmdb-v2/store.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
1
|
+
import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
|
|
2
2
|
import { Semaphore, SerialQueue } from '@aztec/foundation/queue';
|
|
3
3
|
import { MsgpackChannel, NativeLMDBStore } from '@aztec/native';
|
|
4
4
|
|
|
@@ -13,9 +13,7 @@ import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
|
|
|
13
13
|
import type { AztecAsyncSet } from '../interfaces/set.js';
|
|
14
14
|
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
15
15
|
import type { AztecAsyncKVStore } from '../interfaces/store.js';
|
|
16
|
-
// eslint-disable-next-line import/no-cycle
|
|
17
16
|
import { LMDBArray } from './array.js';
|
|
18
|
-
// eslint-disable-next-line import/no-cycle
|
|
19
17
|
import { LMDBMap } from './map.js';
|
|
20
18
|
import {
|
|
21
19
|
Database,
|
|
@@ -27,10 +25,11 @@ import {
|
|
|
27
25
|
import { LMDBMultiMap } from './multi_map.js';
|
|
28
26
|
import { ReadTransaction } from './read_transaction.js';
|
|
29
27
|
import { LMDBSet } from './set.js';
|
|
30
|
-
// eslint-disable-next-line import/no-cycle
|
|
31
28
|
import { LMDBSingleValue } from './singleton.js';
|
|
32
29
|
import { WriteTransaction } from './write_transaction.js';
|
|
33
30
|
|
|
31
|
+
export { execInReadTx, execInWriteTx } from './tx-helpers.js';
|
|
32
|
+
|
|
34
33
|
export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
|
|
35
34
|
private open = false;
|
|
36
35
|
private channel: MsgpackChannel<LMDBMessageType, LMDBRequestBody, LMDBResponseBody>;
|
|
@@ -44,9 +43,10 @@ export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
|
|
|
44
43
|
maxReaders: number,
|
|
45
44
|
private log: Logger,
|
|
46
45
|
private cleanup?: () => Promise<void>,
|
|
46
|
+
ephemeral: boolean = false,
|
|
47
47
|
) {
|
|
48
48
|
this.log.info(`Starting data store with maxReaders ${maxReaders}`);
|
|
49
|
-
this.channel = new MsgpackChannel(new NativeLMDBStore(dataDir, mapSize, maxReaders));
|
|
49
|
+
this.channel = new MsgpackChannel(new NativeLMDBStore(dataDir, mapSize, maxReaders, ephemeral));
|
|
50
50
|
// leave one reader to always be available for regular, atomic, reads
|
|
51
51
|
this.availableCursors = new Semaphore(maxReaders - 1);
|
|
52
52
|
}
|
|
@@ -76,9 +76,11 @@ export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
|
|
|
76
76
|
dbMapSizeKb: number = 10 * 1024 * 1024,
|
|
77
77
|
maxReaders: number = 16,
|
|
78
78
|
cleanup?: () => Promise<void>,
|
|
79
|
-
|
|
79
|
+
bindings?: LoggerBindings,
|
|
80
|
+
ephemeral: boolean = false,
|
|
80
81
|
) {
|
|
81
|
-
const
|
|
82
|
+
const log = createLogger('kv-store:lmdb-v2', bindings);
|
|
83
|
+
const db = new AztecLMDBStoreV2(dataDir, dbMapSizeKb, maxReaders, log, cleanup, ephemeral);
|
|
82
84
|
await db.start();
|
|
83
85
|
return db;
|
|
84
86
|
}
|
|
@@ -217,29 +219,3 @@ export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
|
|
|
217
219
|
};
|
|
218
220
|
}
|
|
219
221
|
}
|
|
220
|
-
|
|
221
|
-
export function execInWriteTx<T>(store: AztecLMDBStoreV2, fn: (tx: WriteTransaction) => Promise<T>): Promise<T> {
|
|
222
|
-
const currentWrite = store.getCurrentWriteTx();
|
|
223
|
-
if (currentWrite) {
|
|
224
|
-
return fn(currentWrite);
|
|
225
|
-
} else {
|
|
226
|
-
return store.transactionAsync(fn);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export async function execInReadTx<T>(
|
|
231
|
-
store: AztecLMDBStoreV2,
|
|
232
|
-
fn: (tx: ReadTransaction) => T | Promise<T>,
|
|
233
|
-
): Promise<T> {
|
|
234
|
-
const currentWrite = store.getCurrentWriteTx();
|
|
235
|
-
if (currentWrite) {
|
|
236
|
-
return await fn(currentWrite);
|
|
237
|
-
} else {
|
|
238
|
-
const tx = store.getReadTx();
|
|
239
|
-
try {
|
|
240
|
-
return await fn(tx);
|
|
241
|
-
} finally {
|
|
242
|
-
tx.close();
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ReadTransaction } from './read_transaction.js';
|
|
2
|
+
import type { AztecLMDBStoreV2 } from './store.js';
|
|
3
|
+
import type { WriteTransaction } from './write_transaction.js';
|
|
4
|
+
|
|
5
|
+
export function execInWriteTx<T>(store: AztecLMDBStoreV2, fn: (tx: WriteTransaction) => Promise<T>): Promise<T> {
|
|
6
|
+
const currentWrite = store.getCurrentWriteTx();
|
|
7
|
+
if (currentWrite) {
|
|
8
|
+
return fn(currentWrite);
|
|
9
|
+
} else {
|
|
10
|
+
return store.transactionAsync(fn);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function execInReadTx<T>(
|
|
15
|
+
store: AztecLMDBStoreV2,
|
|
16
|
+
fn: (tx: ReadTransaction) => T | Promise<T>,
|
|
17
|
+
): Promise<T> {
|
|
18
|
+
const currentWrite = store.getCurrentWriteTx();
|
|
19
|
+
if (currentWrite) {
|
|
20
|
+
return await fn(currentWrite);
|
|
21
|
+
} else {
|
|
22
|
+
const tx = store.getReadTx();
|
|
23
|
+
try {
|
|
24
|
+
return await fn(tx);
|
|
25
|
+
} finally {
|
|
26
|
+
tx.close();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/lmdb-v2/utils.ts
CHANGED
|
@@ -2,124 +2,6 @@ import { MAXIMUM_KEY, fromBufferKey, toBufferKey } from 'ordered-binary';
|
|
|
2
2
|
|
|
3
3
|
import type { Key } from '../interfaces/common.js';
|
|
4
4
|
|
|
5
|
-
type Cmp<T> = (a: T, b: T) => -1 | 0 | 1;
|
|
6
|
-
|
|
7
|
-
export function dedupeSortedArray<T>(arr: T[], cmp: Cmp<T>): void {
|
|
8
|
-
for (let i = 0; i < arr.length; i++) {
|
|
9
|
-
let j = i + 1;
|
|
10
|
-
for (; j < arr.length; j++) {
|
|
11
|
-
const res = cmp(arr[i], arr[j]);
|
|
12
|
-
if (res === 0) {
|
|
13
|
-
continue;
|
|
14
|
-
} else if (res < 0) {
|
|
15
|
-
break;
|
|
16
|
-
} else {
|
|
17
|
-
throw new Error('Array not sorted');
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (j - i > 1) {
|
|
22
|
-
arr.splice(i + 1, j - i - 1);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function insertIntoSortedArray<T>(arr: T[], item: T, cmp: (a: T, b: T) => number): void {
|
|
28
|
-
let left = 0;
|
|
29
|
-
let right = arr.length;
|
|
30
|
-
|
|
31
|
-
while (left < right) {
|
|
32
|
-
const mid = (left + right) >> 1;
|
|
33
|
-
const comparison = cmp(arr[mid], item);
|
|
34
|
-
|
|
35
|
-
if (comparison < 0) {
|
|
36
|
-
left = mid + 1;
|
|
37
|
-
} else {
|
|
38
|
-
right = mid;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
arr.splice(left, 0, item);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function findIndexInSortedArray<T, N>(values: T[], needle: N, cmp: (a: T, b: N) => number): number {
|
|
46
|
-
let start = 0;
|
|
47
|
-
let end = values.length - 1;
|
|
48
|
-
|
|
49
|
-
while (start <= end) {
|
|
50
|
-
const mid = start + (((end - start) / 2) | 0);
|
|
51
|
-
const res = cmp(values[mid], needle);
|
|
52
|
-
if (res === 0) {
|
|
53
|
-
return mid;
|
|
54
|
-
} else if (res > 0) {
|
|
55
|
-
end = mid - 1;
|
|
56
|
-
} else {
|
|
57
|
-
start = mid + 1;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return -1;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function findInSortedArray<T, N>(values: T[], needle: N, cmp: (a: T, b: N) => number): T | undefined {
|
|
65
|
-
const idx = findIndexInSortedArray(values, needle, cmp);
|
|
66
|
-
return idx > -1 ? values[idx] : undefined;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function removeAnyOf<T, N>(arr: T[], vals: N[], cmp: (a: T, b: N) => -1 | 0 | 1): void {
|
|
70
|
-
let writeIdx = 0;
|
|
71
|
-
let readIdx = 0;
|
|
72
|
-
let valIdx = 0;
|
|
73
|
-
|
|
74
|
-
while (readIdx < arr.length && valIdx < vals.length) {
|
|
75
|
-
const comparison = cmp(arr[readIdx], vals[valIdx]);
|
|
76
|
-
|
|
77
|
-
if (comparison < 0) {
|
|
78
|
-
arr[writeIdx++] = arr[readIdx++];
|
|
79
|
-
} else if (comparison > 0) {
|
|
80
|
-
valIdx++;
|
|
81
|
-
} else {
|
|
82
|
-
readIdx++;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
while (readIdx < arr.length) {
|
|
87
|
-
arr[writeIdx++] = arr[readIdx++];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
arr.length = writeIdx;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function removeFromSortedArray<T, N>(arr: T[], val: N, cmp: (a: T, b: N) => -1 | 0 | 1) {
|
|
94
|
-
const idx = findIndexInSortedArray(arr, val, cmp);
|
|
95
|
-
if (idx > -1) {
|
|
96
|
-
arr.splice(idx, 1);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function merge<T>(arr: T[], toInsert: T[], cmp: (a: T, b: T) => -1 | 0 | 1): void {
|
|
101
|
-
const result = new Array<T>(arr.length + toInsert.length);
|
|
102
|
-
let i = 0,
|
|
103
|
-
j = 0,
|
|
104
|
-
k = 0;
|
|
105
|
-
|
|
106
|
-
while (i < arr.length && j < toInsert.length) {
|
|
107
|
-
result[k++] = cmp(arr[i], toInsert[j]) <= 0 ? arr[i++] : toInsert[j++];
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
while (i < arr.length) {
|
|
111
|
-
result[k++] = arr[i++];
|
|
112
|
-
}
|
|
113
|
-
while (j < toInsert.length) {
|
|
114
|
-
result[k++] = toInsert[j++];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
for (i = 0; i < result.length; i++) {
|
|
118
|
-
arr[i] = result[i];
|
|
119
|
-
}
|
|
120
|
-
arr.length = result.length;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
5
|
export function keyCmp(a: [Uint8Array, Uint8Array[] | null], b: [Uint8Array, Uint8Array[] | null]): -1 | 0 | 1 {
|
|
124
6
|
return Buffer.compare(a[0], b[0]);
|
|
125
7
|
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { type Batch, Database, LMDBMessageType } from './message.js';
|
|
2
|
-
import { ReadTransaction } from './read_transaction.js';
|
|
3
1
|
import {
|
|
4
2
|
dedupeSortedArray,
|
|
5
3
|
findInSortedArray,
|
|
6
4
|
findIndexInSortedArray,
|
|
7
5
|
insertIntoSortedArray,
|
|
8
|
-
keyCmp,
|
|
9
6
|
merge,
|
|
10
7
|
removeAnyOf,
|
|
11
8
|
removeFromSortedArray,
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
} from '@aztec/foundation/array';
|
|
10
|
+
|
|
11
|
+
import { type Batch, Database, LMDBMessageType } from './message.js';
|
|
12
|
+
import { ReadTransaction } from './read_transaction.js';
|
|
13
|
+
import { keyCmp, singleKeyCmp } from './utils.js';
|
|
14
14
|
|
|
15
15
|
export class WriteTransaction extends ReadTransaction {
|
|
16
16
|
// exposed for tests
|
|
@@ -44,7 +44,7 @@ export class WriteTransaction extends ReadTransaction {
|
|
|
44
44
|
remove(key: Uint8Array): Promise<void> {
|
|
45
45
|
const removeEntryIndex = findIndexInSortedArray(this.dataBatch.removeEntries, key, singleKeyCmp);
|
|
46
46
|
if (removeEntryIndex === -1) {
|
|
47
|
-
this.dataBatch.removeEntries
|
|
47
|
+
insertIntoSortedArray(this.dataBatch.removeEntries, [key, null], keyCmp);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
const addEntryIndex = findIndexInSortedArray(this.dataBatch.addEntries, key, singleKeyCmp);
|
|
@@ -218,8 +218,9 @@ export class WriteTransaction extends ReadTransaction {
|
|
|
218
218
|
): AsyncIterable<[Uint8Array, T]> {
|
|
219
219
|
this.assertIsOpen();
|
|
220
220
|
|
|
221
|
-
//
|
|
221
|
+
// Snapshot both add and remove entries at the start of iteration to ensure consistency
|
|
222
222
|
const uncommittedEntries = [...batch.addEntries];
|
|
223
|
+
const removeEntries = [...batch.removeEntries];
|
|
223
224
|
// used to check we're in the right order when comparing between a key and uncommittedEntries
|
|
224
225
|
let cmpDirection = -1;
|
|
225
226
|
if (reverse) {
|
|
@@ -262,7 +263,7 @@ export class WriteTransaction extends ReadTransaction {
|
|
|
262
263
|
break;
|
|
263
264
|
}
|
|
264
265
|
|
|
265
|
-
const toRemove = findInSortedArray(
|
|
266
|
+
const toRemove = findInSortedArray(removeEntries, key, singleKeyCmp);
|
|
266
267
|
|
|
267
268
|
// at this point we've either exhausted all uncommitted entries,
|
|
268
269
|
// we reached a key strictly greater/smaller than `key`
|