forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.ts
47 lines (38 loc) · 1.42 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
import { MemoryLevel } from 'memory-level'
import type { BatchDBOp, DB } from '@ethereumjs/trie'
import type { AbstractLevel } from 'abstract-level'
const ENCODING_OPTS = { keyEncoding: 'buffer', valueEncoding: 'buffer' }
export class LevelDB implements DB {
readonly _leveldb: AbstractLevel<string | Buffer | Uint8Array, string | Buffer, string | Buffer>
constructor(
leveldb?: AbstractLevel<string | Buffer | Uint8Array, string | Buffer, string | Buffer> | null
) {
this._leveldb = leveldb ?? new MemoryLevel(ENCODING_OPTS)
}
async get(key: Buffer): Promise<Buffer | null> {
let value: Buffer | null = null
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: Buffer, val: Buffer): Promise<void> {
await this._leveldb.put(key, val, ENCODING_OPTS)
}
async del(key: Buffer): Promise<void> {
await this._leveldb.del(key, ENCODING_OPTS)
}
async batch(opStack: BatchDBOp[]): Promise<void> {
await this._leveldb.batch(opStack, ENCODING_OPTS)
}
copy(): DB {
return new LevelDB(this._leveldb)
}
}