Skip to content

Commit

Permalink
seek resets state
Browse files Browse the repository at this point in the history
  • Loading branch information
Yichao 'Peak' Ji committed Nov 28, 2016
1 parent af00065 commit c375486
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ function Iterator (db, options) {

util.inherits(Iterator, AbstractIterator)

Iterator.prototype.seek = function (key) {
Iterator.prototype.seek = function (target) {
if (this._ended)
throw new Error('cannot call seek() after end()')
if (this._nexting)
throw new Error('cannot call seek() before next() has completed')

this.cache = null
this.binding.seek(key)
this.binding.seek(target)
this.finished = false
}

Iterator.prototype._next = function (callback) {
Expand Down
15 changes: 15 additions & 0 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ make('reverse seek from invalid range', function (db, t, done) {
ite.end(done)
})
})

make('iterator seek resets state', function (db, t, done) {
var ite = db.iterator()
ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
t.equal(key.toString(), 'one', 'key matches')
t.ok(ite.cache, 'has cached items')
t.equal(ite.finished, true, 'finished')
ite.seek('two')
t.notOk(ite.cache, 'cache is removed')
t.equal(ite.finished, false, 'resets finished state')
ite.end(done)
})
})

make('iterator seek respects range', function (db, t, done) {
db.batch(pairs(10), function (err) {
t.error(err, 'no error from batch()')
Expand Down

0 comments on commit c375486

Please sign in to comment.