0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-29 00:32:18 +01:00
mongodb/tools/bsondump.cpp

140 lines
4.0 KiB
C++
Raw Normal View History

2010-06-09 17:31:30 +02:00
// restore.cpp
/**
* Copyright (C) 2008 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/>.
*/
#include "../pch.h"
#include "../client/dbclient.h"
#include "../util/mmap.h"
2010-06-19 18:46:10 +02:00
#include "../util/text.h"
2010-06-09 17:31:30 +02:00
#include "tool.h"
#include <boost/program_options.hpp>
#include <fcntl.h>
using namespace mongo;
namespace po = boost::program_options;
class BSONDump : public BSONTool {
enum OutputType { JSON , DEBUG } _type;
public:
2011-01-04 06:40:41 +01:00
BSONDump() : BSONTool( "bsondump", NONE ) {
2010-06-09 17:31:30 +02:00
add_options()
2011-01-04 06:40:41 +01:00
("type" , po::value<string>()->default_value("json") , "type of output: json,debug" )
;
2010-06-09 17:31:30 +02:00
add_hidden_options()
2011-01-04 06:40:41 +01:00
("file" , po::value<string>() , ".bson file" )
;
2010-06-09 17:31:30 +02:00
addPositionArg( "file" , 1 );
_noconnection = true;
}
2011-01-04 06:40:41 +01:00
2010-06-09 17:31:30 +02:00
virtual void printExtraHelp(ostream& out) {
out << "usage: " << _name << " [options] <bson filename>" << endl;
}
2011-01-04 06:40:41 +01:00
virtual int doRun() {
2010-06-09 17:31:30 +02:00
{
string t = getParam( "type" );
if ( t == "json" )
_type = JSON;
else if ( t == "debug" )
_type = DEBUG;
else {
cerr << "bad type: " << t << endl;
return 1;
}
}
2011-01-04 06:40:41 +01:00
2010-09-10 20:26:34 +02:00
path root = getParam( "file" );
if ( root == "" ) {
printExtraHelp(cout);
return 1;
}
2011-01-04 06:40:41 +01:00
2010-09-10 20:26:34 +02:00
processFile( root );
2010-06-09 17:31:30 +02:00
return 0;
}
2011-01-04 06:40:41 +01:00
bool debug( const BSONObj& o , int depth=0) {
2010-06-09 17:31:30 +02:00
string prefix = "";
2011-01-04 06:40:41 +01:00
for ( int i=0; i<depth; i++ ) {
2010-06-09 17:31:30 +02:00
prefix += "\t\t\t";
}
2011-01-04 06:40:41 +01:00
2010-06-09 17:31:30 +02:00
int read = 4;
try {
cout << prefix << "--- new object ---\n";
cout << prefix << "\t size : " << o.objsize() << "\n";
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();
cout << prefix << "\t\t " << e.fieldName() << "\n" << prefix << "\t\t\t type:" << setw(3) << e.type() << " size: " << e.size() << endl;
2011-01-04 06:40:41 +01:00
if ( ( read + e.size() ) > o.objsize() ) {
2010-06-09 17:31:30 +02:00
cout << prefix << " SIZE DOES NOT WORK" << endl;
return false;
}
read += e.size();
try {
e.validate();
2011-01-04 06:40:41 +01:00
if ( e.isABSONObj() ) {
2010-06-09 17:31:30 +02:00
if ( ! debug( e.Obj() , depth + 1 ) )
return false;
}
2011-01-04 06:40:41 +01:00
else if ( e.type() == String && ! isValidUTF8( e.valuestr() ) ) {
2010-06-19 18:46:10 +02:00
cout << prefix << "\t\t\t" << "bad utf8 String!" << endl;
}
2011-01-04 06:40:41 +01:00
else if ( logLevel > 0 ) {
2010-06-19 18:46:10 +02:00
cout << prefix << "\t\t\t" << e << endl;
}
2011-01-04 06:40:41 +01:00
2010-06-09 17:31:30 +02:00
}
2011-01-04 06:40:41 +01:00
catch ( std::exception& e ) {
2010-06-09 17:31:30 +02:00
cout << prefix << "\t\t\t bad value: " << e.what() << endl;
}
}
}
2011-01-04 06:40:41 +01:00
catch ( std::exception& e ) {
2010-06-09 17:31:30 +02:00
cout << prefix << "\t" << e.what() << endl;
}
return true;
}
2011-01-04 06:40:41 +01:00
virtual void gotObject( const BSONObj& o ) {
switch ( _type ) {
2010-06-09 17:31:30 +02:00
case JSON:
2010-08-14 04:30:57 +02:00
cout << o.jsonString( TenGen ) << endl;
2010-06-09 17:31:30 +02:00
break;
case DEBUG:
debug(o);
break;
default:
cerr << "bad type? : " << _type << endl;
}
}
};
int main( int argc , char ** argv ) {
BSONDump dump;
return dump.main( argc , argv );
}