diff --git a/client/parallel.cpp b/client/parallel.cpp index 42a8e92885a..7abfca70411 100644 --- a/client/parallel.cpp +++ b/client/parallel.cpp @@ -140,10 +140,16 @@ namespace mongo { _advance(); return ret; } + + BSONObj FilteringClientCursor::peek(){ + if ( _next.isEmpty() ) + _advance(); + return _next; + } void FilteringClientCursor::_advance(){ assert( _next.isEmpty() ); - if ( ! _cursor.get() ) + if ( ! _cursor.get() || _done ) return; while ( _cursor->more() ){ @@ -215,7 +221,6 @@ namespace mongo { void ParallelSortClusteredCursor::_init(){ _numServers = _servers.size(); _cursors = new FilteringClientCursor[_numServers]; - _nexts = new BSONObj[_numServers]; // TODO: parellize int num = 0; @@ -228,14 +233,10 @@ namespace mongo { ParallelSortClusteredCursor::~ParallelSortClusteredCursor(){ delete [] _cursors; - delete [] _nexts; } bool ParallelSortClusteredCursor::more(){ for ( int i=0; i<_numServers; i++ ){ - if ( ! _nexts[i].isEmpty() ) - return true; - if ( _cursors[i].more() ) return true; } @@ -243,53 +244,35 @@ namespace mongo { } BSONObj ParallelSortClusteredCursor::next(){ - advance(); - BSONObj best = BSONObj(); int bestFrom = -1; for ( int i=0; i<_numServers; i++){ - if ( _nexts[i].isEmpty() ) + if ( ! _cursors[i].more() ) continue; + + BSONObj me = _cursors[i].peek(); if ( best.isEmpty() ){ - best = _nexts[i]; + best = me; bestFrom = i; continue; } - int comp = best.woSortOrder( _nexts[i] , _sortKey ); + int comp = best.woSortOrder( me , _sortKey ); if ( comp < 0 ) continue; - best = _nexts[i]; + best = me; bestFrom = i; } - + uassert( 10019 , "no more elements" , ! best.isEmpty() ); - _nexts[bestFrom] = BSONObj(); + _cursors[bestFrom].next(); return best; } - void ParallelSortClusteredCursor::advance(){ - for ( int i=0; i<_numServers; i++ ){ - - if ( ! _nexts[i].isEmpty() ){ - // already have a good object there - continue; - } - - if ( ! _cursors[i].more() ){ - // cursor is dead, oh well - continue; - } - - _nexts[i] = _cursors[i].next(); - } - - } - // ----------------- // ---- Future ----- // ----------------- diff --git a/client/parallel.h b/client/parallel.h index dda36eceb8f..a1ef0994207 100644 --- a/client/parallel.h +++ b/client/parallel.h @@ -102,7 +102,8 @@ namespace mongo { bool more(); BSONObj next(); - + + BSONObj peek(); private: void _advance(); @@ -147,14 +148,11 @@ namespace mongo { private: void _init(); - void advance(); - int _numServers; set _servers; BSONObj _sortKey; FilteringClientCursor * _cursors; - BSONObj * _nexts; }; /**