diff options
author | Fernando Lisboa <fernando.lisboa@mongodb.com> | 2022-08-11 22:32:21 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-08-11 23:30:22 +0000 |
commit | 408c0cfc46ca7adccb7f4784a6ec6a6d167b0991 (patch) | |
tree | a788e5cd4fafe4eb56235c7f50477f8ba5263624 /src/mongo/db/commands/list_databases.cpp | |
parent | 183c16e908a882b40b7e704b1d448796af37ada8 (diff) | |
download | mongo-408c0cfc46ca7adccb7f4784a6ec6a6d167b0991.tar.gz |
SERVER-61822 Create new command listDatabasesForAllTenants
Diffstat (limited to 'src/mongo/db/commands/list_databases.cpp')
-rw-r--r-- | src/mongo/db/commands/list_databases.cpp | 63 |
1 files changed, 10 insertions, 53 deletions
diff --git a/src/mongo/db/commands/list_databases.cpp b/src/mongo/db/commands/list_databases.cpp index d454817654c..580ab22f094 100644 --- a/src/mongo/db/commands/list_databases.cpp +++ b/src/mongo/db/commands/list_databases.cpp @@ -31,14 +31,12 @@ #include "mongo/db/auth/authorization_session.h" #include "mongo/db/catalog/database_holder.h" -#include "mongo/db/catalog_raii.h" #include "mongo/db/client.h" #include "mongo/db/commands.h" +#include "mongo/db/commands/list_databases_common.h" #include "mongo/db/commands/list_databases_gen.h" -#include "mongo/db/concurrency/exception_util.h" #include "mongo/db/curop_failpoint_helpers.h" #include "mongo/db/database_name.h" -#include "mongo/db/db_raii.h" #include "mongo/db/matcher/expression.h" #include "mongo/db/namespace_string.h" #include "mongo/db/operation_context.h" @@ -55,7 +53,6 @@ namespace { // Failpoint which causes to hang "listDatabases" cmd after acquiring global lock in IS mode. MONGO_FAIL_POINT_DEFINE(hangBeforeListDatabases); -constexpr auto kName = "name"_sd; class CmdListDatabases final : public ListDatabasesCmdVersion1Gen<CmdListDatabases> { public: AllowedOnSecondary secondaryAllowed(ServiceContext*) const final { @@ -112,16 +109,7 @@ public: })(cmd.getAuthorizedDatabases()); // {filter: matchExpression}. - std::unique_ptr<MatchExpression> filter; - if (auto filterObj = cmd.getFilter()) { - // The collator is null because database metadata objects are compared using simple - // binary comparison. - auto expCtx = make_intrusive<ExpressionContext>( - opCtx, std::unique_ptr<CollatorInterface>(nullptr), ns()); - auto matcher = uassertStatusOK( - MatchExpressionParser::parse(filterObj.value(), std::move(expCtx))); - filter = std::move(matcher); - } + std::unique_ptr<MatchExpression> filter = list_databases::getFilter(cmd, opCtx, ns()); std::vector<DatabaseName> dbNames; StorageEngine* storageEngine = getGlobalServiceContext()->getStorageEngine(); @@ -133,45 +121,14 @@ public: } std::vector<ListDatabasesReplyItem> items; - - const bool filterNameOnly = filter && - filter->getCategory() == MatchExpression::MatchCategory::kLeaf && - filter->path() == kName; - long long totalSize = 0; - for (const auto& dbName : dbNames) { - if (authorizedDatabases && - !as->isAuthorizedForAnyActionOnAnyResourceInDB(dbName.toString())) { - // We don't have listDatabases on the cluster or find on this database. - continue; - } - - ListDatabasesReplyItem item(dbName.db()); - - long long size = 0; - if (!nameOnly) { - // Filtering on name only should not require taking locks on filtered-out names. - if (filterNameOnly && !filter->matchesBSON(item.toBSON())) { - continue; - } - - AutoGetDbForReadMaybeLockFree lockFreeReadBlock(opCtx, dbName); - // The database could have been dropped since we called 'listDatabases()' above. - if (!DatabaseHolder::get(opCtx)->dbExists(opCtx, dbName)) { - continue; - } - - writeConflictRetry(opCtx, "sizeOnDisk", dbName.toString(), [&] { - size = storageEngine->sizeOnDiskForDb(opCtx, dbName); - }); - item.setSizeOnDisk(size); - item.setEmpty( - CollectionCatalog::get(opCtx)->getAllCollectionUUIDsFromDb(dbName).empty()); - } - if (!filter || filter->matchesBSON(item.toBSON())) { - totalSize += size; - items.push_back(std::move(item)); - } - } + int64_t totalSize = list_databases::setReplyItems(opCtx, + dbNames, + items, + storageEngine, + nameOnly, + filter, + false /* setTenantId */, + authorizedDatabases); ListDatabasesReply reply(items); if (!nameOnly) { |