This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathleveldown.js
258 lines (204 loc) · 7.46 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
'use strict'
const inherits = require('inherits')
const abstract = require('abstract-leveldown')
const wrap = require('level-option-wrap')
const reachdown = require('reachdown')
const matchdown = require('./matchdown')
const rangeOptions = ['gt', 'gte', 'lt', 'lte']
const defaultClear = abstract.AbstractLevelDOWN.prototype._clear
const hasOwnProperty = Object.prototype.hasOwnProperty
const nextTick = abstract.AbstractLevelDOWN.prototype._nextTick
const kIterator = Symbol('iterator')
const kPrefix = Symbol('prefix')
const kBeforeOpen = Symbol('beforeOpen')
function concat (prefix, key, force) {
if (typeof key === 'string' && (force || key.length)) return prefix + key
if (Buffer.isBuffer(key) && (force || key.length)) {
return Buffer.concat([Buffer.from(prefix), key])
}
return key
}
function SubIterator (db, ite, prefix) {
this[kIterator] = ite
this[kPrefix] = prefix
abstract.AbstractIterator.call(this, db)
}
inherits(SubIterator, abstract.AbstractIterator)
SubIterator.prototype._next = function (cb) {
if (maybeError(this.db.leveldown, cb)) return
this[kIterator].next((err, key, value) => {
if (err) return cb(err)
if (key) key = key.slice(this[kPrefix].length)
cb(err, key, value)
})
}
SubIterator.prototype._seek = function (key) {
this[kIterator].seek(concat(this[kPrefix], key))
}
SubIterator.prototype._end = function (cb) {
if (maybeError(this.db.leveldown, cb)) return
this[kIterator].end(cb)
}
function SubDown (db, prefix, opts) {
if (!(this instanceof SubDown)) return new SubDown(db, prefix, opts)
if (typeof opts === 'string') opts = { separator: opts }
if (!opts) opts = {}
let separator = opts.separator
if (!prefix) prefix = ''
if (!separator) separator = '!'
if (prefix[0] === separator) prefix = prefix.slice(1)
if (prefix[prefix.length - 1] === separator) prefix = prefix.slice(0, -1)
const code = separator.charCodeAt(0) + 1
const ceiling = String.fromCharCode(code)
Buffer.from(prefix).forEach(function (byte) {
if (byte <= code) {
throw new RangeError('Prefix must sort after ' + code)
}
})
this.db = db
this.prefix = separator + prefix + separator
this[kBeforeOpen] = opts.open
let manifest = db.supports || {}
// The parent db must open itself or be (re)opened by the user because a
// sublevel can't (shouldn't) initiate state changes on the rest of the db.
if (!manifest.deferredOpen && !reachdown.is(db, 'levelup')) {
throw new Error('Parent database must support deferredOpen')
}
const subdb = reachdown(db, 'subleveldown')
if (subdb) {
// Old subleveldown doesn't know its prefix and leveldown until opened
if (!subdb.prefix || !subdb.leveldown) {
throw new Error('Incompatible with subleveldown < 5.0.0')
}
this.prefix = subdb.prefix + this.prefix
this.leveldown = subdb.leveldown
} else {
this.leveldown = reachdown(db, matchdown, false)
}
if (reachdown.is(this.leveldown, 'deferred-leveldown')) {
// Old deferred-leveldown doesn't expose its underlying db until opened
throw new Error('Incompatible with deferred-leveldown < 2.0.0')
} else if (!this.leveldown.status) {
// Old abstract-leveldown doesn't have a status property
throw new Error('Incompatible with abstract-leveldown < 2.4.0')
}
this._wrap = {
gt: (x) => {
return concat(this.prefix, x || '', true)
},
lt: (x) => {
if (!x || isEmptyBuffer(x)) {
return this.prefix.slice(0, -1) + ceiling
} else {
return concat(this.prefix, x)
}
}
}
manifest = {
// Inherit manifest from parent db
...manifest,
// We support this on the levelup interface, but not on the
// abstract-leveldown interface.
deferredOpen: false,
// Disable unsupported features
keyIterator: false,
valueIterator: false,
iteratorNextv: false,
iteratorAll: false,
// Unset additional methods (like approximateSize) which we can't support
// here and should typically be called on the underlying store instead.
additionalMethods: {}
}
abstract.AbstractLevelDOWN.call(this, manifest)
}
inherits(SubDown, abstract.AbstractLevelDOWN)
SubDown.prototype.type = 'subleveldown'
// TODO: remove _open() once abstract-leveldown supports deferredOpen,
// because that means we can always do operations on this.leveldown.
SubDown.prototype._open = function (opts, cb) {
// TODO: start using db.status (added to levelup recently) in a next major.
const m = typeof this.db.isOpening === 'function' ? 'isOpening' : '_isOpening'
const onopen = () => {
// TODO: start using db.status (added to levelup recently) in a next major.
if (!this.db.isOpen()) return cb(new Error('Parent database is not open'))
if (this.leveldown.status !== 'open') return cb(new Error('Inner database is not open'))
// TODO: add hooks to abstract-leveldown
if (this[kBeforeOpen]) return this[kBeforeOpen](cb)
cb()
}
if (this.db[m]()) {
this.db.once('open', onopen)
} else {
this._nextTick(onopen)
}
}
SubDown.prototype._serializeKey = function (key) {
return Buffer.isBuffer(key) ? key : String(key)
}
SubDown.prototype._put = function (key, value, opts, cb) {
if (maybeError(this.leveldown, cb)) return
this.leveldown.put(concat(this.prefix, key), value, opts, cb)
}
SubDown.prototype._get = function (key, opts, cb) {
if (maybeError(this.leveldown, cb)) return
this.leveldown.get(concat(this.prefix, key), opts, cb)
}
SubDown.prototype._getMany = function (keys, opts, cb) {
// maybeError is not necessary here, abstract-leveldown does that
keys = keys.map(key => concat(this.prefix, key))
this.leveldown.getMany(keys, opts, cb)
}
SubDown.prototype._del = function (key, opts, cb) {
if (maybeError(this.leveldown, cb)) return
this.leveldown.del(concat(this.prefix, key), opts, cb)
}
SubDown.prototype._batch = function (operations, opts, cb) {
if (maybeError(this.leveldown, cb)) return
// No need to make a copy of the array, abstract-leveldown does that
for (let i = 0; i < operations.length; i++) {
operations[i].key = concat(this.prefix, operations[i].key)
}
this.leveldown.batch(operations, opts, cb)
}
SubDown.prototype._clear = function (opts, cb) {
if (maybeError(this.leveldown, cb)) return
if (typeof this.leveldown.clear === 'function') {
// Prefer optimized implementation of clear()
opts = addRestOptions(wrap(opts, this._wrap), opts)
this.leveldown.clear(opts, cb)
} else {
// Fall back to iterator-based implementation
defaultClear.call(this, opts, cb)
}
}
function addRestOptions (target, opts) {
for (const k in opts) {
if (hasOwnProperty.call(opts, k) && !isRangeOption(k)) {
target[k] = opts[k]
}
}
return target
}
function isRangeOption (k) {
return rangeOptions.indexOf(k) !== -1
}
function isEmptyBuffer (key) {
return Buffer.isBuffer(key) && key.length === 0
}
// Before any operation, check if the inner db is open. Needed
// because we don't follow open state of the parent db atm.
// TODO: move to abstract-leveldown
function maybeError (leveldown, callback) {
if (leveldown.status !== 'open') {
// Same error message as levelup
// TODO: use require('level-errors').ReadError
;(leveldown._nextTick || nextTick)(callback, new Error('Database is not open'))
return true
}
return false
}
SubDown.prototype._iterator = function (opts) {
const xopts = addRestOptions(wrap(opts, this._wrap), opts)
return new SubIterator(this, this.leveldown.iterator(xopts), this.prefix)
}
module.exports = SubDown