diff --git a/tools/dump.cpp b/tools/dump.cpp index 27e19938173..72b4d2fa5d4 100644 --- a/tools/dump.cpp +++ b/tools/dump.cpp @@ -33,13 +33,13 @@ public: ("out,o" , po::value() , "output directory" ) ; } - + void doCollection( const string coll , path outputFile ) { cout << "\t" << coll << " to " << outputFile.string() << endl; - + int out = open( outputFile.string().c_str() , O_WRONLY | O_CREAT | O_TRUNC , 0666 ); assert( out ); - + auto_ptr cursor = conn( true ).query( coll.c_str() , Query().snapshot() , 0 , 0 , 0 , Option_SlaveOk | Option_NoCursorTimeout ); int num = 0; @@ -48,17 +48,17 @@ public: write( out , obj.objdata() , obj.objsize() ); num++; } - + cout << "\t\t " << num << " objects" << endl; - + close( out ); - } - + } + void go( const string db , const path outdir ) { cout << "DATABASE: " << db << "\t to \t" << outdir.string() << endl; create_directories( outdir ); - + string sns = db + ".system.namespaces"; auto_ptr cursor = conn( true ).query( sns.c_str() , Query() , 0 , 0 , 0 , Option_SlaveOk | Option_NoCursorTimeout ); @@ -66,37 +66,37 @@ public: BSONObj obj = cursor->next(); if ( obj.toString().find( ".$" ) != string::npos ) continue; - + const string name = obj.getField( "name" ).valuestr(); const string filename = name.substr( db.size() + 1 ); - + if ( _coll.length() > 0 && db + "." + _coll != name && _coll != name ) continue; - + doCollection( name.c_str() , outdir / ( filename + ".bson" ) ); - + } - + } int run(){ path root( getParam( "out" , "dump" ) ); string db = _db; - + if ( db == "*" ){ cout << "all dbs" << endl; auth( "admin" ); - + BSONObj res = conn( true ).findOne( "admin.$cmd" , BSON( "listDatabases" << 1 ) ); BSONObj dbs = res.getField( "databases" ).embeddedObjectUserCheck(); set keys; dbs.getFieldNames( keys ); for ( set::iterator i = keys.begin() ; i != keys.end() ; i++ ) { string key = *i; - + BSONObj dbobj = dbs.getField( key ).embeddedObjectUserCheck(); - + const char * dbName = dbobj.getField( "name" ).valuestr(); if ( (string)dbName == "local" ) continue; diff --git a/tools/restore.cpp b/tools/restore.cpp index 615de0bd185..5d2d795dd35 100644 --- a/tools/restore.cpp +++ b/tools/restore.cpp @@ -37,13 +37,13 @@ public: ; addPositionArg( "dir" , 1 ); } - + int run(){ auth(); drillDown( getParam( "dir" ) ); return 0; } - + void drillDown( path root ) { if ( is_directory( root ) ) { @@ -56,15 +56,15 @@ public: } return; } - + if ( ! ( endsWith( root.string().c_str() , ".bson" ) || endsWith( root.string().c_str() , ".bin" ) ) ) { cerr << "don't know what to do with [" << root.string() << "]" << endl; return; } - + out() << root.string() << endl; - + string ns; { string dir = root.branch_path().string(); @@ -73,39 +73,39 @@ public: else ns += dir.substr( dir.find_last_of( "/" ) + 1 ); } - + { string l = root.leaf(); l = l.substr( 0 , l.find_last_of( "." ) ); ns += "." + l; } - + if ( boost::filesystem::file_size( root ) == 0 ) { out() << "file " + root.native_file_string() + " empty, skipping" << endl; return; } out() << "\t going into namespace [" << ns << "]" << endl; - + MemoryMappedFile mmf; long fileLength; assert( mmf.map( root.string().c_str() , fileLength ) ); - + log(1) << "\t file size: " << fileLength << endl; char * data = (char*)mmf.viewOfs(); long read = 0; - + long num = 0; - + int msgDelay = (int)(1000 * ( 1 + ( mmf.length() / ( 1024.0 * 1024 * 400 ) ) ) ); log(1) << "\t msg delay: " << msgDelay << endl; while ( read < mmf.length() ) { BSONObj o( data ); - + conn().insert( ns.c_str() , o ); - + read += o.objsize(); data += o.objsize(); @@ -113,7 +113,7 @@ public: if ( logLevel > 0 && num < 10 || ! ( num % msgDelay ) ) out() << "read " << read << "/" << mmf.length() << " bytes so far. (" << (int)( (read * 100) / mmf.length()) << "%) " << num << " objects" << endl; } - + out() << "\t " << num << " objects" << endl; } };