|
| 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 | +} |
0 commit comments