Skip to content

Commit

Permalink
iterator: Start on correct value when reverse=true
Browse files Browse the repository at this point in the history
  • Loading branch information
kesla committed Mar 30, 2013
1 parent 1a566fe commit 701b3c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ Iterator::~Iterator () {
bool Iterator::GetIterator () {
if (dbIterator == NULL) {
dbIterator = database->NewIterator(options);
if (start != NULL)
if (start != NULL) {
dbIterator->Seek(*start);
if (reverse) {
if (!dbIterator->Valid())
dbIterator->SeekToLast();
else if (start->compare(dbIterator->key()))
dbIterator->Prev();
}
}
else if (reverse)
dbIterator->SeekToLast();
else
Expand Down
22 changes: 20 additions & 2 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ module.exports.iterator = function (leveldown) {
test('test iterator with start being a midway key and reverse=true', function (t) {
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '49.5', reverse: true }), function (err, data) {
t.notOk(err, 'no error')
t.equal(data.length, 51, 'correct number of entries')
var expected = sourceData.slice().reverse().slice(49).map(transformSource)
t.equal(data.length, 50, 'correct number of entries')
var expected = sourceData.slice().reverse().slice(50).map(transformSource)
t.deepEqual(data, expected)
t.end()
})
Expand Down Expand Up @@ -365,6 +365,24 @@ module.exports.iterator = function (leveldown) {
t.end()
})
})

test('test iterator with start after database end and reverse=true', function (t) {
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '9a', reverse: true }), function (err, data) {
t.notOk(err, 'no error')
t.equal(data.length, sourceData.length, 'correct number of entries')
var expected = sourceData.slice().reverse().map(transformSource)
t.deepEqual(data, expected)
t.end()
})
})

test('test iterator with start and end after database and and reverse=true', function (t) {
collectEntries(db.iterator({ start: '9b', end: '9a', reverse: true }), function (err, data) {
t.notOk(err, 'no error')
t.equal(data.length, 0, 'correct number of entries')
t.end()
})
})
}

module.exports.tearDown = function () {
Expand Down

0 comments on commit 701b3c0

Please sign in to comment.