diff options
author | Aaron <aaron@10gen.com> | 2009-05-20 11:39:12 -0400 |
---|---|---|
committer | Aaron <aaron@10gen.com> | 2009-05-20 11:39:12 -0400 |
commit | da7a9b3f71a151b17665d7033370adb5507bf3fd (patch) | |
tree | a5f460a69459909e3878a6661b631465b3f63fe8 | |
parent | b8c6d85506d04c3b9d9b804fcf6d1e763b55f963 (diff) | |
download | mongo-da7a9b3f71a151b17665d7033370adb5507bf3fd.tar.gz |
make tools capable of running standalone, without a mongod instance
-rw-r--r-- | SConstruct | 2 | ||||
-rw-r--r-- | db/db.cpp | 9 | ||||
-rw-r--r-- | db/instance.cpp | 9 | ||||
-rw-r--r-- | db/instance.h | 3 | ||||
-rw-r--r-- | dbtests/dbtests.cpp | 7 | ||||
-rw-r--r-- | tools/Tool.cpp | 40 | ||||
-rw-r--r-- | tools/Tool.h | 9 | ||||
-rw-r--r-- | tools/dump.cpp | 7 | ||||
-rw-r--r-- | tools/export.cpp | 2 | ||||
-rw-r--r-- | tools/files.cpp | 2 | ||||
-rw-r--r-- | tools/importJSON.cpp | 8 | ||||
-rw-r--r-- | tools/restore.cpp | 2 |
12 files changed, 52 insertions, 48 deletions
diff --git a/SConstruct b/SConstruct index 28d87ffd2b5..f408591b96b 100644 --- a/SConstruct +++ b/SConstruct @@ -663,7 +663,7 @@ mongod = env.Program( "mongod" , commonFiles + coreDbFiles + serverOnlyFiles + [ Default( mongod ) # tools -allToolFiles = allClientFiles + [ "tools/Tool.cpp" ] +allToolFiles = commonFiles + coreDbFiles + serverOnlyFiles + [ "client/gridfs.cpp", "tools/Tool.cpp" ] env.Program( "mongodump" , allToolFiles + [ "tools/dump.cpp" ] ) env.Program( "mongorestore" , allToolFiles + [ "tools/restore.cpp" ] ) diff --git a/db/db.cpp b/db/db.cpp index 3d51a465993..58c7896afa5 100644 --- a/db/db.cpp +++ b/db/db.cpp @@ -328,15 +328,6 @@ namespace mongo { Timer startupSrandTimer; - void acquirePathLock() { -#if !defined(_WIN32) && !defined(__sunos__) - string name = ( boost::filesystem::path( dbpath ) / "mongod.lock" ).native_file_string(); - lockFile = open( name.c_str(), O_RDONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO ); - massert( "Unable to create / open lock file for dbpath: " + name, lockFile > 0 ); - massert( "Unable to acquire lock for dbpath: " + name, flock( lockFile, LOCK_EX | LOCK_NB ) == 0 ); -#endif - } - void _initAndListen(int listenPort, const char *appserverLoc = null) { #if !defined(_WIN32) diff --git a/db/instance.cpp b/db/instance.cpp index 8f7141f5d5d..b3ac8e06150 100644 --- a/db/instance.cpp +++ b/db/instance.cpp @@ -698,4 +698,13 @@ namespace mongo { ::exit(rc); } + void acquirePathLock() { +#if !defined(_WIN32) && !defined(__sunos__) + string name = ( boost::filesystem::path( dbpath ) / "mongod.lock" ).native_file_string(); + lockFile = open( name.c_str(), O_RDONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO ); + massert( "Unable to create / open lock file for dbpath: " + name, lockFile > 0 ); + massert( "Unable to acquire lock for dbpath: " + name, flock( lockFile, LOCK_EX | LOCK_NB ) == 0 ); +#endif + } + } // namespace mongo diff --git a/db/instance.h b/db/instance.h index b8920011f6a..75211dfb90f 100644 --- a/db/instance.h +++ b/db/instance.h @@ -144,5 +144,8 @@ namespace mongo { string oldName_; }; }; + + extern int lockFile; + void acquirePathLock(); } // namespace mongo diff --git a/dbtests/dbtests.cpp b/dbtests/dbtests.cpp index f22d5ec563a..c9e2b596694 100644 --- a/dbtests/dbtests.cpp +++ b/dbtests/dbtests.cpp @@ -89,12 +89,7 @@ int main( int argc, char** argv ) { dbpathSpec += "/"; dbpath = dbpathSpec.c_str(); -#if !defined(_WIN32) && !defined(__sunos__) - string name = ( boost::filesystem::path( dbpath ) / "mongod.lock" ).native_file_string(); - int lockFile = open( name.c_str(), O_RDONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO ); - massert( "Unable to create / open lock file for dbpath: " + name, lockFile > 0 ); - massert( "Unable to acquire lock for dbpath: " + name, flock( lockFile, LOCK_EX | LOCK_NB ) == 0 ); -#endif + acquirePathLock(); srand( seed ); printGitVersion(); diff --git a/tools/Tool.cpp b/tools/Tool.cpp index 3f273c86417..8aec59632ce 100644 --- a/tools/Tool.cpp +++ b/tools/Tool.cpp @@ -6,21 +6,13 @@ #include <boost/filesystem/operations.hpp> -namespace mongo { - DBClientBase *createDirectClient() { - cout << "no direct client available" << endl; - assert( false ); - return 0; - } -} // namespace mongo - using namespace std; using namespace mongo; namespace po = boost::program_options; mongo::Tool::Tool( string name , string defaultDB , string defaultCollection ) : - _name( name ) , _db( defaultDB ) , _coll( defaultCollection ){ + _name( name ) , _db( defaultDB ) , _coll( defaultCollection ), _useDirect() { _options = new po::options_description( name + " options" ); _options->add_options() @@ -28,6 +20,7 @@ mongo::Tool::Tool( string name , string defaultDB , string defaultCollection ) : ("host,h",po::value<string>(), "mongo host to connect to" ) ("db,d",po::value<string>(), "database to use" ) ("collection,c",po::value<string>(), "collection to use (some commands)" ) + ("dbpath",po::value<string>(), "directly access mongod data files in this path, instead of connecting to a mongod instance" ) ; } @@ -53,19 +46,26 @@ int mongo::Tool::main( int argc , char ** argv ){ printExtraHelp( cerr ); return 0; } - - const char * host = "127.0.0.1"; - if ( _params.count( "host" ) ) - host = _params["host"].as<string>().c_str(); - - string errmsg; - if ( ! _conn.connect( host , errmsg ) ){ - cerr << "couldn't connect to [" << host << "] " << errmsg << endl; - return -1; + + if ( !hasParam( "dbpath" ) ) { + const char * host = "127.0.0.1"; + if ( _params.count( "host" ) ) + host = _params["host"].as<string>().c_str(); + + string errmsg; + if ( ! _conn.connect( host , errmsg ) ){ + cerr << "couldn't connect to [" << host << "] " << errmsg << endl; + return -1; + } + + cerr << "connected to: " << host << endl; + } else { + _useDirect = true; + static string myDbpath = getParam( "dbpath" ); + mongo::dbpath = myDbpath.c_str(); + mongo::acquirePathLock(); } - cerr << "connected to: " << host << endl; - if ( _params.count( "db" ) ) _db = _params["db"].as<string>(); diff --git a/tools/Tool.h b/tools/Tool.h index 8f7b2fa76c6..a579ad9afd6 100644 --- a/tools/Tool.h +++ b/tools/Tool.h @@ -11,6 +11,7 @@ #endif #include "client/dbclient.h" +#include "db/instance.h" using std::string; @@ -48,17 +49,21 @@ namespace mongo { } virtual int run() = 0; - + virtual void printExtraHelp( ostream & out ); protected: string _name; - mongo::DBClientConnection _conn; string _db; string _coll; + mongo::DBClientBase &conn() { return _useDirect ? (mongo::DBClientBase&)_direct : (mongo::DBClientBase&)_conn; }; + private: + mongo::DBClientConnection _conn; + mongo::DBDirectClient _direct; + bool _useDirect; boost::program_options::options_description * _options; boost::program_options::positional_options_description _positonalOptions; diff --git a/tools/dump.cpp b/tools/dump.cpp index 26604e15f0e..03bcb2c7b0e 100644 --- a/tools/dump.cpp +++ b/tools/dump.cpp @@ -40,7 +40,7 @@ public: int out = open( outputFile.string().c_str() , O_WRONLY | O_CREAT | O_TRUNC , 0666 ); assert( out ); - auto_ptr<DBClientCursor> cursor = _conn.query( coll.c_str() , BSONObj() , 0 , 0 , 0 , Option_SlaveOk ); + auto_ptr<DBClientCursor> cursor = conn().query( coll.c_str() , BSONObj() , 0 , 0 , 0 , Option_SlaveOk ); int num = 0; while ( cursor->more() ) { @@ -61,7 +61,7 @@ public: string sns = db + ".system.namespaces"; - auto_ptr<DBClientCursor> cursor = _conn.query( sns.c_str() , BSONObj() , 0 , 0 , 0 , Option_SlaveOk ); + auto_ptr<DBClientCursor> cursor = conn().query( sns.c_str() , BSONObj() , 0 , 0 , 0 , Option_SlaveOk ); while ( cursor->more() ) { BSONObj obj = cursor->next(); if ( obj.toString().find( ".$" ) != string::npos ) @@ -84,7 +84,7 @@ public: if ( db == "*" ){ cout << "all dbs" << endl; - BSONObj res = _conn.findOne( "admin.$cmd" , BSON( "listDatabases" << 1 ) ); + BSONObj res = conn().findOne( "admin.$cmd" , BSON( "listDatabases" << 1 ) ); BSONObj dbs = res.getField( "databases" ).embeddedObjectUserCheck(); set<string> keys; dbs.getFieldNames( keys ); @@ -105,6 +105,7 @@ public: } return 0; } + }; int main( int argc , char ** argv ) { diff --git a/tools/export.cpp b/tools/export.cpp index 119f0be6d18..2245b7c7328 100644 --- a/tools/export.cpp +++ b/tools/export.cpp @@ -74,7 +74,7 @@ public: } - auto_ptr<DBClientCursor> cursor = _conn.query( ns.c_str() , getParam( "query" , "" ) , 0 , 0 , fieldsToReturn , Option_SlaveOk ); + auto_ptr<DBClientCursor> cursor = conn().query( ns.c_str() , getParam( "query" , "" ) , 0 , 0 , fieldsToReturn , Option_SlaveOk ); if ( csv ){ for ( vector<string>::iterator i=fields.begin(); i != fields.end(); i++ ){ diff --git a/tools/files.cpp b/tools/files.cpp index 04b7dc0da4f..957c546105d 100644 --- a/tools/files.cpp +++ b/tools/files.cpp @@ -65,7 +65,7 @@ public: return -1; } - GridFS g( _conn , _db ); + GridFS g( conn() , _db ); string filename = getParam( "file" ); if ( cmd == "list" ){ diff --git a/tools/importJSON.cpp b/tools/importJSON.cpp index 7eb4f0a9fe9..28bd48b72ba 100644 --- a/tools/importJSON.cpp +++ b/tools/importJSON.cpp @@ -63,11 +63,11 @@ public: if ( hasParam( "drop" ) ){ cout << "dropping: " << ns << endl; - _conn.dropCollection( ns.c_str() ); + conn().dropCollection( ns.c_str() ); } if ( hasParam( "idbefore" ) ){ - _conn.ensureIndex( ns.c_str() , BSON( "_id" << 1 ) ); + conn().ensureIndex( ns.c_str() , BSON( "_id" << 1 ) ); } int num = 0; @@ -87,7 +87,7 @@ public: try { BSONObj o = fromjson( line ); - _conn.insert( ns.c_str() , o ); + conn().insert( ns.c_str() , o ); } catch ( MsgAssertionException& ma ){ cout << "exception:" << ma.toString() << endl; @@ -101,7 +101,7 @@ public: } if ( hasParam( "id" ) ){ - _conn.ensureIndex( ns.c_str() , BSON( "_id" << 1 ) ); + conn().ensureIndex( ns.c_str() , BSON( "_id" << 1 ) ); } return 0; diff --git a/tools/restore.cpp b/tools/restore.cpp index aa36b901ce1..56e360a1699 100644 --- a/tools/restore.cpp +++ b/tools/restore.cpp @@ -95,7 +95,7 @@ public: while ( read < mmf.length() ) { BSONObj o( data ); - _conn.insert( ns.c_str() , o ); + conn().insert( ns.c_str() , o ); read += o.objsize(); data += o.objsize(); |