diff options
Diffstat (limited to 'src/mongo/db/commands')
-rw-r--r-- | src/mongo/db/commands/list_databases.cpp | 44 | ||||
-rw-r--r-- | src/mongo/db/commands/list_databases.idl | 30 |
2 files changed, 50 insertions, 24 deletions
diff --git a/src/mongo/db/commands/list_databases.cpp b/src/mongo/db/commands/list_databases.cpp index 354801cdd7d..fa70ed6ac96 100644 --- a/src/mongo/db/commands/list_databases.cpp +++ b/src/mongo/db/commands/list_databases.cpp @@ -102,7 +102,7 @@ public: BSONObjBuilder& result) final { CommandHelpers::handleMarkKillOnClientDisconnect(opCtx); IDLParserErrorContext ctx("listDatabases"); - auto cmd = ListDatabasesCommand::parse(ctx, cmdObj); + auto cmd = ListDatabases::parse(ctx, cmdObj); auto* as = AuthorizationSession::get(opCtx->getClient()); // {nameOnly: bool} - default false. @@ -146,53 +146,51 @@ public: dbNames = storageEngine->listDatabases(); } - vector<BSONObj> dbInfos; + vector<ListDatabasesReplyItem> items; const bool filterNameOnly = filter && filter->getCategory() == MatchExpression::MatchCategory::kLeaf && filter->path() == kNameField; - intmax_t totalSize = 0; - for (const auto& dbname : dbNames) { - if (authorizedDatabases && !as->isAuthorizedForAnyActionOnAnyResourceInDB(dbname)) { + long long totalSize = 0; + for (const auto& itemName : dbNames) { + if (authorizedDatabases && !as->isAuthorizedForAnyActionOnAnyResourceInDB(itemName)) { // We don't have listDatabases on the cluser or find on this database. continue; } - BSONObjBuilder b; - b.append("name", dbname); + ListDatabasesReplyItem item(itemName); - int64_t size = 0; + long long size = 0; if (!nameOnly) { // Filtering on name only should not require taking locks on filtered-out names. - if (filterNameOnly && !filter->matchesBSON(b.asTempObj())) + if (filterNameOnly && !filter->matchesBSON(item.toBSON())) continue; - AutoGetDb autoDb(opCtx, dbname, MODE_IS); + AutoGetDb autoDb(opCtx, itemName, MODE_IS); Database* const db = autoDb.getDb(); if (!db) continue; - writeConflictRetry(opCtx, "sizeOnDisk", dbname, [&] { - size = storageEngine->sizeOnDiskForDb(opCtx, dbname); + writeConflictRetry(opCtx, "sizeOnDisk", itemName, [&] { + size = storageEngine->sizeOnDiskForDb(opCtx, itemName); }); - b.append("sizeOnDisk", static_cast<double>(size)); - - b.appendBool( - "empty", - CollectionCatalog::get(opCtx)->getAllCollectionUUIDsFromDb(dbname).empty()); + item.setSizeOnDisk(size); + item.setEmpty( + CollectionCatalog::get(opCtx)->getAllCollectionUUIDsFromDb(itemName).empty()); } - BSONObj curDbObj = b.obj(); - - if (!filter || filter->matchesBSON(curDbObj)) { + if (!filter || filter->matchesBSON(item.toBSON())) { totalSize += size; - dbInfos.push_back(curDbObj); + items.push_back(std::move(item)); } } - result.append("databases", dbInfos); + ListDatabasesReply reply(items); if (!nameOnly) { - result.append("totalSize", double(totalSize)); + reply.setTotalSize(totalSize); + reply.setTotalSizeMb(totalSize / (1024 * 1024)); } + + reply.serialize(&result); return true; } } cmdListDatabases; diff --git a/src/mongo/db/commands/list_databases.idl b/src/mongo/db/commands/list_databases.idl index 83e3e8f5351..8342078de30 100644 --- a/src/mongo/db/commands/list_databases.idl +++ b/src/mongo/db/commands/list_databases.idl @@ -32,12 +32,39 @@ global: imports: - "mongo/idl/basic_types.idl" +structs: + ListDatabasesReplyItem: + description: "Individual result" + fields: + name: string + sizeOnDisk: + type: long + optional: true + empty: + type: bool + optional: true + shards: + type: object_owned + optional: true + + ListDatabasesReply: + description: "The listDatabases command's reply." + fields: + databases: array<ListDatabasesReplyItem> + totalSize: + type: long + optional: true + totalSizeMb: + type: long + optional: true commands: - listDatabasesCommand: + listDatabases: description: "listDatabases Command" command_name: "listDatabases" namespace: ignored + api_version: "1" + strict: true fields: nameOnly: description: "Return just the database name without metadata" @@ -51,3 +78,4 @@ commands: description: "Filter description to limit results" type: object optional: true + reply_type: ListDatabasesReply |