@aztec/kv-store 0.71.0 → 0.73.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/dest/config.js +3 -3
- package/dest/indexeddb/store.d.ts +4 -7
- package/dest/indexeddb/store.d.ts.map +1 -1
- package/dest/indexeddb/store.js +5 -2
- package/dest/interfaces/common.d.ts +6 -1
- package/dest/interfaces/common.d.ts.map +1 -1
- package/dest/interfaces/index.d.ts +1 -1
- package/dest/interfaces/index.d.ts.map +1 -1
- package/dest/interfaces/map.d.ts +0 -6
- package/dest/interfaces/map.d.ts.map +1 -1
- package/dest/interfaces/map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/map_test_suite.js +1 -12
- package/dest/interfaces/store.d.ts +11 -11
- package/dest/interfaces/store.d.ts.map +1 -1
- package/dest/lmdb/store.d.ts +2 -6
- package/dest/lmdb/store.d.ts.map +1 -1
- package/dest/lmdb/store.js +3 -3
- package/dest/lmdb-v2/factory.d.ts +7 -0
- package/dest/lmdb-v2/factory.d.ts.map +1 -0
- package/dest/lmdb-v2/factory.js +56 -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 +3 -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 +196 -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 +19 -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 +89 -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 +29 -0
- package/dest/lmdb-v2/store.d.ts +41 -0
- package/dest/lmdb-v2/store.d.ts.map +1 -0
- package/dest/lmdb-v2/store.js +156 -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 +234 -0
- package/dest/stores/l2_tips_store.js +2 -2
- package/package.json +14 -6
- package/src/config.ts +2 -2
- package/src/indexeddb/store.ts +8 -4
- package/src/interfaces/common.ts +3 -1
- package/src/interfaces/index.ts +1 -1
- package/src/interfaces/map.ts +0 -7
- package/src/interfaces/map_test_suite.ts +1 -16
- package/src/interfaces/store.ts +13 -3
- package/src/lmdb/store.ts +4 -4
- package/src/lmdb-v2/factory.ts +79 -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 +210 -0
- package/src/lmdb-v2/utils.ts +150 -0
- package/src/lmdb-v2/write_transaction.ts +314 -0
- package/src/stores/l2_tips_store.ts +1 -1
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
export enum Database {
|
|
2
|
+
DATA = 'data',
|
|
3
|
+
INDEX = 'index',
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export const CURSOR_PAGE_SIZE = 10;
|
|
7
|
+
|
|
8
|
+
export enum LMDBMessageType {
|
|
9
|
+
OPEN_DATABASE = 100,
|
|
10
|
+
GET,
|
|
11
|
+
HAS,
|
|
12
|
+
|
|
13
|
+
START_CURSOR,
|
|
14
|
+
ADVANCE_CURSOR,
|
|
15
|
+
CLOSE_CURSOR,
|
|
16
|
+
|
|
17
|
+
BATCH,
|
|
18
|
+
|
|
19
|
+
STATS,
|
|
20
|
+
|
|
21
|
+
CLOSE,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type Key = Uint8Array;
|
|
25
|
+
type Value = Uint8Array;
|
|
26
|
+
type OptionalValues = Array<Value[] | null>;
|
|
27
|
+
type KeyOptionalValues = [Key, null | Array<Value>];
|
|
28
|
+
type KeyValues = [Key, Value[]];
|
|
29
|
+
|
|
30
|
+
interface OpenDatabaseRequest {
|
|
31
|
+
db: string;
|
|
32
|
+
uniqueKeys?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface GetRequest {
|
|
36
|
+
keys: Key[];
|
|
37
|
+
db: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface GetResponse {
|
|
41
|
+
values: OptionalValues;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface HasRequest {
|
|
45
|
+
entries: KeyOptionalValues[];
|
|
46
|
+
db: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface StartCursorRequest {
|
|
50
|
+
key: Key;
|
|
51
|
+
reverse: boolean;
|
|
52
|
+
count: number | null;
|
|
53
|
+
onePage: boolean | null;
|
|
54
|
+
db: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface AdvanceCursorRequest {
|
|
58
|
+
cursor: number;
|
|
59
|
+
count: number | null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface CloseCursorRequest {
|
|
63
|
+
cursor: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface Batch {
|
|
67
|
+
addEntries: Array<KeyValues>;
|
|
68
|
+
removeEntries: Array<KeyOptionalValues>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface BatchRequest {
|
|
72
|
+
batches: Map<string, Batch>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type LMDBRequestBody = {
|
|
76
|
+
[LMDBMessageType.OPEN_DATABASE]: OpenDatabaseRequest;
|
|
77
|
+
|
|
78
|
+
[LMDBMessageType.GET]: GetRequest;
|
|
79
|
+
[LMDBMessageType.HAS]: HasRequest;
|
|
80
|
+
|
|
81
|
+
[LMDBMessageType.START_CURSOR]: StartCursorRequest;
|
|
82
|
+
[LMDBMessageType.ADVANCE_CURSOR]: AdvanceCursorRequest;
|
|
83
|
+
[LMDBMessageType.CLOSE_CURSOR]: CloseCursorRequest;
|
|
84
|
+
|
|
85
|
+
[LMDBMessageType.BATCH]: BatchRequest;
|
|
86
|
+
|
|
87
|
+
[LMDBMessageType.STATS]: void;
|
|
88
|
+
|
|
89
|
+
[LMDBMessageType.CLOSE]: void;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
interface GetResponse {
|
|
93
|
+
values: OptionalValues;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
interface HasResponse {
|
|
97
|
+
exists: boolean[];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface StartCursorResponse {
|
|
101
|
+
cursor: number | null;
|
|
102
|
+
entries: Array<KeyValues>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface AdvanceCursorResponse {
|
|
106
|
+
entries: Array<KeyValues>;
|
|
107
|
+
done: boolean;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
interface BatchResponse {
|
|
111
|
+
durationNs: number;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface BoolResponse {
|
|
115
|
+
ok: true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
interface StatsResponse {
|
|
119
|
+
stats: Array<{
|
|
120
|
+
name: string;
|
|
121
|
+
numDataItems: bigint | number;
|
|
122
|
+
totalUsedSize: bigint | number;
|
|
123
|
+
}>;
|
|
124
|
+
dbMapSizeBytes: bigint | number;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export type LMDBResponseBody = {
|
|
128
|
+
[LMDBMessageType.OPEN_DATABASE]: BoolResponse;
|
|
129
|
+
|
|
130
|
+
[LMDBMessageType.GET]: GetResponse;
|
|
131
|
+
[LMDBMessageType.HAS]: HasResponse;
|
|
132
|
+
|
|
133
|
+
[LMDBMessageType.START_CURSOR]: StartCursorResponse;
|
|
134
|
+
[LMDBMessageType.ADVANCE_CURSOR]: AdvanceCursorResponse;
|
|
135
|
+
[LMDBMessageType.CLOSE_CURSOR]: BoolResponse;
|
|
136
|
+
|
|
137
|
+
[LMDBMessageType.BATCH]: BatchResponse;
|
|
138
|
+
|
|
139
|
+
[LMDBMessageType.STATS]: StatsResponse;
|
|
140
|
+
|
|
141
|
+
[LMDBMessageType.CLOSE]: BoolResponse;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export interface LMDBMessageChannel {
|
|
145
|
+
sendMessage<T extends LMDBMessageType>(msgType: T, body: LMDBRequestBody[T]): Promise<LMDBResponseBody[T]>;
|
|
146
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { CURSOR_PAGE_SIZE, Database, type LMDBMessageChannel, LMDBMessageType } from './message.js';
|
|
2
|
+
|
|
3
|
+
export class ReadTransaction {
|
|
4
|
+
protected open = true;
|
|
5
|
+
|
|
6
|
+
constructor(protected channel: LMDBMessageChannel) {}
|
|
7
|
+
|
|
8
|
+
public close(): void {
|
|
9
|
+
if (!this.open) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
this.open = false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
protected assertIsOpen() {
|
|
16
|
+
if (!this.open) {
|
|
17
|
+
throw new Error('Transaction is closed');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async get(key: Uint8Array): Promise<Uint8Array | undefined> {
|
|
22
|
+
this.assertIsOpen();
|
|
23
|
+
const response = await this.channel.sendMessage(LMDBMessageType.GET, { keys: [key], db: Database.DATA });
|
|
24
|
+
return response.values[0]?.[0] ?? undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public async getIndex(key: Uint8Array): Promise<Uint8Array[]> {
|
|
28
|
+
this.assertIsOpen();
|
|
29
|
+
const response = await this.channel.sendMessage(LMDBMessageType.GET, { keys: [key], db: Database.INDEX });
|
|
30
|
+
return response.values[0] ?? [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public async *iterate(
|
|
34
|
+
startKey: Uint8Array,
|
|
35
|
+
endKey?: Uint8Array,
|
|
36
|
+
reverse = false,
|
|
37
|
+
limit?: number,
|
|
38
|
+
): AsyncIterable<[Uint8Array, Uint8Array]> {
|
|
39
|
+
yield* this.#iterate(Database.DATA, startKey, endKey, reverse, limit, vals => vals[0]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public async *iterateIndex(
|
|
43
|
+
startKey: Uint8Array,
|
|
44
|
+
endKey?: Uint8Array,
|
|
45
|
+
reverse = false,
|
|
46
|
+
limit?: number,
|
|
47
|
+
): AsyncIterable<[Uint8Array, Uint8Array[]]> {
|
|
48
|
+
yield* this.#iterate(Database.INDEX, startKey, endKey, reverse, limit, vals => vals);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async *#iterate<T>(
|
|
52
|
+
db: string,
|
|
53
|
+
startKey: Uint8Array,
|
|
54
|
+
endKey: Uint8Array | undefined,
|
|
55
|
+
reverse: boolean,
|
|
56
|
+
limit: number | undefined,
|
|
57
|
+
map: (val: Uint8Array[]) => T,
|
|
58
|
+
): AsyncIterable<[Uint8Array, T]> {
|
|
59
|
+
this.assertIsOpen();
|
|
60
|
+
|
|
61
|
+
const response = await this.channel.sendMessage(LMDBMessageType.START_CURSOR, {
|
|
62
|
+
key: startKey,
|
|
63
|
+
reverse,
|
|
64
|
+
count: typeof limit === 'number' ? Math.min(limit, CURSOR_PAGE_SIZE) : CURSOR_PAGE_SIZE,
|
|
65
|
+
onePage: typeof limit === 'number' && limit < CURSOR_PAGE_SIZE,
|
|
66
|
+
db,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const cursor = response.cursor;
|
|
70
|
+
let entries = response.entries;
|
|
71
|
+
let done = typeof cursor !== 'number';
|
|
72
|
+
let count = 0;
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
// emit the first page and any subsequent pages in a while loop
|
|
76
|
+
// NB: end contition is in the middle of the while loop
|
|
77
|
+
while (entries.length > 0) {
|
|
78
|
+
for (const [key, values] of entries) {
|
|
79
|
+
if (typeof limit === 'number' && count >= limit) {
|
|
80
|
+
done = true;
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (endKey) {
|
|
85
|
+
const cmp = Buffer.compare(key, endKey);
|
|
86
|
+
if ((!reverse && cmp >= 0) || (reverse && cmp <= 0)) {
|
|
87
|
+
done = true;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
count++;
|
|
93
|
+
yield [key, map(values)];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// cursor is null if DB returned everything in the first page
|
|
97
|
+
if (typeof cursor !== 'number' || done) {
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const response = await this.channel.sendMessage(LMDBMessageType.ADVANCE_CURSOR, {
|
|
102
|
+
cursor,
|
|
103
|
+
count: CURSOR_PAGE_SIZE,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
done = response.done;
|
|
107
|
+
entries = response.entries;
|
|
108
|
+
}
|
|
109
|
+
} finally {
|
|
110
|
+
// we might not have anything to close
|
|
111
|
+
if (typeof cursor === 'number') {
|
|
112
|
+
await this.channel.sendMessage(LMDBMessageType.CLOSE_CURSOR, { cursor });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Encoder } from 'msgpackr';
|
|
2
|
+
|
|
3
|
+
import { type AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
4
|
+
import { type AztecLMDBStoreV2, execInReadTx, execInWriteTx } from './store.js';
|
|
5
|
+
import { serializeKey } from './utils.js';
|
|
6
|
+
|
|
7
|
+
export class LMDBSingleValue<T> implements AztecAsyncSingleton<T> {
|
|
8
|
+
private key: Uint8Array;
|
|
9
|
+
private encoder = new Encoder();
|
|
10
|
+
constructor(private store: AztecLMDBStoreV2, name: string) {
|
|
11
|
+
this.key = serializeKey(`singleton:${name}`, 'value');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
getAsync(): Promise<T | undefined> {
|
|
15
|
+
return execInReadTx(this.store, async tx => {
|
|
16
|
+
const val = await tx.get(this.key);
|
|
17
|
+
return val ? this.encoder.unpack(val) : undefined;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
set(val: T): Promise<boolean> {
|
|
22
|
+
return execInWriteTx(this.store, async tx => {
|
|
23
|
+
await tx.set(this.key, this.encoder.pack(val));
|
|
24
|
+
return true;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
delete(): Promise<boolean> {
|
|
29
|
+
return execInWriteTx(this.store, async tx => {
|
|
30
|
+
await tx.remove(this.key);
|
|
31
|
+
return true;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
2
|
+
import { Semaphore, SerialQueue } from '@aztec/foundation/queue';
|
|
3
|
+
import { MsgpackChannel, NativeLMDBStore } from '@aztec/native';
|
|
4
|
+
|
|
5
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
6
|
+
import { rm } from 'fs/promises';
|
|
7
|
+
|
|
8
|
+
import type { AztecAsyncArray } from '../interfaces/array.js';
|
|
9
|
+
import type { Key, StoreSize } from '../interfaces/common.js';
|
|
10
|
+
import type { AztecAsyncCounter } from '../interfaces/counter.js';
|
|
11
|
+
import type { AztecAsyncMap, AztecAsyncMultiMap } from '../interfaces/map.js';
|
|
12
|
+
import type { AztecAsyncSet } from '../interfaces/set.js';
|
|
13
|
+
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
14
|
+
import type { AztecAsyncKVStore } from '../interfaces/store.js';
|
|
15
|
+
import { LMDBMap, LMDBMultiMap } from './map.js';
|
|
16
|
+
import {
|
|
17
|
+
Database,
|
|
18
|
+
type LMDBMessageChannel,
|
|
19
|
+
LMDBMessageType,
|
|
20
|
+
type LMDBRequestBody,
|
|
21
|
+
type LMDBResponseBody,
|
|
22
|
+
} from './message.js';
|
|
23
|
+
import { ReadTransaction } from './read_transaction.js';
|
|
24
|
+
import { LMDBSingleValue } from './singleton.js';
|
|
25
|
+
import { WriteTransaction } from './write_transaction.js';
|
|
26
|
+
|
|
27
|
+
export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
|
|
28
|
+
private channel: MsgpackChannel<LMDBMessageType, LMDBRequestBody, LMDBResponseBody>;
|
|
29
|
+
private writerCtx = new AsyncLocalStorage<WriteTransaction>();
|
|
30
|
+
private writerQueue = new SerialQueue();
|
|
31
|
+
private availableCursors: Semaphore;
|
|
32
|
+
|
|
33
|
+
private constructor(
|
|
34
|
+
private dataDir: string,
|
|
35
|
+
mapSize: number,
|
|
36
|
+
maxReaders: number,
|
|
37
|
+
private log: Logger,
|
|
38
|
+
private cleanup?: () => Promise<void>,
|
|
39
|
+
) {
|
|
40
|
+
this.log.info(`Starting data store with maxReaders ${maxReaders}`);
|
|
41
|
+
this.channel = new MsgpackChannel(new NativeLMDBStore(dataDir, mapSize, maxReaders));
|
|
42
|
+
// leave one reader to always be available for regular, atomic, reads
|
|
43
|
+
this.availableCursors = new Semaphore(maxReaders - 1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private async start() {
|
|
47
|
+
this.writerQueue.start();
|
|
48
|
+
|
|
49
|
+
await this.sendMessage(LMDBMessageType.OPEN_DATABASE, {
|
|
50
|
+
db: Database.DATA,
|
|
51
|
+
uniqueKeys: true,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
await this.sendMessage(LMDBMessageType.OPEN_DATABASE, {
|
|
55
|
+
db: Database.INDEX,
|
|
56
|
+
uniqueKeys: false,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public static async new(
|
|
61
|
+
dataDir: string,
|
|
62
|
+
dbMapSizeKb: number = 10 * 1024 * 1024,
|
|
63
|
+
maxReaders: number = 16,
|
|
64
|
+
cleanup?: () => Promise<void>,
|
|
65
|
+
log = createLogger('kv-store:lmdb-v2'),
|
|
66
|
+
) {
|
|
67
|
+
const db = new AztecLMDBStoreV2(dataDir, dbMapSizeKb, maxReaders, log, cleanup);
|
|
68
|
+
await db.start();
|
|
69
|
+
return db;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public getReadTx(): ReadTransaction {
|
|
73
|
+
return new ReadTransaction(this);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public getCurrentWriteTx(): WriteTransaction | undefined {
|
|
77
|
+
const currentWrite = this.writerCtx.getStore();
|
|
78
|
+
return currentWrite;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
openMap<K extends Key, V>(name: string): AztecAsyncMap<K, V> {
|
|
82
|
+
return new LMDBMap(this, name);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
openMultiMap<K extends Key, V>(name: string): AztecAsyncMultiMap<K, V> {
|
|
86
|
+
return new LMDBMultiMap(this, name);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
openSingleton<T>(name: string): AztecAsyncSingleton<T> {
|
|
90
|
+
return new LMDBSingleValue(this, name);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
openArray<T>(_name: string): AztecAsyncArray<T> {
|
|
94
|
+
throw new Error('Not implemented');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
openSet<K extends Key>(_name: string): AztecAsyncSet<K> {
|
|
98
|
+
throw new Error('Not implemented');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
openCounter<K extends Key>(_name: string): AztecAsyncCounter<K> {
|
|
102
|
+
throw new Error('Not implemented');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async transactionAsync<T extends Exclude<any, Promise<any>>>(
|
|
106
|
+
callback: (tx: WriteTransaction) => Promise<T>,
|
|
107
|
+
): Promise<T> {
|
|
108
|
+
// transactionAsync might be called recursively
|
|
109
|
+
// send any writes to the parent tx, but don't close it
|
|
110
|
+
// if the callback throws then the parent tx will rollback automatically
|
|
111
|
+
const currentTx = this.getCurrentWriteTx();
|
|
112
|
+
if (currentTx) {
|
|
113
|
+
return await callback(currentTx);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return this.writerQueue.put(async () => {
|
|
117
|
+
const tx = new WriteTransaction(this);
|
|
118
|
+
try {
|
|
119
|
+
const res = await this.writerCtx.run(tx, callback, tx);
|
|
120
|
+
await tx.commit();
|
|
121
|
+
return res;
|
|
122
|
+
} catch (err) {
|
|
123
|
+
this.log.error(`Failed to commit transaction`, err);
|
|
124
|
+
throw err;
|
|
125
|
+
} finally {
|
|
126
|
+
tx.close();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
clear(): Promise<void> {
|
|
132
|
+
return Promise.resolve();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
fork(): Promise<AztecAsyncKVStore> {
|
|
136
|
+
throw new Error('Not implemented');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async delete(): Promise<void> {
|
|
140
|
+
await this.close();
|
|
141
|
+
await rm(this.dataDir, { recursive: true, force: true });
|
|
142
|
+
this.log.verbose(`Deleted database files at ${this.dataDir}`);
|
|
143
|
+
await this.cleanup?.();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async close() {
|
|
147
|
+
await this.writerQueue.cancel();
|
|
148
|
+
await this.sendMessage(LMDBMessageType.CLOSE, undefined);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public async sendMessage<T extends LMDBMessageType>(
|
|
152
|
+
msgType: T,
|
|
153
|
+
body: LMDBRequestBody[T],
|
|
154
|
+
): Promise<LMDBResponseBody[T]> {
|
|
155
|
+
if (msgType === LMDBMessageType.START_CURSOR) {
|
|
156
|
+
await this.availableCursors.acquire();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let response: LMDBResponseBody[T] | undefined = undefined;
|
|
160
|
+
try {
|
|
161
|
+
({ response } = await this.channel.sendMessage(msgType, body));
|
|
162
|
+
return response;
|
|
163
|
+
} finally {
|
|
164
|
+
if (
|
|
165
|
+
(msgType === LMDBMessageType.START_CURSOR && response === undefined) ||
|
|
166
|
+
msgType === LMDBMessageType.CLOSE_CURSOR ||
|
|
167
|
+
// it's possible for a START_CURSOR command to not return a cursor (e.g. db is empty)
|
|
168
|
+
(msgType === LMDBMessageType.START_CURSOR &&
|
|
169
|
+
typeof (response as LMDBResponseBody[LMDBMessageType.START_CURSOR]).cursor !== 'number')
|
|
170
|
+
) {
|
|
171
|
+
this.availableCursors.release();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public async estimateSize(): Promise<StoreSize> {
|
|
177
|
+
const resp = await this.sendMessage(LMDBMessageType.STATS, undefined);
|
|
178
|
+
return {
|
|
179
|
+
mappingSize: Number(resp.dbMapSizeBytes),
|
|
180
|
+
actualSize: resp.stats.reduce((s, db) => Number(db.totalUsedSize) + s, 0),
|
|
181
|
+
numItems: resp.stats.reduce((s, db) => Number(db.numDataItems) + s, 0),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function execInWriteTx<T>(store: AztecLMDBStoreV2, fn: (tx: WriteTransaction) => Promise<T>): Promise<T> {
|
|
187
|
+
const currentWrite = store.getCurrentWriteTx();
|
|
188
|
+
if (currentWrite) {
|
|
189
|
+
return fn(currentWrite);
|
|
190
|
+
} else {
|
|
191
|
+
return store.transactionAsync(fn);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export async function execInReadTx<T>(
|
|
196
|
+
store: AztecLMDBStoreV2,
|
|
197
|
+
fn: (tx: ReadTransaction) => T | Promise<T>,
|
|
198
|
+
): Promise<T> {
|
|
199
|
+
const currentWrite = store.getCurrentWriteTx();
|
|
200
|
+
if (currentWrite) {
|
|
201
|
+
return await fn(currentWrite);
|
|
202
|
+
} else {
|
|
203
|
+
const tx = store.getReadTx();
|
|
204
|
+
try {
|
|
205
|
+
return await fn(tx);
|
|
206
|
+
} finally {
|
|
207
|
+
tx.close();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { MAXIMUM_KEY, fromBufferKey, toBufferKey } from 'ordered-binary';
|
|
2
|
+
|
|
3
|
+
import { type Key } from '../interfaces/common.js';
|
|
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
|
+
export function keyCmp(a: [Uint8Array, Uint8Array[] | null], b: [Uint8Array, Uint8Array[] | null]): -1 | 0 | 1 {
|
|
124
|
+
return Buffer.compare(a[0], b[0]);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function singleKeyCmp(a: [Uint8Array, Uint8Array[] | null], b: Uint8Array): -1 | 0 | 1 {
|
|
128
|
+
return Buffer.compare(a[0], b);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function minKey(prefix: string) {
|
|
132
|
+
return toBufferKey([prefix]);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function maxKey(prefix: string) {
|
|
136
|
+
return toBufferKey([prefix, MAXIMUM_KEY]);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function serializeKey(prefix: string, key: Key): Buffer {
|
|
140
|
+
return toBufferKey([prefix, key]);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function deserializeKey<K extends Key>(prefix: string, key: Uint8Array): K | false {
|
|
144
|
+
const buf = Buffer.from(key);
|
|
145
|
+
const parsed = fromBufferKey(buf);
|
|
146
|
+
if (!Array.isArray(parsed) || parsed[0] !== prefix) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
return parsed[1] as K;
|
|
150
|
+
}
|