0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00
mongodb/s/cursors.h

118 lines
3.1 KiB
C
Raw Normal View History

2009-02-25 06:57:31 +01:00
// cursors.h
#pragma once
2009-02-25 21:56:15 +01:00
#include "../stdafx.h"
2009-02-25 06:57:31 +01:00
#include "../db/jsobj.h"
#include "../db/dbmessage.h"
#include "../client/dbclient.h"
#include "request.h"
namespace mongo {
class ShardedCursor {
public:
ShardedCursor( QueryMessage& q );
virtual ~ShardedCursor();
virtual bool more() = 0;
virtual BSONObj next() = 0;
2009-02-25 07:23:58 +01:00
long long getId(){ return _id; }
2009-02-25 06:57:31 +01:00
/**
* @return whether there is more data left
*/
2009-02-25 07:23:58 +01:00
bool sendNextBatch( Request& r ){ return sendNextBatch( r , _ntoreturn ); }
bool sendNextBatch( Request& r , int ntoreturn );
2009-02-25 06:57:31 +01:00
protected:
2009-03-19 21:23:04 +01:00
auto_ptr<DBClientCursor> query( const string& server , int num = 0 , BSONObj extraFilter = BSONObj() );
2009-02-25 06:57:31 +01:00
BSONObj concatQuery( const BSONObj& query , const BSONObj& extraFilter );
BSONObj _concatFilter( const BSONObj& filter , const BSONObj& extraFilter );
string _ns;
int _options;
int _skip;
int _ntoreturn;
BSONObj _query;
BSONObj _fields;
long long _id;
int _totalSent;
2009-02-25 07:23:58 +01:00
bool _done;
2009-02-25 06:57:31 +01:00
};
class ServerAndQuery {
public:
2009-03-19 21:23:04 +01:00
ServerAndQuery( const string& server , BSONObj extra = BSONObj() , BSONObj orderObject = BSONObj() ) :
_server( server ) , _extra( extra.getOwned() ) , _orderObject( orderObject.getOwned() ){
2009-02-25 06:57:31 +01:00
}
bool operator<( const ServerAndQuery& other ) const{
if ( ! _orderObject.isEmpty() )
return _orderObject.woCompare( other._orderObject ) < 0;
if ( _server < other._server )
return true;
if ( other._server > _server )
return false;
return _extra.woCompare( other._extra ) < 0;
}
string _server;
BSONObj _extra;
BSONObj _orderObject;
};
class SerialServerShardedCursor : public ShardedCursor {
public:
SerialServerShardedCursor( set<ServerAndQuery> servers , QueryMessage& q , int sortOrder=0);
virtual bool more();
virtual BSONObj next();
private:
vector<ServerAndQuery> _servers;
unsigned _serverIndex;
auto_ptr<DBClientCursor> _current;
};
class ParallelSortShardedCursor : public ShardedCursor {
public:
ParallelSortShardedCursor( set<ServerAndQuery> servers , QueryMessage& q , const BSONObj& sortKey );
virtual ~ParallelSortShardedCursor();
virtual bool more();
virtual BSONObj next();
private:
void advance();
int _numServers;
set<ServerAndQuery> _servers;
BSONObj _sortKey;
auto_ptr<DBClientCursor> * _cursors;
BSONObj * _nexts;
};
2009-02-25 07:23:58 +01:00
class CursorCache {
public:
CursorCache();
~CursorCache();
ShardedCursor* get( long long id );
void store( ShardedCursor* cursor );
void remove( long long id );
private:
map<long long,ShardedCursor*> _cursors;
};
extern CursorCache cursorCache;
2009-02-25 06:57:31 +01:00
}