diff options
author | Eliot Horowitz <eliot@10gen.com> | 2014-06-30 13:37:36 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2014-06-30 21:45:54 -0400 |
commit | 8aa51c901f184564cb0fe44f2f5b9125f69849ee (patch) | |
tree | 0185f6a0c97faab17deb328326528c58af500a0c /src | |
parent | d211a326cd98d8b97c80e2a9945220c543c77d03 (diff) | |
download | mongo-8aa51c901f184564cb0fe44f2f5b9125f69849ee.tar.gz |
SERVER-14378: move listDatabases to its own file
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/commands/list_databases.cpp | 120 | ||||
-rw-r--r-- | src/mongo/db/dbcommands.cpp | 78 |
3 files changed, 121 insertions, 78 deletions
diff --git a/src/mongo/SConscript b/src/mongo/SConscript index 69bad236c26..86d034b29d5 100644 --- a/src/mongo/SConscript +++ b/src/mongo/SConscript @@ -668,6 +668,7 @@ serverOnlyFiles = [ "db/curop.cpp", "db/commands/auth_schema_upgrade_d.cpp", "db/commands/create_indexes.cpp", "db/commands/dbhash.cpp", + "db/commands/list_databases.cpp", "db/commands/merge_chunks_cmd.cpp", "db/commands/cleanup_orphaned_cmd.cpp", "db/commands/collection_to_capped.cpp", diff --git a/src/mongo/db/commands/list_databases.cpp b/src/mongo/db/commands/list_databases.cpp new file mode 100644 index 00000000000..725b8e45bf5 --- /dev/null +++ b/src/mongo/db/commands/list_databases.cpp @@ -0,0 +1,120 @@ +// list_databases.cpp + +/** +* Copyright (C) 2014 10gen Inc. +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License, version 3, +* as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +* +* As a special exception, the copyright holders give permission to link the +* code of portions of this program with the OpenSSL library under certain +* conditions as described in each individual source file and distribute +* linked combinations including the program with the OpenSSL library. You +* must comply with the GNU Affero General Public License in all respects for +* all of the code used other than as permitted herein. If you modify file(s) +* with this exception, you may extend this exception to your version of the +* file(s), but you are not obligated to do so. If you do not wish to do so, +* delete this exception statement from your version. If you delete this +* exception statement from all source files in the program, then also delete +* it in the license file. +*/ + +#include "mongo/db/catalog/database.h" +#include "mongo/db/catalog/database_catalog_entry.h" +#include "mongo/db/catalog/database_holder.h" +#include "mongo/db/client.h" +#include "mongo/db/commands.h" +#include "mongo/db/storage/storage_engine.h" + +namespace mongo { + + // XXX: remove and put into storage api + intmax_t dbSize( const string& database ); + + class CmdListDatabases : public Command { + public: + virtual bool slaveOk() const { + return true; + } + virtual bool slaveOverrideOk() const { + return true; + } + virtual bool adminOnly() const { + return true; + } + virtual bool isWriteCommandForConfigServer() const { return false; } + virtual void help( stringstream& help ) const { help << "list databases on this server"; } + virtual void addRequiredPrivileges(const std::string& dbname, + const BSONObj& cmdObj, + std::vector<Privilege>* out) { + ActionSet actions; + actions.addAction(ActionType::listDatabases); + out->push_back(Privilege(ResourcePattern::forClusterResource(), actions)); + } + CmdListDatabases() : Command("listDatabases" , true ) {} + bool run(OperationContext* txn, const string& dbname , BSONObj& jsobj, int, string& errmsg, BSONObjBuilder& result, bool /*fromRepl*/) { + vector< string > dbNames; + globalStorageEngine->listDatabases( &dbNames ); + + vector< BSONObj > dbInfos; + + set<string> seen; + intmax_t totalSize = 0; + for ( vector< string >::iterator i = dbNames.begin(); i != dbNames.end(); ++i ) { + BSONObjBuilder b; + b.append( "name", *i ); + + intmax_t size = dbSize( i->c_str() ); + b.append( "sizeOnDisk", (double) size ); + totalSize += size; + + { + Client::ReadContext rc(txn, *i + ".system.namespaces"); + b.appendBool( "empty", rc.ctx().db()->getDatabaseCatalogEntry()->isEmpty() ); + } + + dbInfos.push_back( b.obj() ); + + seen.insert( i->c_str() ); + } + + set<string> allShortNames; + { + Lock::GlobalRead lk(txn->lockState()); + dbHolder().getAllShortNames(allShortNames); + } + + for ( set<string>::iterator i = allShortNames.begin(); i != allShortNames.end(); i++ ) { + string name = *i; + + if ( seen.count( name ) ) + continue; + + BSONObjBuilder b; + b.append( "name" , name ); + b.append( "sizeOnDisk" , (double)1.0 ); + + { + Client::ReadContext ctx(txn, name); + b.appendBool( "empty", ctx.ctx().db()->getDatabaseCatalogEntry()->isEmpty() ); + } + + dbInfos.push_back( b.obj() ); + } + + result.append( "databases", dbInfos ); + result.append( "totalSize", double( totalSize ) ); + return true; + } + } cmdListDatabases; + +} diff --git a/src/mongo/db/dbcommands.cpp b/src/mongo/db/dbcommands.cpp index 4d7f8000f15..948e16cfcf2 100644 --- a/src/mongo/db/dbcommands.cpp +++ b/src/mongo/db/dbcommands.cpp @@ -542,84 +542,6 @@ namespace mongo { } } cmdCreate; - class CmdListDatabases : public Command { - public: - virtual bool slaveOk() const { - return true; - } - virtual bool slaveOverrideOk() const { - return true; - } - virtual bool adminOnly() const { - return true; - } - virtual bool isWriteCommandForConfigServer() const { return false; } - virtual void help( stringstream& help ) const { help << "list databases on this server"; } - virtual void addRequiredPrivileges(const std::string& dbname, - const BSONObj& cmdObj, - std::vector<Privilege>* out) { - ActionSet actions; - actions.addAction(ActionType::listDatabases); - out->push_back(Privilege(ResourcePattern::forClusterResource(), actions)); - } - CmdListDatabases() : Command("listDatabases" , true ) {} - bool run(OperationContext* txn, const string& dbname , BSONObj& jsobj, int, string& errmsg, BSONObjBuilder& result, bool /*fromRepl*/) { - vector< string > dbNames; - globalStorageEngine->listDatabases( &dbNames ); - - vector< BSONObj > dbInfos; - - set<string> seen; - intmax_t totalSize = 0; - for ( vector< string >::iterator i = dbNames.begin(); i != dbNames.end(); ++i ) { - BSONObjBuilder b; - b.append( "name", *i ); - - intmax_t size = dbSize( i->c_str() ); - b.append( "sizeOnDisk", (double) size ); - totalSize += size; - - { - Client::ReadContext rc(txn, *i + ".system.namespaces"); - b.appendBool( "empty", rc.ctx().db()->getDatabaseCatalogEntry()->isEmpty() ); - } - - dbInfos.push_back( b.obj() ); - - seen.insert( i->c_str() ); - } - - // TODO: erh 1/1/2010 I think this is broken where - // path != storageGlobalParams.dbpath ?? - set<string> allShortNames; - { - Lock::GlobalRead lk(txn->lockState()); - dbHolder().getAllShortNames(allShortNames); - } - - for ( set<string>::iterator i = allShortNames.begin(); i != allShortNames.end(); i++ ) { - string name = *i; - - if ( seen.count( name ) ) - continue; - - BSONObjBuilder b; - b.append( "name" , name ); - b.append( "sizeOnDisk" , (double)1.0 ); - - { - Client::ReadContext ctx(txn, name); - b.appendBool( "empty", ctx.ctx().db()->getDatabaseCatalogEntry()->isEmpty() ); - } - - dbInfos.push_back( b.obj() ); - } - - result.append( "databases", dbInfos ); - result.append( "totalSize", double( totalSize ) ); - return true; - } - } cmdListDatabases; /* note an access to a database right after this will open it back up - so this is mainly for diagnostic purposes. |