-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmedeaiterator.js
51 lines (35 loc) · 1.15 KB
/
medeaiterator.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
var AbstractIterator = require('abstract-leveldown').AbstractIterator
, MedeaIterator = function (medea, options) {
AbstractIterator.call(this, medea.db)
this.keyAsBuffer = options.keyAsBuffer !== false
this.valueAsBuffer = options.valueAsBuffer !== false
this.keys = medea.keys.range(options)
this.idx = 0
this.snapshot = medea.db.createSnapshot()
}
, setImmediate = global.setImmediate || process.nextTick
require('util').inherits(MedeaIterator, AbstractIterator)
MedeaIterator.prototype._next = function (callback) {
if (this.idx === this.keys.length)
return setImmediate(callback)
var self = this
, key = this.keys[this.idx]
this.idx++
this.db.get(key, this.snapshot, function (err, value) {
if (!err && value === undefined)
return self._next(callback)
if (err)
return callback(err)
if (!self.keyAsBuffer)
key = key.toString()
if (!self.valueAsBuffer)
value = value.toString()
callback(null, key, value)
})
}
MedeaIterator.prototype._end = function (callback) {
this.keys = undefined
this.snapshot.close()
callback()
}
module.exports = MedeaIterator