Skip to content

Commit 41d21d2

Browse files
committed
Added hintString to FindIterable
Also removed deprecations for count hintString JAVA-3441
1 parent af974b2 commit 41d21d2

File tree

13 files changed

+111
-22
lines changed

13 files changed

+111
-22
lines changed

driver-async/src/main/com/mongodb/async/client/FindIterable.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ public interface FindIterable<T> extends MongoIterable<T> {
189189
*/
190190
FindIterable<T> hint(@Nullable Bson hint);
191191

192+
/**
193+
* Sets the hint to apply.
194+
*
195+
* <p>Note: If {@link FindIterable#hint(Bson)} is set that will be used instead of any hint string.</p>
196+
*
197+
* @param hint the name of the index which should be used for the operation
198+
* @return this
199+
* @since 3.12
200+
*/
201+
FindIterable<T> hintString(@Nullable String hint);
202+
192203
/**
193204
* Sets the exclusive upper bound for a specific index. A null value means no max is set.
194205
*

driver-async/src/main/com/mongodb/async/client/FindIterableImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ public FindIterable<TResult> hint(@Nullable final Bson hint) {
154154
return this;
155155
}
156156

157+
@Override
158+
public FindIterable<TResult> hintString(@Nullable final String hint) {
159+
findOptions.hintString(hint);
160+
return this;
161+
}
162+
157163
@Override
158164
public FindIterable<TResult> max(@Nullable final Bson max) {
159165
findOptions.max(max);

driver-async/src/test/unit/com/mongodb/async/client/FindIterableSpecification.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.mongodb.client.model.Collation
2828
import com.mongodb.operation.FindOperation
2929
import org.bson.BsonDocument
3030
import org.bson.BsonInt32
31+
import org.bson.BsonString
3132
import org.bson.Document
3233
import org.bson.codecs.BsonValueCodecProvider
3334
import org.bson.codecs.DocumentCodec
@@ -76,7 +77,7 @@ class FindIterableSpecification extends Specification {
7677
.partial(false)
7778
.collation(null)
7879
.comment('my comment')
79-
.hint(new Document('hint', 1))
80+
.hintString('a_1')
8081
.min(new Document('min', 1))
8182
.max(new Document('max', 1))
8283
.maxScan(42L)
@@ -104,7 +105,7 @@ class FindIterableSpecification extends Specification {
104105
.cursorType(CursorType.NonTailable)
105106
.slaveOk(true)
106107
.comment('my comment')
107-
.hint(new BsonDocument('hint', new BsonInt32(1)))
108+
.hint(new BsonString('a_1'))
108109
.min(new BsonDocument('min', new BsonInt32(1)))
109110
.max(new BsonDocument('max', new BsonInt32(1)))
110111
.maxScan(42L)

driver-core/src/main/com/mongodb/client/model/CountOptions.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ public Bson getHint() {
5151
* Gets the hint string to apply.
5252
*
5353
* @return the hint string, which should be the name of an existing index
54-
* @deprecated Prefer {@link #getHint()}
5554
*/
56-
@Deprecated
5755
@Nullable
5856
public String getHintString() {
5957
return hintString;
@@ -73,11 +71,11 @@ public CountOptions hint(@Nullable final Bson hint) {
7371
/**
7472
* Sets the hint to apply.
7573
*
74+
* <p>Note: If {@link CountOptions#hint(Bson)} is set that will be used instead of any hint string.</p>
75+
*
7676
* @param hint the name of the index which should be used for the operation
7777
* @return this
78-
* @deprecated Prefer {@link #hint(Bson)}
7978
*/
80-
@Deprecated
8179
public CountOptions hintString(@Nullable final String hint) {
8280
this.hintString = hint;
8381
return this;

driver-core/src/main/com/mongodb/client/model/FindOptions.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public final class FindOptions {
5050
private Collation collation;
5151
private String comment;
5252
private Bson hint;
53+
private String hintString;
5354
private Bson max;
5455
private Bson min;
5556
private long maxScan;
@@ -84,6 +85,7 @@ public FindOptions(final FindOptions from) {
8485
partial = from.partial;
8586
comment = from.comment;
8687
hint = from.hint;
88+
hintString = from.hintString;
8789
max = from.max;
8890
min = from.min;
8991
maxScan = from.maxScan;
@@ -453,6 +455,29 @@ public FindOptions hint(@Nullable final Bson hint) {
453455
return this;
454456
}
455457

458+
/**
459+
* Gets the hint string to apply.
460+
*
461+
* @return the hint string, which should be the name of an existing index
462+
* @since 3.12
463+
*/
464+
@Nullable
465+
public String getHintString() {
466+
return hintString;
467+
}
468+
469+
/**
470+
* Sets the hint to apply.
471+
*
472+
* @param hint the name of the index which should be used for the operation
473+
* @return this
474+
* @since 3.12
475+
*/
476+
public FindOptions hintString(@Nullable final String hint) {
477+
this.hintString = hint;
478+
return this;
479+
}
480+
456481
/**
457482
* Returns the exclusive upper bound for a specific index. By default there is no max bound.
458483
*

driver-core/src/main/com/mongodb/internal/operation/Operations.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ <TResult> FindOperation<TResult> find(final MongoNamespace findNamespace, final
147147
@SuppressWarnings("deprecation")
148148
private <TResult> FindOperation<TResult> createFindOperation(final MongoNamespace findNamespace, final Bson filter,
149149
final Class<TResult> resultClass, final FindOptions options) {
150-
return new FindOperation<TResult>(findNamespace, codecRegistry.get(resultClass))
150+
FindOperation<TResult> operation = new FindOperation<TResult>(findNamespace, codecRegistry.get(resultClass))
151151
.retryReads(retryReads)
152152
.filter(filter == null ? new BsonDocument() : filter.toBsonDocument(documentClass, codecRegistry))
153153
.batchSize(options.getBatchSize())
@@ -165,13 +165,19 @@ private <TResult> FindOperation<TResult> createFindOperation(final MongoNamespac
165165
.slaveOk(readPreference.isSlaveOk())
166166
.collation(options.getCollation())
167167
.comment(options.getComment())
168-
.hint(toBsonDocumentOrNull(options.getHint()))
169168
.min(toBsonDocumentOrNull(options.getMin()))
170169
.max(toBsonDocumentOrNull(options.getMax()))
171170
.maxScan(options.getMaxScan())
172171
.returnKey(options.isReturnKey())
173172
.showRecordId(options.isShowRecordId())
174173
.snapshot(options.isSnapshot());
174+
175+
if (options.getHint() != null) {
176+
operation.hint(toBsonDocument(options.getHint()));
177+
} else if (options.getHintString() != null) {
178+
operation.hint(new BsonString(options.getHintString()));
179+
}
180+
return operation;
175181
}
176182

177183
<TResult> DistinctOperation<TResult> distinct(final String fieldName, final Bson filter,

driver-core/src/main/com/mongodb/operation/FindOperation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public class FindOperation<T> implements AsyncReadOperation<AsyncBatchCursor<T>>
109109
private boolean partial;
110110
private Collation collation;
111111
private String comment;
112-
private BsonDocument hint;
112+
private BsonValue hint;
113113
private BsonDocument max;
114114
private BsonDocument min;
115115
private long maxScan;
@@ -529,7 +529,7 @@ public FindOperation<T> comment(final String comment) {
529529
* @return the hint
530530
* @since 3.5
531531
*/
532-
public BsonDocument getHint() {
532+
public BsonValue getHint() {
533533
return hint;
534534
}
535535

@@ -540,7 +540,7 @@ public BsonDocument getHint() {
540540
* @return this
541541
* @since 3.5
542542
*/
543-
public FindOperation<T> hint(final BsonDocument hint) {
543+
public FindOperation<T> hint(final BsonValue hint) {
544544
this.hint = hint;
545545
return this;
546546
}

driver-core/src/test/functional/com/mongodb/operation/FindOperationSpecification.groovy

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class FindOperationSpecification extends OperationFunctionalSpecification {
8686
operation.getFilter() == null
8787
operation.getMaxTime(MILLISECONDS) == 0
8888
operation.getMaxAwaitTime(MILLISECONDS) == 0
89+
operation.getHint() == null
8990
operation.getLimit() == 0
9091
operation.getSkip() == 0
9192
operation.getBatchSize() == 0
@@ -103,6 +104,7 @@ class FindOperationSpecification extends OperationFunctionalSpecification {
103104
def filter = new BsonDocument('filter', new BsonInt32(1))
104105
def projection = new BsonDocument('projection', new BsonInt32(1))
105106
def modifiers = new BsonDocument('modifiers', new BsonInt32(1))
107+
def hint = new BsonString('a_1')
106108

107109
when:
108110
FindOperation operation = new FindOperation<Document>(getNamespace(), new DocumentCodec())
@@ -111,6 +113,7 @@ class FindOperationSpecification extends OperationFunctionalSpecification {
111113
.filter(filter)
112114
.limit(20)
113115
.skip(30)
116+
.hint(hint)
114117
.batchSize(40)
115118
.projection(projection)
116119
.modifiers(modifiers)
@@ -127,6 +130,7 @@ class FindOperationSpecification extends OperationFunctionalSpecification {
127130
operation.getMaxAwaitTime(MILLISECONDS) == 20000
128131
operation.getLimit() == 20
129132
operation.getSkip() == 30
133+
operation.getHint() == hint
130134
operation.getBatchSize() == 40
131135
operation.getProjection() == projection
132136
operation.getModifiers() == modifiers
@@ -433,11 +437,15 @@ class FindOperationSpecification extends OperationFunctionalSpecification {
433437

434438
def 'should apply $hint'() {
435439
given:
436-
def hint = new BsonDocument('a', new BsonInt32(1))
437-
collectionHelper.createIndex(hint)
440+
def index = new BsonDocument('a', new BsonInt32(1))
441+
collectionHelper.createIndex(index)
438442

439443
def operation = new FindOperation<Document>(getNamespace(), new DocumentCodec())
440-
.modifiers(new BsonDocument('$hint', hint))
444+
if (useModifer) {
445+
operation.modifiers(new BsonDocument('$hint', hint))
446+
} else {
447+
operation.hint(hint)
448+
}
441449
operation = async ? operation.asExplainableOperationAsync(QUERY_PLANNER) :
442450
operation.asExplainableOperation(QUERY_PLANNER)
443451

@@ -446,13 +454,14 @@ class FindOperationSpecification extends OperationFunctionalSpecification {
446454

447455
then:
448456
if (serverVersionAtLeast(3, 0)) {
449-
assertEquals(hint, QueryOperationHelper.getKeyPattern(explainPlan))
457+
assertEquals(index, QueryOperationHelper.getKeyPattern(explainPlan))
450458
} else {
451459
assertEquals(new BsonString('BtreeCursor a_1'), explainPlan.cursor)
452460
}
453461

454462
where:
455-
async << [true, false]
463+
[async, useModifer, hint] << [[true, false], [true, false], [new BsonDocument('a', new BsonInt32(1)),
464+
new BsonString('a_1')]].combinations()
456465
}
457466

458467
@IgnoreIf({ isSharded() })

driver-core/src/test/unit/com/mongodb/client/model/FindOptionsSpecification.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.mongodb.client.model
1818

1919
import com.mongodb.CursorType
2020
import org.bson.BsonDocument
21+
import org.bson.Document
2122
import spock.lang.Specification
2223

2324
import static java.util.concurrent.TimeUnit.MILLISECONDS
@@ -35,6 +36,8 @@ class FindOptionsSpecification extends Specification {
3536
options.getMaxAwaitTime(MILLISECONDS) == 0
3637
options.getProjection() == null
3738
options.getSort() == null
39+
options.getHint() == null
40+
options.getHintString() == null
3841
options.getLimit() == 0
3942
options.getSkip() == 0
4043
options.getBatchSize() == 0
@@ -172,4 +175,20 @@ class FindOptionsSpecification extends Specification {
172175
then:
173176
options.getMaxAwaitTime(SECONDS) == 1
174177
}
178+
179+
def 'should set hint'() {
180+
expect:
181+
new FindOptions().hint(hint).getHint() == hint
182+
183+
where:
184+
hint << [null, new BsonDocument(), new Document('a', 1)]
185+
}
186+
187+
def 'should set hintString'() {
188+
expect:
189+
new FindOptions().hintString(hintString).getHintString() == hintString
190+
191+
where:
192+
hintString << [null, 'a_1']
193+
}
175194
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public DBObject getHint() {
6262
* Gets the hint string to apply.
6363
*
6464
* @return the hint string, which should be the name of an existing index
65-
* @deprecated Prefer {@link #getHint()}
6665
*/
67-
@Deprecated
6866
@Nullable
6967
public String getHintString() {
7068
return hintString;
@@ -86,9 +84,7 @@ public DBCollectionCountOptions hint(@Nullable final DBObject hint) {
8684
*
8785
* @param hint the name of the index which should be used for the operation
8886
* @return this
89-
* @deprecated Prefer {@link #hint(DBObject)}
9087
*/
91-
@Deprecated
9288
public DBCollectionCountOptions hintString(@Nullable final String hint) {
9389
this.hintString = hint;
9490
return this;

0 commit comments

Comments
 (0)