Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deltaiterator upperbound check #11662

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
55 changes: 55 additions & 0 deletions utilities/transactions/transaction_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6657,6 +6657,61 @@ TEST_P(TransactionTest, StallTwoWriteQueues) {
ASSERT_TRUE(t2_completed);
}

TEST_P(TransactionTest, TnxIteratorWithUpperBound) {
if (txn_db_options.write_policy != WRITE_COMMITTED) {
return;
}

WriteOptions write_options;
ReadOptions read_options;

Transaction* txn = db->BeginTransaction(write_options);
ASSERT_TRUE(txn);

ASSERT_OK(txn->Put("2", "2"));
ASSERT_OK(txn->Put("1", "1"));
ASSERT_OK(txn->Put("3", "3"));

{
Slice upper_bound = "2";
read_options.iterate_upper_bound = &upper_bound;
Iterator* iter = txn->GetIterator(read_options);
ASSERT_OK(iter->status());

iter->SeekToFirst();
while (iter->Valid()) {
ASSERT_EQ("1", iter->key().ToString());
iter->Next();
}

// iter is reusable
iter->SeekToFirst();
ASSERT_TRUE(iter->Valid());
delete iter;
}

{
Slice upper_bound = "4";
read_options.iterate_upper_bound = &upper_bound;

std::string results[] = {"1", "2", "3"};

Iterator* forward_iter = txn->GetIterator(read_options);

forward_iter->SeekToFirst();
for (int i = 0; i < 3; i++) {
ASSERT_TRUE(forward_iter->Valid());
ASSERT_EQ(results[i], forward_iter->key().ToString());

forward_iter->Next();
}

delete forward_iter;
}

delete txn;
}

// Make sure UnlockWAL does not return until the stall it controls is cleared.
TEST_P(TransactionTest, UnlockWALStallCleared) {
auto dbimpl = static_cast_with_check<DBImpl>(db->GetRootDB());
Expand Down
7 changes: 4 additions & 3 deletions utilities/write_batch_with_index/write_batch_with_index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,10 @@ WBWIIterator* WriteBatchWithIndex::NewIterator(
Iterator* WriteBatchWithIndex::NewIteratorWithBase(
ColumnFamilyHandle* column_family, Iterator* base_iterator,
const ReadOptions* read_options) {
auto wbwiii =
new WBWIIteratorImpl(GetColumnFamilyID(column_family), &(rep->skip_list),
&rep->write_batch, &rep->comparator);
auto wbwiii = new WBWIIteratorImpl(
GetColumnFamilyID(column_family), &(rep->skip_list), &rep->write_batch,
&rep->comparator,
read_options == nullptr ? nullptr : read_options->iterate_upper_bound);
return new BaseDeltaIterator(column_family, base_iterator, wbwiii,
GetColumnFamilyUserComparator(column_family),
read_options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ void BaseDeltaIterator::AdvanceBase() {

bool BaseDeltaIterator::BaseValid() const { return base_iterator_->Valid(); }
bool BaseDeltaIterator::DeltaValid() const { return delta_iterator_->Valid(); }

void BaseDeltaIterator::UpdateCurrent() {
// Suppress false positive clang analyzer warnings.
#ifndef __clang_analyzer__
Expand Down Expand Up @@ -307,7 +308,7 @@ void BaseDeltaIterator::UpdateCurrent() {
if (comparator_->CompareWithoutTimestamp(
delta_entry.key, /*a_has_ts=*/false, *iterate_upper_bound_,
/*b_has_ts=*/false) >= 0) {
// out of upper bound -> finished.
// out of upper bound -> delta finished.
return;
}
}
Expand Down
27 changes: 25 additions & 2 deletions utilities/write_batch_with_index/write_batch_with_index_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ class WBWIIteratorImpl : public WBWIIterator {
: column_family_id_(column_family_id),
skip_list_iter_(skip_list),
write_batch_(write_batch),
iterate_upper_bound_(nullptr),
comparator_(comparator) {}

WBWIIteratorImpl(uint32_t column_family_id,
WriteBatchEntrySkipList* skip_list,
const ReadableWriteBatch* write_batch,
WriteBatchEntryComparator* comparator,
const Slice* iterate_upper_bound)
: column_family_id_(column_family_id),
skip_list_iter_(skip_list),
write_batch_(write_batch),
iterate_upper_bound_(iterate_upper_bound),
comparator_(comparator) {}

~WBWIIteratorImpl() override {}
Expand All @@ -209,8 +221,18 @@ class WBWIIteratorImpl : public WBWIIterator {
return false;
}
const WriteBatchIndexEntry* iter_entry = skip_list_iter_.key();
return (iter_entry != nullptr &&
iter_entry->column_family == column_family_id_);

if (iter_entry == nullptr ||
iter_entry->column_family != column_family_id_) {
return false;
}

if (iterate_upper_bound_ == nullptr) {
return true;
} else {
return comparator_->CompareKey(column_family_id_, *iterate_upper_bound_,
Entry().key) > 0;
}
}

void SeekToFirst() override {
Expand Down Expand Up @@ -288,6 +310,7 @@ class WBWIIteratorImpl : public WBWIIterator {
uint32_t column_family_id_;
WriteBatchEntrySkipList::Iterator skip_list_iter_;
const ReadableWriteBatch* write_batch_;
const Slice* iterate_upper_bound_;
WriteBatchEntryComparator* comparator_;
};

Expand Down