forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlmdb.ts
49 lines (39 loc) · 996 Bytes
/
lmdb.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Database, open } from 'lmdb'
import type { BatchDBOp, DB } from '@ethereumjs/util'
export class LMDB implements DB {
readonly _path: string
readonly _database: Database
constructor(path: string) {
this._path = path
this._database = open({
compression: true,
name: '@ethereumjs/mpt',
path,
})
}
async get(key: Uint8Array): Promise<Uint8Array | undefined> {
return this._database.get(key)
}
async put(key: Uint8Array, val: Uint8Array): Promise<void> {
await this._database.put(key, val)
}
async del(key: Uint8Array): Promise<void> {
await this._database.remove(key)
}
async batch(opStack: BatchDBOp[]): Promise<void> {
for (const op of opStack) {
if (op.type === 'put') {
await this.put(op.key, op.value)
}
if (op.type === 'del') {
await this.del(op.key)
}
}
}
shallowCopy(): DB {
return new LMDB(this._path)
}
open() {
return Promise.resolve()
}
}