Skip to content

Commit 4fb4926

Browse files
author
Ryan
committed
set explain and snapshot in copy() CS-556
1 parent ee7543a commit 4fb4926

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

src/main/com/mongodb/DBCursor.java

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
/** An iterator over database results.
28-
* Doing a <code>find()</code> query on a collection returns a
28+
* Doing a <code>find()</code> query on a collection returns a
2929
* <code>DBCursor</code> thus
3030
*
3131
* <blockquote><pre>
@@ -35,13 +35,13 @@
3535
* </pre></blockquote>
3636
*
3737
* <p><b>Warning:</b> Calling <code>toArray</code> or <code>length</code> on
38-
* a DBCursor will irrevocably turn it into an array. This
38+
* a DBCursor will irrevocably turn it into an array. This
3939
* means that, if the cursor was iterating over ten million results
4040
* (which it was lazily fetching from the database), suddenly there will
4141
* be a ten-million element array in memory. Before converting to an array,
42-
* make sure that there are a reasonable number of results using
42+
* make sure that there are a reasonable number of results using
4343
* <code>skip()</code> and <code>limit()</code>.
44-
* <p>For example, to get an array of the 1000-1100th elements of a cursor, use
44+
* <p>For example, to get an array of the 1000-1100th elements of a cursor, use
4545
*
4646
* <blockquote><pre>
4747
* List<DBObject> obj = collection.find( query ).skip( 1000 ).limit( 100 ).toArray();
@@ -73,7 +73,7 @@ static enum CursorType { ITERATOR , ARRAY };
7373

7474
/**
7575
* Creates a copy of an existing database cursor.
76-
* The new cursor is an iterator, even if the original
76+
* The new cursor is an iterator, even if the original
7777
* was an array.
7878
*
7979
* @return the new cursor
@@ -86,6 +86,8 @@ public DBCursor copy() {
8686
c._skip = _skip;
8787
c._options = _options;
8888
c._batchSize = _batchSize;
89+
c._snapshot = _snapshot;
90+
c._explain = _explain;
8991
if ( _specialFields != null )
9092
c._specialFields = new BasicDBObject( _specialFields.toMap() );
9193
return c;
@@ -143,10 +145,10 @@ public DBCursor addSpecial( String name , Object o ){
143145
public DBCursor hint( DBObject indexKeys ){
144146
if ( _it != null )
145147
throw new IllegalStateException( "can't hint after executing query" );
146-
148+
147149
if ( indexKeys == null )
148150
_hint = null;
149-
else
151+
else
150152
_hint = DBCollection.genIndexName( indexKeys );
151153
return this;
152154
}
@@ -165,9 +167,9 @@ public DBCursor hint( String indexName ){
165167
}
166168

167169
/**
168-
* Use snapshot mode for the query. Snapshot mode assures no duplicates are
169-
* returned, or objects missed, which were present at both the start and end
170-
* of the query's execution (if an object is new during the query, or deleted
170+
* Use snapshot mode for the query. Snapshot mode assures no duplicates are
171+
* returned, or objects missed, which were present at both the start and end
172+
* of the query's execution (if an object is new during the query, or deleted
171173
* during the query, it may or may not be returned, even with snapshot mode).
172174
* Note that short query responses (less than 1MB) are always effectively snapshotted.
173175
* Currently, snapshot mode may not be used with sorting or explicit hints.
@@ -226,14 +228,14 @@ else if (n < 0)
226228
/**
227229
* Limits the number of elements returned in one batch.
228230
* A cursor typically fetches a batch of result objects and store them locally.
229-
*
231+
*
230232
* If <tt>batchSize</tt> is positive, it represents the size of each batch of objects retrieved.
231233
* It can be adjusted to optimize performance and limit data transfer.
232-
*
234+
*
233235
* If <tt>batchSize</tt> is negative, it will limit of number objects returned, that fit within the max batch size limit (usually 4MB), and cursor will be closed.
234236
* For example if <tt>batchSize</tt> is -10, then the server will return a maximum of 10 documents and as many as can fit in 4MB, then close the cursor.
235237
* Note that this feature is different from limit() in that documents must fit within a maximum size, and it removes the need to send a request to close the cursor server-side.
236-
*
238+
*
237239
* The batch size can be changed even after a cursor is iterated, in which case the setting will apply on the next batch retrieval.
238240
*
239241
* @param n the number of elements to return in a batch
@@ -243,7 +245,7 @@ public DBCursor batchSize( int n ){
243245
// check for special case, used to have server bug with 1
244246
if ( n == 1 )
245247
n = 2;
246-
248+
247249
if ( _it != null ) {
248250
if (_it instanceof DBApiLayer.Result)
249251
((DBApiLayer.Result)_it).setBatchSize(n);
@@ -273,18 +275,18 @@ public DBCursor skip( int n ){
273275
public long getCursorId() {
274276
if ( _it instanceof Result )
275277
return ((Result)_it).getCursorId();
276-
278+
277279
return 0;
278280
}
279-
281+
280282
/**
281283
* kills the current cursor on the server.
282284
*/
283285
public void close() {
284286
if ( _it instanceof Result )
285287
((Result)_it).close();
286288
}
287-
289+
288290
/**
289291
* makes this query ok to run on a slave node
290292
* @return
@@ -332,15 +334,15 @@ private void _check()
332334
throws MongoException {
333335
if ( _it != null )
334336
return;
335-
337+
336338
if ( _collection != null && _query != null ){
337339

338340
_lookForHints();
339341

340342
DBObject foo = _query;
341343
if ( hasSpecialQueryFields() ){
342344
foo = _specialFields == null ? new BasicDBObject() : _specialFields;
343-
345+
344346
_addToQueryObject( foo , "query" , _query , true );
345347
_addToQueryObject( foo , "orderby" , _orderBy , false );
346348
_addToQueryObject( foo , "$hint" , _hint );
@@ -359,12 +361,12 @@ private void _check()
359361
_fake = true;
360362
}
361363
}
362-
364+
363365
/**
364366
* if there is a hint to use, use it
365367
*/
366368
private void _lookForHints(){
367-
369+
368370
if ( _hint != null ) // if someone set a hint, then don't do this
369371
return;
370372

@@ -374,7 +376,7 @@ private void _lookForHints(){
374376
Set<String> mykeys = _query.keySet();
375377

376378
for ( DBObject o : _collection._hintFields ){
377-
379+
378380
Set<String> hintKeys = o.keySet();
379381

380382
if ( ! mykeys.containsAll( hintKeys ) )
@@ -391,28 +393,28 @@ boolean hasSpecialQueryFields(){
391393

392394
if ( _orderBy != null && _orderBy.keySet().size() > 0 )
393395
return true;
394-
396+
395397
if ( _hint != null || _snapshot )
396398
return true;
397-
399+
398400
return _explain;
399401
}
400402

401403
void _addToQueryObject( DBObject query , String field , DBObject thing , boolean sendEmpty ){
402404
if ( thing == null )
403405
return;
404-
406+
405407
if ( ! sendEmpty && thing.keySet().size() == 0 )
406408
return;
407-
409+
408410
_addToQueryObject( query , field , thing );
409411
}
410412

411413
void _addToQueryObject( DBObject query , String field , Object thing ){
412414

413415
if ( thing == null )
414416
return;
415-
417+
416418
query.put( field , thing );
417419
}
418420

@@ -478,7 +480,7 @@ public List<Integer> getSizes(){
478480

479481
throw new IllegalArgumentException("_it not a real result" );
480482
}
481-
483+
482484
private boolean _hasNext()
483485
throws MongoException {
484486
_check();
@@ -508,7 +510,7 @@ public boolean hasNext() throws MongoException {
508510
_checkType( CursorType.ITERATOR );
509511
return _hasNext();
510512
}
511-
513+
512514
/**
513515
* Returns the object the cursor is at and moves the cursor ahead by one.
514516
* @return the next element
@@ -545,7 +547,7 @@ void _fill( int n )
545547
_next();
546548
}
547549

548-
/**
550+
/**
549551
* pulls back all items into an array and returns the number of objects.
550552
* Note: this can be resource intensive
551553
* @see #count()
@@ -569,7 +571,7 @@ public List<DBObject> toArray()
569571
throws MongoException {
570572
return toArray( Integer.MAX_VALUE );
571573
}
572-
574+
573575
/**
574576
* Converts this cursor to an array.
575577
* @param max the maximum number of objects to return
@@ -582,7 +584,7 @@ public List<DBObject> toArray( int max )
582584
_fill( max );
583585
return _all;
584586
}
585-
587+
586588
/**
587589
* for testing only!
588590
* Iterates cursor and counts objects
@@ -605,13 +607,13 @@ public int itcount(){
605607
* @return the number of objects
606608
* @throws MongoException
607609
*/
608-
public int count()
610+
public int count()
609611
throws MongoException {
610612
if ( _collection == null )
611613
throw new IllegalArgumentException( "why is _collection null" );
612614
if ( _collection._db == null )
613615
throw new IllegalArgumentException( "why is _collection._db null" );
614-
616+
615617
return (int)_collection.getCount(this._query, this._keysWanted);
616618
}
617619

@@ -622,25 +624,25 @@ public int count()
622624
* @return the number of objects
623625
* @throws MongoException
624626
*/
625-
public int size()
627+
public int size()
626628
throws MongoException {
627629
if ( _collection == null )
628630
throw new IllegalArgumentException( "why is _collection null" );
629631
if ( _collection._db == null )
630632
throw new IllegalArgumentException( "why is _collection._db null" );
631-
633+
632634
return (int)_collection.getCount(this._query, this._keysWanted, this._limit, this._skip );
633635
}
634636

635-
637+
636638
/**
637639
* gets the fields to be returned
638640
* @return
639641
*/
640642
public DBObject getKeysWanted(){
641643
return _keysWanted;
642644
}
643-
645+
644646
/**
645647
* gets the query
646648
* @return
@@ -697,7 +699,7 @@ public String toString() {
697699
private final DBCollection _collection;
698700
private final DBObject _query;
699701
private final DBObject _keysWanted;
700-
702+
701703
private DBObject _orderBy = null;
702704
private String _hint = null;
703705
private boolean _explain = false;

0 commit comments

Comments
 (0)