Skip to content

Commit

Permalink
fix: increase coverage for ascend and descend scan
Browse files Browse the repository at this point in the history
  • Loading branch information
SYaoJun committed Aug 16, 2024
1 parent 3d3b882 commit 5bbe6af
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/btree/BasicKV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,18 @@ OpCode BasicKV::ScanAsc(Slice startKey, ScanCallback callback) {
}

JUMPMU_TRY() {
bool foundKey = false;
auto iter = GetIterator();
for (iter.SeekToFirstGreaterEqual(startKey); iter.Valid(); iter.Next()) {
iter.AssembleKey();
auto key = iter.Key();
auto value = iter.Val();
foundKey = true;
if (!callback(key, value)) {
break;
}
}
JUMPMU_RETURN OpCode::kOK;
JUMPMU_RETURN foundKey ? OpCode::kOK : OpCode::kNotFound;
}
JUMPMU_CATCH() {
}
Expand All @@ -121,12 +123,13 @@ OpCode BasicKV::ScanDesc(Slice scanKey, ScanCallback callback) {
auto key = iter.Key();
auto value = iter.Val();
if (!callback(key, value)) {
JUMPMU_RETURN OpCode::kOK;
break;
}
if (iter.Prev(); !iter.Valid()) {
JUMPMU_RETURN OpCode::kNotFound;
break;
}
}
JUMPMU_RETURN OpCode::kOK;
}
JUMPMU_CATCH() {
}
Expand Down
79 changes: 79 additions & 0 deletions tests/BasicKVTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,84 @@ TEST_F(BasicKVTest, BasicKVInsertDuplicatedKey) {
}
});
}
TEST_F(BasicKVTest, BasicKVScanAscAndScanDesc) {
storage::btree::BasicKV* btree;
// prepare key-value pairs to insert
size_t numKVs(10);
std::vector<std::tuple<std::string, std::string>> kvToTest;
for (size_t i = 0; i < numKVs; ++i) {
std::string key("key_btree_LL_xxxxxxxxxxxx_" + std::to_string(i));
std::string val("VAL_BTREE_LL_YYYYYYYYYYYY_" + std::to_string(i));
kvToTest.push_back(std::make_tuple(key, val));
}
// create leanstore btree for table records
const auto* btreeName = "testTree1";

mStore->ExecSync(0, [&]() {
auto res = mStore->CreateBasicKV(btreeName);
EXPECT_TRUE(res);
EXPECT_NE(res.value(), nullptr);

btree = res.value();
cr::Worker::My().StartTx();
for (size_t i = 0; i < numKVs; ++i) {
const auto& [key, val] = kvToTest[i];
EXPECT_EQ(btree->Insert(Slice(key), Slice(val)), OpCode::kOK);
}

std::vector<std::tuple<std::string, std::string>> copiedKeyValue;
auto copyKeyAndValueOut = [&](Slice key, Slice val) {
copiedKeyValue.emplace_back(key.ToString(), val.ToString());
return true;
};

size_t startIndex = 5;
auto startKey = Slice(std::get<0>(kvToTest[startIndex]));
auto callbackReturnFalse = [&]([[maybe_unused]] Slice key, [[maybe_unused]] Slice val) {
return false;
};
// ScanAsc
{
// no bigger than largestLexicographicalOrderKey in ScanAsc should return OpCode::kNotFound
Slice largestLexicographicalOrderKey("zzzzzzz");
EXPECT_EQ(btree->ScanAsc(largestLexicographicalOrderKey, copyKeyAndValueOut),
OpCode::kNotFound);

// callback return false should terminate scan
EXPECT_EQ(btree->ScanAsc(startKey, callbackReturnFalse), OpCode::kOK);

// query on ScanAsc
EXPECT_EQ(btree->ScanAsc(startKey, copyKeyAndValueOut), OpCode::kOK);
EXPECT_EQ(copiedKeyValue.size(), 5);
for (size_t i = startIndex, j = 0; i < numKVs && j < copiedKeyValue.size(); i++, j++) {
const auto& [key, expectedVal] = kvToTest[i];
const auto& [copiedKey, copiedValue] = copiedKeyValue[j];
EXPECT_EQ(copiedKey, key);
EXPECT_EQ(copiedValue, expectedVal);
}
}
// ScanDesc
{
// no smaller than key in ScanDesc should return OpCode::kNotFound
Slice smallestLexicographicalOrderKey("aaaaaaaa");
EXPECT_EQ(btree->ScanDesc(smallestLexicographicalOrderKey, copyKeyAndValueOut),
OpCode::kNotFound);

// callback return false should terminate scan
EXPECT_EQ(btree->ScanDesc(startKey, callbackReturnFalse), OpCode::kOK);

// query on ScanDesc
copiedKeyValue.clear();
EXPECT_EQ(btree->ScanDesc(startKey, copyKeyAndValueOut), OpCode::kOK);
EXPECT_EQ(copiedKeyValue.size(), 6);
for (int i = startIndex, j = 0; i >= 0 && j < static_cast<int>(copiedKeyValue.size());
i--, j++) {
const auto& [key, expectedVal] = kvToTest[i];
const auto& [copiedKey, copiedValue] = copiedKeyValue[j];
EXPECT_EQ(copiedKey, key);
EXPECT_EQ(copiedValue, expectedVal);
}
}
});
}
} // namespace leanstore::test

0 comments on commit 5bbe6af

Please sign in to comment.