forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.ts
55 lines (46 loc) · 1.49 KB
/
level.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
50
51
52
53
54
55
import { MemoryLevel } from 'memory-level'
import type { BatchDBOp, DB } from '@ethereumjs/util'
import type { AbstractLevel } from 'abstract-level'
const ENCODING_OPTS = { keyEncoding: 'view', valueEncoding: 'view' }
export class LevelDB implements DB {
readonly _leveldb: AbstractLevel<
string | Uint8Array | Uint8Array,
string | Uint8Array,
string | Uint8Array
>
constructor(
leveldb?: AbstractLevel<
string | Uint8Array | Uint8Array,
string | Uint8Array,
string | Uint8Array
> | null
) {
this._leveldb = leveldb ?? new MemoryLevel(ENCODING_OPTS)
}
async get(key: Uint8Array): Promise<Uint8Array | undefined> {
let value
try {
value = await this._leveldb.get(key, ENCODING_OPTS)
} catch (error: any) {
// https://github.com/Level/abstract-level/blob/915ad1317694d0ce8c580b5ab85d81e1e78a3137/abstract-level.js#L309
// This should be `true` if the error came from LevelDB
// so we can check for `NOT true` to identify any non-404 errors
if (error.notFound !== true) {
throw error
}
}
return value
}
async put(key: Uint8Array, val: Uint8Array): Promise<void> {
await this._leveldb.put(key, val, ENCODING_OPTS)
}
async del(key: Uint8Array): Promise<void> {
await this._leveldb.del(key, ENCODING_OPTS)
}
async batch(opStack: BatchDBOp[]): Promise<void> {
await this._leveldb.batch(opStack, ENCODING_OPTS)
}
shallowCopy(): DB {
return new LevelDB(this._leveldb)
}
}