Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(search): Rax TreeMap #3909

Merged
merged 3 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions src/core/search/rax_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <absl/types/span.h>

#include <cstdio>
#include <memory_resource>
#include <optional>
#include <string_view>

extern "C" {
#include "base/pmr/memory_resource.h"

extern "C" {
#include "redis/rax.h"
}

Expand All @@ -25,56 +25,60 @@ template <typename V> struct RaxTreeMap {
friend struct FindIterator;

SeekIterator() {
dranikpg marked this conversation as resolved.
Show resolved Hide resolved
raxStart(&it_, nullptr);
it_.node = nullptr;
}

~SeekIterator() {
if (it)
raxStop(&*it);
raxStop(&it_);
}

SeekIterator(SeekIterator&&) = delete; // self-referential
SeekIterator(const SeekIterator&) = delete; // self-referential

SeekIterator(rax* tree, const char* op, std::string_view key) {
it.emplace();
raxStart(&*it, tree);
raxSeek(&*it, op, to_key_ptr(key), key.size());
raxStart(&it_, tree);
raxSeek(&it_, op, to_key_ptr(key), key.size());
operator++();
}

explicit SeekIterator(rax* tree) : SeekIterator(tree, "^", std::string_view{nullptr, 0}) {
}

bool operator==(const SeekIterator& rhs) const {
return it.has_value() == rhs.it.has_value() && (!it.has_value() || it->key == rhs.it->key);
return it_.node == rhs.it_.node;
}

bool operator!=(const SeekIterator& rhs) const {
return !operator==(rhs);
}

SeekIterator& operator++() {
if (!raxNext(&*it)) {
raxStop(&*it);
it.reset();
if (!raxNext(&it_)) {
raxStop(&it_);
it_.node = nullptr;
}
return *this;
}

std::pair<std::string_view, V&> operator*() const {
return {std::string_view{reinterpret_cast<const char*>(it->key), it->key_len},
*reinterpret_cast<V*>(it->data)};
return {std::string_view{reinterpret_cast<const char*>(it_.key), it_.key_len},
*reinterpret_cast<V*>(it_.data)};
}

private:
std::optional<raxIterator> it;
raxIterator it_;
};

// Result of find() call. Inherits from pair to mimic iterator interface, not incrementable.
struct FindIterator : public std::optional<std::pair<std::string_view, V&>> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a comment that we use optional here to get "->" operators for free.

bool operator==(const SeekIterator& rhs) const {
if (this->has_value() != rhs.it.has_value())
if (this->has_value() != !bool(rhs.it_.flags & RAX_ITER_EOF))
return false;
if (!this->has_value())
return true;
return (*this)->first ==
std::string_view{reinterpret_cast<const char*>(rhs.it->key), rhs.it->key_len};
std::string_view{reinterpret_cast<const char*>(rhs.it_.key), rhs.it_.key_len};
}

bool operator!=(const SeekIterator& rhs) const {
Expand All @@ -83,7 +87,7 @@ template <typename V> struct RaxTreeMap {
};

public:
explicit RaxTreeMap(std::pmr::memory_resource* mr) : tree_(raxNew()), mr_(mr) {
explicit RaxTreeMap(PMR_NS::memory_resource* mr) : tree_(raxNew()), mr_(mr) {
}

size_t size() const {
Expand Down Expand Up @@ -124,7 +128,7 @@ template <typename V> struct RaxTreeMap {
}

rax* tree_;
std::pmr::memory_resource* mr_;
PMR_NS::memory_resource* mr_;
};

template <typename V>
Expand Down
7 changes: 7 additions & 0 deletions src/core/search/rax_tree_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <memory_resource>

#include "base/gtest.h"
#include "base/iterator.h"
#include "base/logging.h"

extern "C" {
Expand Down Expand Up @@ -74,6 +75,12 @@ TEST_F(RaxTreeTest, LowerBound) {

EXPECT_TRUE(it1 == map.end());
EXPECT_TRUE(it2 == keys.end());

// Test lower bound empty string
vector<string> keys2;
for (auto it = map.lower_bound(""); it != map.end(); ++it)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not enough.
"" actually is non-null string_view. I am talking about string_view{}.
Also the same with find and insert (empty string)

keys2.emplace_back((*it).first);
EXPECT_EQ(keys, keys2);
}

TEST_F(RaxTreeTest, Find) {
Expand Down
Loading