2010-02-09 16:48:21 -05:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010 10gen Inc.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-01-27 15:16:09 -05:00
|
|
|
// Tool.h
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
2009-02-10 10:48:41 -05:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
#include <io.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-01-27 15:16:09 -05:00
|
|
|
#include "client/dbclient.h"
|
2009-05-20 11:39:12 -04:00
|
|
|
#include "db/instance.h"
|
2011-06-01 12:26:41 -04:00
|
|
|
#include "db/matcher.h"
|
2009-01-27 15:16:09 -05:00
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
|
|
namespace mongo {
|
2009-06-25 16:11:38 -04:00
|
|
|
|
2009-01-27 15:16:09 -05:00
|
|
|
class Tool {
|
|
|
|
|
public:
|
2010-11-02 10:49:43 -04:00
|
|
|
enum DBAccess {
|
2011-01-04 00:40:41 -05:00
|
|
|
NONE = 0 ,
|
|
|
|
|
REMOTE_SERVER = 1 << 1 ,
|
|
|
|
|
LOCAL_SERVER = 1 << 2 ,
|
2010-11-02 10:49:43 -04:00
|
|
|
SPECIFY_DBCOL = 1 << 3 ,
|
|
|
|
|
ALL = REMOTE_SERVER | LOCAL_SERVER | SPECIFY_DBCOL
|
2010-08-16 13:16:19 -04:00
|
|
|
};
|
|
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
Tool( string name , DBAccess access=ALL, string defaultDB="test" ,
|
2010-08-16 13:16:19 -04:00
|
|
|
string defaultCollection="", bool usesstdout=true);
|
2009-01-27 15:16:09 -05:00
|
|
|
virtual ~Tool();
|
|
|
|
|
|
|
|
|
|
int main( int argc , char ** argv );
|
2009-06-25 16:11:38 -04:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
boost::program_options::options_description_easy_init add_options() {
|
2009-01-27 15:16:09 -05:00
|
|
|
return _options->add_options();
|
|
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
boost::program_options::options_description_easy_init add_hidden_options() {
|
2009-09-01 16:27:08 -04:00
|
|
|
return _hidden_options->add_options();
|
|
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
void addPositionArg( const char * name , int pos ) {
|
2009-01-27 21:37:56 -05:00
|
|
|
_positonalOptions.add( name , pos );
|
|
|
|
|
}
|
2009-06-25 16:11:38 -04:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
string getParam( string name , string def="" ) {
|
2009-01-27 15:16:09 -05:00
|
|
|
if ( _params.count( name ) )
|
|
|
|
|
return _params[name.c_str()].as<string>();
|
|
|
|
|
return def;
|
|
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
int getParam( string name , int def ) {
|
2010-02-28 12:35:18 -05:00
|
|
|
if ( _params.count( name ) )
|
|
|
|
|
return _params[name.c_str()].as<int>();
|
|
|
|
|
return def;
|
|
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
bool hasParam( string name ) {
|
2009-01-29 13:01:45 -05:00
|
|
|
return _params.count( name );
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
string getNS() {
|
|
|
|
|
if ( _coll.size() == 0 ) {
|
2009-01-29 13:01:45 -05:00
|
|
|
cerr << "no collection specified!" << endl;
|
|
|
|
|
throw -1;
|
|
|
|
|
}
|
|
|
|
|
return _db + "." + _coll;
|
|
|
|
|
}
|
2010-10-08 11:23:08 -04:00
|
|
|
|
2011-10-28 16:27:59 -04:00
|
|
|
void useStandardOutput( bool mode ) {
|
|
|
|
|
_usesstdout = mode;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-08 11:23:08 -04:00
|
|
|
bool isMaster();
|
2011-08-17 17:49:03 -04:00
|
|
|
bool isMongos();
|
2011-10-28 16:27:59 -04:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
virtual void preSetup() {}
|
2009-01-27 15:16:09 -05:00
|
|
|
|
2009-01-29 11:56:44 -05:00
|
|
|
virtual int run() = 0;
|
2009-06-25 16:11:38 -04:00
|
|
|
|
2009-06-25 16:15:15 -04:00
|
|
|
virtual void printHelp(ostream &out);
|
|
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
virtual void printExtraHelp( ostream & out ) {}
|
|
|
|
|
virtual void printExtraHelpAfter( ostream & out ) {}
|
2009-09-01 15:53:31 -04:00
|
|
|
|
2011-03-17 15:34:19 -04:00
|
|
|
virtual void printVersion(ostream &out);
|
|
|
|
|
|
2009-01-27 15:16:09 -05:00
|
|
|
protected:
|
2009-08-12 16:31:22 -04:00
|
|
|
|
2009-08-12 16:55:18 -04:00
|
|
|
mongo::DBClientBase &conn( bool slaveIfPaired = false );
|
2009-08-12 16:31:22 -04:00
|
|
|
void auth( string db = "" );
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2009-01-27 15:16:09 -05:00
|
|
|
string _name;
|
2009-09-01 15:53:31 -04:00
|
|
|
|
2009-01-27 15:16:09 -05:00
|
|
|
string _db;
|
|
|
|
|
string _coll;
|
2010-12-09 15:15:24 -05:00
|
|
|
string _fileName;
|
2009-09-01 15:53:31 -04:00
|
|
|
|
2009-08-12 16:31:22 -04:00
|
|
|
string _username;
|
|
|
|
|
string _password;
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-04-15 12:20:21 -04:00
|
|
|
bool _usesstdout;
|
2010-04-16 01:15:09 -04:00
|
|
|
bool _noconnection;
|
2010-06-09 13:20:35 -04:00
|
|
|
bool _autoreconnect;
|
2009-01-27 15:16:09 -05:00
|
|
|
|
2009-10-12 15:05:42 -04:00
|
|
|
void addFieldOptions();
|
2009-10-09 14:55:29 -04:00
|
|
|
void needFields();
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2009-10-09 14:55:29 -04:00
|
|
|
vector<string> _fields;
|
|
|
|
|
BSONObj _fieldsObj;
|
2009-09-01 15:53:31 -04:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2009-08-11 13:35:59 -04:00
|
|
|
string _host;
|
2010-04-16 01:15:09 -04:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
2009-08-11 13:35:59 -04:00
|
|
|
mongo::DBClientBase * _conn;
|
2010-11-24 14:27:36 -05:00
|
|
|
mongo::DBClientBase * _slaveConn;
|
2009-08-12 16:55:18 -04:00
|
|
|
bool _paired;
|
2009-09-01 15:53:31 -04:00
|
|
|
|
2009-01-27 15:16:09 -05:00
|
|
|
boost::program_options::options_description * _options;
|
2009-09-01 16:27:08 -04:00
|
|
|
boost::program_options::options_description * _hidden_options;
|
2009-01-27 21:37:56 -05:00
|
|
|
boost::program_options::positional_options_description _positonalOptions;
|
|
|
|
|
|
2009-01-27 15:16:09 -05:00
|
|
|
boost::program_options::variables_map _params;
|
2009-06-25 16:11:38 -04:00
|
|
|
|
2009-01-27 15:16:09 -05:00
|
|
|
};
|
2009-06-25 16:11:38 -04:00
|
|
|
|
2010-06-09 11:31:30 -04:00
|
|
|
class BSONTool : public Tool {
|
|
|
|
|
bool _objcheck;
|
|
|
|
|
auto_ptr<Matcher> _matcher;
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-06-09 11:31:30 -04:00
|
|
|
public:
|
2010-08-16 13:16:19 -04:00
|
|
|
BSONTool( const char * name , DBAccess access=ALL, bool objcheck = false );
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-06-09 11:31:30 -04:00
|
|
|
virtual int doRun() = 0;
|
|
|
|
|
virtual void gotObject( const BSONObj& obj ) = 0;
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-06-09 11:31:30 -04:00
|
|
|
virtual int run();
|
|
|
|
|
|
|
|
|
|
long long processFile( const path& file );
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-06-09 11:31:30 -04:00
|
|
|
};
|
|
|
|
|
|
2009-01-27 15:16:09 -05:00
|
|
|
}
|