From 949afd24114ac05f00549e6a388b3a6a3784638d Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Mon, 4 Nov 2019 20:25:32 +0200 Subject: [PATCH] Refactor initial seek Cherry-picked from Level/leveldown#689 --- binding.cc | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/binding.cc b/binding.cc index 9e71aeb9..9ead8fac 100644 --- a/binding.cc +++ b/binding.cc @@ -600,27 +600,24 @@ 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(); } } + // TODO: this looks like dead code. Remove it in a + // next major release together with Level/community#86. if (dbIterator_->Valid() && lt_ != NULL) { - if (lt_->compare(dbIterator_->key().ToString()) <= 0) + if (dbIterator_->key().compare(*lt_) >= 0) dbIterator_->Prev(); } } else { if (dbIterator_->Valid() && gt_ != NULL - && gt_->compare(dbIterator_->key().ToString()) == 0) + && dbIterator_->key().compare(*gt_) == 0) dbIterator_->Next(); } } else if (reverse_) { @@ -1368,8 +1365,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();