void BSONObjExternalSorter::finishMap(){ uassert( 10050 , "bad" , _cur ); _curSizeSoFar = 0; if ( _cur->size() == 0 ) return; _sortInMem(); stringstream ss; ss << _root.string() << "/file." << _files.size(); string file = ss.str(); ofstream out; out.open( file.c_str() , ios_base::out | ios_base::binary ); assertStreamGood( 10051 , (string)"couldn't open file: " + file , out ); int num = 0; for ( InMemory::iterator i=_cur->begin(); i != _cur->end(); ++i ){ Data p = *i; out.write( p.first.objdata() , p.first.objsize() ); out.write( (char*)(&p.second) , sizeof( DiskLoc ) ); num++; } _cur->clear(); _files.push_back( file ); out.close(); log(2) << "Added file: " << file << " with " << num << "objects for external sort" << endl; }
void BSONObjExternalSorter::finishMap() { uassert( 10050 , "bad" , _cur ); _curSizeSoFar = 0; if ( _cur->size() == 0 ) return; _sortInMem(); stringstream ss; ss << _root.string() << "/file." << _files.size(); string file = ss.str(); // todo: it may make sense to fadvise that this not be cached so that building the index doesn't // eject other things the db is using from the file system cache. while we will soon be reading // this back, if it fit in ram, there wouldn't have been a need for an external sort in the first // place. ofstream out; out.open( file.c_str() , ios_base::out | ios_base::binary ); assertStreamGood( 10051 , (string)"couldn't open file: " + file , out ); int num = 0; for ( InMemory::iterator i=_cur->begin(); i != _cur->end(); ++i ) { Data p = *i; out.write( p.first.objdata() , p.first.objsize() ); out.write( (char*)(&p.second) , sizeof( DiskLoc ) ); num++; } _cur->clear(); _files.push_back( file ); out.close(); log(2) << "Added file: " << file << " with " << num << "objects for external sort" << endl; }
void BSONObjExternalSorter::sort(){ uassert( 10048 , "already sorted" , ! _sorted ); _sorted = true; if ( _cur && _files.size() == 0 ){ _sortInMem(); log(1) << "\t\t not using file. size:" << _curSizeSoFar << " _compares:" << _compares << endl; return; } if ( _cur ){ finishMap(); } if ( _cur ){ delete _cur; _cur = 0; } if ( _files.size() == 0 ) return; }