Skip to content

Commit f5918b4

Browse files
authoredMar 18, 2025
db: more fix std::basic_string::compare result in snapshot BTree (#2796)
1 parent 66bc87d commit f5918b4

File tree

1 file changed

+10
-17
lines changed
  • silkworm/db/datastore/snapshots/btree

1 file changed

+10
-17
lines changed
 

‎silkworm/db/datastore/snapshots/btree/btree.cpp

+10-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include "btree.hpp"
1818

19-
#include <silkworm/core/common/assert.hpp>
2019
#include <silkworm/core/common/endian.hpp>
2120
#include <silkworm/core/common/util.hpp>
2221
#include <silkworm/infra/common/ensure.hpp>
@@ -43,7 +42,7 @@ static CompareResult compare_key(
4342
BTree::DataIndex key_index,
4443
const BTree::KeyValueIndex& index) {
4544
auto data_key = index.lookup_key(key_index);
46-
ensure(data_key.has_value(), [&]() { return "out-of-bounds data_index=" + std::to_string(key_index); });
45+
ensure(data_key.has_value(), [&]() { return "out-of-bounds key=" + to_hex(key) + " data_index=" + std::to_string(key_index); });
4746
int cmp = data_key->compare(key);
4847
return {cmp, std::move(*data_key)};
4948
}
@@ -111,23 +110,17 @@ std::optional<BTree::GetResult> BTree::get(ByteView key, const KeyValueIndex& in
111110
return GetResult{std::move(kv_pair->second), 0};
112111
}
113112
auto [_, left_index, right_index] = binary_search_in_cache(key); // left_index == right_index when key is found
114-
uint64_t median = 0;
115113
while (left_index < right_index) {
116-
median = (left_index + right_index) >> 1;
114+
const uint64_t median = (left_index + right_index) >> 1;
117115
const auto [cmp, k] = compare_key(key, median, index);
118-
switch (cmp) {
119-
case 0: {
120-
auto kv_pair = index.lookup_key_value(median);
121-
return GetResult{std::move(kv_pair->second), median};
122-
}
123-
case 1:
124-
right_index = median;
125-
break;
126-
case -1:
127-
left_index = median + 1;
128-
break;
129-
default:
130-
SILKWORM_ASSERT(false);
116+
if (cmp == 0) {
117+
auto kv_pair = index.lookup_key_value(median);
118+
return GetResult{std::move(kv_pair->second), median};
119+
}
120+
if (cmp > 0) {
121+
right_index = median;
122+
} else { // cmp < 0
123+
left_index = median + 1;
131124
}
132125
}
133126
const auto [cmp, k] = compare_key(key, left_index, index);

0 commit comments

Comments
 (0)