forked from clvrk/node-leveldb-mcpe
/
index.js
78 lines (61 loc) · 1.86 KB
/
index.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const { Database } = require('bindings')('node_leveldb_mcpe_native.node')
const util = require('util')
const { AbstractLevelDOWN, AbstractIterator } = require('abstract-leveldown')
class MinecraftAbstractIterator extends AbstractIterator {
constructor (store) {
super()
this._store = store
this._iterator_store = this._store.iteratorNew()
}
_next (callback) {
const iterator = this._iterator_store
if (!this._store.iteratorValid(iterator)) {
callback(null, undefined, undefined)
return
}
const key = Buffer.from(this._store.iteratorKey(iterator))
const value = Buffer.from(this._store.iteratorValue(iterator))
this._store.iteratorNext(iterator)
callback(null, Buffer.from(key), Buffer.from(value))
}
_end (callback) {
this._store.iteratorDestroy(this._iterator_store)
callback()
}
}
class MinecraftLevelDOWN {
constructor (location) {
AbstractLevelDOWN.call(this, location)
}
_open (options, callback) {
this._store = new Database(this.location)
process.nextTick(function () { callback(null, this) }.bind(this))
}
_put (key, value, options, callback) {
this._store.put(key, value)
process.nextTick(callback)
}
_get (key, options, callback) {
const value = this._store.get(key)
if (value === undefined) {
// 'NotFound' error, consistent with LevelDOWN API
return process.nextTick(function () { callback(new Error('NotFound')) })
}
process.nextTick(function () {
callback(null, value)
})
}
_del (key, options, callback) {
this._store.delete(key)
process.nextTick(callback)
}
_close (callback) {
this._store.close()
process.nextTick(callback)
}
_iterator (options) {
return new MinecraftAbstractIterator(this._store)
}
}
util.inherits(MinecraftLevelDOWN, AbstractLevelDOWN)
module.exports = MinecraftLevelDOWN