diff options
author | Adam Midvidy <amidvidy@gmail.com> | 2015-10-22 10:32:47 -0400 |
---|---|---|
committer | Adam Midvidy <amidvidy@gmail.com> | 2015-10-30 15:14:06 -0400 |
commit | e62e2e71eff397caf22a0da13ac4669a8546b298 (patch) | |
tree | d170510044130c0c8e34950387b4afac957f097b /src/mongo/db | |
parent | cbc6a790dc4ac537b071626dff77d0073512690c (diff) | |
download | mongo-e62e2e71eff397caf22a0da13ac4669a8546b298.tar.gz |
SERVER-20609 use getFields instead of calling getField 4x
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/commands.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/commands.h | 4 | ||||
-rw-r--r-- | src/mongo/db/dbcommands.cpp | 33 | ||||
-rw-r--r-- | src/mongo/db/pipeline/pipeline.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/query/lite_parsed_query.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/query/lite_parsed_query.h | 9 | ||||
-rw-r--r-- | src/mongo/db/s/operation_shard_version.cpp | 18 | ||||
-rw-r--r-- | src/mongo/db/s/operation_shard_version.h | 3 |
8 files changed, 59 insertions, 22 deletions
diff --git a/src/mongo/db/commands.cpp b/src/mongo/db/commands.cpp index 05f77c86615..fa601fae613 100644 --- a/src/mongo/db/commands.cpp +++ b/src/mongo/db/commands.cpp @@ -383,10 +383,12 @@ Status Command::_checkAuthorization(Command* c, return status; } -bool Command::isHelpRequest(const rpc::RequestInterface& request) { - return request.getCommandArgs()["help"].trueValue(); +bool Command::isHelpRequest(const BSONElement& helpElem) { + return !helpElem.eoo() && helpElem.trueValue(); } +const char Command::kHelpFieldName[] = "help"; + void Command::generateHelpResponse(OperationContext* txn, const rpc::RequestInterface& request, rpc::ReplyBuilderInterface* replyBuilder, diff --git a/src/mongo/db/commands.h b/src/mongo/db/commands.h index f2daf4fe5b4..e835b73c959 100644 --- a/src/mongo/db/commands.h +++ b/src/mongo/db/commands.h @@ -393,7 +393,9 @@ public: /** * Returns true if this a request for the 'help' information associated with the command. */ - static bool isHelpRequest(const rpc::RequestInterface& request); + static bool isHelpRequest(const BSONElement& helpElem); + + static const char kHelpFieldName[]; /** * Generates a reply from the 'help' information associated with a command. The state of diff --git a/src/mongo/db/dbcommands.cpp b/src/mongo/db/dbcommands.cpp index 6019131d55d..d9bba0008ee 100644 --- a/src/mongo/db/dbcommands.cpp +++ b/src/mongo/db/dbcommands.cpp @@ -30,6 +30,7 @@ #include "mongo/platform/basic.h" +#include <array> #include <boost/optional.hpp> #include <time.h> @@ -1161,6 +1162,22 @@ private: bool maintenanceModeSet; }; +namespace { + +// Symbolic names for indexes to make code more readable. +const std::size_t kCmdOptionMaxTimeMSField = 0; +const std::size_t kHelpField = 1; +const std::size_t kShardVersionField = 2; +const std::size_t kQueryOptionMaxTimeMSField = 3; + +// We make an array of the fields we need so we can call getFields once. This saves repeated +// scans over the command object. +const std::array<StringData, 4> neededFieldNames{LiteParsedQuery::cmdOptionMaxTimeMS, + Command::kHelpFieldName, + OperationShardVersion::fieldName(), + LiteParsedQuery::queryOptionMaxTimeMS}; +} // namespace + /** * this handles - auth @@ -1188,7 +1205,12 @@ void Command::execCommand(OperationContext* txn, std::string dbname = request.getDatabase().toString(); unique_ptr<MaintenanceModeSetter> mmSetter; - if (isHelpRequest(request)) { + + std::array<BSONElement, std::tuple_size<decltype(neededFieldNames)>::value> + extractedFields{}; + request.getCommandArgs().getFields(neededFieldNames, &extractedFields); + + if (isHelpRequest(extractedFields[kHelpField])) { CurOp::get(txn)->ensureStarted(); generateHelpResponse(txn, request, replyBuilder, *command); return; @@ -1241,12 +1263,12 @@ void Command::execCommand(OperationContext* txn, } // Handle command option maxTimeMS. - int maxTimeMS = - uassertStatusOK(LiteParsedQuery::parseMaxTimeMSCommand(request.getCommandArgs())); + int maxTimeMS = uassertStatusOK( + LiteParsedQuery::parseMaxTimeMS(extractedFields[kCmdOptionMaxTimeMSField])); uassert(ErrorCodes::InvalidOptions, "no such command option $maxTimeMs; use maxTimeMS instead", - !request.getCommandArgs().hasField("$maxTimeMS")); + extractedFields[kQueryOptionMaxTimeMSField].eoo()); CurOp::get(txn)->setMaxTimeMicros(static_cast<unsigned long long>(maxTimeMS) * 1000); @@ -1259,7 +1281,8 @@ void Command::execCommand(OperationContext* txn, invariant(!operationShardVersion.hasShardVersion()); auto commandNS = NamespaceString(command->parseNs(dbname, request.getCommandArgs())); - operationShardVersion.initializeFromCommand(commandNS, request.getCommandArgs()); + operationShardVersion.initializeFromCommand(commandNS, + extractedFields[kShardVersionField]); auto shardingState = ShardingState::get(txn); if (shardingState->enabled()) { diff --git a/src/mongo/db/pipeline/pipeline.cpp b/src/mongo/db/pipeline/pipeline.cpp index eb6a45b2b75..e237f35a61e 100644 --- a/src/mongo/db/pipeline/pipeline.cpp +++ b/src/mongo/db/pipeline/pipeline.cpp @@ -82,7 +82,7 @@ intrusive_ptr<Pipeline> Pipeline::parseCommand(string& errmsg, } // maxTimeMS is also for the command processor. - if (pFieldName == LiteParsedQuery::cmdOptionMaxTimeMS) { + if (str::equals(pFieldName, LiteParsedQuery::cmdOptionMaxTimeMS)) { continue; } diff --git a/src/mongo/db/query/lite_parsed_query.cpp b/src/mongo/db/query/lite_parsed_query.cpp index 90eed61ff95..b86b0945363 100644 --- a/src/mongo/db/query/lite_parsed_query.cpp +++ b/src/mongo/db/query/lite_parsed_query.cpp @@ -47,8 +47,8 @@ using std::unique_ptr; const std::string LiteParsedQuery::kUnwrappedReadPrefField("$queryOptions"); const std::string LiteParsedQuery::kWrappedReadPrefField("$readPreference"); -const string LiteParsedQuery::cmdOptionMaxTimeMS("maxTimeMS"); -const string LiteParsedQuery::queryOptionMaxTimeMS("$maxTimeMS"); +const char LiteParsedQuery::cmdOptionMaxTimeMS[] = "maxTimeMS"; +const char LiteParsedQuery::queryOptionMaxTimeMS[] = "$maxTimeMS"; const string LiteParsedQuery::metaGeoNearDistance("geoNearDistance"); const string LiteParsedQuery::metaGeoNearPoint("geoNearPoint"); @@ -258,7 +258,7 @@ StatusWith<unique_ptr<LiteParsedQuery>> LiteParsedQuery::makeFromFindCommand(Nam } pq->_maxScan = maxScan; - } else if (str::equals(fieldName, cmdOptionMaxTimeMS.c_str())) { + } else if (str::equals(fieldName, cmdOptionMaxTimeMS)) { StatusWith<int> maxTimeMS = parseMaxTimeMS(el); if (!maxTimeMS.isOK()) { return maxTimeMS.getStatus(); diff --git a/src/mongo/db/query/lite_parsed_query.h b/src/mongo/db/query/lite_parsed_query.h index 1725123e749..af139ef1a7d 100644 --- a/src/mongo/db/query/lite_parsed_query.h +++ b/src/mongo/db/query/lite_parsed_query.h @@ -161,8 +161,9 @@ public: static const std::string kUnwrappedReadPrefField; // Names of the maxTimeMS command and query option. - static const std::string cmdOptionMaxTimeMS; - static const std::string queryOptionMaxTimeMS; + // Char arrays because they are used in static initialization. + static const char cmdOptionMaxTimeMS[]; + static const char queryOptionMaxTimeMS[]; // Names of the $meta projection values. static const std::string metaGeoNearDistance; @@ -297,6 +298,8 @@ public: static StatusWith<std::unique_ptr<LiteParsedQuery>> fromLegacyQueryMessage( const QueryMessage& qm); + static StatusWith<int> parseMaxTimeMS(const BSONElement& maxTimeMSElt); + private: LiteParsedQuery(NamespaceString nss); @@ -315,8 +318,6 @@ private: Status initFullQuery(const BSONObj& top); - static StatusWith<int> parseMaxTimeMS(const BSONElement& maxTimeMSElt); - /** * Updates the projection object with a $meta projection for the returnKey option. */ diff --git a/src/mongo/db/s/operation_shard_version.cpp b/src/mongo/db/s/operation_shard_version.cpp index d51ae7d9cb8..5f47d80ef4f 100644 --- a/src/mongo/db/s/operation_shard_version.cpp +++ b/src/mongo/db/s/operation_shard_version.cpp @@ -39,7 +39,7 @@ namespace { const OperationContext::Decoration<OperationShardVersion> shardingMetadataDecoration = OperationContext::declareDecoration<OperationShardVersion>(); -const char* kShardVersionField = "shardVersion"; +const char kShardVersionField[] = "shardVersion"; const ChunkVersion kUnshardedVersion(ChunkVersion::UNSHARDED()); } // namespace mongo @@ -50,26 +50,32 @@ OperationShardVersion& OperationShardVersion::get(OperationContext* txn) { return shardingMetadataDecoration(txn); } +StringData OperationShardVersion::fieldName() { + return kShardVersionField; +} + void OperationShardVersion::initializeFromCommand(NamespaceString ns, const BSONObj& cmdObj) { + initializeFromCommand(std::move(ns), cmdObj[fieldName()]); +} + +void OperationShardVersion::initializeFromCommand(NamespaceString ns, + const BSONElement& shardVersionElt) { if (ns.isSystemDotIndexes()) { setShardVersion(std::move(ns), ChunkVersion::IGNORED()); return; } - BSONElement versionElt; - Status status = bsonExtractTypedField(cmdObj, kShardVersionField, BSONType::Array, &versionElt); - if (!status.isOK()) { + if (shardVersionElt.eoo() || shardVersionElt.type() != BSONType::Array) { return; } - const BSONArray versionArr(versionElt.Obj()); + const BSONArray versionArr(shardVersionElt.Obj()); bool hasVersion = false; ChunkVersion newVersion = ChunkVersion::fromBSON(versionArr, &hasVersion); if (!hasVersion) { return; } - setShardVersion(std::move(ns), std::move(newVersion)); } diff --git a/src/mongo/db/s/operation_shard_version.h b/src/mongo/db/s/operation_shard_version.h index 33021dcdbae..1ec6e2bc1c1 100644 --- a/src/mongo/db/s/operation_shard_version.h +++ b/src/mongo/db/s/operation_shard_version.h @@ -53,6 +53,8 @@ public: OperationShardVersion(); + static StringData fieldName(); + /** * Retrieves a reference to the shard version decorating the OperationContext, 'txn'. */ @@ -66,6 +68,7 @@ public: * Expects the format { ..., shardVersion: [<version>, <epoch>] }. */ void initializeFromCommand(NamespaceString ns, const BSONObj& cmdObj); + void initializeFromCommand(NamespaceString ns, const BSONElement& shardVersionElement); /** * Returns whether or not there is a shard version associated with this operation. |