Skip to content

Commit 624b071

Browse files
committed
JAVA-2511, JAVA-2614: Support hint and comment for the aggregate command
1 parent 58fc73a commit 624b071

File tree

13 files changed

+427
-37
lines changed

13 files changed

+427
-37
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.mongodb.async.SingleResultCallback;
2020
import com.mongodb.client.model.Collation;
21+
import org.bson.conversions.Bson;
2122

2223
import java.util.concurrent.TimeUnit;
2324

@@ -117,4 +118,24 @@ public interface AggregateIterable<TResult> extends MongoIterable<TResult> {
117118
* @mongodb.server.release 3.4
118119
*/
119120
AggregateIterable<TResult> collation(Collation collation);
121+
122+
/**
123+
* Sets the comment to the aggregation. A null value means no comment is set.
124+
*
125+
* @param comment the comment
126+
* @return this
127+
* @since 3.6
128+
* @mongodb.server.release 3.6
129+
*/
130+
AggregateIterable<TResult> comment(String comment);
131+
132+
/**
133+
* Sets the hint for which index to use. A null value means no hint is set.
134+
*
135+
* @param hint the hint
136+
* @return this
137+
* @since 3.6
138+
* @mongodb.server.release 3.6
139+
*/
140+
AggregateIterable<TResult> hint(Bson hint);
120141
}

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class AggregateIterableImpl<TDocument, TResult> implements AggregateIterable<TRe
6262
private Boolean useCursor;
6363
private Boolean bypassDocumentValidation;
6464
private Collation collation;
65+
private String comment;
66+
private Bson hint;
6567

6668
AggregateIterableImpl(final MongoNamespace namespace, final Class<TDocument> documentClass, final Class<TResult> resultClass,
6769
final CodecRegistry codecRegistry, final ReadPreference readPreference, final ReadConcern readConcern,
@@ -123,7 +125,9 @@ public void toCollection(final SingleResultCallback<Void> callback) {
123125
executor.execute(new AggregateToCollectionOperation(namespace, aggregateList, writeConcern)
124126
.maxTime(maxTimeMS, MILLISECONDS)
125127
.allowDiskUse(allowDiskUse)
126-
.collation(collation), callback);
128+
.collation(collation)
129+
.hint(hint == null ? null : hint.toBsonDocument(documentClass, codecRegistry))
130+
.comment(comment), callback);
127131
}
128132

129133
@Override
@@ -169,6 +173,18 @@ public AggregateIterable<TResult> collation(final Collation collation) {
169173
return this;
170174
}
171175

176+
@Override
177+
public AggregateIterable<TResult> comment(final String comment) {
178+
this.comment = comment;
179+
return this;
180+
}
181+
182+
@Override
183+
public AggregateIterable<TResult> hint(final Bson hint) {
184+
this.hint = hint;
185+
return this;
186+
}
187+
172188
@SuppressWarnings("deprecation")
173189
private MongoIterable<TResult> execute() {
174190
List<BsonDocument> aggregateList = createBsonDocumentList();
@@ -179,7 +195,9 @@ private MongoIterable<TResult> execute() {
179195
.maxTime(maxTimeMS, MILLISECONDS)
180196
.allowDiskUse(allowDiskUse)
181197
.bypassDocumentValidation(bypassDocumentValidation)
182-
.collation(collation);
198+
.collation(collation)
199+
.hint(hint == null ? null : hint.toBsonDocument(documentClass, codecRegistry))
200+
.comment(comment);
183201
MongoIterable<TResult> delegated = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(namespace.getDatabaseName(),
184202
outCollection.asString().getValue()), documentClass, resultClass, codecRegistry, primary(), readConcern,
185203
executor, new BsonDocument(), new FindOptions().collation(collation).maxAwaitTime(maxAwaitTimeMS, MILLISECONDS));
@@ -195,7 +213,9 @@ private MongoIterable<TResult> execute() {
195213
.batchSize(batchSize)
196214
.useCursor(useCursor)
197215
.readConcern(readConcern)
198-
.collation(collation),
216+
.collation(collation)
217+
.hint(hint == null ? null : hint.toBsonDocument(documentClass, codecRegistry))
218+
.comment(comment),
199219
readPreference,
200220
executor);
201221
}

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class AggregateIterableSpecification extends Specification {
8484
.maxAwaitTime(99, MILLISECONDS)
8585
.maxTime(999, MILLISECONDS)
8686
.collation(collation)
87+
.hint(new Document('a', 1))
88+
.comment('this is a comment')
8789
.useCursor(true)
8890
.into([]) { result, t -> }
8991

@@ -95,8 +97,10 @@ class AggregateIterableSpecification extends Specification {
9597
.maxAwaitTime(99, MILLISECONDS)
9698
.maxTime(999, MILLISECONDS)
9799
.useCursor(true)
98-
.collation(collation))
99-
100+
.collation(collation)
101+
.hint(new BsonDocument('a', new BsonInt32(1)))
102+
.comment('this is a comment')
103+
)
100104
}
101105

102106
def 'should build the expected AggregateToCollectionOperation'() {
@@ -119,7 +123,10 @@ class AggregateIterableSpecification extends Specification {
119123
.maxTime(999, MILLISECONDS)
120124
.allowDiskUse(true)
121125
.useCursor(true)
122-
.collation(collation).into([]) { result, t -> }
126+
.collation(collation)
127+
.hint(new Document('a', 1))
128+
.comment('this is a comment')
129+
.into([]) { result, t -> }
123130

124131
def operation = executor.getWriteOperation() as AggregateToCollectionOperation
125132

@@ -128,7 +135,9 @@ class AggregateIterableSpecification extends Specification {
128135
[new BsonDocument('$match', new BsonInt32(1)), new BsonDocument('$out', new BsonString(collectionName))], writeConcern)
129136
.maxTime(999, MILLISECONDS)
130137
.allowDiskUse(true)
131-
.collation(collation))
138+
.collation(collation)
139+
.hint(new BsonDocument('a', new BsonInt32(1)))
140+
.comment('this is a comment'))
132141

133142
when: 'the subsequent read should have the batchSize set'
134143
operation = executor.getReadOperation() as FindOperation<Document>
@@ -144,6 +153,8 @@ class AggregateIterableSpecification extends Specification {
144153
pipeline)
145154
.allowDiskUse(true)
146155
.collation(collation)
156+
.hint(new Document('a', 1))
157+
.comment('this is a comment')
147158
.toCollection(futureResultCallback);
148159
futureResultCallback.get()
149160

@@ -152,7 +163,10 @@ class AggregateIterableSpecification extends Specification {
152163
then:
153164
expect operation, isTheSameAs(new AggregateToCollectionOperation(namespace,
154165
[new BsonDocument('$match', new BsonInt32(1)), new BsonDocument('$out', new BsonString(collectionName))], writeConcern)
155-
.allowDiskUse(true).collation(collation))
166+
.allowDiskUse(true)
167+
.collation(collation)
168+
.hint(new BsonDocument('a', new BsonInt32(1)))
169+
.comment('this is a comment'))
156170
}
157171

158172
def 'should handle exceptions correctly'() {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class AggregateExplainOperation implements AsyncReadOperation<BsonDocument>, Rea
5252
private Boolean allowDiskUse;
5353
private long maxTimeMS;
5454
private Collation collation;
55+
private BsonDocument hint;
5556

5657
AggregateExplainOperation(final MongoNamespace namespace, final List<BsonDocument> pipeline) {
5758
this.namespace = notNull("namespace", namespace);
@@ -100,6 +101,30 @@ public AggregateExplainOperation collation(final Collation collation) {
100101
return this;
101102
}
102103

104+
/**
105+
* Returns the hint for which index to use. The default is not to set a hint.
106+
*
107+
* @return the hint
108+
* @since 3.6
109+
* @mongodb.server.release 3.6
110+
*/
111+
public BsonDocument getHint() {
112+
return hint;
113+
}
114+
115+
/**
116+
* Sets the hint for which index to use. A null value means no hint is set.
117+
*
118+
* @param hint the hint
119+
* @return this
120+
* @since 3.6
121+
* @mongodb.server.release 3.6
122+
*/
123+
public AggregateExplainOperation hint(final BsonDocument hint) {
124+
this.hint = hint;
125+
return this;
126+
}
127+
103128
@Override
104129
public BsonDocument execute(final ReadBinding binding) {
105130
return withConnection(binding, new CallableWithConnection<BsonDocument>() {
@@ -150,6 +175,9 @@ private BsonDocument getCommand() {
150175
if (collation != null) {
151176
commandDocument.put("collation", collation.asDocument());
152177
}
178+
if (hint != null) {
179+
commandDocument.put("hint", hint);
180+
}
153181
return commandDocument;
154182
}
155183

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public class AggregateOperation<T> implements AsyncReadOperation<AsyncBatchCurso
8080
private Boolean allowDiskUse;
8181
private Integer batchSize;
8282
private Collation collation;
83+
private String comment;
84+
private BsonDocument hint;
8385
private long maxAwaitTimeMS;
8486
private long maxTimeMS;
8587
private Boolean useCursor;
@@ -297,6 +299,54 @@ public AggregateOperation<T> collation(final Collation collation) {
297299
return this;
298300
}
299301

302+
/**
303+
* Returns the comment to send with the aggregate. The default is not to include a comment with the aggregation.
304+
*
305+
* @return the comment
306+
* @since 3.6
307+
* @mongodb.server.release 3.6
308+
*/
309+
public String getComment() {
310+
return comment;
311+
}
312+
313+
/**
314+
* Sets the comment to the aggregation. A null value means no comment is set.
315+
*
316+
* @param comment the comment
317+
* @return this
318+
* @since 3.6
319+
* @mongodb.server.release 3.6
320+
*/
321+
public AggregateOperation<T> comment(final String comment) {
322+
this.comment = comment;
323+
return this;
324+
}
325+
326+
/**
327+
* Returns the hint for which index to use. The default is not to set a hint.
328+
*
329+
* @return the hint
330+
* @since 3.6
331+
* @mongodb.server.release 3.6
332+
*/
333+
public BsonDocument getHint() {
334+
return hint;
335+
}
336+
337+
/**
338+
* Sets the hint for which index to use. A null value means no hint is set.
339+
*
340+
* @param hint the hint
341+
* @return this
342+
* @since 3.6
343+
* @mongodb.server.release 3.6
344+
*/
345+
public AggregateOperation<T> hint(final BsonDocument hint) {
346+
this.hint = hint;
347+
return this;
348+
}
349+
300350
@Override
301351
public BatchCursor<T> execute(final ReadBinding binding) {
302352
return withConnection(binding, new CallableWithConnectionAndSource<BatchCursor<T>>() {
@@ -350,7 +400,8 @@ public void call(final AsyncConnectionSource source, final AsyncConnection conne
350400
public ReadOperation<BsonDocument> asExplainableOperation(final ExplainVerbosity explainVerbosity) {
351401
return new AggregateExplainOperation(namespace, pipeline)
352402
.allowDiskUse(allowDiskUse)
353-
.maxTime(maxTimeMS, TimeUnit.MILLISECONDS);
403+
.maxTime(maxTimeMS, TimeUnit.MILLISECONDS)
404+
.hint(hint);
354405
}
355406

356407
/**
@@ -390,7 +441,12 @@ private BsonDocument getCommand(final ConnectionDescription description, final S
390441
if (collation != null) {
391442
commandDocument.put("collation", collation.asDocument());
392443
}
393-
444+
if (comment != null) {
445+
commandDocument.put("comment", new BsonString(comment));
446+
}
447+
if (hint != null) {
448+
commandDocument.put("hint", hint);
449+
}
394450
return commandDocument;
395451
}
396452

@@ -435,6 +491,8 @@ public String toString() {
435491
+ ", allowDiskUse=" + allowDiskUse
436492
+ ", batchSize=" + batchSize
437493
+ ", collation=" + collation
494+
+ ", comment=" + comment
495+
+ ", hint=" + hint
438496
+ ", maxAwaitTimeMS=" + maxAwaitTimeMS
439497
+ ", maxTimeMS=" + maxTimeMS
440498
+ ", useCursor=" + useCursor

0 commit comments

Comments
 (0)