Skip to content

Commit

Permalink
ART: Fix lower bound search within a node's children
Browse files Browse the repository at this point in the history
Previously when doing a lower bound search on a node where the prefix was equal
and there was no child with an equal key chunk, we would move to the first
child. The correct thing to do is to move to the first _larger_ child.
  • Loading branch information
SLieve committed Jan 17, 2024
1 parent 88ca6aa commit 670bfbe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/art/art.c
Original file line number Diff line number Diff line change
Expand Up @@ -1458,8 +1458,9 @@ static bool art_node_iterator_lower_bound(const art_node_t *node,
return art_iterator_move(iterator, true);
}
if (indexed_child.key_chunk > key_chunk) {
// Only larger children, return the first leaf from the node.
return art_node_init_iterator(node, iterator, true);
// Only larger children, return the first larger child.
art_iterator_down(iterator, inner_node, indexed_child.index);
return art_node_init_iterator(indexed_child.child, iterator, true);
}
// We found a child with an equal prefix.
art_iterator_down(iterator, inner_node, indexed_child.index);
Expand Down
48 changes: 33 additions & 15 deletions tests/art_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,22 +254,40 @@ DEFINE_TEST(test_art_iterator_prev) {
}

DEFINE_TEST(test_art_iterator_lower_bound) {
std::vector<const char*> keys = {
"000001", "000002", "000003", "000004", "001005",
};
std::vector<Value> values = {{1}, {2}, {3}, {4}, {5}};
art_t art{NULL};
for (size_t i = 0; i < keys.size(); ++i) {
art_insert(&art, (art_key_chunk_t*)keys[i], &values[i]);
}
{
std::vector<const char*> keys = {
"000001", "000002", "000003", "000004", "001005",
};
std::vector<Value> values = {{1}, {2}, {3}, {4}, {5}};
art_t art{NULL};
for (size_t i = 0; i < keys.size(); ++i) {
art_insert(&art, (art_key_chunk_t*)keys[i], &values[i]);
}

art_iterator_t iterator = art_init_iterator(&art, true);
assert_true(art_iterator_lower_bound(&iterator, (art_key_chunk_t*)keys[2]));
assert_key_eq(iterator.key, (art_key_chunk_t*)keys[2]);
const char* key = "000005";
assert_true(art_iterator_lower_bound(&iterator, (art_key_chunk_t*)key));
assert_key_eq(iterator.key, (art_key_chunk_t*)keys[4]);
art_free(&art);
art_iterator_t iterator = art_init_iterator(&art, true);
assert_true(
art_iterator_lower_bound(&iterator, (art_key_chunk_t*)keys[2]));
assert_key_eq(iterator.key, (art_key_chunk_t*)keys[2]);
const char* key = "000005";
assert_true(art_iterator_lower_bound(&iterator, (art_key_chunk_t*)key));
assert_key_eq(iterator.key, (art_key_chunk_t*)keys[4]);
art_free(&art);
}
{
// Lower bound search within a node's children.
std::vector<const char*> keys = {"000001", "000003", "000004",
"001005"};
std::vector<Value> values = {{1}, {3}, {4}, {5}};
art_t art{NULL};
for (size_t i = 0; i < keys.size(); ++i) {
art_insert(&art, (art_key_chunk_t*)keys[i], &values[i]);
}
art_iterator_t iterator = art_init_iterator(&art, true);
const char* key = "000002";
assert_true(art_iterator_lower_bound(&iterator, (art_key_chunk_t*)key));
assert_key_eq(iterator.key, (art_key_chunk_t*)keys[1]);
art_free(&art);
}
}

DEFINE_TEST(test_art_lower_bound) {
Expand Down

0 comments on commit 670bfbe

Please sign in to comment.