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

Create iterators in order #75

Merged
merged 2 commits into from
Sep 17, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = []
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, damnit, this would affect reachdown 😄 (currently detects deferred-leveldown by presence of _operations and _iterators)

Copy link
Member Author

@vweevers vweevers Sep 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the interest of moving on, I'll restore this._iterators = [], with a comment saying it's temporary.

Edit: or, adding type (Level/community#82) might do the trick.

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()
})