Skip to content

Commit

Permalink
seek with buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
Yichao 'Peak' Ji committed Nov 28, 2016
1 parent 09f59ff commit 4c24d74
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
2 changes: 0 additions & 2 deletions iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ function Iterator (db, options) {
util.inherits(Iterator, AbstractIterator)

Iterator.prototype.seek = function (key) {
if (typeof key !== 'string')
throw new Error('seek requires a string key')
this.cache = null
this.binding.seek(key)
}
Expand Down
14 changes: 13 additions & 1 deletion src/iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,21 @@ void checkEndCallback (Iterator* iterator) {

NAN_METHOD(Iterator::Seek) {
Iterator* iterator = Nan::ObjectWrap::Unwrap<Iterator>(info.This());

if (!node::Buffer::HasInstance(info[0]) && !info[0]->IsString())
return Nan::ThrowError("seek() requires a string or buffer key");

if (StringOrBufferLength(info[0]) == 0)
return Nan::ThrowError("cannot seek() to an empty key");

std::string* key = NULL;
v8::Local<v8::Value> keyBuffer = info[0].As<v8::Value>();
LD_STRING_OR_BUFFER_TO_COPY(_key, keyBuffer, key);
key = new std::string(_keyCh_, _keySz_);
delete[] _keyCh_;

iterator->GetIterator();
leveldb::Iterator* dbIterator = iterator->dbIterator;
Nan::Utf8String key(info[0]);

dbIterator->Seek(*key);
iterator->seeking = true;
Expand Down
47 changes: 37 additions & 10 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@ const test = require('tape')

abstract.all(leveldown, test, testCommon)

make('iterator throws if key is not a string', function (db, t, done) {
var ite = db.iterator()
var error
try {
ite.seek()
} catch (e) {
error = e
}
make('iterator throws if key is not a string or buffer', function (db, t, done) {
var keys = [null, undefined, 1, true, false]
var pending = keys.length

keys.forEach(function (key) {
var error
var ite = db.iterator()

try {
ite.seek(key)
} catch (e) {
error = e
}

t.ok(error, 'had error')
t.end()
t.ok(error, 'had error from seek()')
ite.end(end)
})

function end(err) {
t.error(err, 'no error from end()')
if (!--pending) done()
}
})

make('iterator is seekable', function (db, t, done) {
Expand All @@ -35,6 +46,22 @@ make('iterator is seekable', function (db, t, done) {
})
})

make('iterator is seekable with buffer', function (db, t, done) {
var ite = db.iterator()
ite.seek(Buffer('two'))
ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
t.equal(key.toString(), 'two', 'key matches')
t.equal(value.toString(), '2', 'value matches')
ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
t.equal(key, undefined, 'end of iterator')
t.equal(value, undefined, 'end of iterator')
ite.end(done)
})
})
})

make('reverse seek in the middle', function (db, t, done) {
var ite = db.iterator({reverse: true, limit: 1})
ite.seek('three!')
Expand Down

0 comments on commit 4c24d74

Please sign in to comment.