Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Create iterators in order (#75)
Browse files Browse the repository at this point in the history
* Create iterators in order

* Add type property for reachdown

Because now that db._iterators is gone, reachdown can no longer
feature-detect deferred-leveldown.
  • Loading branch information
vweevers authored Sep 17, 2019
1 parent 2e45a79 commit 1ba5307
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
16 changes: 9 additions & 7 deletions deferred-leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@ function DeferredLevelDOWN (db) {
AbstractLevelDOWN.call(this, '')
this._db = db
this._operations = []
this._iterators = []
closed(this)
}

inherits(DeferredLevelDOWN, AbstractLevelDOWN)

DeferredLevelDOWN.prototype.type = 'deferred-leveldown'

DeferredLevelDOWN.prototype._open = function (options, callback) {
var self = this

this._db.open(options, function (err) {
if (err) return callback(err)

self._operations.forEach(function (op) {
self._db[op.method].apply(self._db, op.args)
if (op.iterator) {
op.iterator.setDb(self._db)
} else {
self._db[op.method].apply(self._db, op.args)
}
})
self._operations = []
self._iterators.forEach(function (it) {
it.setDb(self._db)
})
self._iterators = []

open(self)
callback()
})
Expand Down Expand Up @@ -73,7 +75,7 @@ function closed (self) {
})
self._iterator = function (options) {
var it = new DeferredIterator(options)
this._iterators.push(it)
this._operations.push({ iterator: it })
return it
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"hallmark": "^2.0.0",
"level-community": "^3.0.0",
"nyc": "^14.0.0",
"reachdown": "^1.0.0",
"standard": "^14.0.0",
"tape": "^4.10.0"
},
Expand Down
53 changes: 53 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var test = require('tape')
var reachdown = require('reachdown')
var DeferredLevelDOWN = require('./')
var noop = function () {}

test('deferred open gets correct options', function (t) {
var OPTIONS = { foo: 'BAR' }
Expand Down Expand Up @@ -482,3 +484,54 @@ test('iterator - non deferred operation', function (t) {
})
})
})

test('iterator - is created in order', function (t) {
t.plan(4)

function db () {
return {
order: [],
iterator: function (options) {
this.order.push('iterator created')
return {}
},
put: function (key, value, options, callback) {
this.order.push('put')
},
open: function (options, callback) {
process.nextTick(callback)
}
}
}

var ld1 = new DeferredLevelDOWN(db())
var ld2 = new DeferredLevelDOWN(db())

ld1.iterator()
ld1.put('key', 'value', noop)

ld2.put('key', 'value', noop)
ld2.iterator()

ld1.open(function (err) {
t.error(err, 'no error')
t.same(ld1._db.order, ['iterator created', 'put'])
})

ld2.open(function (err) {
t.error(err, 'no error')
t.same(ld2._db.order, ['put', 'iterator created'])
})
})

test('reachdown supports deferred-leveldown', function (t) {
// Define just enough methods for reachdown to see this as a real db
var db = { open: noop, _batch: noop, _iterator: noop }
var ld = new DeferredLevelDOWN(db)

t.is(ld.type, 'deferred-leveldown')
t.is(reachdown(ld, 'deferred-leveldown'), ld)
t.is(reachdown(ld), db)

t.end()
})

0 comments on commit 1ba5307

Please sign in to comment.