From 54a6c63c05da063f1f231f52fe4a85e54089b392 Mon Sep 17 00:00:00 2001 From: Meirion Hughes Date: Fri, 21 Jun 2019 22:33:03 +0100 Subject: [PATCH] feat: add _seek support --- leveldown.js | 4 ++++ test/index.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/leveldown.js b/leveldown.js index 2aa7335..c5caed2 100644 --- a/leveldown.js +++ b/leveldown.js @@ -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) } diff --git a/test/index.js b/test/index.js index 5ff20be..255d5ab 100644 --- a/test/index.js +++ b/test/index.js @@ -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() + }) + }) + }) + }) + }) + }) })