From c2c136a3f2a28ff471463cb896ea39fb0a64e599 Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Fri, 25 Sep 2009 11:22:43 -0400 Subject: [PATCH] don't use any files for external sort if its in 1 chunk --- db/extsort.cpp | 24 +++++++++++++++++++++++- db/extsort.h | 8 ++++++++ dbtests/jsobjtests.cpp | 7 +++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/db/extsort.cpp b/db/extsort.cpp index d5951055549..45fd95b006a 100644 --- a/db/extsort.cpp +++ b/db/extsort.cpp @@ -56,6 +56,10 @@ namespace mongo { _sorted = true; + if ( _map && _files.size() == 0 ){ + return; + } + if ( _map ){ finishMap(); } @@ -120,11 +124,20 @@ namespace mongo { // --------------------------------- - BSONObjExternalSorter::Iterator::Iterator( BSONObjExternalSorter * sorter ) : _cmp( sorter->_order ){ + BSONObjExternalSorter::Iterator::Iterator( BSONObjExternalSorter * sorter ) : + _cmp( sorter->_order ) , _in( 0 ){ + for ( list::iterator i=sorter->_files.begin(); i!=sorter->_files.end(); i++ ){ _files.push_back( new FileIterator( *i ) ); _stash.push_back( pair( Data( BSONObj() , DiskLoc() ) , false ) ); } + + if ( _files.size() == 0 && sorter->_map ){ + _in = sorter->_map; + _it = sorter->_map->begin(); + } + + } BSONObjExternalSorter::Iterator::~Iterator(){ @@ -134,6 +147,10 @@ namespace mongo { } bool BSONObjExternalSorter::Iterator::more(){ + + if ( _in ) + return _it != _in->end(); + for ( vector::iterator i=_files.begin(); i!=_files.end(); i++ ) if ( (*i)->more() ) return true; @@ -144,6 +161,11 @@ namespace mongo { } pair BSONObjExternalSorter::Iterator::next(){ + + if ( _in ){ + return *(_it++); + } + Data best; int slot = -1; diff --git a/db/extsort.h b/db/extsort.h index 244fedc1e61..9c7e8fe8bca 100644 --- a/db/extsort.h +++ b/db/extsort.h @@ -77,6 +77,10 @@ namespace mongo { MyCmp _cmp; vector _files; vector< pair > _stash; + + InMemory * _in; + InMemory::iterator _it; + }; BSONObjExternalSorter( const BSONObj & order = BSONObj() , long maxFileSize = 1024 * 1024 * 100 ); @@ -95,6 +99,10 @@ namespace mongo { return auto_ptr( new Iterator( this ) ); } + int numFiles(){ + return _files.size(); + } + private: void sort( string file ); diff --git a/dbtests/jsobjtests.cpp b/dbtests/jsobjtests.cpp index d1c88b09394..2555f1d1525 100644 --- a/dbtests/jsobjtests.cpp +++ b/dbtests/jsobjtests.cpp @@ -826,7 +826,9 @@ namespace JsobjTests { ASSERT( 0 ); num++; } - + + + ASSERT_EQUALS( 0 , sorter.numFiles() ); } }; @@ -944,7 +946,7 @@ namespace JsobjTests { } sorter.sort(); - + auto_ptr i = sorter.iterator(); int num=0; double prev = 0; @@ -956,6 +958,7 @@ namespace JsobjTests { prev = cur; } assert( num == total ); + ASSERT( sorter.numFiles() > 2 ); } };