2010-02-09 22:48:21 +01: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 21:16:09 +01:00
|
|
|
// Tool.cpp
|
|
|
|
|
2009-09-24 17:02:00 +02:00
|
|
|
#include "tool.h"
|
2009-01-27 21:16:09 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
2009-10-09 20:55:29 +02:00
|
|
|
#include <pcrecpp.h>
|
2009-01-27 21:16:09 +01:00
|
|
|
|
2009-05-20 18:15:26 +02:00
|
|
|
#include "util/file_allocator.h"
|
2010-05-04 16:47:20 +02:00
|
|
|
#include "util/password.h"
|
2009-05-20 18:15:26 +02:00
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
using namespace std;
|
|
|
|
using namespace mongo;
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2010-03-31 17:57:27 +02:00
|
|
|
CmdLine cmdLine;
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
Tool::Tool( string name , DBAccess access , string defaultDB ,
|
2010-04-15 18:20:21 +02:00
|
|
|
string defaultCollection , bool usesstdout ) :
|
2011-01-04 06:40:41 +01:00
|
|
|
_name( name ) , _db( defaultDB ) , _coll( defaultCollection ) ,
|
2010-11-24 20:27:36 +01:00
|
|
|
_usesstdout(usesstdout), _noconnection(false), _autoreconnect(false), _conn(0), _slaveConn(0), _paired(false) {
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
_options = new po::options_description( "options" );
|
2010-02-21 16:42:38 +01:00
|
|
|
_options->add_options()
|
2011-01-04 06:40:41 +01:00
|
|
|
("help","produce help message")
|
|
|
|
("verbose,v", "be more verbose (include multiple times for more verbosity e.g. -vvvvv)")
|
|
|
|
;
|
2010-08-16 19:16:19 +02:00
|
|
|
|
2010-11-02 15:49:43 +01:00
|
|
|
if ( access & REMOTE_SERVER )
|
2010-08-16 19:16:19 +02:00
|
|
|
_options->add_options()
|
2011-01-04 06:40:41 +01:00
|
|
|
("host,h",po::value<string>(), "mongo host to connect to (\"left,right\" for pairs)" )
|
|
|
|
("port",po::value<string>(), "server port. Can also use --host hostname:port" )
|
|
|
|
("ipv6", "enable IPv6 support (disabled by default)")
|
|
|
|
|
|
|
|
("username,u",po::value<string>(), "username" )
|
|
|
|
("password,p", new PasswordValue( &_password ), "password" )
|
|
|
|
;
|
|
|
|
|
2010-11-02 15:49:43 +01:00
|
|
|
if ( access & LOCAL_SERVER )
|
2010-02-28 18:36:40 +01:00
|
|
|
_options->add_options()
|
2011-01-04 06:40:41 +01:00
|
|
|
("dbpath",po::value<string>(), "directly access mongod database "
|
|
|
|
"files in the given path, instead of connecting to a mongod "
|
|
|
|
"server - needs to lock the data directory, so cannot be "
|
|
|
|
"used if a mongod is currently accessing the same path" )
|
|
|
|
("directoryperdb", "if dbpath specified, each db is in a separate directory" )
|
|
|
|
;
|
|
|
|
|
2010-11-02 15:49:43 +01:00
|
|
|
if ( access & SPECIFY_DBCOL )
|
|
|
|
_options->add_options()
|
2011-01-04 06:40:41 +01:00
|
|
|
("db,d",po::value<string>(), "database to use" )
|
|
|
|
("collection,c",po::value<string>(), "collection to use (some commands)" )
|
|
|
|
;
|
2010-02-28 18:36:40 +01:00
|
|
|
|
|
|
|
_hidden_options = new po::options_description( name + " hidden options" );
|
|
|
|
|
|
|
|
/* support for -vv -vvvv etc. */
|
|
|
|
for (string s = "vv"; s.length() <= 10; s.append("v")) {
|
|
|
|
_hidden_options->add_options()(s.c_str(), "verbose");
|
|
|
|
}
|
2009-09-01 22:52:50 +02:00
|
|
|
}
|
2009-01-27 21:16:09 +01:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
Tool::~Tool() {
|
2010-02-28 18:36:40 +01:00
|
|
|
delete( _options );
|
|
|
|
delete( _hidden_options );
|
|
|
|
if ( _conn )
|
|
|
|
delete _conn;
|
|
|
|
}
|
2009-01-27 21:16:09 +01:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
void Tool::printHelp(ostream &out) {
|
|
|
|
printExtraHelp(out);
|
|
|
|
_options->print(out);
|
2010-07-12 19:17:34 +02:00
|
|
|
printExtraHelpAfter(out);
|
2010-02-28 18:36:40 +01:00
|
|
|
}
|
2009-06-25 22:15:15 +02:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
int Tool::main( int argc , char ** argv ) {
|
2010-03-15 18:02:12 +01:00
|
|
|
static StaticObserver staticObserver;
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
cmdLine.prealloc = false;
|
2010-01-25 21:11:21 +01:00
|
|
|
|
2011-01-12 20:32:51 +01:00
|
|
|
// The default value may vary depending on compile options, but for tools
|
|
|
|
// we want durability to be disabled.
|
|
|
|
cmdLine.dur = false;
|
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
boost::filesystem::path::default_name_check( boost::filesystem::no_check );
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
_name = argv[0];
|
2009-09-01 22:27:08 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
/* using the same style as db.cpp */
|
|
|
|
int command_line_style = (((po::command_line_style::unix_style ^
|
|
|
|
po::command_line_style::allow_guessing) |
|
|
|
|
po::command_line_style::allow_long_disguise) ^
|
|
|
|
po::command_line_style::allow_sticky);
|
|
|
|
try {
|
|
|
|
po::options_description all_options("all options");
|
|
|
|
all_options.add(*_options).add(*_hidden_options);
|
2009-09-01 22:27:08 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
po::store( po::command_line_parser( argc , argv ).
|
|
|
|
options(all_options).
|
|
|
|
positional( _positonalOptions ).
|
|
|
|
style(command_line_style).run() , _params );
|
2009-09-01 21:51:17 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
po::notify( _params );
|
2011-01-04 06:40:41 +01:00
|
|
|
}
|
|
|
|
catch (po::error &e) {
|
2010-02-28 18:36:40 +01:00
|
|
|
cerr << "ERROR: " << e.what() << endl << endl;
|
|
|
|
printHelp(cerr);
|
|
|
|
return EXIT_BADOPTIONS;
|
|
|
|
}
|
2009-01-27 21:16:09 +01:00
|
|
|
|
2010-05-24 22:55:26 +02:00
|
|
|
// hide password from ps output
|
2011-01-04 06:40:41 +01:00
|
|
|
for (int i=0; i < (argc-1); ++i) {
|
|
|
|
if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--password")) {
|
2010-05-24 22:55:26 +02:00
|
|
|
char* arg = argv[i+1];
|
2011-01-04 06:40:41 +01:00
|
|
|
while (*arg) {
|
2010-05-24 22:55:26 +02:00
|
|
|
*arg++ = 'x';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
if ( _params.count( "help" ) ) {
|
2010-04-15 18:20:21 +02:00
|
|
|
printHelp(cout);
|
2010-02-28 18:36:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2009-05-20 17:39:12 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
if ( _params.count( "verbose" ) ) {
|
|
|
|
logLevel = 1;
|
|
|
|
}
|
2009-09-01 22:52:50 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
for (string s = "vv"; s.length() <= 10; s.append("v")) {
|
|
|
|
if (_params.count(s)) {
|
|
|
|
logLevel = s.length();
|
|
|
|
}
|
2009-09-01 22:52:50 +02:00
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-04-16 07:15:09 +02:00
|
|
|
preSetup();
|
2009-07-13 20:09:43 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
bool useDirectClient = hasParam( "dbpath" );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
if ( ! useDirectClient ) {
|
|
|
|
_host = "127.0.0.1";
|
|
|
|
if ( _params.count( "host" ) )
|
|
|
|
_host = _params["host"].as<string>();
|
2010-06-03 22:58:14 +02:00
|
|
|
|
|
|
|
if ( _params.count( "port" ) )
|
|
|
|
_host += ':' + _params["port"].as<string>();
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
|
|
if ( _noconnection ) {
|
2010-04-16 07:15:09 +02:00
|
|
|
// do nothing
|
|
|
|
}
|
2010-07-30 17:40:29 +02:00
|
|
|
else {
|
2010-02-28 18:36:40 +01:00
|
|
|
string errmsg;
|
2010-07-30 17:40:29 +02:00
|
|
|
|
|
|
|
ConnectionString cs = ConnectionString::parse( _host , errmsg );
|
2011-01-04 06:40:41 +01:00
|
|
|
if ( ! cs.isValid() ) {
|
2010-07-30 17:40:29 +02:00
|
|
|
cerr << "invalid hostname [" << _host << "] " << errmsg << endl;
|
2010-02-28 18:36:40 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-07-30 17:40:29 +02:00
|
|
|
_conn = cs.connect( errmsg );
|
2011-01-04 06:40:41 +01:00
|
|
|
if ( ! _conn ) {
|
2010-07-30 17:40:29 +02:00
|
|
|
cerr << "couldn't connect to [" << _host << "] " << errmsg << endl;
|
2010-02-28 18:36:40 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-10-14 22:04:14 +02:00
|
|
|
|
|
|
|
(_usesstdout ? cout : cerr ) << "connected to: " << _host << endl;
|
2009-08-11 19:35:59 +02:00
|
|
|
}
|
2010-10-14 22:04:14 +02:00
|
|
|
|
2009-08-11 19:35:59 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-02-28 18:36:40 +01:00
|
|
|
if ( _params.count( "directoryperdb" ) ) {
|
|
|
|
directoryperdb = true;
|
2009-08-11 19:35:59 +02:00
|
|
|
}
|
2010-03-28 23:15:27 +02:00
|
|
|
assert( lastError.get( true ) );
|
2010-02-28 18:36:40 +01:00
|
|
|
Client::initThread("tools");
|
|
|
|
_conn = new DBDirectClient();
|
|
|
|
_host = "DIRECT";
|
|
|
|
static string myDbpath = getParam( "dbpath" );
|
|
|
|
dbpath = myDbpath.c_str();
|
2010-03-11 17:55:10 +01:00
|
|
|
try {
|
|
|
|
acquirePathLock();
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
catch ( DBException& ) {
|
2010-03-11 17:55:10 +01:00
|
|
|
cerr << endl << "If you are running a mongod on the same "
|
2011-01-04 06:40:41 +01:00
|
|
|
"path you should connect to that instead of direct data "
|
|
|
|
"file access" << endl << endl;
|
2010-03-11 17:55:10 +01:00
|
|
|
dbexit( EXIT_CLEAN );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-01-09 07:45:11 +01:00
|
|
|
FileAllocator::get()->start();
|
2009-05-20 17:39:12 +02:00
|
|
|
}
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
if ( _params.count( "db" ) )
|
|
|
|
_db = _params["db"].as<string>();
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
if ( _params.count( "collection" ) )
|
|
|
|
_coll = _params["collection"].as<string>();
|
2009-09-01 21:43:23 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
if ( _params.count( "username" ) )
|
|
|
|
_username = _params["username"].as<string>();
|
2009-08-12 22:31:22 +02:00
|
|
|
|
2010-05-04 16:47:20 +02:00
|
|
|
if ( _params.count( "password" )
|
2011-01-04 06:40:41 +01:00
|
|
|
&& ( _password.empty() ) ) {
|
2010-05-04 16:47:20 +02:00
|
|
|
_password = askPassword();
|
|
|
|
}
|
2009-09-01 21:43:23 +02:00
|
|
|
|
2010-04-08 21:14:38 +02:00
|
|
|
if (_params.count("ipv6"))
|
|
|
|
enableIPv6();
|
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
int ret = -1;
|
|
|
|
try {
|
|
|
|
ret = run();
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
catch ( DBException& e ) {
|
2010-02-28 18:36:40 +01:00
|
|
|
cerr << "assertion: " << e.toString() << endl;
|
|
|
|
ret = -1;
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
if ( currentClient.get() )
|
|
|
|
currentClient->shutdown();
|
2010-02-02 03:35:54 +01:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
if ( useDirectClient )
|
|
|
|
dbexit( EXIT_CLEAN );
|
|
|
|
return ret;
|
|
|
|
}
|
2009-08-12 22:31:22 +02:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
DBClientBase& Tool::conn( bool slaveIfPaired ) {
|
|
|
|
if ( slaveIfPaired && _conn->type() == ConnectionString::SET ) {
|
2010-11-24 20:27:36 +01:00
|
|
|
if (!_slaveConn)
|
|
|
|
_slaveConn = &((DBClientReplicaSet*)_conn)->slaveConn();
|
|
|
|
return *_slaveConn;
|
|
|
|
}
|
2010-02-28 18:36:40 +01:00
|
|
|
return *_conn;
|
|
|
|
}
|
2009-08-12 22:55:18 +02:00
|
|
|
|
2010-10-08 17:23:08 +02:00
|
|
|
bool Tool::isMaster() {
|
|
|
|
if ( hasParam("dbpath") ) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-10-08 17:23:08 +02:00
|
|
|
BSONObj info;
|
|
|
|
bool isMaster;
|
|
|
|
bool ok = conn().isMaster(isMaster, &info);
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-10-08 17:23:08 +02:00
|
|
|
if (ok && !isMaster) {
|
|
|
|
cerr << "ERROR: trying to write to non-master " << conn().toString() << endl;
|
|
|
|
cerr << "isMaster info: " << info << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-10-08 17:23:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
void Tool::addFieldOptions() {
|
2010-02-28 18:36:40 +01:00
|
|
|
add_options()
|
2011-01-04 06:40:41 +01:00
|
|
|
("fields,f" , po::value<string>() , "comma separated list of field names e.g. -f name,age" )
|
|
|
|
("fieldFile" , po::value<string>() , "file with fields names - 1 per line" )
|
|
|
|
;
|
2010-02-28 18:36:40 +01:00
|
|
|
}
|
2009-10-12 21:05:42 +02:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
void Tool::needFields() {
|
2009-10-12 21:05:42 +02:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
if ( hasParam( "fields" ) ) {
|
2010-02-28 18:36:40 +01:00
|
|
|
BSONObjBuilder b;
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
string fields_arg = getParam("fields");
|
|
|
|
pcrecpp::StringPiece input(fields_arg);
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
string f;
|
2010-06-04 02:54:08 +02:00
|
|
|
pcrecpp::RE re("([#\\w\\.\\s\\-]+),?" );
|
2011-01-04 06:40:41 +01:00
|
|
|
while ( re.Consume( &input, &f ) ) {
|
2010-02-28 18:36:40 +01:00
|
|
|
_fields.push_back( f );
|
2010-07-20 18:58:23 +02:00
|
|
|
b.append( f , 1 );
|
2010-02-28 18:36:40 +01:00
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
_fieldsObj = b.obj();
|
|
|
|
return;
|
|
|
|
}
|
2009-10-12 21:05:42 +02:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
if ( hasParam( "fieldFile" ) ) {
|
2010-02-28 18:36:40 +01:00
|
|
|
string fn = getParam( "fieldFile" );
|
|
|
|
if ( ! exists( fn ) )
|
|
|
|
throw UserException( 9999 , ((string)"file: " + fn ) + " doesn't exist" );
|
|
|
|
|
|
|
|
const int BUF_SIZE = 1024;
|
|
|
|
char line[ 1024 + 128];
|
|
|
|
ifstream file( fn.c_str() );
|
|
|
|
|
|
|
|
BSONObjBuilder b;
|
2011-01-04 06:40:41 +01:00
|
|
|
while ( file.rdstate() == ios_base::goodbit ) {
|
2010-02-28 18:36:40 +01:00
|
|
|
file.getline( line , BUF_SIZE );
|
|
|
|
const char * cur = line;
|
|
|
|
while ( isspace( cur[0] ) ) cur++;
|
2010-06-30 00:08:07 +02:00
|
|
|
if ( cur[0] == '\0' )
|
2010-02-28 18:36:40 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
_fields.push_back( cur );
|
|
|
|
b.append( cur , 1 );
|
|
|
|
}
|
|
|
|
_fieldsObj = b.obj();
|
|
|
|
return;
|
2009-10-12 21:05:42 +02:00
|
|
|
}
|
2010-02-28 18:36:40 +01:00
|
|
|
|
|
|
|
throw UserException( 9998 , "you need to specify fields" );
|
2009-10-12 21:05:42 +02:00
|
|
|
}
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
void Tool::auth( string dbname ) {
|
2010-02-28 18:36:40 +01:00
|
|
|
if ( ! dbname.size() )
|
|
|
|
dbname = _db;
|
2009-10-09 20:55:29 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
if ( ! ( _username.size() || _password.size() ) )
|
|
|
|
return;
|
2009-08-12 22:31:22 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
string errmsg;
|
|
|
|
if ( _conn->auth( dbname , _username , _password , errmsg ) )
|
|
|
|
return;
|
2009-08-12 22:31:22 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
// try against the admin db
|
|
|
|
string err2;
|
|
|
|
if ( _conn->auth( "admin" , _username , _password , errmsg ) )
|
|
|
|
return;
|
2009-09-01 21:43:23 +02:00
|
|
|
|
2010-02-28 18:36:40 +01:00
|
|
|
throw UserException( 9997 , (string)"auth failed: " + errmsg );
|
|
|
|
}
|
2009-09-01 21:43:23 +02:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
BSONTool::BSONTool( const char * name, DBAccess access , bool objcheck )
|
|
|
|
: Tool( name , access , "" , "" ) , _objcheck( objcheck ) {
|
|
|
|
|
2010-06-09 17:31:30 +02:00
|
|
|
add_options()
|
2011-01-04 06:40:41 +01:00
|
|
|
("objcheck" , "validate object before inserting" )
|
|
|
|
("filter" , po::value<string>() , "filter to apply before inserting" )
|
|
|
|
;
|
2010-06-09 17:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
int BSONTool::run() {
|
2010-06-09 17:31:30 +02:00
|
|
|
_objcheck = hasParam( "objcheck" );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-06-09 17:31:30 +02:00
|
|
|
if ( hasParam( "filter" ) )
|
|
|
|
_matcher.reset( new Matcher( fromjson( getParam( "filter" ) ) ) );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-06-09 17:31:30 +02:00
|
|
|
return doRun();
|
|
|
|
}
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
long long BSONTool::processFile( const path& root ) {
|
2010-12-09 21:15:24 +01:00
|
|
|
_fileName = root.string();
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-11-28 06:07:55 +01:00
|
|
|
unsigned long long fileLength = file_size( root );
|
2010-06-09 17:31:30 +02:00
|
|
|
|
|
|
|
if ( fileLength == 0 ) {
|
2010-12-09 21:15:24 +01:00
|
|
|
out() << "file " << _fileName << " empty, skipping" << endl;
|
2010-06-09 17:31:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-09 21:15:24 +01:00
|
|
|
FILE* file = fopen( _fileName.c_str() , "rb" );
|
2011-01-04 06:40:41 +01:00
|
|
|
if ( ! file ) {
|
2010-12-09 21:15:24 +01:00
|
|
|
log() << "error opening file: " << _fileName << endl;
|
2010-06-09 17:31:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-08 02:01:34 +02:00
|
|
|
#if !defined(__sunos__) && defined(POSIX_FADV_SEQUENTIAL)
|
|
|
|
posix_fadvise(fileno(file), 0, fileLength, POSIX_FADV_SEQUENTIAL);
|
|
|
|
#endif
|
|
|
|
|
2010-06-09 17:31:30 +02:00
|
|
|
log(1) << "\t file size: " << fileLength << endl;
|
|
|
|
|
2010-11-28 06:07:55 +01:00
|
|
|
unsigned long long read = 0;
|
|
|
|
unsigned long long num = 0;
|
|
|
|
unsigned long long processed = 0;
|
2010-06-09 17:31:30 +02:00
|
|
|
|
|
|
|
const int BUF_SIZE = 1024 * 1024 * 5;
|
|
|
|
boost::scoped_array<char> buf_holder(new char[BUF_SIZE]);
|
|
|
|
char * buf = buf_holder.get();
|
|
|
|
|
|
|
|
ProgressMeter m( fileLength );
|
|
|
|
|
|
|
|
while ( read < fileLength ) {
|
2010-07-19 15:40:15 +02:00
|
|
|
int readlen = fread(buf, 4, 1, file);
|
2010-06-09 17:31:30 +02:00
|
|
|
int size = ((int*)buf)[0];
|
2011-01-04 06:40:41 +01:00
|
|
|
if ( size >= BUF_SIZE ) {
|
2010-06-09 17:31:30 +02:00
|
|
|
cerr << "got an object of size: " << size << " terminating..." << endl;
|
|
|
|
}
|
|
|
|
uassert( 10264 , "invalid object size" , size < BUF_SIZE );
|
|
|
|
|
2010-07-19 15:40:15 +02:00
|
|
|
readlen = fread(buf+4, size-4, 1, file);
|
2010-06-09 17:31:30 +02:00
|
|
|
|
|
|
|
BSONObj o( buf );
|
2011-01-04 06:40:41 +01:00
|
|
|
if ( _objcheck && ! o.valid() ) {
|
2010-06-09 17:31:30 +02:00
|
|
|
cerr << "INVALID OBJECT - going try and pring out " << endl;
|
|
|
|
cerr << "size: " << size << endl;
|
|
|
|
BSONObjIterator i(o);
|
2011-01-04 06:40:41 +01:00
|
|
|
while ( i.more() ) {
|
2010-06-09 17:31:30 +02:00
|
|
|
BSONElement e = i.next();
|
|
|
|
try {
|
|
|
|
e.validate();
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
catch ( ... ) {
|
2010-06-09 17:31:30 +02:00
|
|
|
cerr << "\t\t NEXT ONE IS INVALID" << endl;
|
|
|
|
}
|
|
|
|
cerr << "\t name : " << e.fieldName() << " " << e.type() << endl;
|
|
|
|
cerr << "\t " << e << endl;
|
|
|
|
}
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
|
|
if ( _matcher.get() == 0 || _matcher->matches( o ) ) {
|
2010-06-09 17:31:30 +02:00
|
|
|
gotObject( o );
|
|
|
|
processed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
read += o.objsize();
|
|
|
|
num++;
|
|
|
|
|
|
|
|
m.hit( o.objsize() );
|
|
|
|
}
|
|
|
|
|
2011-01-16 07:55:36 +01:00
|
|
|
fclose( file );
|
|
|
|
|
2010-06-09 17:31:30 +02:00
|
|
|
uassert( 10265 , "counts don't match" , m.done() == fileLength );
|
|
|
|
out() << "\t " << m.hits() << " objects found" << endl;
|
|
|
|
if ( _matcher.get() )
|
|
|
|
out() << "\t " << processed << " objects processed" << endl;
|
|
|
|
return processed;
|
|
|
|
}
|
|
|
|
|
2010-03-18 21:38:27 +01:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
|
|
void setupSignals( bool inFork ) {}
|
2009-08-12 22:31:22 +02:00
|
|
|
}
|