Skip to content

Commit

Permalink
feat: add _seek support
Browse files Browse the repository at this point in the history
  • Loading branch information
MeirionHughes committed Jun 21, 2019
1 parent 49ff8e3 commit f90c2cc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions leveldown.js
Expand Up @@ -30,6 +30,10 @@ SubIterator.prototype._next = function (cb) {
})
}

SubIterator.prototype._seek = function (key) {
this.iterator.seek(concat(this.prefix, key))
}

SubIterator.prototype._end = function (cb) {
this.iterator.end(cb)
}
Expand Down
35 changes: 35 additions & 0 deletions test/index.js
Expand Up @@ -248,4 +248,39 @@ test('SubDb main function', function (t) {
})
})
})
t.test('can arbitrarily seek', function (t) {
t.plan(7)

var db = levelup(memdown())
var sub = subdb(db, 'sub')

db.once('open', function () {
var it = sub.iterator({ keyAsBuffer: false, valueAsBuffer: false })
sub.batch([
{ type: 'put', key: 'a', value: 'A' },
{ type: 'put', key: 'b', value: 'B' },
{ type: 'put', key: 'c', value: 'C' },
{ type: 'put', key: 'd', value: 'D' },
{ type: 'put', key: 'e', value: 'E' }
], (err) => {
t.error(err, 'no error')
it.seek('c')
it.next((err, key, value) => {
t.error(err, 'no error')
t.same(key, 'c', 'key is as expected')
it.seek('d')
it.next((err, key, value) => {
t.error(err, 'no error')
t.same(key, 'd', 'key is as expected')
it.seek('a')
it.next((err, key, value) => {
t.error(err, 'no error')
t.same(key, 'a', 'key is as expected')
t.end()
})
})
})
})
})
})
})

0 comments on commit f90c2cc

Please sign in to comment.