forked from Level/rocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleveldown.js
140 lines (96 loc) · 3.92 KB
/
leveldown.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const util = require('util')
, AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
, binding = require('bindings')('leveldown').leveldown
, ChainedBatch = require('./chained-batch')
, Iterator = require('./iterator')
, fs = require('fs')
function LevelDOWN (location) {
if (!(this instanceof LevelDOWN))
return new LevelDOWN(location)
AbstractLevelDOWN.call(this, location)
this.binding = binding(location)
}
util.inherits(LevelDOWN, AbstractLevelDOWN)
LevelDOWN.prototype._open = function (options, callback) {
this.binding.open(options, callback)
}
LevelDOWN.prototype._close = function (callback) {
this.binding.close(callback)
}
LevelDOWN.prototype._put = function (key, value, options, callback) {
this.binding.put(key, value, options, callback)
}
LevelDOWN.prototype._get = function (key, options, callback) {
this.binding.get(key, options, callback)
}
LevelDOWN.prototype._del = function (key, options, callback) {
this.binding.del(key, options, callback)
}
LevelDOWN.prototype._chainedBatch = function () {
return new ChainedBatch(this)
}
LevelDOWN.prototype._batch = function (operations, options, callback) {
return this.binding.batch(operations, options, callback)
}
LevelDOWN.prototype.approximateSize = function (start, end, callback) {
if (start == null ||
end == null ||
typeof start === 'function' ||
typeof end === 'function') {
throw new Error('approximateSize() requires valid `start`, `end` and `callback` arguments')
}
if (typeof callback !== 'function') {
throw new Error('approximateSize() requires a callback argument')
}
start = this._serializeKey(start)
end = this._serializeKey(end)
this.binding.approximateSize(start, end, callback)
}
LevelDOWN.prototype.compactRange = function (start, end, callback) {
this.binding.compactRange(start, end, callback)
}
LevelDOWN.prototype.getProperty = function (property) {
if (typeof property != 'string')
throw new Error('getProperty() requires a valid `property` argument')
return this.binding.getProperty(property)
}
LevelDOWN.prototype._iterator = function (options) {
return new Iterator(this, options)
}
LevelDOWN.destroy = function (location, callback) {
if (arguments.length < 2)
throw new Error('destroy() requires `location` and `callback` arguments')
if (typeof location != 'string')
throw new Error('destroy() requires a location string argument')
if (typeof callback != 'function')
throw new Error('destroy() requires a callback function argument')
binding.destroy(location, function (err) {
if (err) return callback(err)
// On Windows, RocksDB silently fails to remove the directory because its
// Logger, which is instantiated on destroy(), has an open file handle on a
// LOG file. Destroy() removes this file but Windows won't actually delete
// it until the handle is released. This happens when destroy() goes out of
// scope, which disposes the Logger. So back in JS-land, we can again
// attempt to remove the directory. This is merely a workaround because
// arguably RocksDB should not instantiate a Logger or open a file at all.
fs.rmdir(location, function (err) {
if (err) {
// Ignore this error in case there are non-RocksDB files left.
if (err.code === 'ENOTEMPTY') return callback()
if (err.code === 'ENOENT') return callback()
return callback(err)
}
callback()
})
})
}
LevelDOWN.repair = function (location, callback) {
if (arguments.length < 2)
throw new Error('repair() requires `location` and `callback` arguments')
if (typeof location != 'string')
throw new Error('repair() requires a location string argument')
if (typeof callback != 'function')
throw new Error('repair() requires a callback function argument')
binding.repair(location, callback)
}
module.exports = LevelDOWN