-
Notifications
You must be signed in to change notification settings - Fork 968
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
fix: acl compatibility #3147
fix: acl compatibility #3147
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,11 @@ | |
#pragma once | ||
|
||
#include "absl/container/flat_hash_map.h" | ||
#include "absl/container/flat_hash_set.h" | ||
#include "base/logging.h" | ||
#include "facade/acl_commands_def.h" | ||
#include "server/command_registry.h" | ||
#include "server/conn_context.h" | ||
|
||
namespace dfly::acl { | ||
|
||
|
@@ -84,6 +88,15 @@ inline const std::vector<std::string> REVERSE_CATEGORY_INDEX_TABLE{ | |
"_RESERVED", "_RESERVED", "_RESERVED", "_RESERVED", "_RESERVED", "_RESERVED", "_RESERVED", | ||
"BLOOM", "FT_SEARCH", "THROTTLE", "JSON"}; | ||
|
||
// bit index to index in the REVERSE_CATEGORY_INDEX_TABLE | ||
using CategoryToIdxStore = absl::flat_hash_map<uint32_t, uint32_t>; | ||
|
||
// inline const CategoryToIdxStore& CategoryToIdx(CategoryToIdxStore store = {}) { | ||
inline const CategoryToIdxStore& CategoryToIdx(CategoryToIdxStore store = {}) { | ||
static CategoryToIdxStore cat_idx = std::move(store); | ||
return cat_idx; | ||
} | ||
|
||
using RevCommandField = std::vector<std::string>; | ||
using RevCommandsIndexStore = std::vector<RevCommandField>; | ||
|
||
|
@@ -104,9 +117,39 @@ inline const RevCommandsIndexStore& CommandsRevIndexer(RevCommandsIndexStore sto | |
return rev_index_store; | ||
} | ||
|
||
inline void BuildIndexers(std::vector<std::vector<std::string>> families) { | ||
using CategoryToCommandsIndexStore = absl::flat_hash_map<std::string, std::vector<uint64_t>>; | ||
|
||
inline const CategoryToCommandsIndexStore& CategoryToCommandsIndex( | ||
CategoryToCommandsIndexStore store = {}) { | ||
static CategoryToCommandsIndexStore index = std::move(store); | ||
return index; | ||
} | ||
|
||
inline void BuildIndexers(RevCommandsIndexStore families, CommandRegistry* cmd_registry) { | ||
acl::NumberOfFamilies(families.size()); | ||
acl::CommandsRevIndexer(std::move(families)); | ||
CategoryToCommandsIndexStore index; | ||
cmd_registry->Traverse([&](std::string_view name, auto& cid) { | ||
auto cat = cid.acl_categories(); | ||
for (size_t i = 0; i < 32; ++i) { | ||
if (cat & (1 << i)) { | ||
std::string_view cat_name = REVERSE_CATEGORY_INDEX_TABLE[i]; | ||
if (index[cat_name].empty()) { | ||
index[cat_name].resize(CommandsRevIndexer().size()); | ||
} | ||
auto family = cid.GetFamily(); | ||
auto bit_index = cid.GetBitIndex(); | ||
index[cat_name][family] = index[cat_name][family] | bit_index; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's just a index[cid.GetBidIndex()][cid.GetFamily()] |= bit_index; |
||
} | ||
} | ||
}); | ||
|
||
CategoryToCommandsIndex(std::move(index)); | ||
CategoryToIdxStore idx_store; | ||
for (size_t i = 0; i < 32; ++i) { | ||
idx_store[1 << i] = i; | ||
} | ||
Comment on lines
+147
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could there be any reason there's not a 1-to-1 mapping? If not, we could elminiate this store allotogether There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
CategoryToIdx(std::move(idx_store)); | ||
} | ||
|
||
} // namespace dfly::acl |
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.
I just had a cool thought that we know this stuff at compiletime, so we could make a alias to a bitset<sizeof(categories)> that's easier to manage than an int flag 😎
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.
We just normalize here. Had we replaced the
flat_hash_map
with that we would loose the index we map to ( CategoryToIdxStore just maps the category, e.g. the mask with an index that can be used to find its name). On the other hand if we replace thekey
,uint32_t
withstd::bitset
it won't really make a difference since we don't manipulate any of the individual bits.Maybe I am missing something so let me know :)