summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrik Edin <henrik.edin@mongodb.com>2023-02-09 12:08:10 -0500
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-02-13 16:55:03 +0000
commit78b5bb22426142e49b30c91cff0f1b6e99725047 (patch)
tree7412d4ee181a221884e52126e460eeb40b895776
parent13e038d492a20bb52c7f3322603815f503020cf5 (diff)
downloadmongo-78b5bb22426142e49b30c91cff0f1b6e99725047.tar.gz
SERVER-73780 Make CollectionPtr constructor explicit
-rw-r--r--src/mongo/db/catalog/capped_utils_test.cpp2
-rw-r--r--src/mongo/db/catalog/coll_mod.cpp4
-rw-r--r--src/mongo/db/catalog/collection.h2
-rw-r--r--src/mongo/db/catalog/collection_catalog.cpp12
-rw-r--r--src/mongo/db/catalog/collection_catalog_test.cpp32
-rw-r--r--src/mongo/db/catalog/collection_uuid_mismatch.cpp2
-rw-r--r--src/mongo/db/catalog/create_collection.cpp3
-rw-r--r--src/mongo/db/catalog/create_collection_test.cpp2
-rw-r--r--src/mongo/db/catalog/database_impl.cpp12
-rw-r--r--src/mongo/db/catalog/drop_collection.cpp7
-rw-r--r--src/mongo/db/catalog/drop_indexes.cpp12
-rw-r--r--src/mongo/db/catalog/index_build_block.cpp2
-rw-r--r--src/mongo/db/catalog/index_catalog_impl.cpp19
-rw-r--r--src/mongo/db/catalog/index_repair.cpp6
-rw-r--r--src/mongo/db/catalog/multi_index_block.cpp11
-rw-r--r--src/mongo/db/catalog/rename_collection.cpp6
-rw-r--r--src/mongo/db/catalog/rename_collection_test.cpp4
-rw-r--r--src/mongo/db/catalog_raii.cpp6
-rw-r--r--src/mongo/db/commands/index_filter_commands_test.cpp4
-rw-r--r--src/mongo/db/commands/plan_cache_commands_test.cpp2
-rw-r--r--src/mongo/db/commands/run_aggregate.cpp4
-rw-r--r--src/mongo/db/commands/test_commands.cpp5
-rw-r--r--src/mongo/db/db_raii.h2
-rw-r--r--src/mongo/db/dbhelpers.cpp3
-rw-r--r--src/mongo/db/index_build_entry_helpers.cpp4
-rw-r--r--src/mongo/db/op_observer/user_write_block_mode_op_observer_test.cpp4
-rw-r--r--src/mongo/db/ops/update.cpp5
-rw-r--r--src/mongo/db/pipeline/process_interface/common_mongod_process_interface.cpp4
-rw-r--r--src/mongo/db/query/cqf_command_utils.cpp4
-rw-r--r--src/mongo/db/query/cqf_get_executor.cpp6
-rw-r--r--src/mongo/db/repl/oplog.cpp26
-rw-r--r--src/mongo/db/repl/oplog_applier_impl_test_fixture.cpp2
-rw-r--r--src/mongo/db/repl/rollback_test_fixture.cpp2
-rw-r--r--src/mongo/db/repl/tenant_migration_util.cpp3
-rw-r--r--src/mongo/db/startup_recovery.cpp7
-rw-r--r--src/mongo/dbtests/catalogtests.cpp3
-rw-r--r--src/mongo/dbtests/counttests.cpp2
-rw-r--r--src/mongo/dbtests/dbhelper_tests.cpp9
-rw-r--r--src/mongo/dbtests/pdfiletests.cpp2
-rw-r--r--src/mongo/dbtests/query_stage_and.cpp2
-rw-r--r--src/mongo/dbtests/query_stage_count.cpp2
-rw-r--r--src/mongo/dbtests/query_stage_fetch.cpp4
-rw-r--r--src/mongo/dbtests/query_stage_ixscan.cpp2
-rw-r--r--src/mongo/dbtests/query_stage_sort.cpp12
-rw-r--r--src/mongo/dbtests/query_stage_subplan.cpp2
-rw-r--r--src/mongo/dbtests/querytests.cpp7
-rw-r--r--src/mongo/dbtests/repltests.cpp6
47 files changed, 161 insertions, 123 deletions
diff --git a/src/mongo/db/catalog/capped_utils_test.cpp b/src/mongo/db/catalog/capped_utils_test.cpp
index cec27d7a23b..70386a62bd0 100644
--- a/src/mongo/db/catalog/capped_utils_test.cpp
+++ b/src/mongo/db/catalog/capped_utils_test.cpp
@@ -88,7 +88,7 @@ ServiceContext::UniqueOperationContext makeOpCtx() {
* Returns true if collection exists.
*/
bool collectionExists(OperationContext* opCtx, const NamespaceString& nss) {
- return AutoGetCollectionForRead(opCtx, nss).getCollection() != nullptr;
+ return static_cast<bool>(AutoGetCollectionForRead(opCtx, nss).getCollection());
}
/**
diff --git a/src/mongo/db/catalog/coll_mod.cpp b/src/mongo/db/catalog/coll_mod.cpp
index 41e6c138dc2..73fd7abc2f5 100644
--- a/src/mongo/db/catalog/coll_mod.cpp
+++ b/src/mongo/db/catalog/coll_mod.cpp
@@ -693,7 +693,7 @@ StatusWith<const IndexDescriptor*> _setUpCollModIndexUnique(OperationContext* op
const auto& collection = coll.getCollection();
if (!collection) {
- checkCollectionUUIDMismatch(opCtx, nss, nullptr, cmd.getCollectionUUID());
+ checkCollectionUUIDMismatch(opCtx, nss, CollectionPtr(), cmd.getCollectionUUID());
return Status(ErrorCodes::NamespaceNotFound,
str::stream() << "ns does not exist for unique index conversion: " << nss);
}
@@ -790,7 +790,7 @@ Status _collModInternal(OperationContext* opCtx,
CollectionShardingState::assertCollectionLockedAndAcquire(opCtx, nss)
->checkShardVersionOrThrow(opCtx);
}
- checkCollectionUUIDMismatch(opCtx, nss, nullptr, cmd.getCollectionUUID());
+ checkCollectionUUIDMismatch(opCtx, nss, CollectionPtr(), cmd.getCollectionUUID());
return Status(ErrorCodes::NamespaceNotFound, "ns does not exist");
}
diff --git a/src/mongo/db/catalog/collection.h b/src/mongo/db/catalog/collection.h
index 55e10fddf56..ca61ea91657 100644
--- a/src/mongo/db/catalog/collection.h
+++ b/src/mongo/db/catalog/collection.h
@@ -729,7 +729,7 @@ public:
// CollectionPtr yieldable call `makeYieldable` and provide appropriate implementation depending
// on context.
CollectionPtr() = default;
- CollectionPtr(const Collection* collection);
+ explicit CollectionPtr(const Collection* collection);
CollectionPtr(const CollectionPtr&) = delete;
CollectionPtr(CollectionPtr&&);
diff --git a/src/mongo/db/catalog/collection_catalog.cpp b/src/mongo/db/catalog/collection_catalog.cpp
index 26e165828c8..5f06c5b92b4 100644
--- a/src/mongo/db/catalog/collection_catalog.cpp
+++ b/src/mongo/db/catalog/collection_catalog.cpp
@@ -404,7 +404,7 @@ CollectionCatalog::iterator::value_type CollectionCatalog::iterator::operator*()
return CollectionPtr();
}
- return {_mapIter->second.get()};
+ return {CollectionPtr(_mapIter->second.get())};
}
Collection* CollectionCatalog::iterator::getWritableCollection(OperationContext* opCtx) {
@@ -1360,7 +1360,7 @@ CollectionPtr CollectionCatalog::lookupCollectionByUUID(OperationContext* opCtx,
auto [found, uncommittedPtr, newColl] =
UncommittedCatalogUpdates::lookupCollection(opCtx, uuid);
if (found) {
- return uncommittedPtr.get();
+ return CollectionPtr(uncommittedPtr.get());
}
// Return any previously instantiated collection on this namespace for this snapshot
@@ -1482,12 +1482,12 @@ CollectionPtr CollectionCatalog::lookupCollectionByNamespace(OperationContext* o
// exists.
auto [found, uncommittedPtr, newColl] = UncommittedCatalogUpdates::lookupCollection(opCtx, nss);
if (uncommittedPtr) {
- return uncommittedPtr.get();
+ return CollectionPtr(uncommittedPtr.get());
}
// Report the drop or rename as nothing new was created.
if (found) {
- return nullptr;
+ return CollectionPtr();
}
// Return any previously instantiated collection on this namespace for this snapshot
@@ -1497,7 +1497,7 @@ CollectionPtr CollectionCatalog::lookupCollectionByNamespace(OperationContext* o
auto it = _collections.find(nss);
auto coll = (it == _collections.end() ? nullptr : it->second);
- return (coll && coll->isCommitted()) ? CollectionPtr(coll.get()) : nullptr;
+ return (coll && coll->isCommitted()) ? CollectionPtr(coll.get()) : CollectionPtr();
}
boost::optional<NamespaceString> CollectionCatalog::lookupNSSByUUID(OperationContext* opCtx,
@@ -1711,7 +1711,7 @@ bool CollectionCatalog::checkIfCollectionSatisfiable(UUID uuid, CollectionInfoFn
return false;
}
- return predicate(collection.get());
+ return predicate(CollectionPtr(collection.get()));
}
std::vector<UUID> CollectionCatalog::getAllCollectionUUIDsFromDb(const DatabaseName& dbName) const {
diff --git a/src/mongo/db/catalog/collection_catalog_test.cpp b/src/mongo/db/catalog/collection_catalog_test.cpp
index bc2c31efe47..b8ecc929fca 100644
--- a/src/mongo/db/catalog/collection_catalog_test.cpp
+++ b/src/mongo/db/catalog/collection_catalog_test.cpp
@@ -161,7 +161,7 @@ public:
++catalogIt, ++orderedIt) {
auto catalogColl = *catalogIt;
- ASSERT(catalogColl != nullptr);
+ ASSERT(catalogColl);
const auto& orderedColl = orderedIt->second;
ASSERT_EQ(catalogColl->ns(), orderedColl->ns());
++counter;
@@ -333,7 +333,7 @@ TEST_F(CollectionCatalogTest, LookupCollectionByUUID) {
// nss.ns().
ASSERT_EQUALS(catalog.lookupCollectionByUUID(opCtx.get(), colUUID)->ns().ns(), nss.ns());
// Ensure lookups of unknown UUIDs result in null pointers.
- ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), UUID::gen()) == nullptr);
+ ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), UUID::gen()).get() == nullptr);
}
TEST_F(CollectionCatalogTest, LookupNSSByUUID) {
@@ -347,10 +347,10 @@ TEST_F(CollectionCatalogTest, InsertAfterLookup) {
auto newUUID = UUID::gen();
NamespaceString newNss = NamespaceString::createNamespaceString_forTest(nss.dbName(), "newcol");
std::shared_ptr<Collection> newCollShared = std::make_shared<CollectionMock>(newNss);
- auto newCol = newCollShared.get();
+ auto newCol = CollectionPtr(newCollShared.get());
// Ensure that looking up non-existing UUIDs doesn't affect later registration of those UUIDs.
- ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), newUUID) == nullptr);
+ ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), newUUID).get() == nullptr);
ASSERT_EQUALS(catalog.lookupNSSByUUID(opCtx.get(), newUUID), boost::none);
catalog.registerCollection(opCtx.get(), newUUID, std::move(newCollShared), boost::none);
ASSERT_EQUALS(catalog.lookupCollectionByUUID(opCtx.get(), newUUID), newCol);
@@ -387,7 +387,7 @@ TEST_F(CollectionCatalogTest, OnDropCollection) {
catalog.deregisterCollection(opCtx.get(), colUUID, /*isDropPending=*/false, boost::none);
// Ensure the lookup returns a null pointer upon removing the colUUID entry.
- ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), colUUID) == nullptr);
+ ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), colUUID).get() == nullptr);
// After dropping the collection, we should fail to restore the CollectionPtr.
yieldableColl.restore();
@@ -402,7 +402,7 @@ TEST_F(CollectionCatalogTest, RenameCollection) {
catalog.registerCollection(opCtx.get(), uuid, std::move(collShared), boost::none);
auto yieldableColl = catalog.lookupCollectionByUUID(opCtx.get(), uuid);
ASSERT(yieldableColl);
- ASSERT_EQUALS(yieldableColl, collection);
+ ASSERT_EQUALS(yieldableColl, CollectionPtr(collection));
// Make the CollectionPtr yieldable by setting yield impl
yieldableColl.makeYieldable(opCtx.get(),
@@ -421,7 +421,7 @@ TEST_F(CollectionCatalogTest, RenameCollection) {
// Before renaming collection, confirm that the CollectionPtr can be restored successfully.
yieldableColl.restore();
ASSERT(yieldableColl);
- ASSERT_EQUALS(yieldableColl, collection);
+ ASSERT_EQUALS(yieldableColl, CollectionPtr(collection));
// Reset CollectionPtr for post-rename restore test.
yieldableColl.yield();
@@ -430,7 +430,7 @@ TEST_F(CollectionCatalogTest, RenameCollection) {
NamespaceString newNss = NamespaceString::createNamespaceString_forTest(nss.dbName(), "newcol");
ASSERT_OK(collection->rename(opCtx.get(), newNss, false));
ASSERT_EQ(collection->ns(), newNss);
- ASSERT_EQUALS(catalog.lookupCollectionByUUID(opCtx.get(), uuid), collection);
+ ASSERT_EQUALS(catalog.lookupCollectionByUUID(opCtx.get(), uuid), CollectionPtr(collection));
// After renaming the collection, we should fail to restore the CollectionPtr.
yieldableColl.restore();
@@ -444,7 +444,7 @@ TEST_F(CollectionCatalogTest, LookupNSSByUUIDForClosedCatalogReturnsOldNSSIfDrop
}
catalog.deregisterCollection(opCtx.get(), colUUID, /*isDropPending=*/false, boost::none);
- ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), colUUID) == nullptr);
+ ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), colUUID).get() == nullptr);
ASSERT_EQUALS(*catalog.lookupNSSByUUID(opCtx.get(), colUUID), nss);
{
@@ -459,7 +459,7 @@ TEST_F(CollectionCatalogTest, LookupNSSByUUIDForClosedCatalogReturnsNewlyCreated
auto newUUID = UUID::gen();
NamespaceString newNss = NamespaceString::createNamespaceString_forTest(nss.dbName(), "newcol");
std::shared_ptr<Collection> newCollShared = std::make_shared<CollectionMock>(newNss);
- auto newCol = newCollShared.get();
+ auto newCol = CollectionPtr(newCollShared.get());
// Ensure that looking up non-existing UUIDs doesn't affect later registration of those UUIDs.
{
@@ -467,7 +467,7 @@ TEST_F(CollectionCatalogTest, LookupNSSByUUIDForClosedCatalogReturnsNewlyCreated
catalog.onCloseCatalog();
}
- ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), newUUID) == nullptr);
+ ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), newUUID).get() == nullptr);
ASSERT_EQUALS(catalog.lookupNSSByUUID(opCtx.get(), newUUID), boost::none);
catalog.registerCollection(opCtx.get(), newUUID, std::move(newCollShared), boost::none);
ASSERT_EQUALS(catalog.lookupCollectionByUUID(opCtx.get(), newUUID), newCol);
@@ -486,7 +486,7 @@ TEST_F(CollectionCatalogTest, LookupNSSByUUIDForClosedCatalogReturnsNewlyCreated
TEST_F(CollectionCatalogTest, LookupNSSByUUIDForClosedCatalogReturnsFreshestNSS) {
NamespaceString newNss = NamespaceString::createNamespaceString_forTest(nss.dbName(), "newcol");
std::shared_ptr<Collection> newCollShared = std::make_shared<CollectionMock>(newNss);
- auto newCol = newCollShared.get();
+ auto newCol = CollectionPtr(newCollShared.get());
{
Lock::GlobalLock globalLk(opCtx.get(), MODE_X);
@@ -494,7 +494,7 @@ TEST_F(CollectionCatalogTest, LookupNSSByUUIDForClosedCatalogReturnsFreshestNSS)
}
catalog.deregisterCollection(opCtx.get(), colUUID, /*isDropPending=*/false, boost::none);
- ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), colUUID) == nullptr);
+ ASSERT(catalog.lookupCollectionByUUID(opCtx.get(), colUUID).get() == nullptr);
ASSERT_EQUALS(*catalog.lookupNSSByUUID(opCtx.get(), colUUID), nss);
{
Lock::GlobalWrite lk(opCtx.get());
@@ -914,7 +914,7 @@ public:
auto writableColl = collection.getWritableCollection(opCtx);
StatusWith<BSONObj> statusWithSpec = writableColl->getIndexCatalog()->prepareSpecForCreate(
- opCtx, writableColl, indexSpec, boost::none);
+ opCtx, CollectionPtr(writableColl), indexSpec, boost::none);
uassertStatusOK(statusWithSpec.getStatus());
indexSpec = statusWithSpec.getValue();
@@ -1745,8 +1745,8 @@ TEST_F(CollectionCatalogTimestampTest, OpenEarlierAlreadyDropPendingCollection)
Lock::GlobalLock globalLock(opCtx.get(), MODE_IS);
// Before openCollection, looking up the collection returns null.
- ASSERT(CollectionCatalog::get(opCtx.get())
- ->lookupCollectionByNamespace(opCtx.get(), secondNss) == nullptr);
+ ASSERT(!CollectionCatalog::get(opCtx.get())
+ ->lookupCollectionByNamespace(opCtx.get(), secondNss));
auto openedColl =
CollectionCatalog::get(opCtx.get())
->establishConsistentCollection(opCtx.get(), secondNss, readTimestamp);
diff --git a/src/mongo/db/catalog/collection_uuid_mismatch.cpp b/src/mongo/db/catalog/collection_uuid_mismatch.cpp
index 76d5f524770..1f7dffc27d7 100644
--- a/src/mongo/db/catalog/collection_uuid_mismatch.cpp
+++ b/src/mongo/db/catalog/collection_uuid_mismatch.cpp
@@ -39,7 +39,7 @@ void checkCollectionUUIDMismatch(OperationContext* opCtx,
const NamespaceString& ns,
const CollectionPtr& coll,
const boost::optional<UUID>& uuid) {
- checkCollectionUUIDMismatch(opCtx, CollectionCatalog::get(opCtx), ns, coll.get(), uuid);
+ checkCollectionUUIDMismatch(opCtx, CollectionCatalog::get(opCtx), ns, coll, uuid);
}
void checkCollectionUUIDMismatch(OperationContext* opCtx,
diff --git a/src/mongo/db/catalog/create_collection.cpp b/src/mongo/db/catalog/create_collection.cpp
index 910dd16e1c8..082bde953e7 100644
--- a/src/mongo/db/catalog/create_collection.cpp
+++ b/src/mongo/db/catalog/create_collection.cpp
@@ -713,7 +713,8 @@ Status createCollectionForApplyOps(OperationContext* opCtx,
// a random temporary name is correct: once all entries are replayed no temporary
// names will remain.
const bool stayTemp = true;
- auto futureColl = db ? catalog->lookupCollectionByNamespace(opCtx, newCollName) : nullptr;
+ auto futureColl =
+ db ? catalog->lookupCollectionByNamespace(opCtx, newCollName) : CollectionPtr();
bool needsRenaming(futureColl);
invariant(!needsRenaming || allowRenameOutOfTheWay,
str::stream() << "Current collection name: " << currentName << ", UUID: " << uuid
diff --git a/src/mongo/db/catalog/create_collection_test.cpp b/src/mongo/db/catalog/create_collection_test.cpp
index 480feb06feb..d787d4f4259 100644
--- a/src/mongo/db/catalog/create_collection_test.cpp
+++ b/src/mongo/db/catalog/create_collection_test.cpp
@@ -129,7 +129,7 @@ void CreateCollectionTest::validateValidator(const std::string& validatorStr,
* Returns true if collection exists.
*/
bool collectionExists(OperationContext* opCtx, const NamespaceString& nss) {
- return AutoGetCollectionForRead(opCtx, nss).getCollection() != nullptr;
+ return static_cast<bool>(AutoGetCollectionForRead(opCtx, nss).getCollection());
}
/**
diff --git a/src/mongo/db/catalog/database_impl.cpp b/src/mongo/db/catalog/database_impl.cpp
index ed157697fa9..66b681a0178 100644
--- a/src/mongo/db/catalog/database_impl.cpp
+++ b/src/mongo/db/catalog/database_impl.cpp
@@ -871,7 +871,8 @@ Collection* DatabaseImpl::_createCollection(
fullIdIndexSpec = uassertStatusOK(ic->createIndexOnEmptyCollection(
opCtx,
collection,
- !idIndex.isEmpty() ? idIndex : ic->getDefaultIdIndexSpec(collection)));
+ !idIndex.isEmpty() ? idIndex
+ : ic->getDefaultIdIndexSpec(CollectionPtr(collection))));
createColumnIndex = createColumnIndexOnAllCollections.shouldFail() &&
doesCollectionModificationsUpdateIndexes(nss);
} else {
@@ -894,8 +895,13 @@ Collection* DatabaseImpl::_createCollection(
hangBeforeLoggingCreateCollection.pauseWhileSet();
- opCtx->getServiceContext()->getOpObserver()->onCreateCollection(
- opCtx, collection, nss, optionsWithUUID, fullIdIndexSpec, createOplogSlot, fromMigrate);
+ opCtx->getServiceContext()->getOpObserver()->onCreateCollection(opCtx,
+ CollectionPtr(collection),
+ nss,
+ optionsWithUUID,
+ fullIdIndexSpec,
+ createOplogSlot,
+ fromMigrate);
// It is necessary to create the system index *after* running the onCreateCollection so that
// the storage timestamp for the index creation is after the storage timestamp for the
diff --git a/src/mongo/db/catalog/drop_collection.cpp b/src/mongo/db/catalog/drop_collection.cpp
index c005bddb28a..408a581c04a 100644
--- a/src/mongo/db/catalog/drop_collection.cpp
+++ b/src/mongo/db/catalog/drop_collection.cpp
@@ -129,7 +129,7 @@ Status _dropView(OperationContext* opCtx,
// Views don't have UUIDs so if the expectedUUID is specified, we will always throw.
try {
- checkCollectionUUIDMismatch(opCtx, collectionName, nullptr, expectedUUID);
+ checkCollectionUUIDMismatch(opCtx, collectionName, CollectionPtr(), expectedUUID);
} catch (const DBException& ex) {
return ex.toStatus();
}
@@ -469,7 +469,8 @@ Status _dropCollection(OperationContext* opCtx,
// and error if so because the caller expects the collection to exist. If no UUID
// was given, then it is OK to return success.
try {
- checkCollectionUUIDMismatch(opCtx, collectionName, nullptr, expectedUUID);
+ checkCollectionUUIDMismatch(
+ opCtx, collectionName, CollectionPtr(), expectedUUID);
} catch (const DBException& ex) {
return ex.toStatus();
}
@@ -492,7 +493,7 @@ Status _dropCollection(OperationContext* opCtx,
// Any unhandled namespace not found errors should be converted into success. Unless the
// caller specified a UUID and expects the collection to exist.
try {
- checkCollectionUUIDMismatch(opCtx, collectionName, nullptr, expectedUUID);
+ checkCollectionUUIDMismatch(opCtx, collectionName, CollectionPtr(), expectedUUID);
} catch (const DBException& ex) {
return ex.toStatus();
}
diff --git a/src/mongo/db/catalog/drop_indexes.cpp b/src/mongo/db/catalog/drop_indexes.cpp
index d258e3b1159..0575e7800fa 100644
--- a/src/mongo/db/catalog/drop_indexes.cpp
+++ b/src/mongo/db/catalog/drop_indexes.cpp
@@ -343,11 +343,13 @@ void dropReadyIndexes(OperationContext* opCtx,
for (const auto& indexName : indexNames) {
if (collDescription.isSharded()) {
- uassert(
- ErrorCodes::CannotDropShardKeyIndex,
- "Cannot drop the only compatible index for this collection's shard key",
- !isLastNonHiddenShardKeyIndex(
- opCtx, collection, indexCatalog, indexName, collDescription.getKeyPattern()));
+ uassert(ErrorCodes::CannotDropShardKeyIndex,
+ "Cannot drop the only compatible index for this collection's shard key",
+ !isLastNonHiddenShardKeyIndex(opCtx,
+ CollectionPtr(collection),
+ indexCatalog,
+ indexName,
+ collDescription.getKeyPattern()));
}
auto desc = indexCatalog->findIndexByName(opCtx,
diff --git a/src/mongo/db/catalog/index_build_block.cpp b/src/mongo/db/catalog/index_build_block.cpp
index 14ef10d4b76..34672efcd59 100644
--- a/src/mongo/db/catalog/index_build_block.cpp
+++ b/src/mongo/db/catalog/index_build_block.cpp
@@ -73,7 +73,7 @@ void IndexBuildBlock::_completeInit(OperationContext* opCtx, Collection* collect
// occurring while an index is being build in the background will be aware of whether or not
// they need to modify any indexes.
auto desc = getEntry(opCtx, collection)->descriptor();
- CollectionQueryInfo::get(collection).rebuildIndexData(opCtx, collection);
+ CollectionQueryInfo::get(collection).rebuildIndexData(opCtx, CollectionPtr(collection));
CollectionIndexUsageTrackerDecoration::get(collection->getSharedDecorations())
.registerIndex(desc->indexName(),
desc->keyPattern(),
diff --git a/src/mongo/db/catalog/index_catalog_impl.cpp b/src/mongo/db/catalog/index_catalog_impl.cpp
index 9e15016e888..9a461c626e4 100644
--- a/src/mongo/db/catalog/index_catalog_impl.cpp
+++ b/src/mongo/db/catalog/index_catalog_impl.cpp
@@ -318,7 +318,7 @@ void IndexCatalogImpl::_init(OperationContext* opCtx,
if (!fromExisting) {
// Only do this when we're not initializing an earlier collection from the shared state of
// an existing collection.
- CollectionQueryInfo::get(collection).init(opCtx, collection);
+ CollectionQueryInfo::get(collection).init(opCtx, CollectionPtr(collection));
}
}
@@ -590,7 +590,7 @@ IndexCatalogEntry* IndexCatalogImpl::createIndexEntry(OperationContext* opCtx,
Collection* collection,
std::unique_ptr<IndexDescriptor> descriptor,
CreateIndexEntryFlags flags) {
- Status status = _isSpecOk(opCtx, collection, descriptor->infoObj());
+ Status status = _isSpecOk(opCtx, CollectionPtr(collection), descriptor->infoObj());
if (!status.isOK()) {
LOGV2_FATAL(28782,
"Found an invalid index",
@@ -609,7 +609,7 @@ IndexCatalogEntry* IndexCatalogImpl::createIndexEntry(OperationContext* opCtx,
auto* const descriptorPtr = descriptor.get();
auto entry = std::make_shared<IndexCatalogEntryImpl>(
- opCtx, collection, ident, std::move(descriptor), frozen);
+ opCtx, CollectionPtr(collection), ident, std::move(descriptor), frozen);
IndexDescriptor* desc = entry->descriptor();
@@ -660,7 +660,8 @@ StatusWith<BSONObj> IndexCatalogImpl::createIndexOnEmptyCollection(OperationCont
<< " UUID: " << collection->uuid()
<< " Count (from size storer): " << collection->numRecords(opCtx));
- StatusWith<BSONObj> statusWithSpec = prepareSpecForCreate(opCtx, collection, spec);
+ StatusWith<BSONObj> statusWithSpec =
+ prepareSpecForCreate(opCtx, CollectionPtr(collection), spec);
Status status = statusWithSpec.getStatus();
if (!status.isOK())
return status;
@@ -1290,8 +1291,10 @@ void IndexCatalogImpl::dropIndexes(OperationContext* opCtx,
if (!didExclude) {
if (numIndexesTotal() || numIndexesInCollectionCatalogEntry || _readyIndexes.size()) {
- _logInternalState(
- opCtx, collection, numIndexesInCollectionCatalogEntry, indexNamesToDrop);
+ _logInternalState(opCtx,
+ CollectionPtr(collection),
+ numIndexesInCollectionCatalogEntry,
+ indexNamesToDrop);
}
fassert(17327, numIndexesTotal() == 0);
fassert(17328, numIndexesInCollectionCatalogEntry == 0);
@@ -1475,7 +1478,7 @@ Status IndexCatalogImpl::dropIndexEntry(OperationContext* opCtx,
opCtx->recoveryUnit()->registerChange(std::make_unique<IndexRemoveChange>(
collection->ns(), collection->uuid(), released, collection->getSharedDecorations()));
- CollectionQueryInfo::get(collection).rebuildIndexData(opCtx, collection);
+ CollectionQueryInfo::get(collection).rebuildIndexData(opCtx, CollectionPtr(collection));
CollectionIndexUsageTrackerDecoration::get(collection->getSharedDecorations())
.unregisterIndex(indexName);
_deleteIndexFromDisk(opCtx, collection, indexName, released);
@@ -1682,7 +1685,7 @@ const IndexDescriptor* IndexCatalogImpl::refreshEntry(OperationContext* opCtx,
IndexFeatures::make(desc, collection->ns().isOnInternalDb()));
// Last rebuild index data for CollectionQueryInfo for this Collection.
- CollectionQueryInfo::get(collection).rebuildIndexData(opCtx, collection);
+ CollectionQueryInfo::get(collection).rebuildIndexData(opCtx, CollectionPtr(collection));
opCtx->recoveryUnit()->onCommit([newEntry](auto commitTime) {
if (commitTime) {
diff --git a/src/mongo/db/catalog/index_repair.cpp b/src/mongo/db/catalog/index_repair.cpp
index df1ea40fc77..a291f7a7017 100644
--- a/src/mongo/db/catalog/index_repair.cpp
+++ b/src/mongo/db/catalog/index_repair.cpp
@@ -30,6 +30,7 @@
#include "mongo/db/catalog/index_repair.h"
#include "mongo/base/status_with.h"
#include "mongo/db/catalog/collection_write_path.h"
+#include "mongo/db/catalog/collection_yield_restore.h"
#include "mongo/db/catalog/validate_state.h"
#include "mongo/db/catalog_raii.h"
#include "mongo/db/concurrency/exception_util.h"
@@ -63,7 +64,8 @@ StatusWith<int> moveRecordToLostAndFound(OperationContext* opCtx,
// duplicate key errors on the _id value.
CollectionOptions collOptions;
collOptions.setNoIdIndex();
- localCollection = db->createCollection(opCtx, lostAndFoundNss, collOptions);
+ localCollection =
+ CollectionPtr(db->createCollection(opCtx, lostAndFoundNss, collOptions));
// Ensure the collection exists.
invariant(localCollection, lostAndFoundNss.ns());
@@ -76,6 +78,8 @@ StatusWith<int> moveRecordToLostAndFound(OperationContext* opCtx,
}
}
+ localCollection.makeYieldable(opCtx, LockedCollectionYieldRestore(opCtx, localCollection));
+
return writeConflictRetry(
opCtx, "writeDupDocToLostAndFoundCollection", nss.ns(), [&]() -> StatusWith<int> {
WriteUnitOfWork wuow(opCtx);
diff --git a/src/mongo/db/catalog/multi_index_block.cpp b/src/mongo/db/catalog/multi_index_block.cpp
index 31d954e8558..fdcc8cda182 100644
--- a/src/mongo/db/catalog/multi_index_block.cpp
+++ b/src/mongo/db/catalog/multi_index_block.cpp
@@ -959,12 +959,14 @@ Status MultiIndexBlock::commit(OperationContext* opCtx,
if (interceptor) {
auto multikeyPaths = interceptor->getMultikeyPaths();
if (multikeyPaths) {
- indexCatalogEntry->setMultikey(opCtx, collection, {}, multikeyPaths.value());
+ indexCatalogEntry->setMultikey(
+ opCtx, CollectionPtr(collection), {}, multikeyPaths.value());
}
multikeyPaths = interceptor->getSkippedRecordTracker()->getMultikeyPaths();
if (multikeyPaths) {
- indexCatalogEntry->setMultikey(opCtx, collection, {}, multikeyPaths.value());
+ indexCatalogEntry->setMultikey(
+ opCtx, CollectionPtr(collection), {}, multikeyPaths.value());
}
}
@@ -975,7 +977,8 @@ Status MultiIndexBlock::commit(OperationContext* opCtx,
// MultikeyPaths into IndexCatalogEntry::setMultikey here.
const auto& bulkBuilder = _indexes[i].bulk;
if (bulkBuilder->isMultikey()) {
- indexCatalogEntry->setMultikey(opCtx, collection, {}, bulkBuilder->getMultikeyPaths());
+ indexCatalogEntry->setMultikey(
+ opCtx, CollectionPtr(collection), {}, bulkBuilder->getMultikeyPaths());
}
if (opCtx->getServiceContext()->getStorageEngine()->supportsCheckpoints()) {
@@ -1012,7 +1015,7 @@ Status MultiIndexBlock::commit(OperationContext* opCtx,
}
}
- CollectionQueryInfo::get(collection).clearQueryCache(opCtx, collection);
+ CollectionQueryInfo::get(collection).clearQueryCache(opCtx, CollectionPtr(collection));
opCtx->recoveryUnit()->onCommit(
[this](boost::optional<Timestamp> commitTime) { _buildIsCleanedUp = true; });
diff --git a/src/mongo/db/catalog/rename_collection.cpp b/src/mongo/db/catalog/rename_collection.cpp
index d923b0700e4..3b40cd96f64 100644
--- a/src/mongo/db/catalog/rename_collection.cpp
+++ b/src/mongo/db/catalog/rename_collection.cpp
@@ -403,7 +403,7 @@ Status renameCollectionWithinDBForApplyOps(OperationContext* opCtx,
opCtx, source, sourceColl->uuid(), db, target, targetColl->uuid());
if (!status.isOK())
return status;
- targetColl = nullptr;
+ targetColl = CollectionPtr();
}
}
@@ -526,7 +526,7 @@ Status renameCollectionAcrossDatabases(OperationContext* opCtx,
// Return a non-OK status if target exists and dropTarget is not true or if the collection
// is sharded.
const auto targetColl =
- targetDB ? catalog->lookupCollectionByNamespace(opCtx, target) : nullptr;
+ targetDB ? catalog->lookupCollectionByNamespace(opCtx, target) : CollectionPtr();
if (targetColl) {
if (sourceColl->uuid() == targetColl->uuid()) {
invariant(source == target);
@@ -769,7 +769,7 @@ void doLocalRenameIfOptionsAndIndexesHaveNotChanged(OperationContext* opCtx,
AutoGetDb dbLock(opCtx, targetNs.dbName(), MODE_X);
auto collection = dbLock.getDb()
? CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, targetNs)
- : nullptr;
+ : CollectionPtr();
BSONObj collectionOptions = {};
if (collection) {
// We do not include the UUID field in the options comparison. It is ok if the target
diff --git a/src/mongo/db/catalog/rename_collection_test.cpp b/src/mongo/db/catalog/rename_collection_test.cpp
index 40de0350973..2f806091548 100644
--- a/src/mongo/db/catalog/rename_collection_test.cpp
+++ b/src/mongo/db/catalog/rename_collection_test.cpp
@@ -415,7 +415,7 @@ UUID _createCollectionWithUUID(OperationContext* opCtx, const NamespaceString& n
* Returns true if collection exists.
*/
bool _collectionExists(OperationContext* opCtx, const NamespaceString& nss) {
- return AutoGetCollectionForRead(opCtx, nss).getCollection() != nullptr;
+ return static_cast<bool>(AutoGetCollectionForRead(opCtx, nss).getCollection());
}
/**
@@ -509,7 +509,7 @@ CollectionPtr _getCollection_inlock(OperationContext* opCtx, const NamespaceStri
auto databaseHolder = DatabaseHolder::get(opCtx);
auto* db = databaseHolder->getDb(opCtx, DatabaseName(boost::none, nss.db()));
if (!db) {
- return nullptr;
+ return CollectionPtr();
}
return CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, nss);
}
diff --git a/src/mongo/db/catalog_raii.cpp b/src/mongo/db/catalog_raii.cpp
index 44641e75e29..816c1e1100d 100644
--- a/src/mongo/db/catalog_raii.cpp
+++ b/src/mongo/db/catalog_raii.cpp
@@ -428,7 +428,7 @@ Collection* AutoGetCollection::getWritableCollection(OperationContext* opCtx) {
});
// Set to writable collection. We are no longer yieldable.
- _coll = _writableColl;
+ _coll = CollectionPtr(_writableColl);
}
return _writableColl;
}
@@ -624,7 +624,7 @@ Collection* CollectionWriter::getWritableCollection(OperationContext* opCtx) {
}
});
if (usingStoredCollection) {
- _storedCollection = _writableCollection;
+ _storedCollection = CollectionPtr(_writableCollection);
}
}
}
@@ -673,7 +673,7 @@ AutoGetOplog::AutoGetOplog(OperationContext* opCtx, OplogAccessMode mode, Date_t
}
_oplogInfo = LocalOplogInfo::get(opCtx);
- _oplog = _oplogInfo->getCollection();
+ _oplog = CollectionPtr(_oplogInfo->getCollection());
_oplog.makeYieldable(opCtx, LockedCollectionYieldRestore(opCtx, _oplog));
}
diff --git a/src/mongo/db/commands/index_filter_commands_test.cpp b/src/mongo/db/commands/index_filter_commands_test.cpp
index 9dc5f8b88a0..affbea293a8 100644
--- a/src/mongo/db/commands/index_filter_commands_test.cpp
+++ b/src/mongo/db/commands/index_filter_commands_test.cpp
@@ -80,12 +80,12 @@ protected:
}
PlanCacheKey makeClassicKey(const CanonicalQuery& cq) {
- return plan_cache_key_factory::make<PlanCacheKey>(cq, _collection.get());
+ return plan_cache_key_factory::make<PlanCacheKey>(cq, _collectionPtr);
}
sbe::PlanCacheKey makeSbeKey(const CanonicalQuery& cq) {
ASSERT_TRUE(cq.isSbeCompatible());
- return plan_cache_key_factory::make<sbe::PlanCacheKey>(cq, _collection.get());
+ return plan_cache_key_factory::make<sbe::PlanCacheKey>(cq, _collectionPtr);
}
Status clearIndexFilter(const std::string& cmdJson) {
diff --git a/src/mongo/db/commands/plan_cache_commands_test.cpp b/src/mongo/db/commands/plan_cache_commands_test.cpp
index 7c91d584801..7513b9320a4 100644
--- a/src/mongo/db/commands/plan_cache_commands_test.cpp
+++ b/src/mongo/db/commands/plan_cache_commands_test.cpp
@@ -43,7 +43,7 @@ static const NamespaceString nss =
PlanCacheKey makeClassicKey(const CanonicalQuery& cq) {
CollectionMock coll(nss);
- return plan_cache_key_factory::make<PlanCacheKey>(cq, &coll);
+ return plan_cache_key_factory::make<PlanCacheKey>(cq, CollectionPtr(&coll));
}
TEST(PlanCacheCommandsTest, CannotCanonicalizeWithMissingQueryField) {
diff --git a/src/mongo/db/commands/run_aggregate.cpp b/src/mongo/db/commands/run_aggregate.cpp
index 8868ce91a8b..9b5e718643c 100644
--- a/src/mongo/db/commands/run_aggregate.cpp
+++ b/src/mongo/db/commands/run_aggregate.cpp
@@ -833,7 +833,7 @@ Status runAggregate(OperationContext* opCtx,
// collation. We do not inherit the collection's default collation or UUID, since
// the stream may be resuming from a point before the current UUID existed.
auto [collator, match] = PipelineD::resolveCollator(
- opCtx, request.getCollation().get_value_or(BSONObj()), nullptr);
+ opCtx, request.getCollation().get_value_or(BSONObj()), CollectionPtr());
collatorToUse.emplace(std::move(collator));
collatorToUseMatchesDefault = match;
@@ -853,7 +853,7 @@ Status runAggregate(OperationContext* opCtx,
AutoStatsTracker::LogMode::kUpdateTopAndCurOp,
0);
auto [collator, match] = PipelineD::resolveCollator(
- opCtx, request.getCollation().get_value_or(BSONObj()), nullptr);
+ opCtx, request.getCollation().get_value_or(BSONObj()), CollectionPtr());
collatorToUse.emplace(std::move(collator));
collatorToUseMatchesDefault = match;
tassert(6235101,
diff --git a/src/mongo/db/commands/test_commands.cpp b/src/mongo/db/commands/test_commands.cpp
index 4cea5de379d..a11d35b4cbd 100644
--- a/src/mongo/db/commands/test_commands.cpp
+++ b/src/mongo/db/commands/test_commands.cpp
@@ -35,6 +35,7 @@
#include "mongo/db/catalog/capped_collection_maintenance.h"
#include "mongo/db/catalog/capped_utils.h"
#include "mongo/db/catalog/collection_write_path.h"
+#include "mongo/db/catalog/collection_yield_restore.h"
#include "mongo/db/client.h"
#include "mongo/db/commands.h"
#include "mongo/db/commands/test_commands_enabled.h"
@@ -106,9 +107,11 @@ public:
CollectionPtr collection =
CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, nss);
if (!collection) {
- collection = db->createCollection(opCtx, nss);
+ collection = CollectionPtr(db->createCollection(opCtx, nss));
uassert(ErrorCodes::CannotCreateCollection, "could not create collection", collection);
}
+ collection.makeYieldable(opCtx, LockedCollectionYieldRestore(opCtx, collection));
+
OpDebug* const nullOpDebug = nullptr;
Status status = collection_internal::insertDocument(
opCtx, collection, InsertStatement(obj), nullOpDebug, false);
diff --git a/src/mongo/db/db_raii.h b/src/mongo/db/db_raii.h
index dadd1b92f50..765beaa5ed2 100644
--- a/src/mongo/db/db_raii.h
+++ b/src/mongo/db/db_raii.h
@@ -247,7 +247,7 @@ private:
AutoGetDb _autoDb;
std::vector<CollectionNamespaceOrUUIDLock> _collLocks;
- CollectionPtr _coll = nullptr;
+ CollectionPtr _coll;
std::shared_ptr<const ViewDefinition> _view;
// If the object was instantiated with a UUID, contains the resolved namespace, otherwise it is
diff --git a/src/mongo/db/dbhelpers.cpp b/src/mongo/db/dbhelpers.cpp
index a05560fd078..d2d7137cd8c 100644
--- a/src/mongo/db/dbhelpers.cpp
+++ b/src/mongo/db/dbhelpers.cpp
@@ -357,7 +357,8 @@ void Helpers::emptyCollection(OperationContext* opCtx, const NamespaceString& ns
repl::UnreplicatedWritesBlock uwb(opCtx);
CollectionPtr collection = context.db()
? CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, nss)
- : nullptr;
+ : CollectionPtr();
+
deleteObjects(opCtx, collection, nss, BSONObj(), false);
}
diff --git a/src/mongo/db/index_build_entry_helpers.cpp b/src/mongo/db/index_build_entry_helpers.cpp
index a0da462cfbc..c462bf91a92 100644
--- a/src/mongo/db/index_build_entry_helpers.cpp
+++ b/src/mongo/db/index_build_entry_helpers.cpp
@@ -182,8 +182,8 @@ void ensureIndexBuildEntriesNamespaceExists(OperationContext* opCtx) {
AutoGetCollection autoColl(
opCtx, NamespaceString::kIndexBuildEntryNamespace, LockMode::MODE_IX);
CollectionOptions defaultCollectionOptions;
- CollectionPtr collection = db->createCollection(
- opCtx, NamespaceString::kIndexBuildEntryNamespace, defaultCollectionOptions);
+ CollectionPtr collection = CollectionPtr(db->createCollection(
+ opCtx, NamespaceString::kIndexBuildEntryNamespace, defaultCollectionOptions));
// Ensure the collection exists.
invariant(collection);
diff --git a/src/mongo/db/op_observer/user_write_block_mode_op_observer_test.cpp b/src/mongo/db/op_observer/user_write_block_mode_op_observer_test.cpp
index 72fd24a8e79..a1a6a230907 100644
--- a/src/mongo/db/op_observer/user_write_block_mode_op_observer_test.cpp
+++ b/src/mongo/db/op_observer/user_write_block_mode_op_observer_test.cpp
@@ -141,7 +141,7 @@ protected:
opObserver.onStartIndexBuild(opCtx, nss, uuid, uuid, {}, false);
opObserver.onStartIndexBuildSinglePhase(opCtx, nss);
opObserver.onCreateCollection(
- opCtx, nullptr, nss, {}, BSONObj(), OplogSlot(), false);
+ opCtx, CollectionPtr(), nss, {}, BSONObj(), OplogSlot(), false);
opObserver.onCollMod(opCtx, nss, uuid, BSONObj(), {}, boost::none);
opObserver.onDropDatabase(opCtx, DatabaseName(boost::none, nss.db()));
opObserver.onDropCollection(
@@ -168,7 +168,7 @@ protected:
AssertionException);
ASSERT_THROWS(opObserver.onStartIndexBuildSinglePhase(opCtx, nss), AssertionException);
ASSERT_THROWS(opObserver.onCreateCollection(
- opCtx, nullptr, nss, {}, BSONObj(), OplogSlot(), false),
+ opCtx, CollectionPtr(), nss, {}, BSONObj(), OplogSlot(), false),
AssertionException);
ASSERT_THROWS(opObserver.onCollMod(opCtx, nss, uuid, BSONObj(), {}, boost::none),
AssertionException);
diff --git a/src/mongo/db/ops/update.cpp b/src/mongo/db/ops/update.cpp
index f019e0d780c..65807d64022 100644
--- a/src/mongo/db/ops/update.cpp
+++ b/src/mongo/db/ops/update.cpp
@@ -33,6 +33,7 @@
#include "mongo/db/ops/update.h"
#include "mongo/db/catalog/collection.h"
+#include "mongo/db/catalog/collection_yield_restore.h"
#include "mongo/db/catalog/database.h"
#include "mongo/db/catalog/database_holder.h"
#include "mongo/db/client.h"
@@ -83,11 +84,13 @@ UpdateResult update(OperationContext* opCtx, Database* db, const UpdateRequest&
<< nsString << " during upsert"));
}
WriteUnitOfWork wuow(opCtx);
- collection = db->createCollection(opCtx, nsString, CollectionOptions());
+ collection = CollectionPtr(db->createCollection(opCtx, nsString, CollectionOptions()));
invariant(collection);
wuow.commit();
});
+ collection.makeYieldable(opCtx, LockedCollectionYieldRestore(opCtx, collection));
+
// Parse the update, get an executor for it, run the executor, get stats out.
const ExtensionsCallbackReal extensionsCallback(opCtx, &request.getNamespaceString());
ParsedUpdate parsedUpdate(opCtx, &request, extensionsCallback);
diff --git a/src/mongo/db/pipeline/process_interface/common_mongod_process_interface.cpp b/src/mongo/db/pipeline/process_interface/common_mongod_process_interface.cpp
index 471727946c4..ec6faecc0df 100644
--- a/src/mongo/db/pipeline/process_interface/common_mongod_process_interface.cpp
+++ b/src/mongo/db/pipeline/process_interface/common_mongod_process_interface.cpp
@@ -601,8 +601,8 @@ bool CommonMongodProcessInterface::fieldsHaveSupportingUniqueIndex(
Lock::CollectionLock collLock(opCtx, nss, MODE_IS);
auto databaseHolder = DatabaseHolder::get(opCtx);
auto db = databaseHolder->getDb(opCtx, nss.dbName());
- auto collection =
- db ? CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, nss) : nullptr;
+ auto collection = db ? CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, nss)
+ : CollectionPtr();
if (!collection) {
return fieldPaths == std::set<FieldPath>{"_id"};
}
diff --git a/src/mongo/db/query/cqf_command_utils.cpp b/src/mongo/db/query/cqf_command_utils.cpp
index 9cb861a8695..55a8987acec 100644
--- a/src/mongo/db/query/cqf_command_utils.cpp
+++ b/src/mongo/db/query/cqf_command_utils.cpp
@@ -1053,7 +1053,7 @@ bool isEligibleCommon(const RequestType& request,
request.getLegacyRuntimeConstants();
bool unsupportedIndexType = [&]() {
- if (collection == nullptr)
+ if (!collection)
return false;
const IndexCatalog& indexCatalog = *collection->getIndexCatalog();
@@ -1079,7 +1079,7 @@ bool isEligibleCommon(const RequestType& request,
}();
bool unsupportedCollectionType = [&]() {
- if (collection == nullptr)
+ if (!collection)
return false;
if (collection->isClustered() || !collection->getCollectionOptions().collation.isEmpty() ||
diff --git a/src/mongo/db/query/cqf_get_executor.cpp b/src/mongo/db/query/cqf_get_executor.cpp
index 08ec36e6839..c031770a454 100644
--- a/src/mongo/db/query/cqf_get_executor.cpp
+++ b/src/mongo/db/query/cqf_get_executor.cpp
@@ -376,7 +376,7 @@ static void populateAdditionalScanDefs(
// multi-collection queries.
AutoGetCollectionForReadCommandMaybeLockFree ctx(opCtx, involvedNss);
const CollectionPtr& collection = ctx ? ctx.getCollection() : CollectionPtr::null;
- const bool collectionExists = collection != nullptr;
+ const bool collectionExists = static_cast<bool>(collection);
const std::string uuidStr =
collectionExists ? collection->uuid().toString() : "<missing_uuid>";
const std::string collNameStr = involvedNss.coll().toString();
@@ -504,7 +504,7 @@ Metadata populateMetadata(boost::intrusive_ptr<ExpressionContext> expCtx,
QueryHints& queryHints,
PrefixId& prefixId) {
auto opCtx = expCtx->opCtx;
- const bool collectionExists = collection != nullptr;
+ const bool collectionExists = static_cast<bool>(collection);
// Add the base collection metadata.
opt::unordered_map<std::string, optimizer::IndexDefinition> indexDefs;
@@ -660,7 +660,7 @@ std::unique_ptr<PlanExecutor, PlanExecutor::Deleter> getSBEExecutorViaCascadesOp
validateCommandOptions(canonicalQuery.get(), collection, indexHint, involvedCollections);
const bool requireRID = canonicalQuery ? canonicalQuery->getForceGenerateRecordId() : false;
- const bool collectionExists = collection != nullptr;
+ const bool collectionExists = static_cast<bool>(collection);
const std::string uuidStr = collectionExists ? collection->uuid().toString() : "<missing_uuid>";
const std::string collNameStr = nss.coll().toString();
const std::string scanDefName = collNameStr + "_" + uuidStr;
diff --git a/src/mongo/db/repl/oplog.cpp b/src/mongo/db/repl/oplog.cpp
index 0a086206e73..f3620ad9ac0 100644
--- a/src/mongo/db/repl/oplog.cpp
+++ b/src/mongo/db/repl/oplog.cpp
@@ -222,8 +222,9 @@ void createIndexForApplyOps(OperationContext* opCtx,
// Check if collection exists.
auto databaseHolder = DatabaseHolder::get(opCtx);
auto db = databaseHolder->getDb(opCtx, indexNss.dbName());
- auto indexCollection =
- db ? CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, indexNss) : nullptr;
+ auto indexCollection = db
+ ? CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, indexNss)
+ : CollectionPtr();
uassert(ErrorCodes::NamespaceNotFound,
str::stream() << "Failed to create index due to missing collection: " << indexNss.ns(),
indexCollection);
@@ -533,7 +534,7 @@ OpTime logOp(OperationContext* opCtx, MutableOplogEntry* oplogEntry) {
oplogEntry->getNss(),
&records,
timestamps,
- oplog,
+ CollectionPtr(oplog),
slot,
wallClockTime,
isAbortIndexBuild);
@@ -647,11 +648,17 @@ std::vector<OpTime> logInsertOps(
invariant(!opTimes.empty());
auto lastOpTime = opTimes.back();
invariant(!lastOpTime.isNull());
- const auto& oplog = oplogInfo->getCollection();
+ const Collection* oplog = oplogInfo->getCollection();
auto wallClockTime = oplogEntryTemplate->getWallClockTime();
const bool isAbortIndexBuild = false;
- _logOpsInner(
- opCtx, nss, &records, timestamps, oplog, lastOpTime, wallClockTime, isAbortIndexBuild);
+ _logOpsInner(opCtx,
+ nss,
+ &records,
+ timestamps,
+ CollectionPtr(oplog),
+ lastOpTime,
+ wallClockTime,
+ isAbortIndexBuild);
wuow.commit();
return opTimes;
}
@@ -1350,7 +1357,7 @@ Status applyOperation_inlock(OperationContext* opCtx,
}
NamespaceString requestNss;
- CollectionPtr collection = nullptr;
+ CollectionPtr collection;
if (auto uuid = op.getUuid()) {
auto catalog = CollectionCatalog::get(opCtx);
collection = catalog->lookupCollectionByUUID(opCtx, uuid.value());
@@ -1388,8 +1395,7 @@ Status applyOperation_inlock(OperationContext* opCtx,
if (op.getObject2())
o2 = op.getObject2().value();
- const IndexCatalog* indexCatalog =
- collection == nullptr ? nullptr : collection->getIndexCatalog();
+ const IndexCatalog* indexCatalog = !collection ? nullptr : collection->getIndexCatalog();
const bool haveWrappingWriteUnitOfWork = opCtx->lockState()->inAWriteUnitOfWork();
uassert(ErrorCodes::CommandNotSupportedOnView,
str::stream() << "applyOps not supported on view: " << requestNss.ns(),
@@ -1770,7 +1776,7 @@ Status applyOperation_inlock(OperationContext* opCtx,
// such as an updateCriteria of the form
// { _id:..., { x : {$size:...} }
// thus this is not ideal.
- if (collection == nullptr ||
+ if (!collection ||
(indexCatalog->haveIdIndex(opCtx) &&
Helpers::findById(opCtx, collection, updateCriteria).isNull()) ||
// capped collections won't have an _id index
diff --git a/src/mongo/db/repl/oplog_applier_impl_test_fixture.cpp b/src/mongo/db/repl/oplog_applier_impl_test_fixture.cpp
index c543979c1d4..3a7f642bb85 100644
--- a/src/mongo/db/repl/oplog_applier_impl_test_fixture.cpp
+++ b/src/mongo/db/repl/oplog_applier_impl_test_fixture.cpp
@@ -503,7 +503,7 @@ void createDatabase(OperationContext* opCtx, StringData dbName) {
}
bool collectionExists(OperationContext* opCtx, const NamespaceString& nss) {
- return AutoGetCollectionForRead(opCtx, nss).getCollection() != nullptr;
+ return static_cast<bool>(AutoGetCollectionForRead(opCtx, nss).getCollection());
}
void createIndex(OperationContext* opCtx,
diff --git a/src/mongo/db/repl/rollback_test_fixture.cpp b/src/mongo/db/repl/rollback_test_fixture.cpp
index f18d7e593b7..f88973b38b9 100644
--- a/src/mongo/db/repl/rollback_test_fixture.cpp
+++ b/src/mongo/db/repl/rollback_test_fixture.cpp
@@ -252,7 +252,7 @@ void RollbackTest::_insertDocument(OperationContext* opCtx,
} else {
CollectionOptions options;
options.uuid = UUID::gen();
- insertDoc(_createCollection(opCtx, nss, options));
+ insertDoc(CollectionPtr(_createCollection(opCtx, nss, options)));
}
}
diff --git a/src/mongo/db/repl/tenant_migration_util.cpp b/src/mongo/db/repl/tenant_migration_util.cpp
index c89f933bb2c..7a573c325cf 100644
--- a/src/mongo/db/repl/tenant_migration_util.cpp
+++ b/src/mongo/db/repl/tenant_migration_util.cpp
@@ -115,7 +115,8 @@ void createOplogViewForTenantMigrations(OperationContext* opCtx, Database* db) {
CollectionPtr coll = CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(
opCtx, NamespaceString(db->getSystemViewsName()));
if (!coll) {
- coll = db->createCollection(opCtx, NamespaceString(db->getSystemViewsName()));
+ coll = CollectionPtr(
+ db->createCollection(opCtx, NamespaceString(db->getSystemViewsName())));
}
invariant(coll);
wuow.commit();
diff --git a/src/mongo/db/startup_recovery.cpp b/src/mongo/db/startup_recovery.cpp
index 57546bd2bca..49207205bfe 100644
--- a/src/mongo/db/startup_recovery.cpp
+++ b/src/mongo/db/startup_recovery.cpp
@@ -164,8 +164,9 @@ Status buildMissingIdIndex(OperationContext* opCtx, Collection* collection) {
indexer.abortIndexBuild(opCtx, collWriter, MultiIndexBlock::kNoopOnCleanUpFn);
});
+ CollectionPtr collPtr(collection);
const auto indexCatalog = collection->getIndexCatalog();
- const auto idIndexSpec = indexCatalog->getDefaultIdIndexSpec(collection);
+ const auto idIndexSpec = indexCatalog->getDefaultIdIndexSpec(collPtr);
CollectionWriter collWriter(collection);
auto swSpecs = indexer.init(opCtx, collWriter, idIndexSpec, MultiIndexBlock::kNoopOnInitFn);
@@ -173,12 +174,12 @@ Status buildMissingIdIndex(OperationContext* opCtx, Collection* collection) {
return swSpecs.getStatus();
}
- auto status = indexer.insertAllDocumentsInCollection(opCtx, collection);
+ auto status = indexer.insertAllDocumentsInCollection(opCtx, collPtr);
if (!status.isOK()) {
return status;
}
- status = indexer.checkConstraints(opCtx, collection);
+ status = indexer.checkConstraints(opCtx, collPtr);
if (!status.isOK()) {
return status;
}
diff --git a/src/mongo/dbtests/catalogtests.cpp b/src/mongo/dbtests/catalogtests.cpp
index 929805a58a2..4c802579522 100644
--- a/src/mongo/dbtests/catalogtests.cpp
+++ b/src/mongo/dbtests/catalogtests.cpp
@@ -45,7 +45,8 @@ namespace mongo {
namespace {
bool collectionExists(OperationContext* opCtx, NamespaceString nss) {
- return CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, nss) != nullptr;
+ return static_cast<bool>(
+ CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, nss));
}
class ConcurrentCreateCollectionTest {
diff --git a/src/mongo/dbtests/counttests.cpp b/src/mongo/dbtests/counttests.cpp
index b1c5aa77295..90c72adde34 100644
--- a/src/mongo/dbtests/counttests.cpp
+++ b/src/mongo/dbtests/counttests.cpp
@@ -64,7 +64,7 @@ public:
wunit.commit();
- _collection = collection;
+ _collection = CollectionPtr(collection);
}
}
diff --git a/src/mongo/dbtests/dbhelper_tests.cpp b/src/mongo/dbtests/dbhelper_tests.cpp
index ba9bfbc7e5e..27f2c5af53d 100644
--- a/src/mongo/dbtests/dbhelper_tests.cpp
+++ b/src/mongo/dbtests/dbhelper_tests.cpp
@@ -135,8 +135,9 @@ public:
CollectionPtr collection1;
{
WriteUnitOfWork wuow(opCtx1.get());
- collection1 = db->createCollection(opCtx1.get(), nss, CollectionOptions(), true);
- ASSERT_TRUE(collection1 != nullptr);
+ collection1 =
+ CollectionPtr(db->createCollection(opCtx1.get(), nss, CollectionOptions(), true));
+ ASSERT_TRUE(collection1);
ASSERT_TRUE(
collection_internal::insertDocument(
opCtx1.get(), collection1, InsertStatement(doc), nullptr /* opDebug */, false)
@@ -194,7 +195,7 @@ private:
WriteUnitOfWork wuow2(opCtx2);
auto collection2 =
CollectionCatalog::get(opCtx2)->lookupCollectionByNamespace(opCtx2, nss);
- ASSERT(collection2 != nullptr);
+ ASSERT(collection2);
auto lastApplied = repl::ReplicationCoordinator::get(opCtx2->getServiceContext())
->getMyLastAppliedOpTime()
.getTimestamp();
@@ -241,7 +242,7 @@ private:
WriteUnitOfWork wuow2(opCtx2);
auto collection2 =
CollectionCatalog::get(opCtx2)->lookupCollectionByNamespace(opCtx2, nss);
- ASSERT(collection2 != nullptr);
+ ASSERT(collection2);
BSONObj res;
ASSERT_THROWS(Helpers::findByIdAndNoopUpdate(opCtx2, collection2, idQuery, res),
diff --git a/src/mongo/dbtests/pdfiletests.cpp b/src/mongo/dbtests/pdfiletests.cpp
index c5d41844fa6..9780e7a80a8 100644
--- a/src/mongo/dbtests/pdfiletests.cpp
+++ b/src/mongo/dbtests/pdfiletests.cpp
@@ -73,7 +73,7 @@ public:
CollectionPtr coll =
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!coll) {
- coll = _context.db()->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(_context.db()->createCollection(&_opCtx, nss()));
}
ASSERT(coll);
OpDebug* const nullOpDebug = nullptr;
diff --git a/src/mongo/dbtests/query_stage_and.cpp b/src/mongo/dbtests/query_stage_and.cpp
index bd9f7154dbe..d25ca0d7622 100644
--- a/src/mongo/dbtests/query_stage_and.cpp
+++ b/src/mongo/dbtests/query_stage_and.cpp
@@ -799,7 +799,7 @@ public:
CollectionPtr coll = ctx.getCollection();
if (!coll) {
WriteUnitOfWork wuow(&_opCtx);
- coll = db->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(db->createCollection(&_opCtx, nss()));
wuow.commit();
}
diff --git a/src/mongo/dbtests/query_stage_count.cpp b/src/mongo/dbtests/query_stage_count.cpp
index c4b7df24d5d..0f1a3e97a3a 100644
--- a/src/mongo/dbtests/query_stage_count.cpp
+++ b/src/mongo/dbtests/query_stage_count.cpp
@@ -75,7 +75,7 @@ public:
<< "x_1"
<< "v" << 1))
.status_with_transitional_ignore();
- _coll = coll;
+ _coll = CollectionPtr(coll);
for (int i = 0; i < kDocuments; i++) {
insert(BSON(GENOID << "x" << i));
diff --git a/src/mongo/dbtests/query_stage_fetch.cpp b/src/mongo/dbtests/query_stage_fetch.cpp
index 60ff39c0010..4075fa65a8b 100644
--- a/src/mongo/dbtests/query_stage_fetch.cpp
+++ b/src/mongo/dbtests/query_stage_fetch.cpp
@@ -106,7 +106,7 @@ public:
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!coll) {
WriteUnitOfWork wuow(&_opCtx);
- coll = db->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(db->createCollection(&_opCtx, nss()));
wuow.commit();
}
@@ -173,7 +173,7 @@ public:
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!coll) {
WriteUnitOfWork wuow(&_opCtx);
- coll = db->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(db->createCollection(&_opCtx, nss()));
wuow.commit();
}
diff --git a/src/mongo/dbtests/query_stage_ixscan.cpp b/src/mongo/dbtests/query_stage_ixscan.cpp
index 9273003b7c5..6cde679ae98 100644
--- a/src/mongo/dbtests/query_stage_ixscan.cpp
+++ b/src/mongo/dbtests/query_stage_ixscan.cpp
@@ -59,7 +59,7 @@ public:
_ctx.db()->dropCollection(&_opCtx, nss()).transitional_ignore();
_coll = _ctx.db()->createCollection(&_opCtx, nss());
- _collPtr = _coll;
+ _collPtr = CollectionPtr(_coll);
ASSERT_OK(_coll->getIndexCatalog()->createIndexOnEmptyCollection(
&_opCtx,
diff --git a/src/mongo/dbtests/query_stage_sort.cpp b/src/mongo/dbtests/query_stage_sort.cpp
index 89426285c20..b8403c7ca8c 100644
--- a/src/mongo/dbtests/query_stage_sort.cpp
+++ b/src/mongo/dbtests/query_stage_sort.cpp
@@ -271,7 +271,7 @@ public:
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!coll) {
WriteUnitOfWork wuow(&_opCtx);
- coll = db->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(db->createCollection(&_opCtx, nss()));
wuow.commit();
}
@@ -294,7 +294,7 @@ public:
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!coll) {
WriteUnitOfWork wuow(&_opCtx);
- coll = db->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(db->createCollection(&_opCtx, nss()));
wuow.commit();
}
@@ -326,7 +326,7 @@ public:
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!coll) {
WriteUnitOfWork wuow(&_opCtx);
- coll = db->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(db->createCollection(&_opCtx, nss()));
wuow.commit();
}
@@ -352,7 +352,7 @@ public:
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!coll) {
WriteUnitOfWork wuow(&_opCtx);
- coll = db->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(db->createCollection(&_opCtx, nss()));
wuow.commit();
}
@@ -481,7 +481,7 @@ public:
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!coll) {
WriteUnitOfWork wuow(&_opCtx);
- coll = db->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(db->createCollection(&_opCtx, nss()));
wuow.commit();
}
@@ -587,7 +587,7 @@ public:
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!coll) {
WriteUnitOfWork wuow(&_opCtx);
- coll = db->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(db->createCollection(&_opCtx, nss()));
wuow.commit();
}
diff --git a/src/mongo/dbtests/query_stage_subplan.cpp b/src/mongo/dbtests/query_stage_subplan.cpp
index d4b7005b93c..13ce49ecad2 100644
--- a/src/mongo/dbtests/query_stage_subplan.cpp
+++ b/src/mongo/dbtests/query_stage_subplan.cpp
@@ -575,7 +575,7 @@ TEST_F(QueryStageSubplanTest, ShouldReportErrorIfKilledDuringPlanning) {
}
TEST_F(QueryStageSubplanTest, ShouldThrowOnRestoreIfIndexDroppedBeforePlanSelection) {
- CollectionPtr collection = nullptr;
+ CollectionPtr collection;
{
dbtests::WriteContextForTests ctx{opCtx(), nss.ns()};
addIndex(BSON("p1" << 1 << "opt1" << 1));
diff --git a/src/mongo/dbtests/querytests.cpp b/src/mongo/dbtests/querytests.cpp
index 16716f90191..51dac9cd2f6 100644
--- a/src/mongo/dbtests/querytests.cpp
+++ b/src/mongo/dbtests/querytests.cpp
@@ -83,7 +83,7 @@ public:
if (collection) {
_database->dropCollection(&_opCtx, nss()).transitional_ignore();
}
- collection = _database->createCollection(&_opCtx, nss());
+ collection = CollectionPtr(_database->createCollection(&_opCtx, nss()));
wunit.commit();
_collection = std::move(collection);
}
@@ -209,10 +209,11 @@ public:
WriteUnitOfWork wunit(&_opCtx);
Database* db = ctx.db();
if (CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss())) {
- _collection = nullptr;
+ _collection = CollectionPtr();
db->dropCollection(&_opCtx, nss()).transitional_ignore();
}
- _collection = db->createCollection(&_opCtx, nss(), CollectionOptions(), false);
+ _collection =
+ CollectionPtr(db->createCollection(&_opCtx, nss(), CollectionOptions(), false));
wunit.commit();
}
ASSERT(_collection);
diff --git a/src/mongo/dbtests/repltests.cpp b/src/mongo/dbtests/repltests.cpp
index 00386c41938..9bf826dfba1 100644
--- a/src/mongo/dbtests/repltests.cpp
+++ b/src/mongo/dbtests/repltests.cpp
@@ -130,7 +130,7 @@ public:
CollectionPtr c =
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!c) {
- c = ctx.db()->createCollection(&_opCtx, nss());
+ c = CollectionPtr(ctx.db()->createCollection(&_opCtx, nss()));
}
ASSERT(c->getIndexCatalog()->haveIdIndex(&_opCtx));
@@ -197,7 +197,7 @@ protected:
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!coll) {
WriteUnitOfWork wunit(&_opCtx);
- coll = db->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(db->createCollection(&_opCtx, nss()));
wunit.commit();
}
@@ -293,7 +293,7 @@ protected:
CollectionPtr coll =
CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss());
if (!coll) {
- coll = db->createCollection(&_opCtx, nss());
+ coll = CollectionPtr(db->createCollection(&_opCtx, nss()));
}
auto lastApplied = repl::ReplicationCoordinator::get(_opCtx.getServiceContext())