-
Notifications
You must be signed in to change notification settings - Fork 981
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
Conversation
src/core/search/rax_tree.h
Outdated
template <typename... Args> | ||
std::pair<FindIterator, bool> try_emplace(std::string_view key, Args&&... args) { | ||
if (auto it = find(key); it) | ||
return {it, false}; | ||
|
||
void* ptr = mr_->allocate(sizeof(V), alignof(V)); | ||
V* data = new (ptr) V(std::forward<Args>(args)...); | ||
|
||
V* old = nullptr; | ||
raxInsert(tree_, to_key_ptr(key), key.size(), data, reinterpret_cast<void**>(&old)); | ||
assert(old == nullptr); | ||
|
||
auto it = std::make_optional(std::make_pair(key, data)); | ||
return std::make_pair(FindIterator{it}, true); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's so ugly 😭
Signed-off-by: Vladislav Oleshko <vlad@dragonflydb.io>
6e7bdbb
to
715049f
Compare
I will replace the built-in absl::flat_hash_map with this data structure and we'll have prefix searches automatically, for suffix/infix searches we'll build a suffix tree on top of this struct |
Forgot to mention, properly using polymorphic allocators is a pain because we use redis/zmalloc internally 😕 Not sure what to do about it 🤷🏻♂️ |
Ah, I forgot our PMR_NS namespace 😅 Will fix it |
std::optional<raxIterator> it; | ||
}; | ||
|
||
struct FindIterator : public std::optional<std::pair<std::string_view, V&>> { |
There was a problem hiding this comment.
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.
} | ||
} | ||
|
||
TEST_F(RaxTreeTest, LowerBound) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check how it works with empty string_views, strings. Sometimes redis code has problems with zero length slices that have nullptr pointers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Signed-off-by: Vladislav Oleshko <vlad@dragonflydb.io>
0f3071e
to
93ea774
Compare
src/core/search/rax_tree_test.cc
Outdated
|
||
// Test lower bound empty string | ||
vector<string> keys2; | ||
for (auto it = map.lower_bound(""); it != map.end(); ++it) |
There was a problem hiding this comment.
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)
Signed-off-by: Vladislav Oleshko <vlad@dragonflydb.io>
38bac9d
to
ebf5238
Compare
Self contained RaxTreeMap that hides rax functions + allocations