diff options
author | Kristina Chodorow <k@ubuntu.(none)> | 2010-08-06 18:20:54 -0400 |
---|---|---|
committer | Kristina Chodorow <k@ubuntu.(none)> | 2010-08-06 18:20:54 -0400 |
commit | b4636218357869d22a790d986498b27cc6cddffb (patch) | |
tree | 97a7ebd6939b060c6c9a763a6b04ef6c7970aded /tools | |
parent | 0dd9c00ad2e68742c4affc732b79389ac1165fa8 (diff) | |
download | mongo-b4636218357869d22a790d986498b27cc6cddffb.tar.gz |
allow dumping a collection to stdout SERVER-763
Diffstat (limited to 'tools')
-rw-r--r-- | tools/dump.cpp | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/tools/dump.cpp b/tools/dump.cpp index 7bb38ca3b7c..8f4169f76a5 100644 --- a/tools/dump.cpp +++ b/tools/dump.cpp @@ -28,22 +28,14 @@ namespace po = boost::program_options; class Dump : public Tool { public: - Dump() : Tool( "dump" , true , "*" ){ + Dump() : Tool( "dump" , true , "*" , "*" , false ){ add_options() - ("out,o", po::value<string>()->default_value("dump"), "output directory") + ("out,o", po::value<string>()->default_value("dump"), "output directory or stdout") ("query,q", po::value<string>() , "json query" ) ; } - void doCollection( const string coll , path outputFile ) { - cout << "\t" << coll << " to " << outputFile.string() << endl; - - ofstream out; - out.open( outputFile.string().c_str() , ios_base::out | ios_base::binary ); - assertStreamGood( 10262 , "couldn't open file" , out ); - - ProgressMeter m( conn( true ).count( coll.c_str() , BSONObj() , QueryOption_SlaveOk ) ); - + void doCollection( const string coll , ostream &out , ProgressMeter *m ) { Query q; if ( _query.isEmpty() ) q.snapshot(); @@ -55,14 +47,34 @@ public: while ( cursor->more() ) { BSONObj obj = cursor->next(); out.write( obj.objdata() , obj.objsize() ); - m.hit(); + + // if there's a progress bar, hit it + if (m) { + m->hit(); + } } + } + + void writeCollectionFile( const string coll , path outputFile ) { + cout << "\t" << coll << " to " << outputFile.string() << endl; + + ofstream out; + out.open( outputFile.string().c_str() , ios_base::out | ios_base::binary ); + assertStreamGood( 10262 , "couldn't open file" , out ); + + ProgressMeter m( conn( true ).count( coll.c_str() , BSONObj() , QueryOption_SlaveOk ) ); + + doCollection(coll, out, &m); cout << "\t\t " << m.done() << " objects" << endl; out.close(); } + void writeCollectionStdout( const string coll ) { + doCollection(coll, cout, NULL); + } + void go( const string db , const path outdir ) { cout << "DATABASE: " << db << "\t to \t" << outdir.string() << endl; @@ -79,10 +91,10 @@ public: const string name = obj.getField( "name" ).valuestr(); const string filename = name.substr( db.size() + 1 ); - if ( _coll.length() > 0 && db + "." + _coll != name && _coll != name ) + if ( _coll != "*" && db + "." + _coll != name && _coll != name ) continue; - doCollection( name.c_str() , outdir / ( filename + ".bson" ) ); + writeCollectionFile( name.c_str() , outdir / ( filename + ".bson" ) ); } @@ -96,7 +108,20 @@ public: _query = fromjson( q ); } - path root( getParam("out") ); + // check if we're outputting to stdout + string out = getParam("out"); + if ( out == "stdout" ) { + if ( _db != "*" && _coll != "*" ) { + writeCollectionStdout( _db+"."+_coll ); + return 0; + } + else { + cout << "You must specify database and collection to print to stdout" << endl; + return -1; + } + } + + path root( out ); string db = _db; if ( db == "*" ){ |