diff options
Diffstat (limited to 'src/mongo/db/ops/update.cpp')
-rw-r--r-- | src/mongo/db/ops/update.cpp | 124 |
1 files changed, 62 insertions, 62 deletions
diff --git a/src/mongo/db/ops/update.cpp b/src/mongo/db/ops/update.cpp index cf60978b4b8..43c9be211ce 100644 --- a/src/mongo/db/ops/update.cpp +++ b/src/mongo/db/ops/update.cpp @@ -54,74 +54,74 @@ namespace mongo { - UpdateResult update(OperationContext* txn, - Database* db, - const UpdateRequest& request, - OpDebug* opDebug) { - invariant(db); - - // Explain should never use this helper. - invariant(!request.isExplain()); - - const NamespaceString& nsString = request.getNamespaceString(); - Collection* collection = db->getCollection(nsString.ns()); - - // The update stage does not create its own collection. As such, if the update is - // an upsert, create the collection that the update stage inserts into beforehand. - if (!collection && request.isUpsert()) { - // We have to have an exclusive lock on the db to be allowed to create the collection. - // Callers should either get an X or create the collection. - const Locker* locker = txn->lockState(); - invariant(locker->isW() || - locker->isLockHeldForMode(ResourceId(RESOURCE_DATABASE, nsString.db()), - MODE_X)); - - MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(txn, MODE_IX); - Lock::DBLock lk(txn->lockState(), nsString.db(), MODE_X); - - const bool userInitiatedWritesAndNotPrimary = txn->writesAreReplicated() && - !repl::getGlobalReplicationCoordinator()->canAcceptWritesFor(nsString); - - if (userInitiatedWritesAndNotPrimary) { - uassertStatusOK(Status(ErrorCodes::NotMaster, str::stream() - << "Not primary while creating collection " << nsString.ns() - << " during upsert")); - } - WriteUnitOfWork wuow(txn); - collection = db->createCollection(txn, nsString.ns(), CollectionOptions()); - invariant(collection); - wuow.commit(); - } MONGO_WRITE_CONFLICT_RETRY_LOOP_END(txn, "createCollection", nsString.ns()); +UpdateResult update(OperationContext* txn, + Database* db, + const UpdateRequest& request, + OpDebug* opDebug) { + invariant(db); + + // Explain should never use this helper. + invariant(!request.isExplain()); + + const NamespaceString& nsString = request.getNamespaceString(); + Collection* collection = db->getCollection(nsString.ns()); + + // The update stage does not create its own collection. As such, if the update is + // an upsert, create the collection that the update stage inserts into beforehand. + if (!collection && request.isUpsert()) { + // We have to have an exclusive lock on the db to be allowed to create the collection. + // Callers should either get an X or create the collection. + const Locker* locker = txn->lockState(); + invariant(locker->isW() || + locker->isLockHeldForMode(ResourceId(RESOURCE_DATABASE, nsString.db()), MODE_X)); + + MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { + ScopedTransaction transaction(txn, MODE_IX); + Lock::DBLock lk(txn->lockState(), nsString.db(), MODE_X); + + const bool userInitiatedWritesAndNotPrimary = txn->writesAreReplicated() && + !repl::getGlobalReplicationCoordinator()->canAcceptWritesFor(nsString); + + if (userInitiatedWritesAndNotPrimary) { + uassertStatusOK(Status(ErrorCodes::NotMaster, + str::stream() << "Not primary while creating collection " + << nsString.ns() << " during upsert")); + } + WriteUnitOfWork wuow(txn); + collection = db->createCollection(txn, nsString.ns(), CollectionOptions()); + invariant(collection); + wuow.commit(); } + MONGO_WRITE_CONFLICT_RETRY_LOOP_END(txn, "createCollection", nsString.ns()); + } - // Parse the update, get an executor for it, run the executor, get stats out. - ParsedUpdate parsedUpdate(txn, &request); - uassertStatusOK(parsedUpdate.parseRequest()); - - PlanExecutor* rawExec; - uassertStatusOK(getExecutorUpdate(txn, collection, &parsedUpdate, opDebug, &rawExec)); - std::unique_ptr<PlanExecutor> exec(rawExec); + // Parse the update, get an executor for it, run the executor, get stats out. + ParsedUpdate parsedUpdate(txn, &request); + uassertStatusOK(parsedUpdate.parseRequest()); - uassertStatusOK(exec->executePlan()); - return UpdateStage::makeUpdateResult(exec.get(), opDebug); - } + PlanExecutor* rawExec; + uassertStatusOK(getExecutorUpdate(txn, collection, &parsedUpdate, opDebug, &rawExec)); + std::unique_ptr<PlanExecutor> exec(rawExec); - BSONObj applyUpdateOperators(const BSONObj& from, const BSONObj& operators) { - UpdateDriver::Options opts; - UpdateDriver driver(opts); - Status status = driver.parse(operators); - if (!status.isOK()) { - uasserted(16838, status.reason()); - } + uassertStatusOK(exec->executePlan()); + return UpdateStage::makeUpdateResult(exec.get(), opDebug); +} - mutablebson::Document doc(from, mutablebson::Document::kInPlaceDisabled); - status = driver.update(StringData(), &doc); - if (!status.isOK()) { - uasserted(16839, status.reason()); - } +BSONObj applyUpdateOperators(const BSONObj& from, const BSONObj& operators) { + UpdateDriver::Options opts; + UpdateDriver driver(opts); + Status status = driver.parse(operators); + if (!status.isOK()) { + uasserted(16838, status.reason()); + } - return doc.getObject(); + mutablebson::Document doc(from, mutablebson::Document::kInPlaceDisabled); + status = driver.update(StringData(), &doc); + if (!status.isOK()) { + uasserted(16839, status.reason()); } + return doc.getObject(); +} + } // namespace mongo |