summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tools/Tool.cpp5
-rw-r--r--tools/Tool.h5
-rw-r--r--tools/import.cpp204
3 files changed, 89 insertions, 125 deletions
diff --git a/tools/Tool.cpp b/tools/Tool.cpp
index abcaf099142..a052f57d6d2 100644
--- a/tools/Tool.cpp
+++ b/tools/Tool.cpp
@@ -31,7 +31,10 @@ mongo::Tool::~Tool(){
int mongo::Tool::main( int argc , char ** argv ){
boost::filesystem::path::default_name_check( boost::filesystem::no_check );
- po::store( po::parse_command_line( argc, argv, *_options ), _params );
+ po::store( po::command_line_parser( argc , argv ).
+ options( *_options ).
+ positional( _positonalOptions ).run() , _params );
+
po::notify( _params );
if ( _params.count( "help" ) ){
diff --git a/tools/Tool.h b/tools/Tool.h
index ae8a6917807..a72ca6d4964 100644
--- a/tools/Tool.h
+++ b/tools/Tool.h
@@ -22,6 +22,9 @@ namespace mongo {
boost::program_options::options_description_easy_init add_options(){
return _options->add_options();
}
+ void addPositionArg( const char * name , int pos ){
+ _positonalOptions.add( name , pos );
+ }
string getParam( string name , string def="" ){
if ( _params.count( name ) )
@@ -40,6 +43,8 @@ namespace mongo {
private:
boost::program_options::options_description * _options;
+ boost::program_options::positional_options_description _positonalOptions;
+
boost::program_options::variables_map _params;
};
diff --git a/tools/import.cpp b/tools/import.cpp
index 3e11c101b2f..c036e7fa6c0 100644
--- a/tools/import.cpp
+++ b/tools/import.cpp
@@ -19,141 +19,97 @@
#include "../stdafx.h"
#include "../client/dbclient.h"
#include "../util/mmap.h"
+#include "Tool.h"
#include <boost/program_options.hpp>
#include <fcntl.h>
-namespace mongo {
-
- namespace po = boost::program_options;
-
- namespace import {
-
- void drillDown( DBClientConnection & conn , path root ) {
-
- if ( is_directory( root ) ) {
- directory_iterator end;
- directory_iterator i(root);
- while ( i != end ) {
- path p = *i;
- drillDown( conn , p );
- i++;
- }
- 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();
- if ( dir.find( "/" ) == string::npos )
- ns += dir;
- else
- ns += dir.substr( dir.find_last_of( "/" ) + 1 );
- }
-
- {
- string l = root.leaf();
- l = l.substr( 0 , l.find_last_of( "." ) );
- ns += "." + l;
- }
-
- out() << "\t going into namespace [" << ns << "]" << endl;
-
- MemoryMappedFile mmf;
- assert( mmf.map( root.string().c_str() ) );
-
- char * data = (char*)mmf.viewOfs();
- int read = 0;
-
- int num = 0;
-
- while ( read < mmf.length() ) {
- if ( ! *data ) {
- out() << "\t ** got unexpected end of file ** continuing..." << endl;
- break;
- }
-
- BSONObj o( data );
-
- conn.insert( ns.c_str() , o );
+using namespace mongo;
- read += o.objsize();
- data += o.objsize();
+namespace po = boost::program_options;
- if ( ! ( ++num % 1000 ) )
- out() << "read " << read << "/" << mmf.length() << " bytes so far. " << num << " objects" << endl;
+class Import : public Tool {
+public:
+ Import() : Tool( "import" ){
+ add_options()
+ ("dir",po::value<string>() , "directory to import from" )
+ ;
+ addPositionArg( "dir" , 1 );
+ }
+
+ void run(){
+ drillDown( getParam( "dir" ) );
+ }
+
+ void drillDown( path root ) {
+
+ if ( is_directory( root ) ) {
+ directory_iterator end;
+ directory_iterator i(root);
+ while ( i != end ) {
+ path p = *i;
+ drillDown( p );
+ i++;
}
-
- out() << "\t " << num << " objects" << endl;
-
+ return;
}
-
-
- void go( const char * dbHost , const char * dirRoot ) {
- DBClientConnection conn;
- string errmsg;
- if ( ! conn.connect( dbHost , errmsg ) ) {
- out() << "couldn't connect : " << errmsg << endl;
- throw -11;
+
+ 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();
+ if ( dir.find( "/" ) == string::npos )
+ ns += dir;
+ else
+ ns += dir.substr( dir.find_last_of( "/" ) + 1 );
+ }
+
+ {
+ string l = root.leaf();
+ l = l.substr( 0 , l.find_last_of( "." ) );
+ ns += "." + l;
+ }
+
+ out() << "\t going into namespace [" << ns << "]" << endl;
+
+ MemoryMappedFile mmf;
+ assert( mmf.map( root.string().c_str() ) );
+
+ char * data = (char*)mmf.viewOfs();
+ int read = 0;
+
+ int num = 0;
+
+ while ( read < mmf.length() ) {
+ if ( ! *data ) {
+ out() << "\t ** got unexpected end of file ** continuing..." << endl;
+ break;
}
-
- drillDown( conn , dirRoot );
+
+ BSONObj o( data );
+
+ _conn.insert( ns.c_str() , o );
+
+ read += o.objsize();
+ data += o.objsize();
+
+ if ( ! ( ++num % 1000 ) )
+ out() << "read " << read << "/" << mmf.length() << " bytes so far. " << num << " objects" << endl;
}
- } // namespace import
-
-} // namespace mongo
-
-using namespace mongo;
-
-int main( int argc , char ** argv ) {
-
- boost::filesystem::path::default_name_check( boost::filesystem::no_check );
-
- po::options_description options("import parameters");
- options.add_options()
- ("help", "produce help message")
- ("host,h", po::value<string>() , "mongo host to connect to")
- ("dir" , po::value<string>(), "directory to import from" )
- ;
-
- po::positional_options_description argsOptions;
- argsOptions.add( "dir" , 1 );
-
- po::variables_map vm;
-
- po::store( po::command_line_parser( argc , argv ).
- options(options).positional(argsOptions).run(), vm );
-
- po::notify(vm);
-
- if ( vm.count("help") ) {
- options.print( cerr );
- return 1;
+
+ out() << "\t " << num << " objects" << endl;
}
+};
- const char * host = "127.0.0.1";
- const char * dir = "dump";
-
- if ( vm.count( "host" ) )
- host = vm["host"].as<string>().c_str();
-
- if ( vm.count( "dir" ) )
- dir = vm["dir"].as<string>().c_str();
-
- out() << "mongo dump" << endl;
- out() << "\t host \t" << host << endl;
- out() << "\t dir \t" << dir << endl;
-
- import::go( host , dir );
- return 0;
+int main( int argc , char ** argv ) {
+ Import import;
+ return import.main( argc , argv );
}