diff options
author | Aaron <aaron@10gen.com> | 2008-12-29 13:24:24 -0500 |
---|---|---|
committer | Aaron <aaron@10gen.com> | 2008-12-29 13:24:24 -0500 |
commit | 59c6f232248829827200affee37a6faa7ace333f (patch) | |
tree | ed9a4f4b84bb9336a2632cd04b3ab37e542d87ae | |
parent | 3211918f046a8cd7b0dcf3865f66279e7a49734b (diff) | |
download | mongo-59c6f232248829827200affee37a6faa7ace333f.tar.gz |
Add listDatabases command
-rw-r--r-- | db/db.cpp | 43 | ||||
-rw-r--r-- | db/db.h | 2 | ||||
-rw-r--r-- | db/dbcommands.cpp | 22 | ||||
-rw-r--r-- | db/pdfile.cpp | 2 | ||||
-rw-r--r-- | db/pdfile.h | 2 |
5 files changed, 52 insertions, 19 deletions
diff --git a/db/db.cpp b/db/db.cpp index 85a8463d557..3c00724477a 100644 --- a/db/db.cpp +++ b/db/db.cpp @@ -46,6 +46,16 @@ void closeAllSockets(); void startReplication(); void pairWith(const char *remoteEnd, const char *arb); +void getDatabaseNames( vector< string > &names ) { + boost::filesystem::path path( dbpath ); + for ( boost::filesystem::directory_iterator i( path ); + i != boost::filesystem::directory_iterator(); ++i ) { + string fileName = i->leaf(); + if ( fileName.length() > 3 && fileName.substr( fileName.length() - 3, 3 ) == ".ns" ) + names.push_back( fileName.substr( 0, fileName.length() - 3 ) ); + } +} + struct MyStartupTests { MyStartupTests() { assert( sizeof(OID) == 12 ); @@ -282,24 +292,21 @@ void setupSignals() {} void repairDatabases() { dblock lk; - boost::filesystem::path path( dbpath ); - for ( boost::filesystem::directory_iterator i( path ); - i != boost::filesystem::directory_iterator(); ++i ) { - string fileName = i->leaf(); - if ( fileName.length() > 3 && fileName.substr( fileName.length() - 3, 3 ) == ".ns" ) { - string dbName = fileName.substr( 0, fileName.length() - 3 ); - assert( !setClientTempNs( dbName.c_str() ) ); - PhysicalDataFile *p = database->getFile( 0 ); - PDFHeader *h = p->getHeader(); - if ( !h->currentVersion() ) { - // QUESTION: Repair even if file format is higher version than code? - log() << "repairing database " << dbName << " with pdfile version " << h->version << "." << h->versionMinor << ", "; - log() << "new version: " << VERSION << "." << VERSION_MINOR << endl; - string errmsg; - assert( repairDatabase( dbName.c_str(), errmsg ) ); - } else { - closeClient( dbName.c_str() ); - } + vector< string > dbNames; + getDatabaseNames( dbNames ); + for( vector< string >::iterator i = dbNames.begin(); i != dbNames.end(); ++i ) { + string dbName = *i; + assert( !setClientTempNs( dbName.c_str() ) ); + PhysicalDataFile *p = database->getFile( 0 ); + PDFHeader *h = p->getHeader(); + if ( !h->currentVersion() ) { + // QUESTION: Repair even if file format is higher version than code? + log() << "repairing database " << dbName << " with pdfile version " << h->version << "." << h->versionMinor << ", "; + log() << "new version: " << VERSION << "." << VERSION_MINOR << endl; + string errmsg; + assert( repairDatabase( dbName.c_str(), errmsg ) ); + } else { + closeClient( dbName.c_str() ); } } } @@ -107,6 +107,8 @@ inline string getKey( const char *ns, const char *path ) { return string( cl ) + ":" + path; } +void getDatabaseNames( vector< string > &names ); + /* returns true if the database ("database") did not exist, and it was created on this call */ inline bool setClient(const char *ns, const char *path=dbpath) { /* we must be in critical section at this point as these are global diff --git a/db/dbcommands.cpp b/db/dbcommands.cpp index 556ab601b7e..95c84fe9e40 100644 --- a/db/dbcommands.cpp +++ b/db/dbcommands.cpp @@ -505,6 +505,28 @@ public: } } cmdDeleteIndexes; +class CmdListDatabases : public Command { +public: + virtual bool logTheOp() { return false; } + virtual bool slaveOk() { return true; } + virtual bool adminOnly() { return true; } + CmdListDatabases() : Command("listDatabases") {} + bool run(const char *ns, BSONObj& jsobj, string& errmsg, BSONObjBuilder& result, bool /*fromRepl*/) { + vector< string > dbNames; + getDatabaseNames( dbNames ); + vector< BSONObj > dbInfos; + for( vector< string >::iterator i = dbNames.begin(); i != dbNames.end(); ++i ) { + BSONObjBuilder s; + s.append( "diskSize", dbSize( i->c_str() ) ); + BSONObjBuilder b; + b.append( i->c_str(), s.done() ); + dbInfos.push_back( b.doneAndDecouple() ); + } + result.append( "databases", dbInfos ); + return true; + } +} cmdListDatabases; + extern map<string,Command*> *commands; /* TODO make these all command objects -- legacy stuff here diff --git a/db/pdfile.cpp b/db/pdfile.cpp index 03c6ac4d510..3f8825c0b81 100644 --- a/db/pdfile.cpp +++ b/db/pdfile.cpp @@ -1035,7 +1035,7 @@ boost::intmax_t dbSize( const char *database ) { SizeAccumulator() : totalSize_( 0 ) {} boost::intmax_t size() const { return totalSize_; } private: - virtual bool apply( const Path &p ) { + virtual bool apply( const boost::filesystem::path &p ) { if( !boost::filesystem::exists( p ) ) return false; totalSize_ += boost::filesystem::file_size( p ); diff --git a/db/pdfile.h b/db/pdfile.h index c80776bcd8d..6f314774973 100644 --- a/db/pdfile.h +++ b/db/pdfile.h @@ -392,6 +392,8 @@ inline void _deleteDataFiles(const char *database) { _applyOpToDataFiles( database, deleter ); } +boost::intmax_t dbSize( const char *database ); + inline NamespaceIndex* nsindex(const char *ns) { DEV { char buf[256]; |