forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lmdb.js
52 lines (41 loc) · 944 Bytes
/
lmdb.js
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
const { open } = require('lmdb')
const { Trie } = require('../dist')
class LMDB {
constructor(path) {
this.path = path
this.database = open({
compression: true,
name: '@ethereumjs/trie',
path,
})
}
async get(key) {
return this.database.get(key)
}
async put(key, val) {
await this.database.put(key, val)
}
async del(key) {
await this.database.remove(key)
}
async batch(opStack) {
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() {
return new LMDB(this.path)
}
}
const trie = new Trie({ db: new LMDB('MY_TRIE_DB_LOCATION') })
async function test() {
await trie.put(Buffer.from('test'), Buffer.from('one'))
const value = await trie.get(Buffer.from('test'))
console.log(value.toString()) // 'one'
}
test()