Skip to content

Commit bf76d5d

Browse files
committed
Deprecate options-related DBCursor method
Deprecate: * addOption * setOption * resetOptions * getOptions Replace with per-option methods: * cursorType * oplogReplay * noCursorTimeout * partial JAVA-3021
1 parent 13a46b1 commit bf76d5d

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

driver-legacy/src/main/com/mongodb/DBCursor.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ public void remove() {
220220
* @return {@code this} so calls can be chained
221221
* @see Bytes
222222
* @mongodb.driver.manual ../meta-driver/latest/legacy/mongodb-wire-protocol/#op-query Query Flags
223+
* @deprecated Prefer per-option methods, e.g. {@link #cursorType(CursorType)}, {@link #noCursorTimeout(boolean)}, etc.
223224
*/
225+
@Deprecated
224226
public DBCursor addOption(final int option) {
225227
setOptions(this.options |= option);
226228
return this;
@@ -234,6 +236,7 @@ public DBCursor addOption(final int option) {
234236
* @see Bytes
235237
* @mongodb.driver.manual ../meta-driver/latest/legacy/mongodb-wire-protocol/#op-query Query Flags
236238
*/
239+
@Deprecated
237240
public DBCursor setOptions(final int options) {
238241
if ((options & Bytes.QUERYOPTION_EXHAUST) != 0) {
239242
throw new UnsupportedOperationException("exhaust query option is not supported");
@@ -248,6 +251,7 @@ public DBCursor setOptions(final int options) {
248251
* @return {@code this} so calls can be chained
249252
* @mongodb.driver.manual ../meta-driver/latest/legacy/mongodb-wire-protocol/#op-query Query Flags
250253
*/
254+
@Deprecated
251255
public DBCursor resetOptions() {
252256
this.options = 0;
253257
return this;
@@ -259,6 +263,7 @@ public DBCursor resetOptions() {
259263
* @return the bitmask of options
260264
* @mongodb.driver.manual ../meta-driver/latest/legacy/mongodb-wire-protocol/#op-query Query Flags
261265
*/
266+
@Deprecated
262267
public int getOptions() {
263268
return options;
264269
}
@@ -493,6 +498,55 @@ public DBObject explain() {
493498
getReadPreference(), getReadConcern()));
494499
}
495500

501+
/**
502+
* Sets the cursor type.
503+
*
504+
* @param cursorType the cursor type, which may not be null
505+
* @return this
506+
* @since 3.9
507+
*/
508+
public DBCursor cursorType(final CursorType cursorType) {
509+
findOptions.cursorType(cursorType);
510+
return this;
511+
}
512+
513+
/**
514+
* Users should not set this under normal circumstances.
515+
*
516+
* @param oplogReplay if oplog replay is enabled
517+
* @return this
518+
* @since 3.9
519+
*/
520+
public DBCursor oplogReplay(final boolean oplogReplay) {
521+
findOptions.oplogReplay(oplogReplay);
522+
return this;
523+
}
524+
525+
/**
526+
* The server normally times out idle cursors after an inactivity period (10 minutes)
527+
* to prevent excess memory use. Set this option to prevent that.
528+
*
529+
* @param noCursorTimeout true if cursor timeout is disabled
530+
* @return this
531+
* @since 3.9
532+
*/
533+
public DBCursor noCursorTimeout(final boolean noCursorTimeout) {
534+
findOptions.noCursorTimeout(noCursorTimeout);
535+
return this;
536+
}
537+
538+
/**
539+
* Get partial results from a sharded cluster if one or more shards are unreachable (instead of throwing an error).
540+
*
541+
* @param partial if partial results for sharded clusters is enabled
542+
* @return this
543+
* @since 3.9
544+
*/
545+
public DBCursor partial(final boolean partial) {
546+
findOptions.partial(partial);
547+
return this;
548+
}
549+
496550
@SuppressWarnings("deprecation")
497551
private FindOperation<DBObject> getQueryOperation(final Decoder<DBObject> decoder) {
498552
FindOperation<DBObject> operation = new FindOperation<DBObject>(collection.getNamespace(), decoder)

driver-legacy/src/main/com/mongodb/client/model/DBCollectionFindOptions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ public DBCollectionFindOptions batchSize(final int batchSize) {
219219
*
220220
* @return the query modifiers, which may be null
221221
* @mongodb.driver.manual reference/operator/query-modifier/ Query Modifiers
222+
* @deprecated use the individual setters instead
222223
*/
224+
@Deprecated
223225
public DBObject getModifiers() {
224226
return modifiers;
225227
}
@@ -230,7 +232,9 @@ public DBObject getModifiers() {
230232
* @param modifiers the query modifiers to apply, which may be null.
231233
* @return this
232234
* @mongodb.driver.manual reference/operator/query-modifier/ Query Modifiers
235+
* @deprecated use the individual setters instead
233236
*/
237+
@Deprecated
234238
public DBCollectionFindOptions modifiers(@Nullable final DBObject modifiers) {
235239
this.modifiers = notNull("modifiers", modifiers);
236240
return this;

driver-legacy/src/test/unit/com/mongodb/DBCursorSpecification.groovy

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,56 @@ class DBCursorSpecification extends Specification {
147147
.modifiers(new BsonDocument()))
148148
}
149149

150+
def 'DBCursor methods should be used to create the expected operation'() {
151+
given:
152+
def executor = new TestOperationExecutor([stubBatchCursor()]);
153+
def collection = new DB(getMongoClient(), 'myDatabase', executor).getCollection('test')
154+
def collation = Collation.builder().locale('en').build()
155+
def cursorType = CursorType.NonTailable
156+
def filter = new BasicDBObject()
157+
def sort = BasicDBObject.parse('{a: 1}')
158+
def bsonFilter = new BsonDocument()
159+
def bsonSort = BsonDocument.parse(sort.toJson())
160+
def readConcern = ReadConcern.LOCAL
161+
def readPreference = ReadPreference.nearest()
162+
def findOptions = new DBCollectionFindOptions()
163+
def cursor = new DBCursor(collection, filter, findOptions)
164+
.setReadConcern(readConcern)
165+
.setReadPreference(readPreference)
166+
.setCollation(collation)
167+
.batchSize(1)
168+
.cursorType(cursorType)
169+
.limit(1)
170+
.maxTime(1, TimeUnit.MILLISECONDS)
171+
.noCursorTimeout(true)
172+
.oplogReplay(true)
173+
.partial(true)
174+
.skip(1)
175+
.sort(sort)
176+
177+
when:
178+
cursor.toArray()
179+
180+
then:
181+
expect executor.getReadOperation(), isTheSameAs(new FindOperation(collection.getNamespace(), collection.getObjectCodec())
182+
.batchSize(1)
183+
.collation(collation)
184+
.cursorType(cursorType)
185+
.filter(bsonFilter)
186+
.limit(1)
187+
.maxTime(1, TimeUnit.MILLISECONDS)
188+
.noCursorTimeout(true)
189+
.oplogReplay(true)
190+
.partial(true)
191+
.skip(1)
192+
.sort(bsonSort)
193+
.modifiers(new BsonDocument())
194+
)
195+
196+
executor.getReadPreference() == readPreference
197+
executor.getReadConcern() == readConcern
198+
}
199+
150200
def 'DBCollectionFindOptions should be used to create the expected operation'() {
151201
given:
152202
def executor = new TestOperationExecutor([stubBatchCursor()]);

0 commit comments

Comments
 (0)