diff options
Diffstat (limited to 'client/dbclientcursor.h')
-rw-r--r-- | client/dbclientcursor.h | 132 |
1 files changed, 75 insertions, 57 deletions
diff --git a/client/dbclientcursor.h b/client/dbclientcursor.h index 51cdc13..5d795f4 100644 --- a/client/dbclientcursor.h +++ b/client/dbclientcursor.h @@ -1,4 +1,4 @@ -// file dbclientcursor.h +// file dbclientcursor.h /* Copyright 2009 10gen Inc. * @@ -24,41 +24,55 @@ #include <stack> namespace mongo { - + class AScopedConnection; - - /** Queries return a cursor object */ - class DBClientCursor : boost::noncopyable { + + /** for mock purposes only -- do not create variants of DBClientCursor, nor hang code here */ + class DBClientCursorInterface { public: - /** If true, safe to call next(). Requests more from server if necessary. */ + virtual ~DBClientCursorInterface() {} + + virtual bool more() = 0; + virtual BSONObj next() = 0; + + // TODO bring more of the DBClientCursor interface to here + + protected: + DBClientCursorInterface() {} + }; + + /** Queries return a cursor object */ + class DBClientCursor : public DBClientCursorInterface { + public: + /** If true, safe to call next(). Requests more from server if necessary. */ bool more(); - /** If true, there is more in our local buffers to be fetched via next(). Returns - false when a getMore request back to server would be required. You can use this - if you want to exhaust whatever data has been fetched to the client already but + /** If true, there is more in our local buffers to be fetched via next(). Returns + false when a getMore request back to server would be required. You can use this + if you want to exhaust whatever data has been fetched to the client already but then perhaps stop. */ int objsLeftInBatch() const { _assertIfNull(); return _putBack.size() + nReturned - pos; } bool moreInCurrentBatch() { return objsLeftInBatch() > 0; } /** next - @return next object in the result cursor. + @return next object in the result cursor. on an error at the remote server, you will get back: { $err: <string> } if you do not want to handle that yourself, call nextSafe(). */ BSONObj next(); - - /** + + /** restore an object previously returned by next() to the cursor */ void putBack( const BSONObj &o ) { _putBack.push( o.getOwned() ); } - /** throws AssertionException if get back { $err : ... } */ + /** throws AssertionException if get back { $err : ... } */ BSONObj nextSafe() { BSONObj o = next(); BSONElement e = o.firstElement(); - if( strcmp(e.fieldName(), "$err") == 0 ) { + if( strcmp(e.fieldName(), "$err") == 0 ) { if( logLevel >= 5 ) log() << "nextSafe() error " << o.toString() << endl; uassert(13106, "nextSafe(): " + o.toString(), false); @@ -67,7 +81,7 @@ namespace mongo { } /** peek ahead at items buffered for future next() calls. - never requests new data from the server. so peek only effective + never requests new data from the server. so peek only effective with what is already buffered. WARNING: no support for _putBack yet! */ @@ -76,9 +90,9 @@ namespace mongo { /** iterate the rest of the cursor and return the number if items */ - int itcount(){ + int itcount() { int c = 0; - while ( more() ){ + while ( more() ) { next(); c++; } @@ -97,48 +111,48 @@ namespace mongo { bool tailable() const { return (opts & QueryOption_CursorTailable) != 0; } - - /** see ResultFlagType (constants.h) for flag values - mostly these flags are for internal purposes - + + /** see ResultFlagType (constants.h) for flag values + mostly these flags are for internal purposes - ResultFlag_ErrSet is the possible exception to that */ - bool hasResultFlag( int flag ){ + bool hasResultFlag( int flag ) { _assertIfNull(); return (resultFlags & flag) != 0; } - DBClientCursor( DBConnector *_connector, const string &_ns, BSONObj _query, int _nToReturn, + DBClientCursor( DBClientBase* client, const string &_ns, BSONObj _query, int _nToReturn, int _nToSkip, const BSONObj *_fieldsToReturn, int queryOptions , int bs ) : - connector(_connector), - ns(_ns), - query(_query), - nToReturn(_nToReturn), - haveLimit( _nToReturn > 0 && !(queryOptions & QueryOption_CursorTailable)), - nToSkip(_nToSkip), - fieldsToReturn(_fieldsToReturn), - opts(queryOptions), - batchSize(bs==1?2:bs), - m(new Message()), - cursorId(), - nReturned(), - pos(), - data(), - _ownCursor( true ){ + _client(client), + ns(_ns), + query(_query), + nToReturn(_nToReturn), + haveLimit( _nToReturn > 0 && !(queryOptions & QueryOption_CursorTailable)), + nToSkip(_nToSkip), + fieldsToReturn(_fieldsToReturn), + opts(queryOptions), + batchSize(bs==1?2:bs), + m(new Message()), + cursorId(), + nReturned(), + pos(), + data(), + _ownCursor( true ) { + } + + DBClientCursor( DBClientBase* client, const string &_ns, long long _cursorId, int _nToReturn, int options ) : + _client(client), + ns(_ns), + nToReturn( _nToReturn ), + haveLimit( _nToReturn > 0 && !(options & QueryOption_CursorTailable)), + opts( options ), + m(new Message()), + cursorId( _cursorId ), + nReturned(), + pos(), + data(), + _ownCursor( true ) { } - - DBClientCursor( DBConnector *_connector, const string &_ns, long long _cursorId, int _nToReturn, int options ) : - connector(_connector), - ns(_ns), - nToReturn( _nToReturn ), - haveLimit( _nToReturn > 0 && !(options & QueryOption_CursorTailable)), - opts( options ), - m(new Message()), - cursorId( _cursorId ), - nReturned(), - pos(), - data(), - _ownCursor( true ){ - } virtual ~DBClientCursor(); @@ -148,15 +162,15 @@ namespace mongo { message when ~DBClientCursor() is called. This function overrides that. */ void decouple() { _ownCursor = false; } - + void attach( AScopedConnection * conn ); - + private: friend class DBClientBase; friend class DBClientConnection; - bool init(); + bool init(); int nextBatchSize(); - DBConnector *connector; + DBClientBase* _client; string ns; BSONObj query; int nToReturn; @@ -180,8 +194,12 @@ namespace mongo { // Don't call from a virtual function void _assertIfNull() const { uassert(13348, "connection died", this); } + + // non-copyable , non-assignable + DBClientCursor( const DBClientCursor& ); + DBClientCursor& operator=( const DBClientCursor& ); }; - + /** iterate over objects in current batch only - will not cause a network call */ class DBClientCursorBatchIterator { @@ -198,7 +216,7 @@ namespace mongo { DBClientCursor &_c; int _n; }; - + } // namespace mongo #include "undef_macros.h" |