16
16
17
17
#include " btree.hpp"
18
18
19
- #include < silkworm/core/common/assert.hpp>
20
19
#include < silkworm/core/common/endian.hpp>
21
20
#include < silkworm/core/common/util.hpp>
22
21
#include < silkworm/infra/common/ensure.hpp>
@@ -43,7 +42,7 @@ static CompareResult compare_key(
43
42
BTree::DataIndex key_index,
44
43
const BTree::KeyValueIndex& index) {
45
44
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); });
47
46
int cmp = data_key->compare (key);
48
47
return {cmp, std::move (*data_key)};
49
48
}
@@ -111,23 +110,17 @@ std::optional<BTree::GetResult> BTree::get(ByteView key, const KeyValueIndex& in
111
110
return GetResult{std::move (kv_pair->second ), 0 };
112
111
}
113
112
auto [_, left_index, right_index] = binary_search_in_cache (key); // left_index == right_index when key is found
114
- uint64_t median = 0 ;
115
113
while (left_index < right_index) {
116
- median = (left_index + right_index) >> 1 ;
114
+ const uint64_t median = (left_index + right_index) >> 1 ;
117
115
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 ;
131
124
}
132
125
}
133
126
const auto [cmp, k] = compare_key (key, left_index, index );
0 commit comments