diff options
author | Katherine Wu <katherine.wu@mongodb.com> | 2020-04-07 12:55:14 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-04-16 16:41:37 +0000 |
commit | 2b58a77d1d353d6727b02f145a93c41f2ee55812 (patch) | |
tree | 8ec55083480f467523d43e45ba2b897476504279 | |
parent | 5a3b48b31445888d06b593055ffcd13fa0d701b4 (diff) | |
download | mongo-2b58a77d1d353d6727b02f145a93c41f2ee55812.tar.gz |
SERVER-46772 Use IDL to represent UpdateRequest
-rw-r--r-- | src/mongo/db/commands/find_and_modify.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/commands/write_commands/write_commands.cpp | 12 | ||||
-rw-r--r-- | src/mongo/db/dbhelpers.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/ops/update_request.h | 164 | ||||
-rw-r--r-- | src/mongo/db/ops/write_ops_exec.cpp | 13 | ||||
-rw-r--r-- | src/mongo/db/repl/oplog.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/repl/rs_rollback.cpp | 3 | ||||
-rw-r--r-- | src/mongo/db/repl/storage_interface_impl.cpp | 9 | ||||
-rw-r--r-- | src/mongo/db/s/sharding_initialization_mongod.cpp | 3 | ||||
-rw-r--r-- | src/mongo/db/s/sharding_state_recovery.cpp | 3 | ||||
-rw-r--r-- | src/mongo/db/transaction_participant.cpp | 3 | ||||
-rw-r--r-- | src/mongo/dbtests/query_stage_update.cpp | 12 |
12 files changed, 110 insertions, 130 deletions
diff --git a/src/mongo/db/commands/find_and_modify.cpp b/src/mongo/db/commands/find_and_modify.cpp index c0af48bc376..24cdf5f0c97 100644 --- a/src/mongo/db/commands/find_and_modify.cpp +++ b/src/mongo/db/commands/find_and_modify.cpp @@ -284,7 +284,8 @@ public: auto bodyBuilder = result->getBodyBuilder(); Explain::explainStages(exec.get(), collection, verbosity, BSONObj(), &bodyBuilder); } else { - UpdateRequest request(nsString); + auto request = UpdateRequest(); + request.setNamespaceString(nsString); const bool isExplain = true; makeUpdateRequest(opCtx, args, isExplain, &request); @@ -425,7 +426,8 @@ public: appendCommandResponse(exec.get(), args.isRemove(), docFound, &result); } else { - UpdateRequest request(nsString); + auto request = UpdateRequest(); + request.setNamespaceString(nsString); const bool isExplain = false; makeUpdateRequest(opCtx, args, isExplain, &request); diff --git a/src/mongo/db/commands/write_commands/write_commands.cpp b/src/mongo/db/commands/write_commands/write_commands.cpp index 61f4ee7ace9..2e8d5f7e51d 100644 --- a/src/mongo/db/commands/write_commands/write_commands.cpp +++ b/src/mongo/db/commands/write_commands/write_commands.cpp @@ -413,19 +413,11 @@ private: "explained write batches must be of size 1", _batch.getUpdates().size() == 1); - UpdateRequest updateRequest(_batch.getNamespace()); - updateRequest.setQuery(_batch.getUpdates()[0].getQ()); - updateRequest.setUpdateModification(_batch.getUpdates()[0].getU()); - updateRequest.setUpdateConstants(_batch.getUpdates()[0].getC()); + UpdateRequest updateRequest(_batch.getUpdates()[0]); + updateRequest.setNamespaceString(_batch.getNamespace()); updateRequest.setRuntimeConstants( _batch.getRuntimeConstants().value_or(Variables::generateRuntimeConstants(opCtx))); - updateRequest.setCollation(write_ops::collationOf(_batch.getUpdates()[0])); - updateRequest.setArrayFilters(write_ops::arrayFiltersOf(_batch.getUpdates()[0])); - updateRequest.setMulti(_batch.getUpdates()[0].getMulti()); - updateRequest.setUpsert(_batch.getUpdates()[0].getUpsert()); - updateRequest.setUpsertSuppliedDocument(_batch.getUpdates()[0].getUpsertSupplied()); updateRequest.setYieldPolicy(PlanExecutor::YIELD_AUTO); - updateRequest.setHint(_batch.getUpdates()[0].getHint()); updateRequest.setExplain(); const ExtensionsCallbackReal extensionsCallback(opCtx, diff --git a/src/mongo/db/dbhelpers.cpp b/src/mongo/db/dbhelpers.cpp index ef6c35ffe8b..3e21e04ab45 100644 --- a/src/mongo/db/dbhelpers.cpp +++ b/src/mongo/db/dbhelpers.cpp @@ -242,7 +242,8 @@ void Helpers::upsert(OperationContext* opCtx, OldClientContext context(opCtx, ns); const NamespaceString requestNs(ns); - UpdateRequest request(requestNs); + auto request = UpdateRequest(); + request.setNamespaceString(requestNs); request.setQuery(filter); request.setUpdateModification(updateMod); @@ -257,7 +258,8 @@ void Helpers::putSingleton(OperationContext* opCtx, const char* ns, BSONObj obj) OldClientContext context(opCtx, ns); const NamespaceString requestNs(ns); - UpdateRequest request(requestNs); + auto request = UpdateRequest(); + request.setNamespaceString(requestNs); request.setUpdateModification(obj); request.setUpsert(); diff --git a/src/mongo/db/ops/update_request.h b/src/mongo/db/ops/update_request.h index 5fa0a561b7e..5570f544c3e 100644 --- a/src/mongo/db/ops/update_request.h +++ b/src/mongo/db/ops/update_request.h @@ -33,6 +33,7 @@ #include "mongo/db/jsobj.h" #include "mongo/db/logical_session_id.h" #include "mongo/db/namespace_string.h" +#include "mongo/db/ops/write_ops_gen.h" #include "mongo/db/ops/write_ops_parsers.h" #include "mongo/db/pipeline/runtime_constants_gen.h" #include "mongo/db/query/explain.h" @@ -40,6 +41,11 @@ namespace mongo { +namespace { +const std::vector<BSONObj> emptyArrayFilters{}; +const BSONObj emptyCollation{}; +} // namespace + class FieldRef; class UpdateRequest { @@ -56,80 +62,85 @@ public: RETURN_NEW }; - inline UpdateRequest(const NamespaceString& nsString) : _nsString(nsString) {} + UpdateRequest(const write_ops::UpdateOpEntry& updateOp = write_ops::UpdateOpEntry()) + : _updateOp(updateOp) {} + + void setNamespaceString(const NamespaceString& nsString) { + _nsString = nsString; + } const NamespaceString& getNamespaceString() const { return _nsString; } - inline void setQuery(const BSONObj& query) { - _query = query; + void setQuery(const BSONObj& query) { + _updateOp.setQ(query); } - inline const BSONObj& getQuery() const { - return _query; + const BSONObj& getQuery() const { + return _updateOp.getQ(); } - inline void setProj(const BSONObj& proj) { + void setProj(const BSONObj& proj) { _proj = proj; } - inline const BSONObj& getProj() const { + const BSONObj& getProj() const { return _proj; } - inline void setSort(const BSONObj& sort) { + void setSort(const BSONObj& sort) { _sort = sort; } - inline const BSONObj& getSort() const { + const BSONObj& getSort() const { return _sort; } - inline void setCollation(const BSONObj& collation) { - _collation = collation; + void setCollation(const BSONObj& collation) { + _updateOp.setCollation(collation); } - inline const BSONObj& getCollation() const { - return _collation; + const BSONObj& getCollation() const { + return _updateOp.getCollation().get_value_or(emptyCollation); } - inline void setUpdateModification(const write_ops::UpdateModification& updateMod) { - _updateMod = updateMod; + void setUpdateModification(const write_ops::UpdateModification& updateMod) { + _updateOp.setU(updateMod); } - inline const write_ops::UpdateModification& getUpdateModification() const { - return _updateMod; + const write_ops::UpdateModification& getUpdateModification() const { + return _updateOp.getU(); } - inline void setUpdateConstants(const boost::optional<BSONObj>& updateConstants) { - _updateConstants = updateConstants; + void setUpdateConstants(const boost::optional<BSONObj>& updateConstants) { + _updateOp.setC(updateConstants); } - inline const boost::optional<BSONObj>& getUpdateConstants() const { - return _updateConstants; + const boost::optional<BSONObj>& getUpdateConstants() const { + return _updateOp.getC(); } - inline void setRuntimeConstants(RuntimeConstants runtimeConstants) { + void setRuntimeConstants(RuntimeConstants runtimeConstants) { _runtimeConstants = std::move(runtimeConstants); } - inline const boost::optional<RuntimeConstants>& getRuntimeConstants() const { + const boost::optional<RuntimeConstants>& getRuntimeConstants() const { return _runtimeConstants; } - inline void setArrayFilters(const std::vector<BSONObj>& arrayFilters) { - _arrayFilters = arrayFilters; + void setArrayFilters(const std::vector<BSONObj>& arrayFilters) { + _updateOp.setArrayFilters(arrayFilters); } - inline const std::vector<BSONObj>& getArrayFilters() const { - return _arrayFilters; + const std::vector<BSONObj>& getArrayFilters() const { + return _updateOp.getArrayFilters().get_value_or(emptyArrayFilters); } // Please see documentation on the private members matching these names for // explanations of the following fields. - inline void setGod(bool value = true) { + void setGod(bool value = true) { _god = value; } @@ -137,31 +148,31 @@ public: return _god; } - inline void setUpsert(bool value = true) { - _upsert = value; + void setUpsert(bool value = true) { + _updateOp.setUpsert(value); } bool isUpsert() const { - return _upsert; + return _updateOp.getUpsert(); } - inline void setUpsertSuppliedDocument(bool value = true) { - _upsertSuppliedDocument = value; + void setUpsertSuppliedDocument(bool value = true) { + _updateOp.setUpsertSupplied(value); } bool shouldUpsertSuppliedDocument() const { - return _upsertSuppliedDocument; + return _updateOp.getUpsertSupplied(); } - inline void setMulti(bool value = true) { - _multi = value; + void setMulti(bool value = true) { + _updateOp.setMulti(value); } bool isMulti() const { - return _multi; + return _updateOp.getMulti(); } - inline void setFromMigration(bool value = true) { + void setFromMigration(bool value = true) { _fromMigration = value; } @@ -169,7 +180,7 @@ public: return _fromMigration; } - inline void setFromOplogApplication(bool value = true) { + void setFromOplogApplication(bool value = true) { _fromOplogApplication = value; } @@ -177,66 +188,66 @@ public: return _fromOplogApplication; } - inline void setExplain(bool value = true) { + void setExplain(bool value = true) { _isExplain = value; } - inline bool isExplain() const { + bool isExplain() const { return _isExplain; } - inline void setReturnDocs(ReturnDocOption value) { + void setReturnDocs(ReturnDocOption value) { _returnDocs = value; } void setHint(const BSONObj& hint) { - _hint = hint; + _updateOp.setHint(hint); } BSONObj getHint() const { - return _hint; + return _updateOp.getHint(); } - inline bool shouldReturnOldDocs() const { + bool shouldReturnOldDocs() const { return _returnDocs == ReturnDocOption::RETURN_OLD; } - inline bool shouldReturnNewDocs() const { + bool shouldReturnNewDocs() const { return _returnDocs == ReturnDocOption::RETURN_NEW; } - inline bool shouldReturnAnyDocs() const { + bool shouldReturnAnyDocs() const { return shouldReturnOldDocs() || shouldReturnNewDocs(); } - inline void setYieldPolicy(PlanExecutor::YieldPolicy yieldPolicy) { + void setYieldPolicy(PlanExecutor::YieldPolicy yieldPolicy) { _yieldPolicy = yieldPolicy; } - inline PlanExecutor::YieldPolicy getYieldPolicy() const { + PlanExecutor::YieldPolicy getYieldPolicy() const { return _yieldPolicy; } - inline void setStmtId(StmtId stmtId) { + void setStmtId(StmtId stmtId) { _stmtId = std::move(stmtId); } - inline StmtId getStmtId() const { + StmtId getStmtId() const { return _stmtId; } const std::string toString() const { StringBuilder builder; - builder << " query: " << _query; + builder << " query: " << getQuery(); builder << " projection: " << _proj; builder << " sort: " << _sort; - builder << " collation: " << _collation; - builder << " updateModification: " << _updateMod.toString(); + builder << " collation: " << getCollation(); + builder << " updateModification: " << getUpdateModification().toString(); builder << " stmtId: " << _stmtId; builder << " arrayFilters: ["; bool first = true; - for (auto arrayFilter : _arrayFilters) { + for (auto arrayFilter : getArrayFilters()) { if (!first) { builder << ", "; } @@ -245,8 +256,8 @@ public: } builder << "]"; - if (_updateConstants) { - builder << " updateConstants: " << *_updateConstants; + if (getUpdateConstants()) { + builder << " updateConstants: " << *getUpdateConstants(); } if (_runtimeConstants) { @@ -254,8 +265,8 @@ public: } builder << " god: " << _god; - builder << " upsert: " << _upsert; - builder << " multi: " << _multi; + builder << " upsert: " << isUpsert(); + builder << " multi: " << isMulti(); builder << " fromMigration: " << _fromMigration; builder << " fromOplogApplication: " << _fromOplogApplication; builder << " isExplain: " << _isExplain; @@ -263,15 +274,9 @@ public: } private: - const NamespaceString& _nsString; + NamespaceString _nsString; - // The hint provided, if any. If the hint was by index key pattern, the value of '_hint' is - // the key pattern hinted. If the hint was by index name, the value of '_hint' is - // {$hint: <String>}, where <String> is the index name hinted. - BSONObj _hint; - - // Contains the query that selects documents to update. - BSONObj _query; + write_ops::UpdateOpEntry _updateOp; // Contains the projection information. BSONObj _proj; @@ -279,24 +284,9 @@ private: // Contains the sort order information. BSONObj _sort; - // Contains the collation information. - BSONObj _collation; - - // Contains the modifiers to apply to matched objects, or a replacement document. - write_ops::UpdateModification _updateMod; - - // User-defined constant values to be used with a pipeline-style update. Those are different - // from the '_runtimeConstants' as they can be specified by the user for each individual - // element of the 'updates' array in the 'update' command. The '_runtimeConstants' contains - // runtime system constant values which remain unchanged for all update statements in the - // 'update' command. - boost::optional<BSONObj> _updateConstants; - // System-defined constant values which may be required by the query or update operation. boost::optional<RuntimeConstants> _runtimeConstants; - // Filters to specify which array elements should be updated. - std::vector<BSONObj> _arrayFilters; // The statement id of this request. StmtId _stmtId = kUninitializedStmtId; @@ -307,16 +297,6 @@ private: // updates, never user updates. bool _god = false; - // True if this should insert if no matching document is found. - bool _upsert = false; - - // True if this upsert operation should insert the document supplied as 'c.new' if the query - // does not match any documents. - bool _upsertSuppliedDocument = false; - - // True if this update is allowed to affect more than one document. - bool _multi = false; - // True if this update is on behalf of a chunk migration. bool _fromMigration = false; diff --git a/src/mongo/db/ops/write_ops_exec.cpp b/src/mongo/db/ops/write_ops_exec.cpp index ac0e9c764fb..d281456291f 100644 --- a/src/mongo/db/ops/write_ops_exec.cpp +++ b/src/mongo/db/ops/write_ops_exec.cpp @@ -729,19 +729,10 @@ static SingleWriteResult performSingleUpdateOpWithDupKeyRetry(OperationContext* "Cannot use (or request) retryable writes with multi=true", opCtx->inMultiDocumentTransaction() || !opCtx->getTxnNumber() || !op.getMulti()); - UpdateRequest request(ns); - request.setQuery(op.getQ()); - request.setUpdateModification(op.getU()); - request.setUpdateConstants(op.getC()); + UpdateRequest request(op); + request.setNamespaceString(ns); request.setRuntimeConstants(std::move(runtimeConstants)); - request.setCollation(write_ops::collationOf(op)); request.setStmtId(stmtId); - request.setArrayFilters(write_ops::arrayFiltersOf(op)); - request.setMulti(op.getMulti()); - request.setUpsert(op.getUpsert()); - request.setUpsertSuppliedDocument(op.getUpsertSupplied()); - request.setHint(op.getHint()); - request.setYieldPolicy(opCtx->inMultiDocumentTransaction() ? PlanExecutor::INTERRUPT_ONLY : PlanExecutor::YIELD_AUTO); diff --git a/src/mongo/db/repl/oplog.cpp b/src/mongo/db/repl/oplog.cpp index a4bdc3301c4..a047442c447 100644 --- a/src/mongo/db/repl/oplog.cpp +++ b/src/mongo/db/repl/oplog.cpp @@ -1209,7 +1209,8 @@ Status applyOperation_inlock(OperationContext* opCtx, BSONObjBuilder b; b.append(o.getField("_id")); - UpdateRequest request(requestNss); + auto request = UpdateRequest(); + request.setNamespaceString(requestNss); request.setQuery(b.done()); request.setUpdateModification(o); request.setUpsert(); @@ -1261,7 +1262,8 @@ Status applyOperation_inlock(OperationContext* opCtx, const bool upsertOplogEntry = op.getUpsert().value_or(false); const bool upsert = alwaysUpsert || upsertOplogEntry; - UpdateRequest request(requestNss); + auto request = UpdateRequest(); + request.setNamespaceString(requestNss); request.setQuery(updateCriteria); request.setUpdateModification(o); request.setUpsert(upsert); diff --git a/src/mongo/db/repl/rs_rollback.cpp b/src/mongo/db/repl/rs_rollback.cpp index f62d8d39998..d0219dddceb 100644 --- a/src/mongo/db/repl/rs_rollback.cpp +++ b/src/mongo/db/repl/rs_rollback.cpp @@ -1880,7 +1880,8 @@ void rollback_internal::syncFixUp(OperationContext* opCtx, // TODO faster... updates++; - UpdateRequest request(*nss); + auto request = UpdateRequest(); + request.setNamespaceString(*nss); request.setQuery(pattern); request.setUpdateModification(idAndDoc.second); diff --git a/src/mongo/db/repl/storage_interface_impl.cpp b/src/mongo/db/repl/storage_interface_impl.cpp index c78a7efc763..1521b29a465 100644 --- a/src/mongo/db/repl/storage_interface_impl.cpp +++ b/src/mongo/db/repl/storage_interface_impl.cpp @@ -936,7 +936,8 @@ Status StorageInterfaceImpl::upsertById(OperationContext* opCtx, // We can create an UpdateRequest now that the collection's namespace has been resolved, in // the event it was specified as a UUID. - UpdateRequest request(collection->ns()); + auto request = UpdateRequest(); + request.setNamespaceString(collection->ns()); request.setQuery(query); request.setUpdateModification(update); request.setUpsert(true); @@ -977,7 +978,8 @@ Status StorageInterfaceImpl::upsertById(OperationContext* opCtx, Status StorageInterfaceImpl::putSingleton(OperationContext* opCtx, const NamespaceString& nss, const TimestampedBSONObj& update) { - UpdateRequest request(nss); + auto request = UpdateRequest(); + request.setNamespaceString(nss); request.setQuery({}); request.setUpdateModification(update.obj); request.setUpsert(true); @@ -988,7 +990,8 @@ Status StorageInterfaceImpl::updateSingleton(OperationContext* opCtx, const NamespaceString& nss, const BSONObj& query, const TimestampedBSONObj& update) { - UpdateRequest request(nss); + auto request = UpdateRequest(); + request.setNamespaceString(nss); request.setQuery(query); request.setUpdateModification(update.obj); invariant(!request.isUpsert()); diff --git a/src/mongo/db/s/sharding_initialization_mongod.cpp b/src/mongo/db/s/sharding_initialization_mongod.cpp index 69dbf589e48..e338d2e63f1 100644 --- a/src/mongo/db/s/sharding_initialization_mongod.cpp +++ b/src/mongo/db/s/sharding_initialization_mongod.cpp @@ -385,7 +385,8 @@ void ShardingInitializationMongoD::updateShardIdentityConfigString( BSONObj updateObj( ShardIdentityType::createConfigServerUpdateObject(newConnectionString.toString())); - UpdateRequest updateReq(NamespaceString::kServerConfigurationNamespace); + auto updateReq = UpdateRequest(); + updateReq.setNamespaceString(NamespaceString::kServerConfigurationNamespace); updateReq.setQuery(BSON("_id" << ShardIdentityType::IdName)); updateReq.setUpdateModification(updateObj); diff --git a/src/mongo/db/s/sharding_state_recovery.cpp b/src/mongo/db/s/sharding_state_recovery.cpp index 99c00c7faab..49e429aadd2 100644 --- a/src/mongo/db/s/sharding_state_recovery.cpp +++ b/src/mongo/db/s/sharding_state_recovery.cpp @@ -157,7 +157,8 @@ Status modifyRecoveryDocument(OperationContext* opCtx, "Changing sharding recovery document", "update"_attr = redact(updateObj)); - UpdateRequest updateReq(NamespaceString::kServerConfigurationNamespace); + auto updateReq = UpdateRequest(); + updateReq.setNamespaceString(NamespaceString::kServerConfigurationNamespace); updateReq.setQuery(RecoveryDocument::getQuery()); updateReq.setUpdateModification(updateObj); updateReq.setUpsert(); diff --git a/src/mongo/db/transaction_participant.cpp b/src/mongo/db/transaction_participant.cpp index 03d585b55a1..16243ce5e81 100644 --- a/src/mongo/db/transaction_participant.cpp +++ b/src/mongo/db/transaction_participant.cpp @@ -2320,7 +2320,8 @@ boost::optional<repl::OpTime> TransactionParticipant::Participant::_checkStateme UpdateRequest TransactionParticipant::Participant::_makeUpdateRequest( const SessionTxnRecord& sessionTxnRecord) const { - UpdateRequest updateRequest(NamespaceString::kSessionTransactionsTableNamespace); + auto updateRequest = UpdateRequest(); + updateRequest.setNamespaceString(NamespaceString::kSessionTransactionsTableNamespace); updateRequest.setUpdateModification(sessionTxnRecord.toBSON()); updateRequest.setQuery(BSON(SessionTxnRecord::kSessionIdFieldName << _sessionId().toBSON())); diff --git a/src/mongo/dbtests/query_stage_update.cpp b/src/mongo/dbtests/query_stage_update.cpp index f576c8199b9..c135f1d916d 100644 --- a/src/mongo/dbtests/query_stage_update.cpp +++ b/src/mongo/dbtests/query_stage_update.cpp @@ -210,7 +210,8 @@ public: // Collection should be empty. ASSERT_EQUALS(0U, count(BSONObj())); - UpdateRequest request(nss); + auto request = UpdateRequest(); + request.setNamespaceString(nss); // Update is the upsert {_id: 0, x: 1}, {$set: {y: 2}}. BSONObj query = fromjson("{_id: 0, x: 1}"); @@ -282,7 +283,8 @@ public: vector<RecordId> recordIds; getRecordIds(coll, CollectionScanParams::FORWARD, &recordIds); - UpdateRequest request(nss); + auto request = UpdateRequest(); + request.setNamespaceString(nss); // Update is a multi-update that sets 'bar' to 3 in every document // where foo is less than 5. @@ -386,7 +388,8 @@ public: OpDebug* opDebug = &CurOp::get(_opCtx)->debug(); Collection* coll = ctx.getCollection(); ASSERT(coll); - UpdateRequest request(nss); + auto request = UpdateRequest(); + request.setNamespaceString(nss); UpdateDriver driver(_expCtx); const int targetDocIndex = 0; // We'll be working with the first doc in the collection. const BSONObj query = BSON("foo" << BSON("$gte" << targetDocIndex)); @@ -477,7 +480,8 @@ public: OpDebug* opDebug = &CurOp::get(_opCtx)->debug(); Collection* coll = ctx.getCollection(); ASSERT(coll); - UpdateRequest request(nss); + auto request = UpdateRequest(); + request.setNamespaceString(nss); UpdateDriver driver(_expCtx); const int targetDocIndex = 10; const BSONObj query = BSON("foo" << BSON("$gte" << targetDocIndex)); |