From 21cef57838cba1f2c7adee382ada264a50f7c97e Mon Sep 17 00:00:00 2001 From: Wojtek Date: Fri, 5 Dec 2025 10:26:03 +0100 Subject: [PATCH] fix(v_bindex::bulkErase) closes #572 --- .../core/collections/b_index/v_bindex.hpp | 5 +++- .../collections/vector/v_sorted_vector.hpp | 12 +++++----- tests/unit_tests/VBIndexTests.cpp | 24 +++++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/dbzero/core/collections/b_index/v_bindex.hpp b/src/dbzero/core/collections/b_index/v_bindex.hpp index 41e4035a..6e789abc 100644 --- a/src/dbzero/core/collections/b_index/v_bindex.hpp +++ b/src/dbzero/core/collections/b_index/v_bindex.hpp @@ -860,9 +860,12 @@ namespace db0 // this is safe as order not violated m_it_node.modify().m_data.lo_bound = m_data_buf->front(); } - + // move on to the next node ++m_it_node; + if (m_it_node != m_ref.m_index.end()) { + m_data_buf = data_vector(m_ref.getMemspace().myPtr(m_it_node->m_data.ptr_b_data), m_ref.m_item_destroy_func); + } return erase_count; } diff --git a/src/dbzero/core/collections/vector/v_sorted_vector.hpp b/src/dbzero/core/collections/vector/v_sorted_vector.hpp index 12a13b52..de97fdc3 100644 --- a/src/dbzero/core/collections/vector/v_sorted_vector.hpp +++ b/src/dbzero/core/collections/vector/v_sorted_vector.hpp @@ -18,11 +18,10 @@ namespace db0 { -DB0_PACKED_BEGIN /** * inverting comparator - */ + */ template struct inverted_comp_t { comp_t _comp; @@ -37,7 +36,7 @@ DB0_PACKED_BEGIN bool operator()(const T &val0,const T &val1) const { return _comp(val1,val0); } - }; + }; /** * Sorted vector state types @@ -53,6 +52,7 @@ DB0_PACKED_BEGIN * data_t - contained element type (comparable) * comp_t - data comparer */ +DB0_PACKED_BEGIN template > class DB0_PACKED_ATTR o_sv_container : public o_base, 0, false > { @@ -666,7 +666,8 @@ DB0_PACKED_BEGIN return reinterpret_cast(reinterpret_cast(this) + sizeof(self)); } }; - +DB0_PACKED_END + /** * NOTICE : destroy does not call any overlaid item destructors * in order to destroy items, call erase / clear first @@ -1070,7 +1071,7 @@ DB0_PACKED_BEGIN /** * @return true on object relocated */ - bool compactShrinking() + bool compactShrinking() { if ((*this)->isShrinking()) { return compact(); @@ -1155,5 +1156,4 @@ DB0_PACKED_BEGIN DestroyF m_item_destroy_func; }; -DB0_PACKED_END } diff --git a/tests/unit_tests/VBIndexTests.cpp b/tests/unit_tests/VBIndexTests.cpp index c11cf607..fd06096e 100644 --- a/tests/unit_tests/VBIndexTests.cpp +++ b/tests/unit_tests/VBIndexTests.cpp @@ -179,5 +179,29 @@ namespace tests timer.printLog(std::cout) << std::endl; } + TEST_F( VBIndexTests , testVBIndexBulkErase ) + { + using ItemT = db0::key_value; + auto memspace = getMemspace(); + std::vector values(1500); + for (std::uint32_t i = 0; i < 1500; ++i) { + values[i] = { i, 0 }; + } + db0::v_bindex cut(memspace, memspace.getPageSize()); + cut.bulkInsert(values.begin(), values.end()); + ASSERT_EQ(cut.size(), 1500u); + std::function selector = [&](ItemT item) { + return item.key < 1000; + }; + cut.bulkErase(selector); + ASSERT_EQ(cut.size(), 500u); + // verify remaining items + auto it = cut.begin(), end = cut.end(); + while (it != end) { + ASSERT_EQ((*it).key >= 1000, true); + ++it; + } + } + }