Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add iterator support #6

Merged
merged 1 commit into from Mar 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 39 additions & 3 deletions deferred-leveldown.js
@@ -1,10 +1,12 @@
var util = require('util')
, AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
, AbstractIterator = require('abstract-leveldown').AbstractIterator

function DeferredLevelDOWN (location) {
AbstractLevelDOWN.call(this, typeof location == 'string' ? location : '') // optional location, who cares?
this._db = undefined
this._operations = []
this._iterators = []
}

util.inherits(DeferredLevelDOWN, AbstractLevelDOWN)
Expand All @@ -15,6 +17,9 @@ DeferredLevelDOWN.prototype.setDb = function (db) {
this._operations.forEach(function (op) {
db[op.method].apply(db, op.args)
})
this._iterators.forEach(function (it) {
it.setDb(db)
})
}

DeferredLevelDOWN.prototype._open = function (options, callback) {
Expand All @@ -39,9 +44,40 @@ DeferredLevelDOWN.prototype._isBuffer = function (obj) {
return Buffer.isBuffer(obj)
}

// don't need to implement this as LevelUP's ReadStream checks for 'ready' state
DeferredLevelDOWN.prototype._iterator = function () {
throw new TypeError('not implemented')
DeferredLevelDOWN.prototype._iterator = function (options) {
var it = new Iterator(options)
this._iterators.push(it)
return it
}

function Iterator (options) {
AbstractIterator.call(this, options)

this._options = options
this._iterator = null
this._operations = []
}

util.inherits(Iterator, AbstractIterator)

Iterator.prototype.setDb = function (db) {
var it = this._iterator = db.iterator(this._options)
this._operations.forEach(function (op) {
it[op.method].apply(it, op.args)
})
}

Iterator.prototype._operation = function (method, args) {
if (this._iterator)
return this._iterator[method].apply(this._iterator, args)
this._operations.push({ method: method, args: args })
}

'next end'.split(' ').forEach(function (m) {
Iterator.prototype['_' + m] = function () {
this._operation(m, arguments)
}
})

module.exports = DeferredLevelDOWN

32 changes: 32 additions & 0 deletions test.js
Expand Up @@ -99,3 +99,35 @@ test('many operations', function (t) {

t.end()
})

test('iterators', function (t) {
var ld = new DeferredLevelDOWN('loc')
var it = ld.iterator()
var nextFirst = false

it.next(function (err, key, value) {
nextFirst = true
t.error(err)
t.equal(key, 'key')
t.equal(value, 'value')
})

it.end(function (err) {
t.error(err)
t.ok(nextFirst)
t.end()
})

ld.setDb({ iterator: function (options) {
return {
next : function (cb) {
cb(null, 'key', 'value')
}
, end : function (cb) {
process.nextTick(cb)
}
}
}})

t.end()
})