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

Commit

Permalink
Make iterator seek target a local variable
Browse files Browse the repository at this point in the history
Cherry-picked from Level/leveldown#683
  • Loading branch information
vweevers committed Nov 4, 2019
1 parent 99918c5 commit 9b08726
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,6 @@ struct Iterator {
highWaterMark_(highWaterMark),
dbIterator_(NULL),
count_(0),
target_(NULL),
seeking_(false),
landed_(false),
nexting_(false),
Expand All @@ -558,7 +557,6 @@ struct Iterator {

~Iterator () {
assert(ended_);
ReleaseTarget();

if (start_ != NULL) delete start_;
if (end_ != NULL) delete end_;
Expand All @@ -570,16 +568,6 @@ struct Iterator {
delete options_;
}

void ReleaseTarget () {
if (target_ != NULL) {
if (!target_->empty()) {
delete [] target_->data();
}
delete target_;
target_ = NULL;
}
}

void Attach (napi_ref ref) {
ref_ = ref;
database_->AttachIterator(id_, this);
Expand Down Expand Up @@ -684,21 +672,21 @@ struct Iterator {
return false;
}

bool OutOfRange (leveldb::Slice* target) {
if ((lt_ != NULL && target->compare(*lt_) >= 0) ||
(lte_ != NULL && target->compare(*lte_) > 0) ||
(start_ != NULL && reverse_ && target->compare(*start_) > 0)) {
bool OutOfRange (leveldb::Slice& target) {
if ((lt_ != NULL && target.compare(*lt_) >= 0) ||
(lte_ != NULL && target.compare(*lte_) > 0) ||
(start_ != NULL && reverse_ && target.compare(*start_) > 0)) {
return true;
}

if (end_ != NULL) {
int d = target->compare(*end_);
int d = target.compare(*end_);
if (reverse_ ? d < 0 : d > 0) return true;
}

return ((gt_ != NULL && target->compare(*gt_) <= 0) ||
(gte_ != NULL && target->compare(*gte_) < 0) ||
(start_ != NULL && !reverse_ && target->compare(*start_) < 0));
return ((gt_ != NULL && target.compare(*gt_) <= 0) ||
(gte_ != NULL && target.compare(*gte_) < 0) ||
(start_ != NULL && !reverse_ && target.compare(*start_) < 0));
}

bool IteratorNext (std::vector<std::pair<std::string, std::string> >& result) {
Expand Down Expand Up @@ -746,7 +734,6 @@ struct Iterator {
uint32_t highWaterMark_;
leveldb::Iterator* dbIterator_;
int count_;
leveldb::Slice* target_;
bool seeking_;
bool landed_;
bool nexting_;
Expand Down Expand Up @@ -1364,17 +1351,16 @@ NAPI_METHOD(iterator_seek) {
napi_throw_error(env, NULL, "iterator has ended");
}

iterator->ReleaseTarget();
iterator->target_ = new leveldb::Slice(ToSlice(env, argv[1]));
leveldb::Slice target = ToSlice(env, argv[1]);
iterator->GetIterator();

leveldb::Iterator* dbIterator = iterator->dbIterator_;
dbIterator->Seek(*iterator->target_);
dbIterator->Seek(target);

iterator->seeking_ = true;
iterator->landed_ = false;

if (iterator->OutOfRange(iterator->target_)) {
if (iterator->OutOfRange(target)) {
if (iterator->reverse_) {
dbIterator->SeekToFirst();
dbIterator->Prev();
Expand All @@ -1384,7 +1370,7 @@ NAPI_METHOD(iterator_seek) {
}
}
else if (dbIterator->Valid()) {
int cmp = dbIterator->key().compare(*iterator->target_);
int cmp = dbIterator->key().compare(target);
if (cmp > 0 && iterator->reverse_) {
dbIterator->Prev();
} else if (cmp < 0 && !iterator->reverse_) {
Expand All @@ -1397,7 +1383,7 @@ NAPI_METHOD(iterator_seek) {
dbIterator->SeekToFirst();
}
if (dbIterator->Valid()) {
int cmp = dbIterator->key().compare(*iterator->target_);
int cmp = dbIterator->key().compare(target);
if (cmp > 0 && iterator->reverse_) {
dbIterator->SeekToFirst();
dbIterator->Prev();
Expand All @@ -1408,6 +1394,7 @@ NAPI_METHOD(iterator_seek) {
}
}

DisposeSliceBuffer(target);
NAPI_RETURN_UNDEFINED();
}

Expand Down Expand Up @@ -1469,7 +1456,6 @@ NAPI_METHOD(iterator_end) {
* for this function being a separate function pointer.
*/
void CheckEndCallback (Iterator* iterator) {
iterator->ReleaseTarget();
iterator->nexting_ = false;
if (iterator->endWorker_ != NULL) {
iterator->endWorker_->Queue();
Expand Down

0 comments on commit 9b08726

Please sign in to comment.