/
levelup.js
360 lines (283 loc) · 8.22 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
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
var EventEmitter = require('events').EventEmitter
var inherits = require('util').inherits
var extend = require('xtend')
var DeferredLevelDOWN = require('deferred-leveldown')
var IteratorStream = require('level-iterator-stream')
var Batch = require('./batch')
var errors = require('level-errors')
var supports = require('level-supports')
var assert = require('assert')
var promisify = require('./promisify')
var getCallback = require('./common').getCallback
var getOptions = require('./common').getOptions
var WriteError = errors.WriteError
var ReadError = errors.ReadError
var NotFoundError = errors.NotFoundError
var OpenError = errors.OpenError
var InitializationError = errors.InitializationError
// Possible AbstractLevelDOWN#status values:
// - 'new' - newly created, not opened or closed
// - 'opening' - waiting for the database to be opened, post open()
// - 'open' - successfully opened the database, available for use
// - 'closing' - waiting for the database to be closed, post close()
// - 'closed' - database has been successfully closed, should not be
// used except for another open() operation
function LevelUP (db, options, callback) {
if (!(this instanceof LevelUP)) {
return new LevelUP(db, options, callback)
}
var error
var self = this
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 process.nextTick(callback, error)
}
throw error
}
assert.strictEqual(typeof db.status, 'string', '.status required, old abstract-leveldown')
this.options = getOptions(options)
this._db = db
this.db = new DeferredLevelDOWN(db)
this.open(callback || function (err) {
if (err) self.emit('error', err)
})
// Create manifest based on deferred-leveldown's
this.supports = supports(this.db.supports, {
status: false,
deferredOpen: true,
openCallback: true,
promises: true,
streams: true
})
// Experimental: enrich levelup interface
Object.keys(this.supports.additionalMethods).forEach(function (method) {
if (this[method] != null) return
// Don't do this.db[method].bind() because this.db is dynamic.
this[method] = function () {
return this.db[method].apply(this.db, arguments)
}
}, this)
}
LevelUP.prototype.emit = EventEmitter.prototype.emit
LevelUP.prototype.once = EventEmitter.prototype.once
inherits(LevelUP, EventEmitter)
LevelUP.prototype.open = function (opts, callback) {
var self = this
var promise
if (typeof opts === 'function') {
callback = opts
opts = null
}
if (!callback) {
callback = promisify()
promise = callback.promise
}
if (!opts) {
opts = this.options
}
if (this.isOpen()) {
process.nextTick(callback, null, self)
return promise
}
if (this._isOpening()) {
this.once('open', function () { callback(null, self) })
return promise
}
this.emit('opening')
this.db.open(opts, function (err) {
if (err) {
return callback(new OpenError(err))
}
self.db = self._db
callback(null, self)
self.emit('open')
self.emit('ready')
})
return promise
}
LevelUP.prototype.close = function (callback) {
var self = this
var promise
if (!callback) {
callback = promisify()
promise = callback.promise
}
if (this.isOpen()) {
this.db.close(function () {
self.emit('closed')
callback.apply(null, arguments)
})
this.emit('closing')
this.db = new DeferredLevelDOWN(this._db)
} else if (this.isClosed()) {
process.nextTick(callback)
} else if (this.db.status === 'closing') {
this.once('closed', callback)
} else if (this._isOpening()) {
this.once('open', function () {
self.close(callback)
})
}
return promise
}
LevelUP.prototype.isOpen = function () {
return this.db.status === 'open'
}
LevelUP.prototype._isOpening = function () {
return this.db.status === 'opening'
}
LevelUP.prototype.isClosed = function () {
return (/^clos|new/).test(this.db.status)
}
LevelUP.prototype.get = function (key, options, callback) {
if (key === null || key === undefined) {
throw new ReadError('get() requires a key argument')
}
var promise
callback = getCallback(options, callback)
if (!callback) {
callback = promisify()
promise = callback.promise
}
if (maybeError(this, callback)) { return 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 promise
}
LevelUP.prototype.put = function (key, value, options, callback) {
if (key === null || key === undefined) {
throw new WriteError('put() requires a key argument')
}
var self = this
var promise
callback = getCallback(options, callback)
if (!callback) {
callback = promisify()
promise = callback.promise
}
if (maybeError(this, callback)) { return promise }
options = getOptions(options)
this.db.put(key, value, options, function (err) {
if (err) {
return callback(new WriteError(err))
}
self.emit('put', key, value)
callback()
})
return promise
}
LevelUP.prototype.del = function (key, options, callback) {
if (key === null || key === undefined) {
throw new WriteError('del() requires a key argument')
}
var self = this
var promise
callback = getCallback(options, callback)
if (!callback) {
callback = promisify()
promise = callback.promise
}
if (maybeError(this, callback)) { return promise }
options = getOptions(options)
this.db.del(key, options, function (err) {
if (err) {
return callback(new WriteError(err))
}
self.emit('del', key)
callback()
})
return promise
}
LevelUP.prototype.batch = function (arr, options, callback) {
if (!arguments.length) {
return new Batch(this)
}
if (!Array.isArray(arr)) {
throw new WriteError('batch() requires an array argument')
}
var self = this
var promise
callback = getCallback(options, callback)
if (!callback) {
callback = promisify()
promise = callback.promise
}
if (maybeError(this, callback)) { return promise }
options = getOptions(options)
this.db.batch(arr, options, function (err) {
if (err) {
return callback(new WriteError(err))
}
self.emit('batch', arr)
callback()
})
return promise
}
LevelUP.prototype.iterator = function (options) {
return this.db.iterator(options)
}
LevelUP.prototype.clear = function (options, callback) {
var self = this
var promise
callback = getCallback(options, callback)
options = getOptions(options)
if (!callback) {
callback = promisify()
promise = callback.promise
}
if (maybeError(this, callback)) {
return promise
}
this.db.clear(options, function (err) {
if (err) {
return callback(new WriteError(err))
}
self.emit('clear', options)
callback()
})
return promise
}
LevelUP.prototype.readStream =
LevelUP.prototype.createReadStream = function (options) {
options = extend({ 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(extend(options, { keys: true, values: false }))
}
LevelUP.prototype.valueStream =
LevelUP.prototype.createValueStream = function (options) {
return this.createReadStream(extend(options, { keys: false, values: true }))
}
LevelUP.prototype.toString = function () {
return 'LevelUP'
}
LevelUP.prototype.type = 'levelup'
function maybeError (db, callback) {
if (!db._isOpening() && !db.isOpen()) {
process.nextTick(callback, new ReadError('Database is not open'))
return true
}
}
LevelUP.errors = errors
module.exports = LevelUP.default = LevelUP