Skip to content

Commit

Permalink
Small improvement to MultiCFIteratorImpl (#13075)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #13075

The patch simplifies the iteration logic in `MultiCFIteratorImpl::{Advance,Populate}Iterator` a bit and adds some assertions to uniformly enforce the invariant that any iterators currently on the heap should be valid and have an OK status.

Reviewed By: jaykorean

Differential Revision: D64429566

fbshipit-source-id: 36bc22465285b670f859692a048e10f21df7da7a
  • Loading branch information
ltamasi authored and facebook-github-bot committed Oct 16, 2024
1 parent 2cb00c6 commit 55de265
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions db/multi_cf_iterator_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,32 +211,41 @@ class MultiCfIteratorImpl {
// 2. Make sure all others have iterated past the top iterator key slice
// 3. Advance the top iterator, and add it back to the heap if valid
auto top = heap.top();
assert(top.iterator);
assert(top.iterator->Valid());
assert(top.iterator->status().ok());

heap.pop();
if (!heap.empty()) {

while (!heap.empty()) {
auto current = heap.top();
assert(current.iterator);
while (current.iterator->Valid() &&
comparator_->Compare(top.iterator->key(),
current.iterator->key()) == 0) {
assert(current.iterator->Valid());
assert(current.iterator->status().ok());

if (comparator_->Compare(current.iterator->key(), top.iterator->key()) !=
0) {
break;
}

advance_func(current.iterator);

if (current.iterator->Valid()) {
assert(current.iterator->status().ok());
advance_func(current.iterator);
if (current.iterator->Valid()) {
heap.replace_top(heap.top());
heap.replace_top(current);
} else {
considerStatus(current.iterator->status());
if (!status_.ok()) {
heap.clear();
return;
} else {
considerStatus(current.iterator->status());
if (!status_.ok()) {
heap.clear();
return;
} else {
heap.pop();
}
}
if (!heap.empty()) {
current = heap.top();
heap.pop();
}
}
}

advance_func(top.iterator);

if (top.iterator->Valid()) {
assert(top.iterator->status().ok());
heap.push(top);
Expand Down Expand Up @@ -264,30 +273,37 @@ class MultiCfIteratorImpl {
// populate the value/columns and attribute_groups from the list
// collected in step 1 and 2 and add all the iters back to the heap
assert(!heap.empty());

auto top = heap.top();
assert(top.iterator);
assert(top.iterator->Valid());
assert(top.iterator->status().ok());

heap.pop();

autovector<MultiCfIteratorInfo> to_populate;
to_populate.push_back(top);
if (!heap.empty()) {

while (!heap.empty()) {
auto current = heap.top();
assert(current.iterator);
while (current.iterator->Valid() &&
comparator_->Compare(top.iterator->key(),
current.iterator->key()) == 0) {
assert(current.iterator->status().ok());
to_populate.push_back(current);
heap.pop();
if (!heap.empty()) {
current = heap.top();
} else {
break;
}
assert(current.iterator->Valid());
assert(current.iterator->status().ok());

if (comparator_->Compare(current.iterator->key(), top.iterator->key()) !=
0) {
break;
}

to_populate.push_back(current);
heap.pop();
}

// Add the items back to the heap
for (auto& item : to_populate) {
heap.push(item);
}

populate_func_(to_populate);
}
};
Expand Down

0 comments on commit 55de265

Please sign in to comment.