From 265af2d1f9c438903d23569e250bd5f54d354212 Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Tue, 15 Sep 2009 13:48:26 -0400 Subject: [PATCH] restore can import 64-bit data files with 32-bit mongorestore SERVER-141 --- tools/restore.cpp | 66 ++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/tools/restore.cpp b/tools/restore.cpp index 5d2d795dd35..f49f72524c8 100644 --- a/tools/restore.cpp +++ b/tools/restore.cpp @@ -45,6 +45,7 @@ public: } void drillDown( path root ) { + log(2) << "drillDown: " << root.string() << endl; if ( is_directory( root ) ) { directory_iterator end; @@ -80,40 +81,51 @@ public: ns += "." + l; } - if ( boost::filesystem::file_size( root ) == 0 ) { + long long fileLength = file_size( root ); + + if ( fileLength == 0 ) { out() << "file " + root.native_file_string() + " empty, skipping" << endl; return; } out() << "\t going into namespace [" << ns << "]" << endl; - - MemoryMappedFile mmf; - long fileLength; - assert( mmf.map( root.string().c_str() , fileLength ) ); - - log(1) << "\t file size: " << fileLength << endl; - - char * data = (char*)mmf.viewOfs(); - long read = 0; - - long num = 0; - - int msgDelay = (int)(1000 * ( 1 + ( mmf.length() / ( 1024.0 * 1024 * 400 ) ) ) ); - log(1) << "\t msg delay: " << msgDelay << endl; - - while ( read < mmf.length() ) { - BSONObj o( data ); - - conn().insert( ns.c_str() , o ); - - read += o.objsize(); - data += o.objsize(); - - num++; - if ( logLevel > 0 && num < 10 || ! ( num % msgDelay ) ) - out() << "read " << read << "/" << mmf.length() << " bytes so far. (" << (int)( (read * 100) / mmf.length()) << "%) " << num << " objects" << endl; + + string fileString = root.string(); + ifstream file( fileString.c_str() , ios_base::in | ios_base::binary); + if ( ! file.is_open() ){ + log() << "error opening file: " << fileString << endl; + return; } + + log(1) << "\t file size: " << fileLength << endl; + + long long read = 0; + long long num = 0; + int msgDelay = (int)(1000 * ( 1 + ( fileLength / ( 1024.0 * 1024 * 400 ) ) ) ); + log(1) << "\t msg delay: " << msgDelay << endl; + + const int BUF_SIZE = 1024 * 1024 * 5; + char * buf = (char*)malloc( BUF_SIZE ); + + while ( read < fileLength ) { + file.read( buf , 4 ); + int size = ((int*)buf)[0]; + assert( size < BUF_SIZE ); + + file.read( buf + 4 , size - 4 ); + + BSONObj o( buf ); + conn().insert( ns.c_str() , o ); + + read += o.objsize(); + num++; + + if ( ( logLevel > 0 && num < 10 ) || ! ( num % msgDelay ) ) + out() << "read " << read << "/" << fileLength << " bytes so far. (" << (int)( (read * 100) / fileLength) << "%) " << num << " objects" << endl; + } + + free( buf ); out() << "\t " << num << " objects" << endl; } };