2009-02-25 00:57:31 -05:00
|
|
|
// cursors.cpp
|
2010-02-09 16:48:21 -05:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-02-25 00:57:31 -05:00
|
|
|
|
2010-04-27 15:27:52 -04:00
|
|
|
#include "pch.h"
|
2009-02-25 00:57:31 -05:00
|
|
|
#include "cursors.h"
|
|
|
|
|
#include "../client/connpool.h"
|
|
|
|
|
#include "../db/queryutil.h"
|
|
|
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
|
|
|
|
|
// -------- ShardedCursor -----------
|
|
|
|
|
|
2009-11-03 10:35:48 -05:00
|
|
|
ShardedClientCursor::ShardedClientCursor( QueryMessage& q , ClusteredCursor * cursor ){
|
|
|
|
|
assert( cursor );
|
|
|
|
|
_cursor = cursor;
|
|
|
|
|
|
2009-02-25 00:57:31 -05:00
|
|
|
_skip = q.ntoskip;
|
|
|
|
|
_ntoreturn = q.ntoreturn;
|
|
|
|
|
|
|
|
|
|
_totalSent = 0;
|
2009-02-25 01:23:58 -05:00
|
|
|
_done = false;
|
2009-02-25 00:57:31 -05:00
|
|
|
|
|
|
|
|
do {
|
2009-12-23 15:57:32 -05:00
|
|
|
// TODO: only create _id when needed
|
2009-02-25 00:57:31 -05:00
|
|
|
_id = security.getNonce();
|
|
|
|
|
} while ( _id == 0 );
|
2009-02-25 14:23:53 -05:00
|
|
|
|
2009-02-25 00:57:31 -05:00
|
|
|
}
|
|
|
|
|
|
2009-11-03 10:35:48 -05:00
|
|
|
ShardedClientCursor::~ShardedClientCursor(){
|
|
|
|
|
assert( _cursor );
|
|
|
|
|
delete _cursor;
|
|
|
|
|
_cursor = 0;
|
2009-02-25 00:57:31 -05:00
|
|
|
}
|
2009-03-25 17:35:38 -04:00
|
|
|
|
2009-11-03 10:35:48 -05:00
|
|
|
bool ShardedClientCursor::sendNextBatch( Request& r , int ntoreturn ){
|
2009-12-28 16:43:43 -05:00
|
|
|
uassert( 10191 , "cursor already done" , ! _done );
|
2009-02-25 14:23:53 -05:00
|
|
|
|
2009-02-25 00:57:31 -05:00
|
|
|
int maxSize = 1024 * 1024;
|
|
|
|
|
if ( _totalSent > 0 )
|
|
|
|
|
maxSize *= 3;
|
|
|
|
|
|
|
|
|
|
BufBuilder b(32768);
|
|
|
|
|
|
|
|
|
|
int num = 0;
|
|
|
|
|
bool sendMore = true;
|
2009-02-25 14:23:53 -05:00
|
|
|
|
2009-11-03 10:35:48 -05:00
|
|
|
while ( _cursor->more() ){
|
|
|
|
|
BSONObj o = _cursor->next();
|
2009-02-25 00:57:31 -05:00
|
|
|
|
|
|
|
|
b.append( (void*)o.objdata() , o.objsize() );
|
|
|
|
|
num++;
|
|
|
|
|
|
2010-01-15 01:27:01 -05:00
|
|
|
if ( b.len() > maxSize ){
|
2009-02-25 00:57:31 -05:00
|
|
|
break;
|
2010-01-15 01:27:01 -05:00
|
|
|
}
|
2009-02-25 00:57:31 -05:00
|
|
|
|
2009-02-25 01:23:58 -05:00
|
|
|
if ( num == ntoreturn ){
|
2009-02-25 00:57:31 -05:00
|
|
|
// soft limit aka batch size
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-21 10:10:30 -04:00
|
|
|
if ( ntoreturn != 0 && ( -1 * num + _totalSent ) == ntoreturn ){
|
2009-02-25 00:57:31 -05:00
|
|
|
// hard limit - total to send
|
|
|
|
|
sendMore = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-03 10:35:48 -05:00
|
|
|
bool hasMore = sendMore && _cursor->more();
|
2009-04-21 10:10:30 -04:00
|
|
|
log(6) << "\t hasMore:" << hasMore << " wouldSendMoreIfHad: " << sendMore << " id:" << _id << " totalSent: " << _totalSent << endl;
|
2009-02-25 14:23:53 -05:00
|
|
|
|
2009-04-21 20:12:41 -04:00
|
|
|
replyToQuery( 0 , r.p() , r.m() , b.buf() , b.len() , num , _totalSent , hasMore ? _id : 0 );
|
2009-02-25 00:57:31 -05:00
|
|
|
_totalSent += num;
|
2009-02-25 01:23:58 -05:00
|
|
|
_done = ! hasMore;
|
2009-02-25 14:23:53 -05:00
|
|
|
|
2009-02-25 00:57:31 -05:00
|
|
|
return hasMore;
|
|
|
|
|
}
|
2009-02-25 14:23:53 -05:00
|
|
|
|
2009-02-25 00:57:31 -05:00
|
|
|
|
2009-02-25 01:23:58 -05:00
|
|
|
CursorCache::CursorCache(){
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CursorCache::~CursorCache(){
|
|
|
|
|
// TODO: delete old cursors?
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-03 10:35:48 -05:00
|
|
|
ShardedClientCursor* CursorCache::get( long long id ){
|
|
|
|
|
map<long long,ShardedClientCursor*>::iterator i = _cursors.find( id );
|
2009-02-25 01:23:58 -05:00
|
|
|
if ( i == _cursors.end() ){
|
|
|
|
|
OCCASIONALLY log() << "Sharded CursorCache missing cursor id: " << id << endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return i->second;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-03 10:35:48 -05:00
|
|
|
void CursorCache::store( ShardedClientCursor * cursor ){
|
2009-02-25 01:23:58 -05:00
|
|
|
_cursors[cursor->getId()] = cursor;
|
|
|
|
|
}
|
|
|
|
|
void CursorCache::remove( long long id ){
|
|
|
|
|
_cursors.erase( id );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CursorCache cursorCache;
|
2009-02-25 00:57:31 -05:00
|
|
|
}
|