-
Notifications
You must be signed in to change notification settings - Fork 722
/
map.ts
40 lines (31 loc) · 938 Bytes
/
map.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
import { DB, BatchDBOp } from '@ethereumjs/util'
import { bytesToHex } from 'ethereum-cryptography/utils'
export class MapDB implements DB {
_database: Map<string, Uint8Array>
constructor(database?: Map<string, Uint8Array>) {
this._database = database ?? new Map()
}
async get(key: Uint8Array): Promise<Uint8Array | null> {
const result = this._database.get(bytesToHex(key))
return result ?? null
}
async put(key: Uint8Array, val: Uint8Array): Promise<void> {
this._database.set(bytesToHex(key), val)
}
async del(key: Uint8Array): Promise<void> {
this._database.delete(bytesToHex(key))
}
async batch(opStack: BatchDBOp[]): Promise<void> {
for (const op of opStack) {
if (op.type === 'del') {
await this.del(op.key)
}
if (op.type === 'put') {
await this.put(op.key, op.value)
}
}
}
copy(): DB {
return new MapDB(this._database)
}
}