diff --git a/src/core/search/rax_tree.h b/src/core/search/rax_tree.h index 7e1356e3a94f..f99f3d3d4514 100644 --- a/src/core/search/rax_tree.h +++ b/src/core/search/rax_tree.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -72,7 +73,7 @@ template struct RaxTreeMap { }; // Result of find() call. Inherits from pair to mimic iterator interface, not incrementable. - struct FindIterator : public std::optional> { + struct FindIterator : public std::optional> { bool operator==(const SeekIterator& rhs) const { if (this->has_value() != !bool(rhs.it_.flags & RAX_ITER_EOF)) return false; @@ -92,6 +93,11 @@ template struct RaxTreeMap { } ~RaxTreeMap() { + for (auto it = begin(); it != end(); ++it) { + V* ptr = &(*it).second; + std::allocator_traits::destroy(alloc_, ptr); + alloc_.deallocate(ptr, 1); + } raxFree(tree_); } @@ -113,7 +119,8 @@ template struct RaxTreeMap { FindIterator find(std::string_view key) const { if (void* ptr = raxFind(tree_, to_key_ptr(key), key.size()); ptr != raxNotFound) - return FindIterator{std::pair(key, *reinterpret_cast(ptr))}; + return FindIterator{std::pair(std::string(key), *reinterpret_cast(ptr))}; + return FindIterator{std::nullopt}; } @@ -155,8 +162,8 @@ std::pair::FindIterator, bool> RaxTreeMap::try_emplace raxInsert(tree_, to_key_ptr(key), key.size(), ptr, reinterpret_cast(&old)); assert(old == nullptr); - auto it = std::make_optional(std::pair(key, *ptr)); - return std::make_pair(FindIterator{it}, true); + auto it = std::make_optional(std::pair(std::string(key), *ptr)); + return std::make_pair(std::move(FindIterator{it}), true); } } // namespace dfly::search