-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
levelup.js
333 lines (265 loc) · 7.91 KB
/
levelup.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
'use strict'
const EventEmitter = require('events').EventEmitter
const inherits = require('util').inherits
const DeferredLevelDOWN = require('deferred-leveldown')
const IteratorStream = require('level-iterator-stream')
const Batch = require('./batch')
const errors = require('level-errors')
const supports = require('level-supports')
const catering = require('catering')
const getCallback = require('./common').getCallback
const getOptions = require('./common').getOptions
// TODO: after we drop node 10, also use queueMicrotask() in node
const nextTick = require('./next-tick')
const WriteError = errors.WriteError
const ReadError = errors.ReadError
const NotFoundError = errors.NotFoundError
const OpenError = errors.OpenError
const InitializationError = errors.InitializationError
function LevelUP (db, options, callback) {
if (!(this instanceof LevelUP)) {
return new LevelUP(db, options, callback)
}
let error
EventEmitter.call(this)
this.setMaxListeners(Infinity)
if (typeof options === 'function') {
callback = options
options = {}
}
options = options || {}
if (!db || typeof db !== 'object') {
error = new InitializationError('First argument must be an abstract-leveldown compliant store')
if (typeof callback === 'function') {
return nextTick(callback, error)
}
throw error
}
if (typeof db.status !== 'string') {
throw new Error('.status required, old abstract-leveldown')
}
this.options = getOptions(options)
this._db = db
this.db = null
this.open(callback || ((err) => {
if (err) this.emit('error', err)
}))
// Create manifest based on deferred-leveldown's
this.supports = supports(this.db.supports, {
status: true,
deferredOpen: true,
openCallback: true,
promises: true,
streams: true
})
// Experimental: enrich levelup interface
for (const method of Object.keys(this.supports.additionalMethods)) {
if (this[method] != null) continue
// Don't do this.db[method].bind() because this.db is dynamic.
this[method] = function (...args) {
return this.db[method](...args)
}
}
}
LevelUP.prototype.emit = EventEmitter.prototype.emit
LevelUP.prototype.once = EventEmitter.prototype.once
inherits(LevelUP, EventEmitter)
// TODO: tests
Object.defineProperty(LevelUP.prototype, 'status', {
enumerable: true,
get () {
return this.db.status
}
})
// TODO: tests
LevelUP.prototype.isOperational = function () {
return this.db.status === 'open' || this.db.status === 'opening'
}
LevelUP.prototype.open = function (opts, callback) {
if (typeof opts === 'function') {
callback = opts
opts = null
}
callback = catering.fromCallback(callback)
if (!opts) {
opts = this.options
}
// 1) Don't check db.status until levelup has opened,
// in order for levelup events to be consistent
if (this.db && this.isOpen()) {
nextTick(callback, null, this)
return callback.promise
}
if (this.db && this._isOpening()) {
this.once('open', () => { callback(null, this) })
return callback.promise
}
// 2) Instead let deferred-leveldown handle already-open cases.
// TODO: ideally though, levelup would have its own status
this.db = new DeferredLevelDOWN(this._db)
this.emit('opening')
this.db.open(opts, (err) => {
if (err) {
return callback(new OpenError(err))
}
this.db = this._db
callback(null, this)
this.emit('open')
this.emit('ready')
})
return callback.promise
}
LevelUP.prototype.close = function (callback) {
callback = catering.fromCallback(callback)
if (this.isOpen()) {
this.db.close((err, ...rest) => {
this.emit('closed')
callback(err, ...rest)
})
this.emit('closing')
} else if (this.isClosed()) {
nextTick(callback)
} else if (this.db.status === 'closing') {
this.once('closed', callback)
} else if (this._isOpening()) {
this.once('open', () => {
this.close(callback)
})
}
return callback.promise
}
// TODO: remove in future major
LevelUP.prototype.isOpen = function () {
return this.db.status === 'open'
}
// TODO: remove in future major
LevelUP.prototype._isOpening = function () {
return this.db.status === 'opening'
}
// TODO: remove in future major
LevelUP.prototype.isClosed = function () {
return (/^clos|new/).test(this.db.status)
}
LevelUP.prototype.get = function (key, options, callback) {
callback = getCallback(options, callback)
callback = catering.fromCallback(callback)
if (maybeError(this, callback)) {
return callback.promise
}
options = getOptions(options)
this.db.get(key, options, function (err, value) {
if (err) {
if ((/notfound/i).test(err) || err.notFound) {
err = new NotFoundError('Key not found in database [' + key + ']', err)
} else {
err = new ReadError(err)
}
return callback(err)
}
callback(null, value)
})
return callback.promise
}
LevelUP.prototype.getMany = function (keys, options, callback) {
return this.db.getMany(keys, options, callback)
}
LevelUP.prototype.put = function (key, value, options, callback) {
callback = getCallback(options, callback)
callback = catering.fromCallback(callback)
if (maybeError(this, callback)) {
return callback.promise
}
options = getOptions(options)
this.db.put(key, value, options, (err) => {
if (err) {
return callback(new WriteError(err))
}
this.emit('put', key, value)
callback()
})
return callback.promise
}
LevelUP.prototype.del = function (key, options, callback) {
callback = getCallback(options, callback)
callback = catering.fromCallback(callback)
if (maybeError(this, callback)) {
return callback.promise
}
options = getOptions(options)
this.db.del(key, options, (err) => {
if (err) {
return callback(new WriteError(err))
}
this.emit('del', key)
callback()
})
return callback.promise
}
LevelUP.prototype.batch = function (arr, options, callback) {
if (!arguments.length) {
return new Batch(this)
}
if (typeof arr === 'function') callback = arr
else callback = getCallback(options, callback)
callback = catering.fromCallback(callback)
if (maybeError(this, callback)) {
return callback.promise
}
options = getOptions(options)
this.db.batch(arr, options, (err) => {
if (err) {
return callback(new WriteError(err))
}
this.emit('batch', arr)
callback()
})
return callback.promise
}
LevelUP.prototype.iterator = function (options) {
return this.db.iterator(options)
}
LevelUP.prototype.clear = function (options, callback) {
callback = getCallback(options, callback)
options = getOptions(options)
callback = catering.fromCallback(callback)
if (maybeError(this, callback)) {
return callback.promise
}
this.db.clear(options, (err) => {
if (err) {
return callback(new WriteError(err))
}
this.emit('clear', options)
callback()
})
return callback.promise
}
LevelUP.prototype.readStream =
LevelUP.prototype.createReadStream = function (options) {
options = Object.assign({ keys: true, values: true }, options)
if (typeof options.limit !== 'number') { options.limit = -1 }
return new IteratorStream(this.db.iterator(options), options)
}
LevelUP.prototype.keyStream =
LevelUP.prototype.createKeyStream = function (options) {
return this.createReadStream(Object.assign({}, options, { keys: true, values: false }))
}
LevelUP.prototype.valueStream =
LevelUP.prototype.createValueStream = function (options) {
return this.createReadStream(Object.assign({}, options, { keys: false, values: true }))
}
LevelUP.prototype.toString = function () {
return 'LevelUP'
}
LevelUP.prototype.type = 'levelup'
// Expose nextTick for API parity with abstract-leveldown
LevelUP.prototype._nextTick = nextTick
function maybeError (db, callback) {
if (!db.isOperational()) {
nextTick(callback, new ReadError('Database is not open'))
return true
}
return false
}
LevelUP.errors = errors
module.exports = LevelUP