diff --git a/src/art/art.c b/src/art/art.c index 3c2459142..cb8ae43bb 100644 --- a/src/art/art.c +++ b/src/art/art.c @@ -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); diff --git a/tests/art_unit.cpp b/tests/art_unit.cpp index 5655a34dd..721433000 100644 --- a/tests/art_unit.cpp +++ b/tests/art_unit.cpp @@ -254,22 +254,40 @@ DEFINE_TEST(test_art_iterator_prev) { } DEFINE_TEST(test_art_iterator_lower_bound) { - std::vector keys = { - "000001", "000002", "000003", "000004", "001005", - }; - std::vector 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 keys = { + "000001", "000002", "000003", "000004", "001005", + }; + std::vector 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 keys = {"000001", "000003", "000004", + "001005"}; + std::vector 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) {