Skip to content

Commit 8aa51c9

Browse files
committed
SERVER-14378: move listDatabases to its own file
1 parent d211a32 commit 8aa51c9

File tree

3 files changed

+121
-78
lines changed

3 files changed

+121
-78
lines changed

src/mongo/SConscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ serverOnlyFiles = [ "db/curop.cpp",
668668
"db/commands/auth_schema_upgrade_d.cpp",
669669
"db/commands/create_indexes.cpp",
670670
"db/commands/dbhash.cpp",
671+
"db/commands/list_databases.cpp",
671672
"db/commands/merge_chunks_cmd.cpp",
672673
"db/commands/cleanup_orphaned_cmd.cpp",
673674
"db/commands/collection_to_capped.cpp",
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// list_databases.cpp
2+
3+
/**
4+
* Copyright (C) 2014 10gen Inc.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License, version 3,
8+
* as published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
* As a special exception, the copyright holders give permission to link the
19+
* code of portions of this program with the OpenSSL library under certain
20+
* conditions as described in each individual source file and distribute
21+
* linked combinations including the program with the OpenSSL library. You
22+
* must comply with the GNU Affero General Public License in all respects for
23+
* all of the code used other than as permitted herein. If you modify file(s)
24+
* with this exception, you may extend this exception to your version of the
25+
* file(s), but you are not obligated to do so. If you do not wish to do so,
26+
* delete this exception statement from your version. If you delete this
27+
* exception statement from all source files in the program, then also delete
28+
* it in the license file.
29+
*/
30+
31+
#include "mongo/db/catalog/database.h"
32+
#include "mongo/db/catalog/database_catalog_entry.h"
33+
#include "mongo/db/catalog/database_holder.h"
34+
#include "mongo/db/client.h"
35+
#include "mongo/db/commands.h"
36+
#include "mongo/db/storage/storage_engine.h"
37+
38+
namespace mongo {
39+
40+
// XXX: remove and put into storage api
41+
intmax_t dbSize( const string& database );
42+
43+
class CmdListDatabases : public Command {
44+
public:
45+
virtual bool slaveOk() const {
46+
return true;
47+
}
48+
virtual bool slaveOverrideOk() const {
49+
return true;
50+
}
51+
virtual bool adminOnly() const {
52+
return true;
53+
}
54+
virtual bool isWriteCommandForConfigServer() const { return false; }
55+
virtual void help( stringstream& help ) const { help << "list databases on this server"; }
56+
virtual void addRequiredPrivileges(const std::string& dbname,
57+
const BSONObj& cmdObj,
58+
std::vector<Privilege>* out) {
59+
ActionSet actions;
60+
actions.addAction(ActionType::listDatabases);
61+
out->push_back(Privilege(ResourcePattern::forClusterResource(), actions));
62+
}
63+
CmdListDatabases() : Command("listDatabases" , true ) {}
64+
bool run(OperationContext* txn, const string& dbname , BSONObj& jsobj, int, string& errmsg, BSONObjBuilder& result, bool /*fromRepl*/) {
65+
vector< string > dbNames;
66+
globalStorageEngine->listDatabases( &dbNames );
67+
68+
vector< BSONObj > dbInfos;
69+
70+
set<string> seen;
71+
intmax_t totalSize = 0;
72+
for ( vector< string >::iterator i = dbNames.begin(); i != dbNames.end(); ++i ) {
73+
BSONObjBuilder b;
74+
b.append( "name", *i );
75+
76+
intmax_t size = dbSize( i->c_str() );
77+
b.append( "sizeOnDisk", (double) size );
78+
totalSize += size;
79+
80+
{
81+
Client::ReadContext rc(txn, *i + ".system.namespaces");
82+
b.appendBool( "empty", rc.ctx().db()->getDatabaseCatalogEntry()->isEmpty() );
83+
}
84+
85+
dbInfos.push_back( b.obj() );
86+
87+
seen.insert( i->c_str() );
88+
}
89+
90+
set<string> allShortNames;
91+
{
92+
Lock::GlobalRead lk(txn->lockState());
93+
dbHolder().getAllShortNames(allShortNames);
94+
}
95+
96+
for ( set<string>::iterator i = allShortNames.begin(); i != allShortNames.end(); i++ ) {
97+
string name = *i;
98+
99+
if ( seen.count( name ) )
100+
continue;
101+
102+
BSONObjBuilder b;
103+
b.append( "name" , name );
104+
b.append( "sizeOnDisk" , (double)1.0 );
105+
106+
{
107+
Client::ReadContext ctx(txn, name);
108+
b.appendBool( "empty", ctx.ctx().db()->getDatabaseCatalogEntry()->isEmpty() );
109+
}
110+
111+
dbInfos.push_back( b.obj() );
112+
}
113+
114+
result.append( "databases", dbInfos );
115+
result.append( "totalSize", double( totalSize ) );
116+
return true;
117+
}
118+
} cmdListDatabases;
119+
120+
}

src/mongo/db/dbcommands.cpp

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -542,84 +542,6 @@ namespace mongo {
542542
}
543543
} cmdCreate;
544544

545-
class CmdListDatabases : public Command {
546-
public:
547-
virtual bool slaveOk() const {
548-
return true;
549-
}
550-
virtual bool slaveOverrideOk() const {
551-
return true;
552-
}
553-
virtual bool adminOnly() const {
554-
return true;
555-
}
556-
virtual bool isWriteCommandForConfigServer() const { return false; }
557-
virtual void help( stringstream& help ) const { help << "list databases on this server"; }
558-
virtual void addRequiredPrivileges(const std::string& dbname,
559-
const BSONObj& cmdObj,
560-
std::vector<Privilege>* out) {
561-
ActionSet actions;
562-
actions.addAction(ActionType::listDatabases);
563-
out->push_back(Privilege(ResourcePattern::forClusterResource(), actions));
564-
}
565-
CmdListDatabases() : Command("listDatabases" , true ) {}
566-
bool run(OperationContext* txn, const string& dbname , BSONObj& jsobj, int, string& errmsg, BSONObjBuilder& result, bool /*fromRepl*/) {
567-
vector< string > dbNames;
568-
globalStorageEngine->listDatabases( &dbNames );
569-
570-
vector< BSONObj > dbInfos;
571-
572-
set<string> seen;
573-
intmax_t totalSize = 0;
574-
for ( vector< string >::iterator i = dbNames.begin(); i != dbNames.end(); ++i ) {
575-
BSONObjBuilder b;
576-
b.append( "name", *i );
577-
578-
intmax_t size = dbSize( i->c_str() );
579-
b.append( "sizeOnDisk", (double) size );
580-
totalSize += size;
581-
582-
{
583-
Client::ReadContext rc(txn, *i + ".system.namespaces");
584-
b.appendBool( "empty", rc.ctx().db()->getDatabaseCatalogEntry()->isEmpty() );
585-
}
586-
587-
dbInfos.push_back( b.obj() );
588-
589-
seen.insert( i->c_str() );
590-
}
591-
592-
// TODO: erh 1/1/2010 I think this is broken where
593-
// path != storageGlobalParams.dbpath ??
594-
set<string> allShortNames;
595-
{
596-
Lock::GlobalRead lk(txn->lockState());
597-
dbHolder().getAllShortNames(allShortNames);
598-
}
599-
600-
for ( set<string>::iterator i = allShortNames.begin(); i != allShortNames.end(); i++ ) {
601-
string name = *i;
602-
603-
if ( seen.count( name ) )
604-
continue;
605-
606-
BSONObjBuilder b;
607-
b.append( "name" , name );
608-
b.append( "sizeOnDisk" , (double)1.0 );
609-
610-
{
611-
Client::ReadContext ctx(txn, name);
612-
b.appendBool( "empty", ctx.ctx().db()->getDatabaseCatalogEntry()->isEmpty() );
613-
}
614-
615-
dbInfos.push_back( b.obj() );
616-
}
617-
618-
result.append( "databases", dbInfos );
619-
result.append( "totalSize", double( totalSize ) );
620-
return true;
621-
}
622-
} cmdListDatabases;
623545

624546
/* note an access to a database right after this will open it back up - so this is mainly
625547
for diagnostic purposes.

0 commit comments

Comments
 (0)