MongoDB C++ Driver  legacy-1.0.5
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dbclientcursor.h
1 // file dbclientcursor.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <stack>
21 
23 #include "mongo/client/export_macros.h"
24 #include "mongo/db/jsobj.h"
25 #include "mongo/db/json.h"
26 #include "mongo/util/net/message.h"
27 
28 namespace mongo {
29 
30  class DBClientCursorShim;
31  class DBClientCursorShimCursorID;
32  class DBClientCursorShimArray;
33  class DBClientCursorShimTransform;
34 
38  class MONGO_CLIENT_API DBClientCursorInterface : boost::noncopyable {
39  public:
40  virtual ~DBClientCursorInterface() {}
41  virtual bool more() = 0;
42  virtual BSONObj next() = 0;
43  // TODO bring more of the DBClientCursor interface to here
44  protected:
46  };
47 
49  class MONGO_CLIENT_API DBClientCursor : public DBClientCursorInterface {
50  public:
52  bool more();
53 
59  int objsLeftInBatch() const { _assertIfNull(); return _putBack.size() + batch.nReturned - batch.pos; }
60  bool moreInCurrentBatch() { return objsLeftInBatch() > 0; }
61 
71  BSONObj next();
72 
76  void putBack( const BSONObj &o ) { _putBack.push( o.getOwned() ); }
77 
79  BSONObj nextSafe();
80 
86  void peek(std::vector<BSONObj>&, int atMost);
87 
88  // Peeks at first element, if exists
89  BSONObj peekFirst();
90 
94  bool peekError(BSONObj* error = NULL);
95 
99  int itcount() {
100  int c = 0;
101  while ( more() ) {
102  next();
103  c++;
104  }
105  return c;
106  }
107 
113  bool isDead() const { return cursorId == 0; }
114 
115  bool tailable() const { return (opts & QueryOption_CursorTailable) != 0; }
116 
121  bool hasResultFlag( int flag ) {
122  _assertIfNull();
123  return (resultFlags & flag) != 0;
124  }
125 
127  void setBatchSize(int newBatchSize) { batchSize = newBatchSize; }
128 
129  DBClientCursor( DBClientBase* client, const std::string &_ns, BSONObj _query, int _nToReturn,
130  int _nToSkip, const BSONObj *_fieldsToReturn, int queryOptions , int bs );
131  DBClientCursor( DBClientBase* client, const std::string &_ns, long long _cursorId, int _nToReturn, int options, int _batchSize );
132 
133  virtual ~DBClientCursor();
134 
135  long long getCursorId() const { return cursorId; }
136 
140  void decouple() { _ownCursor = false; }
141 
142  std::string originalHost() const { return _originalHost; }
143 
144  std::string getns() const { return ns; }
145 
146  Message* getMessage(){ return batch.m.get(); }
147 
156  bool initCommand();
157 
161  bool init();
162 
163  void initLazy( bool isRetry = false );
164  bool initLazyFinish( bool& retry );
165 
166  class Batch : boost::noncopyable {
167  friend class DBClientCursor;
168  std::auto_ptr<Message> m;
169  int nReturned;
170  int pos;
171  const char *data;
172  public:
173  Batch() : m( new Message() ), nReturned(), pos(), data() { }
174  };
175 
176  private:
177  friend class DBClientBase;
178  friend class DBClientConnection;
179  friend class DBClientCursorShimCursorID;
180  friend class DBClientCursorShimArray;
181  friend class DBClientCursorShimTransform;
182  friend class DBClientWithCommands;
183 
184  int nextBatchSize();
185  void _finishConsInit();
186 
187  BSONObj rawNext();
188  bool rawMore();
189 
190  std::auto_ptr<DBClientCursorShim> shim;
191 
192  Batch batch;
193  DBClientBase* _client;
194  std::string _originalHost;
195  std::string ns;
196  BSONObj query;
197  int nToReturn;
198  int nToSkip;
199  long long nReturned;
200  const BSONObj *fieldsToReturn;
201  int opts;
202  int batchSize;
203  std::stack< BSONObj > _putBack;
204  int resultFlags;
205  long long cursorId;
206  bool _ownCursor; // see decouple()
207  std::string _scopedHost;
208  std::string _lazyHost;
209  bool wasError;
210 
211  void dataReceived() { bool retry; std::string lazyHost; dataReceived( retry, lazyHost ); }
212  void dataReceived( bool& retry, std::string& lazyHost );
213  void requestMore();
214  void exhaustReceiveMore(); // for exhaust
215 
216  // Don't call from a virtual function
217  void _assertIfNull() const { uassert(13348, "connection died", this); }
218 
219  // non-copyable , non-assignable
220  DBClientCursor( const DBClientCursor& );
221  DBClientCursor& operator=( const DBClientCursor& );
222 
223  // init pieces
224  void _assembleInit( Message& toSend );
225  };
226 
229  class MONGO_CLIENT_API DBClientCursorBatchIterator {
230  public:
231  DBClientCursorBatchIterator( DBClientCursor &c ) : _c( c ), _n() {}
232  bool moreInCurrentBatch() { return _c.moreInCurrentBatch(); }
233  BSONObj nextSafe() {
234  massert( 13383, "BatchIterator empty", moreInCurrentBatch() );
235  ++_n;
236  return _c.nextSafe();
237  }
238  int n() const { return _n; }
239  private:
240  DBClientCursor &_c;
241  int _n;
242  };
243 
244 } // namespace mongo
245 
DBClientCursorShimCursorID implements the shim interface over a cursor reply document, rather than the traditional OP_REPLY.
Definition: dbclientcursorshimcursorid.h:29
void setBatchSize(int newBatchSize)
Change batchSize after construction. Can change after requesting first batch.
Definition: dbclientcursor.h:127
DB "commands" Basically just invocations of connection.
Definition: dbclientinterface.h:662
Definition: message.h:298
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
void putBack(const BSONObj &o)
restore an object previously returned by next() to the cursor
Definition: dbclientcursor.h:76
BSON classes.
Definition: dbclientcursor.h:166
Queries return a cursor object.
Definition: dbclientcursor.h:49
bool isDead() const
cursor no longer valid – use with tailable cursors.
Definition: dbclientcursor.h:113
BSONObj getOwned() const
assure the data buffer is under the control of this BSONObj and not a remote buffer ...
int objsLeftInBatch() const
If true, there is more in our local buffers to be fetched via next().
Definition: dbclientcursor.h:59
for mock purposes only – do not create variants of DBClientCursor, nor hang code here ...
Definition: dbclientcursor.h:38
A basic connection to the database.
Definition: dbclientinterface.h:1551
int itcount()
iterate the rest of the cursor and return the number if items
Definition: dbclientcursor.h:99
void decouple()
by default we "own" the cursor and will send the server a KillCursor message when ~DBClientCursor() i...
Definition: dbclientcursor.h:140
Core MongoDB C++ driver interfaces are defined here.
DBClientCursorShimTransform implements the shim interface over a cursor reply document by allowing a ...
Definition: dbclientcursorshimtransform.h:29
iterate over objects in current batch only - will not cause a network call
Definition: dbclientcursor.h:229
bool hasResultFlag(int flag)
see ResultFlagType (constants.h) for flag values mostly these flags are for internal purposes - Resul...
Definition: dbclientcursor.h:121
abstract class that implements the core db operations
Definition: dbclientinterface.h:1330
DBClientCursorShimArray implements the shim interface over an array of bson obj's.
Definition: dbclientcursorshimarray.h:28
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
QueryOption_CursorTailable
Tailable means cursor is not closed when the last data is retrieved.
Definition: dbclientinterface.h:55