This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
abstract-leveldown.js
391 lines (292 loc) · 10.4 KB
/
abstract-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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
'use strict'
const supports = require('level-supports')
const isBuffer = require('is-buffer')
const catering = require('catering')
const AbstractIterator = require('./abstract-iterator')
const AbstractChainedBatch = require('./abstract-chained-batch')
const getCallback = require('./lib/common').getCallback
const getOptions = require('./lib/common').getOptions
const hasOwnProperty = Object.prototype.hasOwnProperty
const rangeOptions = ['lt', 'lte', 'gt', 'gte']
function AbstractLevelDOWN (manifest) {
this.status = 'new'
// TODO (next major): make this mandatory
this.supports = supports(manifest, {
status: true
})
}
AbstractLevelDOWN.prototype.open = function (options, callback) {
const oldStatus = this.status
if (typeof options === 'function') callback = options
if (typeof callback !== 'function') {
throw new Error('open() requires a callback argument')
}
if (typeof options !== 'object' || options === null) options = {}
options.createIfMissing = options.createIfMissing !== false
options.errorIfExists = !!options.errorIfExists
this.status = 'opening'
this._open(options, (err) => {
if (err) {
this.status = oldStatus
return callback(err)
}
this.status = 'open'
callback()
})
}
AbstractLevelDOWN.prototype._open = function (options, callback) {
this._nextTick(callback)
}
AbstractLevelDOWN.prototype.close = function (callback) {
const oldStatus = this.status
if (typeof callback !== 'function') {
throw new Error('close() requires a callback argument')
}
this.status = 'closing'
this._close((err) => {
if (err) {
this.status = oldStatus
return callback(err)
}
this.status = 'closed'
callback()
})
}
AbstractLevelDOWN.prototype._close = function (callback) {
this._nextTick(callback)
}
AbstractLevelDOWN.prototype.get = function (key, options, callback) {
if (typeof options === 'function') callback = options
if (typeof callback !== 'function') {
throw new Error('get() requires a callback argument')
}
const err = this._checkKey(key)
if (err) return this._nextTick(callback, err)
key = this._serializeKey(key)
if (typeof options !== 'object' || options === null) options = {}
options.asBuffer = options.asBuffer !== false
this._get(key, options, callback)
}
AbstractLevelDOWN.prototype._get = function (key, options, callback) {
this._nextTick(function () { callback(new Error('NotFound')) })
}
AbstractLevelDOWN.prototype.getMany = function (keys, options, callback) {
callback = getCallback(options, callback)
callback = catering.fromCallback(callback)
options = getOptions(options)
if (maybeError(this, callback)) {
return callback.promise
}
if (!Array.isArray(keys)) {
this._nextTick(callback, new Error('getMany() requires an array argument'))
return callback.promise
}
if (keys.length === 0) {
this._nextTick(callback, null, [])
return callback.promise
}
if (typeof options.asBuffer !== 'boolean') {
options = { ...options, asBuffer: true }
}
const serialized = new Array(keys.length)
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const err = this._checkKey(key)
if (err) {
this._nextTick(callback, err)
return callback.promise
}
serialized[i] = this._serializeKey(key)
}
this._getMany(serialized, options, callback)
return callback.promise
}
AbstractLevelDOWN.prototype._getMany = function (keys, options, callback) {
this._nextTick(callback, null, new Array(keys.length).fill(undefined))
}
AbstractLevelDOWN.prototype.put = function (key, value, options, callback) {
if (typeof options === 'function') callback = options
if (typeof callback !== 'function') {
throw new Error('put() requires a callback argument')
}
const err = this._checkKey(key) || this._checkValue(value)
if (err) return this._nextTick(callback, err)
key = this._serializeKey(key)
value = this._serializeValue(value)
if (typeof options !== 'object' || options === null) options = {}
this._put(key, value, options, callback)
}
AbstractLevelDOWN.prototype._put = function (key, value, options, callback) {
this._nextTick(callback)
}
AbstractLevelDOWN.prototype.del = function (key, options, callback) {
if (typeof options === 'function') callback = options
if (typeof callback !== 'function') {
throw new Error('del() requires a callback argument')
}
const err = this._checkKey(key)
if (err) return this._nextTick(callback, err)
key = this._serializeKey(key)
if (typeof options !== 'object' || options === null) options = {}
this._del(key, options, callback)
}
AbstractLevelDOWN.prototype._del = function (key, options, callback) {
this._nextTick(callback)
}
AbstractLevelDOWN.prototype.batch = function (array, options, callback) {
if (!arguments.length) return this._chainedBatch()
if (typeof options === 'function') callback = options
if (typeof array === 'function') callback = array
if (typeof callback !== 'function') {
throw new Error('batch(array) requires a callback argument')
}
if (!Array.isArray(array)) {
return this._nextTick(callback, new Error('batch(array) requires an array argument'))
}
if (array.length === 0) {
return this._nextTick(callback)
}
if (typeof options !== 'object' || options === null) options = {}
const serialized = new Array(array.length)
for (let i = 0; i < array.length; i++) {
if (typeof array[i] !== 'object' || array[i] === null) {
return this._nextTick(callback, new Error('batch(array) element must be an object and not `null`'))
}
const e = Object.assign({}, array[i])
if (e.type !== 'put' && e.type !== 'del') {
return this._nextTick(callback, new Error("`type` must be 'put' or 'del'"))
}
const err = this._checkKey(e.key)
if (err) return this._nextTick(callback, err)
e.key = this._serializeKey(e.key)
if (e.type === 'put') {
const valueErr = this._checkValue(e.value)
if (valueErr) return this._nextTick(callback, valueErr)
e.value = this._serializeValue(e.value)
}
serialized[i] = e
}
this._batch(serialized, options, callback)
}
AbstractLevelDOWN.prototype._batch = function (array, options, callback) {
this._nextTick(callback)
}
AbstractLevelDOWN.prototype.clear = function (options, callback) {
if (typeof options === 'function') {
callback = options
} else if (typeof callback !== 'function') {
throw new Error('clear() requires a callback argument')
}
options = cleanRangeOptions(this, options)
options.reverse = !!options.reverse
options.limit = 'limit' in options ? options.limit : -1
this._clear(options, callback)
}
AbstractLevelDOWN.prototype._clear = function (options, callback) {
// Avoid setupIteratorOptions, would serialize range options a second time.
options.keys = true
options.values = false
options.keyAsBuffer = true
options.valueAsBuffer = true
const iterator = this._iterator(options)
const emptyOptions = {}
const next = (err) => {
if (err) {
return iterator.end(function () {
callback(err)
})
}
iterator.next((err, key) => {
if (err) return next(err)
if (key === undefined) return iterator.end(callback)
// This could be optimized by using a batch, but the default _clear
// is not meant to be fast. Implementations have more room to optimize
// if they override _clear. Note: using _del bypasses key serialization.
this._del(key, emptyOptions, next)
})
}
next()
}
AbstractLevelDOWN.prototype._setupIteratorOptions = function (options) {
options = cleanRangeOptions(this, options)
options.reverse = !!options.reverse
options.keys = options.keys !== false
options.values = options.values !== false
options.limit = 'limit' in options ? options.limit : -1
options.keyAsBuffer = options.keyAsBuffer !== false
options.valueAsBuffer = options.valueAsBuffer !== false
return options
}
function cleanRangeOptions (db, options) {
const result = {}
for (const k in options) {
if (!hasOwnProperty.call(options, k)) continue
if (k === 'start' || k === 'end') {
throw new Error('Legacy range options ("start" and "end") have been removed')
}
let opt = options[k]
if (isRangeOption(k)) {
// Note that we don't reject nullish and empty options here. While
// those types are invalid as keys, they are valid as range options.
opt = db._serializeKey(opt)
}
result[k] = opt
}
return result
}
function isRangeOption (k) {
return rangeOptions.indexOf(k) !== -1
}
AbstractLevelDOWN.prototype.iterator = function (options) {
if (typeof options !== 'object' || options === null) options = {}
options = this._setupIteratorOptions(options)
return this._iterator(options)
}
AbstractLevelDOWN.prototype._iterator = function (options) {
return new AbstractIterator(this)
}
AbstractLevelDOWN.prototype._chainedBatch = function () {
return new AbstractChainedBatch(this)
}
AbstractLevelDOWN.prototype._serializeKey = function (key) {
return key
}
AbstractLevelDOWN.prototype._serializeValue = function (value) {
return value
}
AbstractLevelDOWN.prototype._checkKey = function (key) {
if (key === null || key === undefined) {
return new Error('key cannot be `null` or `undefined`')
} else if (isBuffer(key) && key.length === 0) { // TODO: replace with typed array check
return new Error('key cannot be an empty Buffer')
} else if (key === '') {
return new Error('key cannot be an empty String')
} else if (Array.isArray(key) && key.length === 0) {
return new Error('key cannot be an empty Array')
}
}
AbstractLevelDOWN.prototype._checkValue = function (value) {
if (value === null || value === undefined) {
return new Error('value cannot be `null` or `undefined`')
}
}
// TODO: docs and tests
AbstractLevelDOWN.prototype.isOperational = function () {
return this.status === 'open' || this._isOperational()
}
// Implementation may accept operations in other states too
AbstractLevelDOWN.prototype._isOperational = function () {
return false
}
// Expose browser-compatible nextTick for dependents
// TODO: rename _nextTick to _queueMicrotask
// TODO: after we drop node 10, also use queueMicrotask in node
AbstractLevelDOWN.prototype._nextTick = require('./next-tick')
module.exports = AbstractLevelDOWN
function maybeError (db, callback) {
if (!db.isOperational()) {
db._nextTick(callback, new Error('Database is not open'))
return true
}
return false
}