summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAmirsaman Memaripour <amirsaman.memaripour@10gen.com>2020-01-23 17:14:41 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-08-28 20:38:39 +0000
commit4e0bb24f64db02c8dde9967c9571e08fb16bcec3 (patch)
treeebebd2c9045bed040b9972603e1f6db6a2f3a112 /src
parentbfecd6ba7c6bd490c19a7bc6bd0e97b577711a6b (diff)
downloadmongo-4e0bb24f64db02c8dde9967c9571e08fb16bcec3.tar.gz
SERVER-45202 Improve command alias infrastructurer3.6.20-rc1
(cherry picked from commit 3cd3a6da3fe0b6f022b721094bda0b97c3527d23)
Diffstat (limited to 'src')
-rw-r--r--src/mongo/db/commands.cpp14
-rw-r--r--src/mongo/db/commands.h18
2 files changed, 27 insertions, 5 deletions
diff --git a/src/mongo/db/commands.cpp b/src/mongo/db/commands.cpp
index f7aa71e9184..9f3afeda2a4 100644
--- a/src/mongo/db/commands.cpp
+++ b/src/mongo/db/commands.cpp
@@ -186,7 +186,7 @@ ResourcePattern Command::parseResourcePattern(const std::string& dbname,
return ResourcePattern::forExactNamespace(NamespaceString(ns));
}
-Command::Command(StringData name, StringData oldName)
+Command::Command(StringData name, std::vector<StringData> aliases)
: _name(name.toString()),
_commandsExecutedMetric("commands." + _name + ".total", &_commandsExecuted),
_commandsFailedMetric("commands." + _name + ".failed", &_commandsFailed) {
@@ -201,8 +201,12 @@ Command::Command(StringData name, StringData oldName)
c = this;
(*_commandsByBestName)[name] = this;
- if (!oldName.empty())
- (*_commands)[oldName.toString()] = this;
+ for (auto key : aliases) {
+ if (key.empty()) {
+ continue;
+ }
+ (*_commands)[key.toString()] = this;
+ }
}
void Command::help(stringstream& help) const {
@@ -388,6 +392,10 @@ void Command::generateHelpResponse(OperationContext* opCtx,
replyBuilder->setMetadata(rpc::makeEmptyMetadata());
}
+bool Command::hasAlias(const StringData& alias) const {
+ return findCommand(alias) == this;
+}
+
namespace {
const stdx::unordered_set<std::string> userManagementCommands{"createUser",
"updateUser",
diff --git a/src/mongo/db/commands.h b/src/mongo/db/commands.h
index 779500a8a12..23c8f0e23e4 100644
--- a/src/mongo/db/commands.h
+++ b/src/mongo/db/commands.h
@@ -268,9 +268,15 @@ public:
* Constructs a new command and causes it to be registered with the global commands list. It is
* not safe to construct commands other than when the server is starting up.
*
- * @param oldName an optional old, deprecated name for the command
+ * @param oldName an old, deprecated name for the command
*/
- Command(StringData name, StringData oldName = StringData());
+ Command(StringData name, StringData oldName)
+ : Command(name, std::vector<StringData>({oldName})) {}
+
+ /**
+ * @param aliases the optional list of aliases (e.g., old names) for the command
+ */
+ Command(StringData name, std::vector<StringData> aliases = {});
// NOTE: Do not remove this declaration, or relocate it in this class. We
// are using this method to control where the vtable is emitted.
@@ -373,6 +379,11 @@ public:
static Counter64 unknownCommands;
/**
+ * Checks if the command is also known by the provided alias.
+ */
+ bool hasAlias(const StringData& alias) const;
+
+ /**
* Runs a command directly and returns the result. Does not do any other work normally handled
* by command dispatch, such as checking auth, dealing with CurOp or waiting for write concern.
* It is illegal to call this if the command does not exist.
@@ -551,6 +562,9 @@ private:
// The full name of the command
const std::string _name;
+ // The list of aliases for the command
+ const std::vector<StringData> _aliases;
+
// Pointers to hold the metrics tree references
ServerStatusMetricField<Counter64> _commandsExecutedMetric;
ServerStatusMetricField<Counter64> _commandsFailedMetric;