diff options
author | Billy Donahue <billy.donahue@mongodb.com> | 2018-03-26 17:03:57 -0400 |
---|---|---|
committer | Billy Donahue <billy.donahue@mongodb.com> | 2018-04-13 14:13:27 -0400 |
commit | 64f24ed205a4d83aece37c5f2c64af26624113c1 (patch) | |
tree | dba4eb4288562a5b75ab4201390f500bf665bc38 /src/mongo/db/commands.h | |
parent | 431ebb76fb1dfd296afd913f4d83b0387c89e992 (diff) | |
download | mongo-64f24ed205a4d83aece37c5f2c64af26624113c1.tar.gz |
SERVER-34148 TypedCommand
Diffstat (limited to 'src/mongo/db/commands.h')
-rw-r--r-- | src/mongo/db/commands.h | 198 |
1 files changed, 195 insertions, 3 deletions
diff --git a/src/mongo/db/commands.h b/src/mongo/db/commands.h index b01d366d866..a1708a1f733 100644 --- a/src/mongo/db/commands.h +++ b/src/mongo/db/commands.h @@ -402,6 +402,50 @@ public: void reset(); + /** + * Appends a key:object field to this reply. + */ + template <typename T> + void append(StringData key, const T& object) { + getBodyBuilder() << key << object; + } + + /** + * Write the specified 'status' and associated fields into this reply body, as with + * CommandHelpers::appendCommandStatus. Appends the "ok" and related fields if they + * haven't already been set. + * - If 'status' is not OK, this reply is reset before adding result. + * - Otherwise, any data previously written to the body is left in place. + */ + void fillFrom(const Status& status); + + /** + * The specified 'object' must be BSON-serializable. + * Appends the "ok" and related fields if they haven't already been set. + * + * BSONSerializable 'x' means 'x.serialize(bob)' appends a representation of 'x' + * into 'BSONObjBuilder* bob'. + */ + template <typename T> + void fillFrom(const T& object) { + auto bob = getBodyBuilder(); + object.serialize(&bob); + CommandHelpers::appendCommandStatus(bob, Status::OK()); + } + + /** + * Equivalent to calling fillFrom with sw.getValue() or sw.getStatus(), whichever + * 'sw' is holding. + */ + template <typename T> + void fillFrom(const StatusWith<T>& sw) { + if (sw.isOK()) { + fillFrom(sw.getValue()); + } else { + fillFrom(sw.getStatus()); + } + } + private: BufBuilder* const _bodyBuf; const std::size_t _bodyOffset; @@ -427,7 +471,10 @@ public: virtual void explain(OperationContext* opCtx, ExplainOptions::Verbosity verbosity, - BSONObjBuilder* result) = 0; + BSONObjBuilder* result) { + uasserted(ErrorCodes::IllegalOperation, + str::stream() << "Cannot explain cmd: " << definition()->getName()); + } /** * The primary namespace on which this command operates. May just be the db. @@ -630,7 +677,150 @@ class ErrmsgCommandDeprecated : public BasicCommand { BSONObjBuilder& result) = 0; }; -// See the 'globalCommandRegistry()' singleton accessor. +/** + * A CRTP base class for typed commands, which simplifies writing commands that + * accept requests generated by IDL. Derive from it as follows: + * + * class MyCommand : public TypedCommand<MyCommand> {...}; + * + * The 'Derived' type paramter must have: + * + * - 'Request' naming a usable request type. + * A usable Request type must have: + * + * - a static member factory function 'parse', callable as: + * + * const IDLParserErrorContext& idlCtx = ...; + * const OpMsgRequest& opMsgRequest = ...; + * Request r = Request::parse(idlCtx, opMsgRequest); + * + * which enables it to be parsed as an IDL command. + * + * - a 'constexpr StringData kCommandName' member. + * + * Any type generated by the "commands:" section in the IDL syntax meets these + * requirements. Note that IDL "structs:" will not. This is the recommended way to + * provide this Derived::Request type rather than writing it by hand. + * + * - 'Invocation' - names a type derived from either of the nested invocation + * base classes provided: InvocationBase or MinimalInvocationBase. + */ +template <typename Derived> +class TypedCommand : public Command { +public: + std::unique_ptr<CommandInvocation> parse(OperationContext* opCtx, + const OpMsgRequest& opMsgRequest) final; + +protected: + class InvocationBase; + + // Used instead of InvocationBase when a command must customize the 'run()' member. + class MinimalInvocationBase; + + // Commands that only have a single name don't need to define any constructors. + TypedCommand() : TypedCommand(Derived::Request::kCommandName) {} + explicit TypedCommand(StringData name) : TypedCommand(name, {}) {} + TypedCommand(StringData name, StringData altName) : Command(name, altName) {} + +private: + class InvocationBaseInternal; +}; + +template <typename Derived> +class TypedCommand<Derived>::InvocationBaseInternal : public CommandInvocation { +public: + using RequestType = typename Derived::Request; + + InvocationBaseInternal(OperationContext*, + const Command* command, + const OpMsgRequest& opMsgRequest) + : CommandInvocation(command), _request{_parseRequest(command->getName(), opMsgRequest)} {} + +protected: + const RequestType& request() const { + return _request; + } + +private: + static RequestType _parseRequest(StringData name, const OpMsgRequest& opMsgRequest) { + return RequestType::parse(IDLParserErrorContext(name), opMsgRequest); + } + + RequestType _request; +}; + +template <typename Derived> +class TypedCommand<Derived>::MinimalInvocationBase : public InvocationBaseInternal { + // Implemented as just a strong typedef for InvocationBaseInternal. + using InvocationBaseInternal::InvocationBaseInternal; +}; + +/* + * Classes derived from TypedCommand::InvocationBase must: + * + * - inherit constructors with 'using InvocationBase::InvocationBase;'. + * + * - define a 'typedRun' method like: + * + * R typedRun(OperationContext* opCtx); + * + * where R is either void or usable as an argument to 'CommandReplyBuilder::fillFrom'. + * So it's one of: + * - void + * - mongo::Status + * - T, where T is usable with fillFrom. + * - mongo::StatusWith<T>, where T usable with fillFrom. + * + * Note: a void typedRun produces a "pass-fail" command. If it runs to completion + * the result will be considered and formatted as an "ok". + * + * If the TypedCommand's Request type was specified with the IDL attribute: + * + * namespace: concatenate_with_db + * + * then the ns() method of its Invocation class method should be: + * + * NamespaceString ns() const override { + * return request.getNamespace(); + * } + */ +template <typename Derived> +class TypedCommand<Derived>::InvocationBase : public InvocationBaseInternal { +public: + using InvocationBaseInternal::InvocationBaseInternal; + +private: + using Invocation = typename Derived::Invocation; + + /** + * _callTypedRun and _runImpl implement the tagged dispatch from 'run'. + */ + decltype(auto) _callTypedRun(OperationContext* opCtx) { + return static_cast<Invocation*>(this)->typedRun(opCtx); + } + void _runImpl(std::true_type, OperationContext* opCtx, CommandReplyBuilder*) { + _callTypedRun(opCtx); + } + void _runImpl(std::false_type, OperationContext* opCtx, CommandReplyBuilder* reply) { + reply->fillFrom(_callTypedRun(opCtx)); + } + + void run(OperationContext* opCtx, CommandReplyBuilder* reply) final { + using VoidResultTag = std::is_void<decltype(_callTypedRun(opCtx))>; + _runImpl(VoidResultTag{}, opCtx, reply); + } +}; + +template <typename Derived> +std::unique_ptr<CommandInvocation> TypedCommand<Derived>::parse(OperationContext* opCtx, + const OpMsgRequest& opMsgRequest) { + return std::make_unique<typename Derived::Invocation>(opCtx, this, opMsgRequest); +} + + +/** + * See the 'globalCommandRegistry()' singleton accessor. + */ class CommandRegistry { public: using CommandMap = Command::CommandMap; @@ -659,7 +849,9 @@ private: CommandMap _commands; }; -// Accessor to the command registry, an always-valid singleton. +/** + * Accessor to the command registry, an always-valid singleton. + */ CommandRegistry* globalCommandRegistry(); } // namespace mongo |