Skip to content

Commit bbdac60

Browse files
committed
feat(server): Add support for command aliasing
A command can be aliased by supplying the flag: --command_alias=__PING=PING to the server startup. A map of alias is retained. When finding a command id, first we look up into the original command map, then if not found there we lookup through the command alias map. This results in two find operations for an aliased command. Signed-off-by: Abhijat Malviya <[email protected]>
1 parent fffd079 commit bbdac60

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/server/command_registry.cc

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ using namespace std;
2222
ABSL_FLAG(vector<string>, rename_command, {},
2323
"Change the name of commands, format is: <cmd1_name>=<cmd1_new_name>, "
2424
"<cmd2_name>=<cmd2_new_name>");
25+
ABSL_FLAG(vector<string>, command_alias, {},
26+
"Add an alias for given commands, format is: <alias>=<original>, "
27+
"<alias>=<original>");
2528
ABSL_FLAG(vector<string>, restricted_commands, {},
2629
"Commands restricted to connections on the admin port");
2730

@@ -171,6 +174,7 @@ optional<facade::ErrorReply> CommandId::Validate(CmdArgList tail_args) const {
171174

172175
CommandRegistry::CommandRegistry() {
173176
cmd_rename_map_ = ParseCmdlineArgMap(FLAGS_rename_command);
177+
cmd_aliases_ = ParseCmdlineArgMap(FLAGS_command_alias, true);
174178

175179
for (string name : GetFlag(FLAGS_restricted_commands)) {
176180
restricted_cmds_.emplace(AsciiStrToUpper(name));

src/server/command_registry.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,15 @@ class CommandRegistry {
169169
CommandRegistry& operator<<(CommandId cmd);
170170

171171
const CommandId* Find(std::string_view cmd) const {
172-
auto it = cmd_map_.find(cmd);
172+
const auto it = cmd_map_.find(cmd);
173+
if (it == cmd_map_.end()) {
174+
if (const auto alias_it = cmd_aliases_.find(cmd); alias_it != cmd_aliases_.end()) {
175+
if (alias_it->first == alias_it->second) {
176+
return nullptr;
177+
}
178+
return Find(alias_it->second);
179+
}
180+
}
173181
return it == cmd_map_.end() ? nullptr : &it->second;
174182
}
175183

@@ -215,6 +223,11 @@ class CommandRegistry {
215223
private:
216224
absl::flat_hash_map<std::string, CommandId> cmd_map_;
217225
absl::flat_hash_map<std::string, std::string> cmd_rename_map_;
226+
// Stores a mapping from alias to original command. During the find operation, the first lookup is
227+
// done in the cmd_map_, then in the alias map. This results in two lookups but only for commands
228+
// which are not in original map, ie either typos or aliases. While it would be faster, we cannot
229+
// store iterators into cmd_map_ here as they may be invalidated on rehashing.
230+
absl::flat_hash_map<std::string, std::string> cmd_aliases_;
218231
absl::flat_hash_set<std::string> restricted_cmds_;
219232
absl::flat_hash_set<std::string> oomdeny_cmds_;
220233

0 commit comments

Comments
 (0)