Files
mongo/tools/tool.h

161 lines
4.1 KiB
C
Raw Normal View History

/*
* 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"
#include "db/instance.h"
#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
};
2011-01-04 00:40:41 -05:00
Tool( string name , DBAccess access=ALL, string defaultDB="test" ,
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() {
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;
}
void useStandardOutput( bool mode ) {
_usesstdout = mode;
}
bool isMaster();
bool isMongos();
2011-01-04 00:40:41 -05:00
virtual void preSetup() {}
2009-01-27 15:16:09 -05:00
virtual int run() = 0;
2009-06-25 16:11:38 -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
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;
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
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();
void needFields();
2011-01-04 00:40:41 -05:00
vector<string> _fields;
BSONObj _fieldsObj;
2009-09-01 15:53:31 -04:00
2011-01-04 00:40:41 -05:00
string _host;
2010-04-16 01:15:09 -04:00
protected:
mongo::DBClientBase * _conn;
mongo::DBClientBase * _slaveConn;
bool _paired;
2009-09-01 15:53:31 -04:00
2009-01-27 15:16:09 -05:00
boost::program_options::options_description * _options;
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:
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
}