Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Refactor initial seek #689

Merged
merged 3 commits into from
Nov 4, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -587,27 +587,22 @@ struct Iterator {
if (!dbIterator_->Valid()) {
dbIterator_->SeekToLast();
} else {
std::string keyStr = dbIterator_->key().ToString();

if (lt_ != NULL) {
if (lt_->compare(keyStr) <= 0)
dbIterator_->Prev();
} else if (lte_ != NULL) {
if (lte_->compare(keyStr) < 0)
dbIterator_->Prev();
} else if (start_ != NULL) {
if (start_->compare(keyStr))
dbIterator_->Prev();
leveldb::Slice key = dbIterator_->key();

if ((lt_ != NULL && key.compare(*lt_) >= 0) ||
(lte_ != NULL && key.compare(*lte_) > 0) ||
(start_ != NULL && key.compare(*start_) > 0)) {
dbIterator_->Prev();
}
}

if (dbIterator_->Valid() && lt_ != NULL) {
if (lt_->compare(dbIterator_->key().ToString()) <= 0)
dbIterator_->Prev();
if (dbIterator_->key().compare(*lt_) >= 0)
assert(false);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ralphtheninja @peakji Can you think of a case where we hit this code path?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is dead code.

If we're using start_ correctly, there can be only 3 possible cases for a valid reversed iterator in L584-L599, so the key should never be less than lt on L600:

seek

}
} else {
if (dbIterator_->Valid() && gt_ != NULL
&& gt_->compare(dbIterator_->key().ToString()) == 0)
&& dbIterator_->key().compare(*gt_) == 0)
dbIterator_->Next();
}
} else if (reverse_) {
Expand Down Expand Up @@ -1306,8 +1301,7 @@ NAPI_METHOD(iterator_seek) {
dbIterator->SeekToLast();
dbIterator->Next();
}
}
else if (dbIterator->Valid()) {
} else if (dbIterator->Valid()) {
int cmp = dbIterator->key().compare(target);
if (cmp > 0 && iterator->reverse_) {
dbIterator->Prev();
Expand Down