diff options
author | ADAM David Alan Martin <adam.martin@10gen.com> | 2018-04-27 16:09:09 -0400 |
---|---|---|
committer | ADAM David Alan Martin <adam.martin@10gen.com> | 2018-04-27 17:23:21 -0400 |
commit | 2227f272a7a0a3e43625cb2d4a2704e1ccb6f893 (patch) | |
tree | eeb3a3fe22df48b6819a5d7e719befd680b3dedc /src/mongo/db/catalog | |
parent | 7c25d109bda4c56cc329ee4f1a1521a2f9bc690a (diff) | |
download | mongo-2227f272a7a0a3e43625cb2d4a2704e1ccb6f893.tar.gz |
SERVER-32645 Create a shim helper framework.
The `MONGO_DECLARE_SHIM`, `MONGO_DEFINE_SHIM`, and `MONGO_REGISTER_SHIM`
macros can be used to create specialized types which are customization
and auto-registration points for late-binding functions. In some
sense they work like weak-symbols; however, they also are useful for
tracking dependencies upon shimmed-out implementations.
Diffstat (limited to 'src/mongo/db/catalog')
30 files changed, 250 insertions, 538 deletions
diff --git a/src/mongo/db/catalog/SConscript b/src/mongo/db/catalog/SConscript index c2f57ca82a4..5ed9e0ad896 100644 --- a/src/mongo/db/catalog/SConscript +++ b/src/mongo/db/catalog/SConscript @@ -130,6 +130,7 @@ env.Library( "database.cpp", ], LIBDEPS=[ + '$BUILD_DIR/mongo/base', ], ) diff --git a/src/mongo/db/catalog/catalog_control.cpp b/src/mongo/db/catalog/catalog_control.cpp index 684862b6a9f..f97cb254d72 100644 --- a/src/mongo/db/catalog/catalog_control.cpp +++ b/src/mongo/db/catalog/catalog_control.cpp @@ -56,7 +56,7 @@ void closeCatalog(OperationContext* opCtx) { // Close all databases. log() << "closeCatalog: closing all databases"; constexpr auto reason = "closing databases for closeCatalog"; - dbHolder().closeAll(opCtx, reason); + DatabaseHolder::getDatabaseHolder().closeAll(opCtx, reason); // Close the storage engine's catalog. log() << "closeCatalog: closing storage engine catalog"; @@ -145,7 +145,7 @@ void openCatalog(OperationContext* opCtx) { storageEngine->listDatabases(&databasesToOpen); for (auto&& dbName : databasesToOpen) { LOG(1) << "openCatalog: dbholder reopening database " << dbName; - auto db = dbHolder().openDb(opCtx, dbName); + auto db = DatabaseHolder::getDatabaseHolder().openDb(opCtx, dbName); invariant(db, str::stream() << "failed to reopen database " << dbName); std::list<std::string> collections; diff --git a/src/mongo/db/catalog/collection.cpp b/src/mongo/db/catalog/collection.cpp index 13bb493e5a6..915980a34d3 100644 --- a/src/mongo/db/catalog/collection.cpp +++ b/src/mongo/db/catalog/collection.cpp @@ -70,56 +70,14 @@ namespace mongo { // Emit the vtable in this TU Collection::Impl::~Impl() = default; -namespace { -stdx::function<Collection::factory_function_type> factory; -} // namespace +MONGO_DEFINE_SHIM(Collection::makeImpl); -void Collection::registerFactory(decltype(factory) newFactory) { - factory = std::move(newFactory); -} +MONGO_DEFINE_SHIM(Collection::parseValidationLevel); -auto Collection::makeImpl(Collection* _this, - OperationContext* const opCtx, - const StringData fullNS, - OptionalCollectionUUID uuid, - CollectionCatalogEntry* const details, - RecordStore* const recordStore, - DatabaseCatalogEntry* const dbce) -> std::unique_ptr<Impl> { - return factory(_this, opCtx, fullNS, uuid, details, recordStore, dbce); -} +MONGO_DEFINE_SHIM(Collection::parseValidationAction); void Collection::TUHook::hook() noexcept {} - -namespace { -stdx::function<decltype(Collection::parseValidationLevel)> parseValidationLevelImpl; -} // namespace - -void Collection::registerParseValidationLevelImpl( - stdx::function<decltype(parseValidationLevel)> impl) { - parseValidationLevelImpl = std::move(impl); -} - -auto Collection::parseValidationLevel(const StringData data) -> StatusWith<ValidationLevel> { - return parseValidationLevelImpl(data); -} - -namespace { -stdx::function<decltype(Collection::parseValidationAction)> parseValidationActionImpl; -} // namespace - -void Collection::registerParseValidationActionImpl( - stdx::function<decltype(parseValidationAction)> impl) { - parseValidationActionImpl = std::move(impl); -} - -auto Collection::parseValidationAction(const StringData data) -> StatusWith<ValidationAction> { - return parseValidationActionImpl(data); -} -} // namespace mongo - - -namespace mongo { std::string CompactOptions::toString() const { std::stringstream ss; ss << "paddingMode: "; diff --git a/src/mongo/db/catalog/collection.h b/src/mongo/db/catalog/collection.h index 042c7a3a0c5..9c41f8478f3 100644 --- a/src/mongo/db/catalog/collection.h +++ b/src/mongo/db/catalog/collection.h @@ -330,19 +330,16 @@ public: virtual const CollatorInterface* getDefaultCollator() const = 0; }; -private: - static std::unique_ptr<Impl> makeImpl(Collection* _this, - OperationContext* opCtx, - StringData fullNS, - OptionalCollectionUUID uuid, - CollectionCatalogEntry* details, - RecordStore* recordStore, - DatabaseCatalogEntry* dbce); - public: - using factory_function_type = decltype(makeImpl); - - static void registerFactory(stdx::function<factory_function_type> factory); + static MONGO_DECLARE_SHIM((Collection * _this, + OperationContext* opCtx, + StringData fullNS, + OptionalCollectionUUID uuid, + CollectionCatalogEntry* details, + RecordStore* recordStore, + DatabaseCatalogEntry* dbce, + PrivateTo<Collection>) + ->std::unique_ptr<Impl>) makeImpl; explicit inline Collection(OperationContext* const opCtx, const StringData fullNS, @@ -350,7 +347,8 @@ public: CollectionCatalogEntry* const details, // does not own RecordStore* const recordStore, // does not own DatabaseCatalogEntry* const dbce) // does not own - : _pimpl(makeImpl(this, opCtx, fullNS, uuid, details, recordStore, dbce)) { + : _pimpl(makeImpl( + this, opCtx, fullNS, uuid, details, recordStore, dbce, PrivateCall<Collection>{})) { this->_impl().init(opCtx); } @@ -627,13 +625,8 @@ public: opCtx, validator, allowedFeatures, maxFeatureCompatibilityVersion); } - static StatusWith<ValidationLevel> parseValidationLevel(StringData); - static StatusWith<ValidationAction> parseValidationAction(StringData); - - static void registerParseValidationLevelImpl( - stdx::function<decltype(parseValidationLevel)> impl); - static void registerParseValidationActionImpl( - stdx::function<decltype(parseValidationAction)> impl); + static MONGO_DECLARE_SHIM((StringData)->StatusWith<ValidationLevel>) parseValidationLevel; + static MONGO_DECLARE_SHIM((StringData)->StatusWith<ValidationAction>) parseValidationAction; /** * Sets the validator for this collection. diff --git a/src/mongo/db/catalog/collection_impl.cpp b/src/mongo/db/catalog/collection_impl.cpp index 74909e46f0c..f08d5be410e 100644 --- a/src/mongo/db/catalog/collection_impl.cpp +++ b/src/mongo/db/catalog/collection_impl.cpp @@ -76,34 +76,30 @@ namespace mongo { -namespace { -MONGO_INITIALIZER(InitializeCollectionFactory)(InitializerContext* const) { - Collection::registerFactory( - [](Collection* const _this, - OperationContext* const opCtx, - const StringData fullNS, - OptionalCollectionUUID uuid, - CollectionCatalogEntry* const details, - RecordStore* const recordStore, - DatabaseCatalogEntry* const dbce) -> std::unique_ptr<Collection::Impl> { - return stdx::make_unique<CollectionImpl>( - _this, opCtx, fullNS, uuid, details, recordStore, dbce); - }); - return Status::OK(); +MONGO_REGISTER_SHIM(Collection::makeImpl) +(Collection* const _this, + OperationContext* const opCtx, + const StringData fullNS, + OptionalCollectionUUID uuid, + CollectionCatalogEntry* const details, + RecordStore* const recordStore, + DatabaseCatalogEntry* const dbce, + PrivateTo<Collection>) + ->std::unique_ptr<Collection::Impl> { + return std::make_unique<CollectionImpl>(_this, opCtx, fullNS, uuid, details, recordStore, dbce); } -MONGO_INITIALIZER(InitializeParseValidationLevelImpl)(InitializerContext* const) { - Collection::registerParseValidationLevelImpl( - [](const StringData data) { return CollectionImpl::parseValidationLevel(data); }); - return Status::OK(); +MONGO_REGISTER_SHIM(Collection::parseValidationLevel) +(const StringData data)->StatusWith<Collection::ValidationLevel> { + return CollectionImpl::parseValidationLevel(data); } -MONGO_INITIALIZER(InitializeParseValidationActionImpl)(InitializerContext* const) { - Collection::registerParseValidationActionImpl( - [](const StringData data) { return CollectionImpl::parseValidationAction(data); }); - return Status::OK(); +MONGO_REGISTER_SHIM(Collection::parseValidationAction) +(const StringData data)->StatusWith<Collection::ValidationAction> { + return CollectionImpl::parseValidationAction(data); } +namespace { // Used below to fail during inserts. MONGO_FP_DECLARE(failCollectionInserts); diff --git a/src/mongo/db/catalog/collection_info_cache.cpp b/src/mongo/db/catalog/collection_info_cache.cpp index 4bd1e4715f7..0a36bb07448 100644 --- a/src/mongo/db/catalog/collection_info_cache.cpp +++ b/src/mongo/db/catalog/collection_info_cache.cpp @@ -33,20 +33,9 @@ #include "mongo/db/catalog/collection_info_cache.h" namespace mongo { -namespace { -stdx::function<CollectionInfoCache::factory_function_type> factory; -} // namespace - CollectionInfoCache::Impl::~Impl() = default; -void CollectionInfoCache::registerFactory(decltype(factory) newFactory) { - factory = std::move(newFactory); -} - -auto CollectionInfoCache::makeImpl(Collection* const collection, const NamespaceString& ns) - -> std::unique_ptr<Impl> { - return factory(collection, ns); -} +MONGO_DEFINE_SHIM(CollectionInfoCache::makeImpl); void CollectionInfoCache::TUHook::hook() noexcept {} } // namespace mongo diff --git a/src/mongo/db/catalog/collection_info_cache.h b/src/mongo/db/catalog/collection_info_cache.h index 9bc07e80c61..6fb0fa5278e 100644 --- a/src/mongo/db/catalog/collection_info_cache.h +++ b/src/mongo/db/catalog/collection_info_cache.h @@ -28,6 +28,7 @@ #pragma once +#include "mongo/base/shim.h" #include "mongo/db/collection_index_usage_tracker.h" #include "mongo/db/query/plan_cache.h" #include "mongo/db/query/query_settings.h" @@ -69,16 +70,15 @@ public: const std::set<std::string>& indexesUsed) = 0; }; -private: - static std::unique_ptr<Impl> makeImpl(Collection* collection, const NamespaceString& ns); public: - using factory_function_type = decltype(makeImpl); - - static void registerFactory(stdx::function<factory_function_type> factory); + static MONGO_DECLARE_SHIM((Collection * collection, + const NamespaceString& ns, + PrivateTo<CollectionInfoCache>) + ->std::unique_ptr<Impl>) makeImpl; explicit inline CollectionInfoCache(Collection* const collection, const NamespaceString& ns) - : _pimpl(makeImpl(collection, ns)) {} + : _pimpl(makeImpl(collection, ns, PrivateCall<CollectionInfoCache>{})) {} inline ~CollectionInfoCache() = default; diff --git a/src/mongo/db/catalog/collection_info_cache_impl.cpp b/src/mongo/db/catalog/collection_info_cache_impl.cpp index b3366f70ae2..d23da8c5a81 100644 --- a/src/mongo/db/catalog/collection_info_cache_impl.cpp +++ b/src/mongo/db/catalog/collection_info_cache_impl.cpp @@ -49,15 +49,11 @@ #include "mongo/util/log.h" namespace mongo { -namespace { -MONGO_INITIALIZER(InitializeCollectionInfoCacheFactory)(InitializerContext* const) { - CollectionInfoCache::registerFactory( - [](Collection* const collection, const NamespaceString& ns) { - return stdx::make_unique<CollectionInfoCacheImpl>(collection, ns); - }); - return Status::OK(); +MONGO_REGISTER_SHIM(CollectionInfoCache::makeImpl) +(Collection* const collection, const NamespaceString& ns, PrivateTo<CollectionInfoCache>) + ->std::unique_ptr<CollectionInfoCache::Impl> { + return std::make_unique<CollectionInfoCacheImpl>(collection, ns); } -} // namespace CollectionInfoCacheImpl::CollectionInfoCacheImpl(Collection* collection, const NamespaceString& ns) : _collection(collection), diff --git a/src/mongo/db/catalog/collection_info_cache_impl.h b/src/mongo/db/catalog/collection_info_cache_impl.h index 58be19ed045..38e03f68a7b 100644 --- a/src/mongo/db/catalog/collection_info_cache_impl.h +++ b/src/mongo/db/catalog/collection_info_cache_impl.h @@ -30,6 +30,7 @@ #include "mongo/db/catalog/collection_info_cache.h" +#include "mongo/base/shim.h" #include "mongo/db/collection_index_usage_tracker.h" #include "mongo/db/query/plan_cache.h" #include "mongo/db/query/query_settings.h" diff --git a/src/mongo/db/catalog/create_collection.cpp b/src/mongo/db/catalog/create_collection.cpp index 950420fa787..f8258d36f07 100644 --- a/src/mongo/db/catalog/create_collection.cpp +++ b/src/mongo/db/catalog/create_collection.cpp @@ -101,8 +101,8 @@ Status createCollection(OperationContext* opCtx, // Create collection. const bool createDefaultIndexes = true; - status = - userCreateNS(opCtx, ctx.db(), nss.ns(), options, kind, createDefaultIndexes, idIndex); + status = Database::userCreateNS( + opCtx, ctx.db(), nss.ns(), options, kind, createDefaultIndexes, idIndex); if (!status.isOK()) { return status; @@ -137,7 +137,7 @@ Status createCollectionForApplyOps(OperationContext* opCtx, auto newCmd = cmdObj; auto* const serviceContext = opCtx->getServiceContext(); - auto* const db = dbHolder().get(opCtx, dbName); + auto* const db = DatabaseHolder::getDatabaseHolder().get(opCtx, dbName); // If a UUID is given, see if we need to rename a collection out of the way, and whether the // collection already exists under a different name. If so, rename it into place. As this is diff --git a/src/mongo/db/catalog/database.cpp b/src/mongo/db/catalog/database.cpp index 527c9e847ab..a9963a2dc7d 100644 --- a/src/mongo/db/catalog/database.cpp +++ b/src/mongo/db/catalog/database.cpp @@ -36,66 +36,13 @@ namespace mongo { Database::Impl::~Impl() = default; -namespace { -stdx::function<Database::factory_function_type> factory; -} // namespace - -void Database::registerFactory(decltype(factory) newFactory) { - factory = std::move(newFactory); -} - -auto Database::makeImpl(Database* const this_, - OperationContext* const opCtx, - const StringData name, - DatabaseCatalogEntry* const dbEntry) -> std::unique_ptr<Impl> { - return factory(this_, opCtx, name, dbEntry); -} +MONGO_DEFINE_SHIM(Database::makeImpl); void Database::TUHook::hook() noexcept {} -namespace { -stdx::function<decltype(Database::dropDatabase)> dropDatabaseImpl; -} - -void Database::dropDatabase(OperationContext* const opCtx, Database* const db) { - return dropDatabaseImpl(opCtx, db); -} +MONGO_DEFINE_SHIM(Database::dropDatabase); -void Database::registerDropDatabaseImpl(stdx::function<decltype(dropDatabase)> impl) { - dropDatabaseImpl = std::move(impl); -} +MONGO_DEFINE_SHIM(Database::userCreateNS); -namespace { -stdx::function<decltype(userCreateNS)> userCreateNSImpl; -stdx::function<decltype(dropAllDatabasesExceptLocal)> dropAllDatabasesExceptLocalImpl; -} // namespace +MONGO_DEFINE_SHIM(Database::dropAllDatabasesExceptLocal); } // namespace mongo - -auto mongo::userCreateNS(OperationContext* const opCtx, - Database* const db, - const StringData ns, - const BSONObj options, - const CollectionOptions::ParseKind parseKind, - const bool createDefaultIndexes, - const BSONObj& idIndex) -> Status { - return userCreateNSImpl(opCtx, db, ns, options, parseKind, createDefaultIndexes, idIndex); -} - -void mongo::registerUserCreateNSImpl(stdx::function<decltype(userCreateNS)> impl) { - userCreateNSImpl = std::move(impl); -} - -void mongo::dropAllDatabasesExceptLocal(OperationContext* const opCtx) { - return dropAllDatabasesExceptLocalImpl(opCtx); -} - -/** - * Registers an implementation of `dropAllDatabaseExceptLocal` for use by library clients. - * This is necessary to allow `catalog/database` to be a vtable edge. - * @param impl Implementation of `dropAllDatabaseExceptLocal` to install. - * @note This call is not thread safe. - */ -void mongo::registerDropAllDatabasesExceptLocalImpl( - stdx::function<decltype(dropAllDatabasesExceptLocal)> impl) { - dropAllDatabasesExceptLocalImpl = std::move(impl); -} diff --git a/src/mongo/db/catalog/database.h b/src/mongo/db/catalog/database.h index e5fab444357..2a5e281c1b5 100644 --- a/src/mongo/db/catalog/database.h +++ b/src/mongo/db/catalog/database.h @@ -31,6 +31,8 @@ #include <memory> #include <string> +#include "mongo/base/shim.h" +#include "mongo/base/status.h" #include "mongo/base/string_data.h" #include "mongo/bson/bsonobj.h" #include "mongo/db/catalog/collection.h" @@ -45,6 +47,7 @@ #include "mongo/util/string_map.h" namespace mongo { + /** * Represents a logical database containing Collections. * @@ -123,16 +126,34 @@ public: virtual const CollectionMap& collections() const = 0; }; -private: - static std::unique_ptr<Impl> makeImpl(Database* _this, - OperationContext* opCtx, - StringData name, - DatabaseCatalogEntry* dbEntry); - public: - using factory_function_type = decltype(makeImpl); + static MONGO_DECLARE_SHIM((OperationContext * opCtx)->void) dropAllDatabasesExceptLocal; - static void registerFactory(stdx::function<factory_function_type> factory); + /** + * Creates the namespace 'ns' in the database 'db' according to 'options'. If + * 'createDefaultIndexes' + * is true, creates the _id index for the collection (and the system indexes, in the case of + * system + * collections). Creates the collection's _id index according to 'idIndex', if it is non-empty. + * When + * 'idIndex' is empty, creates the default _id index. + */ + static MONGO_DECLARE_SHIM( + (OperationContext * opCtx, + Database* db, + StringData ns, + BSONObj options, + CollectionOptions::ParseKind parseKind = CollectionOptions::parseForCommand, + bool createDefaultIndexes = true, + const BSONObj& idIndex = BSONObj()) + ->Status) userCreateNS; + + static MONGO_DECLARE_SHIM((Database * this_, + OperationContext* opCtx, + StringData name, + DatabaseCatalogEntry*, + PrivateTo<Database>) + ->std::unique_ptr<Impl>) makeImpl; /** * Iterating over a Database yields Collection* pointers. @@ -182,7 +203,7 @@ public: explicit inline Database(OperationContext* const opCtx, const StringData name, DatabaseCatalogEntry* const dbEntry) - : _pimpl(makeImpl(this, opCtx, name, dbEntry)) { + : _pimpl(makeImpl(this, opCtx, name, dbEntry, PrivateCall<Database>{})) { this->_impl().init(opCtx); } @@ -335,17 +356,7 @@ public: * * Must be called with the specified database locked in X mode. */ - static void dropDatabase(OperationContext* opCtx, Database* db); - - /** - * Registers an implementation of `Database::dropDatabase` for use by library clients. - * This is necessary to allow `catalog/database` to be a vtable edge. - * @param impl Implementation of `dropDatabase` to install. - * @note This call is not thread safe. - */ - static void registerDropDatabaseImpl(stdx::function<decltype(dropDatabase)> impl); - - // static Status validateDBName( StringData dbname ); + static MONGO_DECLARE_SHIM((OperationContext * opCtx, Database* db)->void) dropDatabase; inline const NamespaceString& getSystemIndexesName() const { return this->_impl().getSystemIndexesName(); @@ -395,37 +406,4 @@ private: std::unique_ptr<Impl> _pimpl; }; - -void dropAllDatabasesExceptLocal(OperationContext* opCtx); - -/** - * Registers an implementation of `dropAllDatabaseExceptLocal` for use by library clients. - * This is necessary to allow `catalog/database` to be a vtable edge. - * @param impl Implementation of `dropAllDatabaseExceptLocal` to install. - * @note This call is not thread safe. - */ -void registerDropAllDatabasesExceptLocalImpl( - stdx::function<decltype(dropAllDatabasesExceptLocal)> impl); - -/** - * Creates the namespace 'ns' in the database 'db' according to 'options'. If 'createDefaultIndexes' - * is true, creates the _id index for the collection (and the system indexes, in the case of system - * collections). Creates the collection's _id index according to 'idIndex', if it is non-empty. When - * 'idIndex' is empty, creates the default _id index. - */ -Status userCreateNS(OperationContext* opCtx, - Database* db, - StringData ns, - BSONObj options, - CollectionOptions::ParseKind parseKind = CollectionOptions::parseForCommand, - bool createDefaultIndexes = true, - const BSONObj& idIndex = BSONObj()); - -/** - * Registers an implementation of `userCreateNS` for use by library clients. - * This is necessary to allow `catalog/database` to be a vtable edge. - * @param impl Implementation of `userCreateNS` to install. - * @note This call is not thread safe. - */ -void registerUserCreateNSImpl(stdx::function<decltype(userCreateNS)> impl); } // namespace mongo diff --git a/src/mongo/db/catalog/database_holder.cpp b/src/mongo/db/catalog/database_holder.cpp index 0b0c4336662..b40577d797f 100644 --- a/src/mongo/db/catalog/database_holder.cpp +++ b/src/mongo/db/catalog/database_holder.cpp @@ -36,32 +36,9 @@ namespace mongo { DatabaseHolder::Impl::~Impl() = default; -namespace { -stdx::function<DatabaseHolder::factory_function_type> factory; -} // namespace - -void DatabaseHolder::registerFactory(decltype(factory) newFactory) { - factory = std::move(newFactory); -} - -auto DatabaseHolder::makeImpl() -> std::unique_ptr<Impl> { - return factory(); -} - void DatabaseHolder::TUHook::hook() noexcept {} -namespace { -stdx::function<decltype(dbHolder)> dbHolderImpl; -} // namespace -} // namespace mongo - -// The `mongo::` prefix is necessary to placate MSVC -- it is unable to properly identify anonymous -// nested namespace members in `decltype` expressions when defining functions using scope-resolution -// syntax. -void mongo::registerDbHolderImpl(decltype(mongo::dbHolderImpl) impl) { - dbHolderImpl = std::move(impl); -} +MONGO_DEFINE_SHIM(DatabaseHolder::makeImpl); +MONGO_DEFINE_SHIM(DatabaseHolder::getDatabaseHolder); -auto mongo::dbHolder() -> DatabaseHolder& { - return dbHolderImpl(); -} +} // namespace mongo diff --git a/src/mongo/db/catalog/database_holder.h b/src/mongo/db/catalog/database_holder.h index 0edde9cfccf..3af319ab963 100644 --- a/src/mongo/db/catalog/database_holder.h +++ b/src/mongo/db/catalog/database_holder.h @@ -31,6 +31,7 @@ #include <set> #include <string> +#include "mongo/base/shim.h" #include "mongo/base/string_data.h" #include "mongo/db/namespace_string.h" #include "mongo/stdx/functional.h" @@ -62,17 +63,14 @@ public: virtual std::set<std::string> getNamesWithConflictingCasing(StringData name) = 0; }; -private: - static std::unique_ptr<Impl> makeImpl(); - public: - using factory_function_type = decltype(makeImpl); + static MONGO_DECLARE_SHIM(()->DatabaseHolder&) getDatabaseHolder; - static void registerFactory(stdx::function<factory_function_type> factory); + static MONGO_DECLARE_SHIM((PrivateTo<DatabaseHolder>)->std::unique_ptr<Impl>) makeImpl; inline ~DatabaseHolder() = default; - inline explicit DatabaseHolder() : _pimpl(makeImpl()) {} + inline explicit DatabaseHolder() : _pimpl(makeImpl(PrivateCall<DatabaseHolder>{})) {} /** * Retrieves an already opened database or returns NULL. Must be called with the database @@ -147,7 +145,4 @@ private: std::unique_ptr<Impl> _pimpl; }; - -extern DatabaseHolder& dbHolder(); -extern void registerDbHolderImpl(stdx::function<decltype(dbHolder)> impl); } // namespace mongo diff --git a/src/mongo/db/catalog/database_holder_impl.cpp b/src/mongo/db/catalog/database_holder_impl.cpp index d5b5560df67..df70ef25c27 100644 --- a/src/mongo/db/catalog/database_holder_impl.cpp +++ b/src/mongo/db/catalog/database_holder_impl.cpp @@ -50,32 +50,29 @@ namespace mongo { namespace { - -DatabaseHolder* _dbHolder = nullptr; - -DatabaseHolder& dbHolderImpl() { - return *_dbHolder; -} +std::unique_ptr<DatabaseHolder> dbHolderStorage; GlobalInitializerRegisterer dbHolderImplInitializer("InitializeDbHolderimpl", - {"InitializeDatabaseHolderFactory"}, [](InitializerContext* const) { - _dbHolder = new DatabaseHolder(); - registerDbHolderImpl(dbHolderImpl); + dbHolderStorage = + std::make_unique<DatabaseHolder>(); return Status::OK(); }, [](DeinitializerContext* const) { - delete _dbHolder; - _dbHolder = nullptr; + dbHolderStorage = nullptr; return Status::OK(); }); +} // namespace -MONGO_INITIALIZER(InitializeDatabaseHolderFactory)(InitializerContext* const) { - DatabaseHolder::registerFactory([] { return stdx::make_unique<DatabaseHolderImpl>(); }); - return Status::OK(); +MONGO_REGISTER_SHIM(DatabaseHolder::getDatabaseHolder) +()->DatabaseHolder& { + return *dbHolderStorage; } -} // namespace +MONGO_REGISTER_SHIM(DatabaseHolder::makeImpl) +(PrivateTo<DatabaseHolder>)->std::unique_ptr<DatabaseHolder::Impl> { + return std::make_unique<DatabaseHolderImpl>(); +} using std::set; using std::size_t; diff --git a/src/mongo/db/catalog/database_holder_mock.cpp b/src/mongo/db/catalog/database_holder_mock.cpp index 694091f8afc..561713fb555 100644 --- a/src/mongo/db/catalog/database_holder_mock.cpp +++ b/src/mongo/db/catalog/database_holder_mock.cpp @@ -33,23 +33,15 @@ #include "mongo/stdx/memory.h" namespace mongo { -namespace { -DatabaseHolder& dbHolderImpl() { +MONGO_REGISTER_SHIM(DatabaseHolder::getDatabaseHolder)()->DatabaseHolder& { static DatabaseHolder _dbHolder; return _dbHolder; } -MONGO_INITIALIZER_WITH_PREREQUISITES(InitializeDbHolderimpl, ("InitializeDatabaseHolderFactory")) -(InitializerContext* const) { - registerDbHolderImpl(dbHolderImpl); - return Status::OK(); +MONGO_REGISTER_SHIM(DatabaseHolder::makeImpl) +(PrivateTo<DatabaseHolder>)->std::unique_ptr<DatabaseHolder::Impl> { + return stdx::make_unique<DatabaseHolderMock>(); } -MONGO_INITIALIZER(InitializeDatabaseHolderFactory)(InitializerContext* const) { - DatabaseHolder::registerFactory([] { return stdx::make_unique<DatabaseHolderMock>(); }); - return Status::OK(); -} - -} // namespace } // namespace mongo diff --git a/src/mongo/db/catalog/database_impl.cpp b/src/mongo/db/catalog/database_impl.cpp index 038061c36b8..44e229aaf26 100644 --- a/src/mongo/db/catalog/database_impl.cpp +++ b/src/mongo/db/catalog/database_impl.cpp @@ -78,16 +78,17 @@ #include "mongo/util/log.h" namespace mongo { -namespace { -MONGO_INITIALIZER(InitializeDatabaseFactory)(InitializerContext* const) { - Database::registerFactory([](Database* const this_, - OperationContext* const opCtx, - const StringData name, - DatabaseCatalogEntry* const dbEntry) { - return stdx::make_unique<DatabaseImpl>(this_, opCtx, name, dbEntry); - }); - return Status::OK(); +MONGO_REGISTER_SHIM(Database::makeImpl) +(Database* const this_, + OperationContext* const opCtx, + const StringData name, + DatabaseCatalogEntry* const dbEntry, + PrivateTo<Database>) + ->std::unique_ptr<Database::Impl> { + return stdx::make_unique<DatabaseImpl>(this_, opCtx, name, dbEntry); } + +namespace { MONGO_FP_DECLARE(hangBeforeLoggingCreateCollection); } // namespace @@ -865,7 +866,7 @@ void DatabaseImpl::dropDatabase(OperationContext* opCtx, Database* db) { Top::get(serviceContext).collectionDropped(coll->ns().ns(), true); } - dbHolder().close(opCtx, name, "database dropped"); + DatabaseHolder::getDatabaseHolder().close(opCtx, name, "database dropped"); auto const storageEngine = serviceContext->getGlobalStorageEngine(); writeConflictRetry(opCtx, "dropDatabase", name, [&] { @@ -896,7 +897,7 @@ StatusWith<NamespaceString> DatabaseImpl::makeUniqueCollectionNamespace( if (!_uniqueCollectionNamespacePseudoRandom) { Timestamp ts; _uniqueCollectionNamespacePseudoRandom = - stdx::make_unique<PseudoRandom>(Date_t::now().asInt64()); + std::make_unique<PseudoRandom>(Date_t::now().asInt64()); } const auto charsToChooseFrom = @@ -936,60 +937,19 @@ StatusWith<NamespaceString> DatabaseImpl::makeUniqueCollectionNamespace( << " attempts due to namespace conflicts with existing collections."); } -namespace { -MONGO_INITIALIZER(InitializeDropDatabaseImpl)(InitializerContext* const) { - Database::registerDropDatabaseImpl(DatabaseImpl::dropDatabase); - return Status::OK(); -} -MONGO_INITIALIZER(InitializeUserCreateNSImpl)(InitializerContext* const) { - registerUserCreateNSImpl(userCreateNSImpl); - return Status::OK(); -} - -MONGO_INITIALIZER(InitializeDropAllDatabasesExceptLocalImpl)(InitializerContext* const) { - registerDropAllDatabasesExceptLocalImpl(dropAllDatabasesExceptLocalImpl); - return Status::OK(); +MONGO_REGISTER_SHIM(Database::dropDatabase)(OperationContext* opCtx, Database* db)->void { + return DatabaseImpl::dropDatabase(opCtx, db); } -} // namespace -} // namespace mongo - -void mongo::dropAllDatabasesExceptLocalImpl(OperationContext* opCtx) { - Lock::GlobalWrite lk(opCtx); - - vector<string> n; - StorageEngine* storageEngine = opCtx->getServiceContext()->getGlobalStorageEngine(); - storageEngine->listDatabases(&n); - - if (n.size() == 0) - return; - log() << "dropAllDatabasesExceptLocal " << n.size(); - - repl::ReplicationCoordinator::get(opCtx)->dropAllSnapshots(); - - for (const auto& dbName : n) { - if (dbName != "local") { - writeConflictRetry(opCtx, "dropAllDatabasesExceptLocal", dbName, [&opCtx, &dbName] { - Database* db = dbHolder().get(opCtx, dbName); - // This is needed since dropDatabase can't be rolled back. - // This is safe be replaced by "invariant(db);dropDatabase(opCtx, db);" once fixed - if (db == nullptr) { - log() << "database disappeared after listDatabases but before drop: " << dbName; - } else { - DatabaseImpl::dropDatabase(opCtx, db); - } - }); - } - } -} - -auto mongo::userCreateNSImpl(OperationContext* opCtx, - Database* db, - StringData ns, - BSONObj options, - CollectionOptions::ParseKind parseKind, - bool createDefaultIndexes, - const BSONObj& idIndex) -> Status { +MONGO_REGISTER_SHIM(Database::userCreateNS) +(OperationContext* opCtx, + Database* db, + StringData ns, + BSONObj options, + CollectionOptions::ParseKind parseKind, + bool createDefaultIndexes, + const BSONObj& idIndex) + ->Status { invariant(db); LOG(1) << "create collection " << ns << ' ' << options; @@ -1088,3 +1048,34 @@ auto mongo::userCreateNSImpl(OperationContext* opCtx, return Status::OK(); } + +MONGO_REGISTER_SHIM(Database::dropAllDatabasesExceptLocal)(OperationContext* opCtx)->void { + Lock::GlobalWrite lk(opCtx); + + vector<string> n; + StorageEngine* storageEngine = opCtx->getServiceContext()->getGlobalStorageEngine(); + storageEngine->listDatabases(&n); + + if (n.size() == 0) + return; + log() << "dropAllDatabasesExceptLocal " << n.size(); + + repl::ReplicationCoordinator::get(opCtx)->dropAllSnapshots(); + + for (const auto& dbName : n) { + if (dbName != "local") { + writeConflictRetry(opCtx, "dropAllDatabasesExceptLocal", dbName, [&opCtx, &dbName] { + Database* db = DatabaseHolder::getDatabaseHolder().get(opCtx, dbName); + + // This is needed since dropDatabase can't be rolled back. + // This is safe be replaced by "invariant(db);dropDatabase(opCtx, db);" once fixed + if (db == nullptr) { + log() << "database disappeared after listDatabases but before drop: " << dbName; + } else { + DatabaseImpl::dropDatabase(opCtx, db); + } + }); + } + } +} +} // namespace mongo diff --git a/src/mongo/db/catalog/database_impl.h b/src/mongo/db/catalog/database_impl.h index e2102e3154b..8a341037778 100644 --- a/src/mongo/db/catalog/database_impl.h +++ b/src/mongo/db/catalog/database_impl.h @@ -309,18 +309,4 @@ private: void dropAllDatabasesExceptLocalImpl(OperationContext* opCtx); -/** - * Creates the namespace 'ns' in the database 'db' according to 'options'. If 'createDefaultIndexes' - * is true, creates the _id index for the collection (and the system indexes, in the case of system - * collections). Creates the collection's _id index according to 'idIndex', if it is non-empty. When - * 'idIndex' is empty, creates the default _id index. - */ -Status userCreateNSImpl(OperationContext* opCtx, - Database* db, - StringData ns, - BSONObj options, - CollectionOptions::ParseKind parseKind = CollectionOptions::parseForCommand, - bool createDefaultIndexes = true, - const BSONObj& idIndex = BSONObj()); - } // namespace mongo diff --git a/src/mongo/db/catalog/index_catalog.cpp b/src/mongo/db/catalog/index_catalog.cpp index 4d9288e841d..7a2a28a241a 100644 --- a/src/mongo/db/catalog/index_catalog.cpp +++ b/src/mongo/db/catalog/index_catalog.cpp @@ -37,19 +37,7 @@ namespace mongo { IndexCatalog::Impl::~Impl() = default; -namespace { -IndexCatalog::factory_function_type factory; -} // namespace - -void IndexCatalog::registerFactory(decltype(factory) newFactory) { - factory = std::move(newFactory); -} - -auto IndexCatalog::makeImpl(IndexCatalog* const this_, - Collection* const collection, - const int maxNumIndexesAllowed) -> std::unique_ptr<Impl> { - return factory(this_, collection, maxNumIndexesAllowed); -} +MONGO_DEFINE_SHIM(IndexCatalog::makeImpl); void IndexCatalog::TUHook::hook() noexcept {} @@ -63,38 +51,11 @@ IndexCatalogEntry* IndexCatalog::_setupInMemoryStructures( IndexCatalog::IndexIterator::Impl::~Impl() = default; -namespace { -IndexCatalog::IndexIterator::factory_function_type iteratorFactory; -} // namespace - -void IndexCatalog::IndexIterator::registerFactory(decltype(iteratorFactory) newFactory) { - iteratorFactory = std::move(newFactory); -} - -auto IndexCatalog::IndexIterator::makeImpl(OperationContext* const opCtx, - const IndexCatalog* const cat, - const bool includeUnfinishedIndexes) - -> std::unique_ptr<Impl> { - return iteratorFactory(opCtx, cat, includeUnfinishedIndexes); -} +MONGO_DEFINE_SHIM(IndexCatalog::IndexIterator::makeImpl); void IndexCatalog::IndexIterator::TUHook::hook() noexcept {} -namespace { -stdx::function<decltype(IndexCatalog::fixIndexKey)> fixIndexKeyImpl; -} // namespace - -void IndexCatalog::registerFixIndexKeyImpl(decltype(fixIndexKeyImpl) impl) { - fixIndexKeyImpl = std::move(impl); -} - -BSONObj IndexCatalog::fixIndexKey(const BSONObj& key) { - return fixIndexKeyImpl(key); -} - -namespace { -stdx::function<decltype(IndexCatalog::prepareInsertDeleteOptions)> prepareInsertDeleteOptionsImpl; -} // namespace +MONGO_DEFINE_SHIM(IndexCatalog::fixIndexKey); std::string::size_type IndexCatalog::getLongestIndexNameLength(OperationContext* opCtx) const { IndexCatalog::IndexIterator it = getIndexIterator(opCtx, true); @@ -107,14 +68,5 @@ std::string::size_type IndexCatalog::getLongestIndexNameLength(OperationContext* return longestIndexNameLength; } -void IndexCatalog::prepareInsertDeleteOptions(OperationContext* const opCtx, - const IndexDescriptor* const desc, - InsertDeleteOptions* const options) { - return prepareInsertDeleteOptionsImpl(opCtx, desc, options); -} - -void IndexCatalog::registerPrepareInsertDeleteOptionsImpl( - stdx::function<decltype(prepareInsertDeleteOptions)> impl) { - prepareInsertDeleteOptionsImpl = std::move(impl); -} +MONGO_DEFINE_SHIM(IndexCatalog::prepareInsertDeleteOptions); } // namespace mongo diff --git a/src/mongo/db/catalog/index_catalog.h b/src/mongo/db/catalog/index_catalog.h index 5e5b17281e2..bb9902f91cd 100644 --- a/src/mongo/db/catalog/index_catalog.h +++ b/src/mongo/db/catalog/index_catalog.h @@ -32,6 +32,7 @@ #include <vector> #include "mongo/base/clonable_ptr.h" +#include "mongo/base/shim.h" #include "mongo/db/catalog/index_catalog_entry.h" #include "mongo/db/index/multikey_paths.h" #include "mongo/db/jsobj.h" @@ -76,20 +77,20 @@ public: virtual IndexCatalogEntry* catalogEntry(const IndexDescriptor* desc) = 0; }; - private: - static std::unique_ptr<Impl> makeImpl(OperationContext* opCtx, - const IndexCatalog* cat, - bool includeUnfinishedIndexes); + static MONGO_DECLARE_SHIM((OperationContext * opCtx, + const IndexCatalog* cat, + bool includeUnfinishedIndexes, + PrivateTo<IndexIterator>) + ->std::unique_ptr<Impl>) makeImpl; + private: explicit inline IndexIterator(OperationContext* const opCtx, const IndexCatalog* const cat, const bool includeUnfinishedIndexes) - : _pimpl(makeImpl(opCtx, cat, includeUnfinishedIndexes)) {} + : _pimpl(makeImpl(opCtx, cat, includeUnfinishedIndexes, PrivateCall<IndexIterator>{})) { + } public: - using factory_function_type = stdx::function<decltype(makeImpl)>; - static void registerFactory(factory_function_type factory); - inline ~IndexIterator() = default; inline IndexIterator(const IndexIterator& copy) = default; @@ -255,19 +256,17 @@ public: friend IndexCatalog; }; -private: - static std::unique_ptr<Impl> makeImpl(IndexCatalog* this_, - Collection* collection, - int maxNumIndexesAllowed); - public: - using factory_function_type = stdx::function<decltype(makeImpl)>; - static void registerFactory(factory_function_type factory); + static MONGO_DECLARE_SHIM((IndexCatalog * this_, + Collection* collection, + int maxNumIndexesAllowed, + PrivateTo<IndexCatalog>) + ->std::unique_ptr<Impl>) makeImpl; inline ~IndexCatalog() = default; explicit inline IndexCatalog(Collection* const collection, const int maxNumIndexesAllowed) - : _pimpl(makeImpl(this, collection, maxNumIndexesAllowed)) {} + : _pimpl(makeImpl(this, collection, maxNumIndexesAllowed, PrivateCall<IndexCatalog>{})) {} inline IndexCatalog(IndexCatalog&&) = delete; inline IndexCatalog& operator=(IndexCatalog&&) = delete; @@ -538,18 +537,15 @@ public: // public static helpers - static BSONObj fixIndexKey(const BSONObj& key); - static void registerFixIndexKeyImpl(stdx::function<decltype(fixIndexKey)> impl); + static MONGO_DECLARE_SHIM((const BSONObj& key)->BSONObj) fixIndexKey; /** * Fills out 'options' in order to indicate whether to allow dups or relax * index constraints, as needed by replication. */ - static void prepareInsertDeleteOptions(OperationContext* opCtx, - const IndexDescriptor* desc, - InsertDeleteOptions* options); - static void registerPrepareInsertDeleteOptionsImpl( - stdx::function<decltype(prepareInsertDeleteOptions)> impl); + static MONGO_DECLARE_SHIM( + (OperationContext * opCtx, const IndexDescriptor* desc, InsertDeleteOptions* options)->void) + prepareInsertDeleteOptions; private: inline const Collection* _getCollection() const { diff --git a/src/mongo/db/catalog/index_catalog_entry.cpp b/src/mongo/db/catalog/index_catalog_entry.cpp index 94a05fde7ac..1972c2237d4 100644 --- a/src/mongo/db/catalog/index_catalog_entry.cpp +++ b/src/mongo/db/catalog/index_catalog_entry.cpp @@ -38,22 +38,7 @@ namespace mongo { IndexCatalogEntry::Impl::~Impl() = default; -namespace { -stdx::function<IndexCatalogEntry::factory_function_type> factory; -} // namespace - -void IndexCatalogEntry::registerFactory(decltype(factory) newFactory) { - factory = std::move(newFactory); -} - -auto IndexCatalogEntry::makeImpl(IndexCatalogEntry* const this_, - OperationContext* const opCtx, - const StringData ns, - CollectionCatalogEntry* const collection, - std::unique_ptr<IndexDescriptor> descriptor, - CollectionInfoCache* const infoCache) -> std::unique_ptr<Impl> { - return factory(this_, opCtx, ns, collection, std::move(descriptor), infoCache); -} +MONGO_DEFINE_SHIM(IndexCatalogEntry::makeImpl); void IndexCatalogEntry::TUHook::hook() noexcept {} @@ -62,7 +47,13 @@ IndexCatalogEntry::IndexCatalogEntry(OperationContext* opCtx, CollectionCatalogEntry* collection, std::unique_ptr<IndexDescriptor> descriptor, CollectionInfoCache* infoCache) - : _pimpl(makeImpl(this, opCtx, ns, collection, std::move(descriptor), infoCache)) {} + : _pimpl(makeImpl(this, + opCtx, + ns, + collection, + std::move(descriptor), + infoCache, + PrivateCall<IndexCatalogEntry>{})) {} void IndexCatalogEntry::init(std::unique_ptr<IndexAccessMethod> accessMethod) { return this->_impl().init(std::move(accessMethod)); diff --git a/src/mongo/db/catalog/index_catalog_entry.h b/src/mongo/db/catalog/index_catalog_entry.h index fecfd197bb1..20d748c9846 100644 --- a/src/mongo/db/catalog/index_catalog_entry.h +++ b/src/mongo/db/catalog/index_catalog_entry.h @@ -32,6 +32,7 @@ #include <string> #include "mongo/base/owned_pointer_vector.h" +#include "mongo/base/shim.h" #include "mongo/bson/ordering.h" #include "mongo/bson/timestamp.h" #include "mongo/db/index/multikey_paths.h" @@ -102,18 +103,15 @@ public: virtual void setMinimumVisibleSnapshot(Timestamp name) = 0; }; -private: - static std::unique_ptr<Impl> makeImpl(IndexCatalogEntry* this_, - OperationContext* opCtx, - StringData ns, - CollectionCatalogEntry* collection, - std::unique_ptr<IndexDescriptor> descriptor, - CollectionInfoCache* infoCache); - public: - using factory_function_type = decltype(makeImpl); - - static void registerFactory(stdx::function<factory_function_type> factory); + static MONGO_DECLARE_SHIM((IndexCatalogEntry * this_, + OperationContext* opCtx, + StringData ns, + CollectionCatalogEntry* collection, + std::unique_ptr<IndexDescriptor> descriptor, + CollectionInfoCache* infoCache, + PrivateTo<IndexCatalogEntry>) + ->std::unique_ptr<Impl>) makeImpl; explicit IndexCatalogEntry( OperationContext* opCtx, diff --git a/src/mongo/db/catalog/index_catalog_entry_impl.cpp b/src/mongo/db/catalog/index_catalog_entry_impl.cpp index 5e717b97e71..1ef395b85b4 100644 --- a/src/mongo/db/catalog/index_catalog_entry_impl.cpp +++ b/src/mongo/db/catalog/index_catalog_entry_impl.cpp @@ -53,20 +53,18 @@ #include "mongo/util/scopeguard.h" namespace mongo { -namespace { -MONGO_INITIALIZER(InitializeIndexCatalogEntryFactory)(InitializerContext* const) { - IndexCatalogEntry::registerFactory([](IndexCatalogEntry* const this_, - OperationContext* const opCtx, - const StringData ns, - CollectionCatalogEntry* const collection, - std::unique_ptr<IndexDescriptor> descriptor, - CollectionInfoCache* const infoCache) { - return stdx::make_unique<IndexCatalogEntryImpl>( - this_, opCtx, ns, collection, std::move(descriptor), infoCache); - }); - return Status::OK(); +MONGO_REGISTER_SHIM(IndexCatalogEntry::makeImpl) +(IndexCatalogEntry* const this_, + OperationContext* const opCtx, + const StringData ns, + CollectionCatalogEntry* const collection, + std::unique_ptr<IndexDescriptor> descriptor, + CollectionInfoCache* const infoCache, + PrivateTo<IndexCatalogEntry>) + ->std::unique_ptr<IndexCatalogEntry::Impl> { + return std::make_unique<IndexCatalogEntryImpl>( + this_, opCtx, ns, collection, std::move(descriptor), infoCache); } -} // namespace using std::string; diff --git a/src/mongo/db/catalog/index_catalog_impl.cpp b/src/mongo/db/catalog/index_catalog_impl.cpp index e19773b34fd..76bb7aa4b8a 100644 --- a/src/mongo/db/catalog/index_catalog_impl.cpp +++ b/src/mongo/db/catalog/index_catalog_impl.cpp @@ -70,38 +70,33 @@ #include "mongo/util/represent_as.h" namespace mongo { -namespace { -MONGO_INITIALIZER(InitializeIndexCatalogFactory)(InitializerContext* const) { - IndexCatalog::registerFactory([]( - IndexCatalog* const this_, Collection* const collection, const int maxNumIndexesAllowed) { - return stdx::make_unique<IndexCatalogImpl>(this_, collection, maxNumIndexesAllowed); - }); - return Status::OK(); +MONGO_REGISTER_SHIM(IndexCatalog::makeImpl) +(IndexCatalog* const this_, + Collection* const collection, + const int maxNumIndexesAllowed, + PrivateTo<IndexCatalog>) + ->std::unique_ptr<IndexCatalog::Impl> { + return std::make_unique<IndexCatalogImpl>(this_, collection, maxNumIndexesAllowed); } -MONGO_INITIALIZER(InitializeIndexCatalogIndexIteratorFactory)(InitializerContext* const) { - IndexCatalog::IndexIterator::registerFactory([](OperationContext* const opCtx, - const IndexCatalog* const cat, - const bool includeUnfinishedIndexes) { - return stdx::make_unique<IndexCatalogImpl::IndexIteratorImpl>( - opCtx, cat, includeUnfinishedIndexes); - }); - return Status::OK(); +MONGO_REGISTER_SHIM(IndexCatalog::IndexIterator::makeImpl) +(OperationContext* const opCtx, + const IndexCatalog* const cat, + const bool includeUnfinishedIndexes, + PrivateTo<IndexCatalog::IndexIterator>) + ->std::unique_ptr<IndexCatalog::IndexIterator::Impl> { + return std::make_unique<IndexCatalogImpl::IndexIteratorImpl>( + opCtx, cat, includeUnfinishedIndexes); } - -MONGO_INITIALIZER(InitializeFixIndexKeyImpl)(InitializerContext* const) { - IndexCatalog::registerFixIndexKeyImpl(&IndexCatalogImpl::fixIndexKey); - return Status::OK(); +MONGO_REGISTER_SHIM(IndexCatalog::fixIndexKey)(const BSONObj& key)->BSONObj { + return IndexCatalogImpl::fixIndexKey(key); } -MONGO_INITIALIZER(InitializePrepareInsertDeleteOptionsImpl)(InitializerContext* const) { - IndexCatalog::registerPrepareInsertDeleteOptionsImpl( - &IndexCatalogImpl::prepareInsertDeleteOptions); - return Status::OK(); +MONGO_REGISTER_SHIM(IndexCatalog::prepareInsertDeleteOptions) +(OperationContext* opCtx, const IndexDescriptor* desc, InsertDeleteOptions* options)->void { + return IndexCatalogImpl::prepareInsertDeleteOptions(opCtx, desc, options); } -} // namespace - using std::unique_ptr; using std::endl; using std::string; diff --git a/src/mongo/db/catalog/index_consistency.cpp b/src/mongo/db/catalog/index_consistency.cpp index f1237212a1b..7caf6e1ed86 100644 --- a/src/mongo/db/catalog/index_consistency.cpp +++ b/src/mongo/db/catalog/index_consistency.cpp @@ -506,7 +506,7 @@ uint32_t IndexConsistency::_hashKeyString(const KeyString& ks, int indexNumber) Status IndexConsistency::_throwExceptionIfError() { - Database* database = dbHolder().get(_opCtx, _nss.db()); + Database* database = DatabaseHolder::getDatabaseHolder().get(_opCtx, _nss.db()); // Ensure the database still exists. if (!database) { diff --git a/src/mongo/db/catalog/index_create.cpp b/src/mongo/db/catalog/index_create.cpp index 06f9676ca4b..1f4c169000c 100644 --- a/src/mongo/db/catalog/index_create.cpp +++ b/src/mongo/db/catalog/index_create.cpp @@ -57,19 +57,7 @@ namespace mongo { MultiIndexBlock::Impl::~Impl() = default; -namespace { -stdx::function<MultiIndexBlock::factory_function_type> factory; -} // namespace - -void MultiIndexBlock::registerFactory(decltype(factory) newFactory) { - factory = std::move(newFactory); -} - -auto MultiIndexBlock::makeImpl(OperationContext* const opCtx, Collection* const collection) - -> std::unique_ptr<Impl> { - return factory(opCtx, collection); -} - +MONGO_DEFINE_SHIM(MultiIndexBlock::makeImpl); void MultiIndexBlock::TUHook::hook() noexcept {} } // namespace mongo diff --git a/src/mongo/db/catalog/index_create.h b/src/mongo/db/catalog/index_create.h index 56c85609a1f..acdf744da9e 100644 --- a/src/mongo/db/catalog/index_create.h +++ b/src/mongo/db/catalog/index_create.h @@ -114,12 +114,11 @@ private: return *this->_pimpl; } - static std::unique_ptr<Impl> makeImpl(OperationContext* opCtx, Collection* collection); - public: - using factory_function_type = decltype(makeImpl); - - static void registerFactory(stdx::function<factory_function_type> factory); + static MONGO_DECLARE_SHIM((OperationContext * opCtx, + Collection* collection, + PrivateTo<MultiIndexBlock>) + ->std::unique_ptr<Impl>) makeImpl; inline ~MultiIndexBlock() = default; @@ -127,7 +126,7 @@ public: * Neither pointer is owned. */ inline explicit MultiIndexBlock(OperationContext* const opCtx, Collection* const collection) - : _pimpl(makeImpl(opCtx, collection)) {} + : _pimpl(makeImpl(opCtx, collection, PrivateCall<MultiIndexBlock>{})) {} /** * By default we ignore the 'background' flag in specs when building an index. If this is diff --git a/src/mongo/db/catalog/index_create_impl_servers.cpp b/src/mongo/db/catalog/index_create_impl_servers.cpp index 5fab9ebee48..541a19a0434 100644 --- a/src/mongo/db/catalog/index_create_impl_servers.cpp +++ b/src/mongo/db/catalog/index_create_impl_servers.cpp @@ -41,14 +41,12 @@ class MultiIndexBlockImplServers : public MultiIndexBlockImpl { return spec["background"].trueValue(); } }; +} // namespace -MONGO_INITIALIZER(InitializeMultiIndexBlockFactory)(InitializerContext* const) { - MultiIndexBlock::registerFactory( - [](OperationContext* const opCtx, Collection* const collection) { - return stdx::make_unique<MultiIndexBlockImplServers>(opCtx, collection); - }); - return Status::OK(); +MONGO_REGISTER_SHIM(MultiIndexBlock::makeImpl) +(OperationContext* const opCtx, Collection* const collection, PrivateTo<MultiIndexBlock>) + ->std::unique_ptr<MultiIndexBlock::Impl> { + return std::make_unique<MultiIndexBlockImplServers>(opCtx, collection); } -} // namespace } // namespace mongo diff --git a/src/mongo/db/catalog/rename_collection.cpp b/src/mongo/db/catalog/rename_collection.cpp index fd5d2df9706..0d7722093e6 100644 --- a/src/mongo/db/catalog/rename_collection.cpp +++ b/src/mongo/db/catalog/rename_collection.cpp @@ -152,7 +152,7 @@ Status renameCollectionCommon(OperationContext* opCtx, << target.ns()); } - Database* const sourceDB = dbHolder().get(opCtx, source.db()); + Database* const sourceDB = DatabaseHolder::getDatabaseHolder().get(opCtx, source.db()); if (sourceDB) { DatabaseShardingState::get(sourceDB).checkDbVersion(opCtx); } @@ -180,7 +180,7 @@ Status renameCollectionCommon(OperationContext* opCtx, BackgroundOperation::assertNoBgOpInProgForNs(source.ns()); - Database* const targetDB = dbHolder().openDb(opCtx, target.db()); + Database* const targetDB = DatabaseHolder::getDatabaseHolder().openDb(opCtx, target.db()); // Check if the target namespace exists and if dropTarget is true. // Return a non-OK status if target exists and dropTarget is not true or if the collection diff --git a/src/mongo/db/catalog/uuid_catalog.cpp b/src/mongo/db/catalog/uuid_catalog.cpp index 3ec994d7ef0..0a50cadfa71 100644 --- a/src/mongo/db/catalog/uuid_catalog.cpp +++ b/src/mongo/db/catalog/uuid_catalog.cpp @@ -92,7 +92,7 @@ repl::OpTime UUIDCatalogObserver::onRenameCollection(OperationContext* opCtx, if (!uuid) return {}; auto getNewCollection = [opCtx, toCollection] { - auto db = dbHolder().get(opCtx, toCollection.db()); + auto db = DatabaseHolder::getDatabaseHolder().get(opCtx, toCollection.db()); auto newColl = db->getCollection(opCtx, toCollection); invariant(newColl); return newColl; |