Skip to content

Commit

Permalink
Merge pull request #329 from peakji/fast-seek
Browse files Browse the repository at this point in the history
250x faster iterator seek
  • Loading branch information
ralphtheninja committed Dec 19, 2016
2 parents 2936971 + 9d44401 commit 959b78f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Iterator::Iterator (
count = 0;
target = NULL;
seeking = false;
landed = false;
nexting = false;
ended = false;
endWorker = NULL;
Expand Down Expand Up @@ -215,6 +216,11 @@ bool Iterator::IteratorNext (std::vector<std::pair<std::string, std::string> >&
result.push_back(std::make_pair(key, value));
size = size + key.size() + value.size();

if (!landed) {
landed = true;
return true;
}

if (size > highWaterMark)
return true;

Expand Down Expand Up @@ -273,6 +279,7 @@ NAN_METHOD(Iterator::Seek) {

dbIterator->Seek(*iterator->target);
iterator->seeking = true;
iterator->landed = false;

if (iterator->OutOfRange(iterator->target)) {
if (iterator->reverse) {
Expand Down
1 change: 1 addition & 0 deletions src/iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Iterator : public Nan::ObjectWrap {
leveldb::Slice* target;
std::string* end;
bool seeking;
bool landed;
bool reverse;
bool keys;
bool values;
Expand Down
46 changes: 35 additions & 11 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,41 @@ make('reverse seek from invalid range', function (db, t, 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 optimized for seek', function (db, t, done) {
var batch = db.batch()
batch.put('a', 1)
batch.put('b', 1)
batch.put('c', 1)
batch.put('d', 1)
batch.put('e', 1)
batch.put('f', 1)
batch.put('g', 1)
batch.write(function (err) {
var ite = db.iterator()
t.error(err, 'no error from batch')
ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
t.equal(key.toString(), 'a', 'key matches')
t.equal(ite.cache.length, 0, "no cache")
ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
t.equal(key.toString(), 'b', 'key matches')
t.ok(ite.cache.length > 0, "has cached items")
ite.seek('d')
t.notOk(ite.cache, 'cache is removed')
ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
t.equal(key.toString(), 'd', 'key matches')
t.equal(ite.cache.length, 0, "no cache")
ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
t.equal(key.toString(), 'e', 'key matches')
t.ok(ite.cache.length > 0, "has cached items")
done();
})
})
})
})
})
})

Expand Down

0 comments on commit 959b78f

Please sign in to comment.