32
32
* @author Oliver Gierke
33
33
* @author Christoph Strobl
34
34
* @author Mark Paluch
35
+ * @author Yadhukrishna S Pai
35
36
* @see Aggregation#withOptions(AggregationOptions)
36
37
* @see TypedAggregation#withOptions(AggregationOptions)
37
38
* @since 1.6
@@ -45,12 +46,14 @@ public class AggregationOptions {
45
46
private static final String COLLATION = "collation" ;
46
47
private static final String COMMENT = "comment" ;
47
48
private static final String MAX_TIME = "maxTimeMS" ;
49
+ private static final String HINT = "hint" ;
48
50
49
51
private final boolean allowDiskUse ;
50
52
private final boolean explain ;
51
53
private final Optional <Document > cursor ;
52
54
private final Optional <Collation > collation ;
53
55
private final Optional <String > comment ;
56
+ private final Optional <Document > hint ;
54
57
private Duration maxTime = Duration .ZERO ;
55
58
private ResultOptions resultOptions = ResultOptions .READ ;
56
59
@@ -77,7 +80,7 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, Document cursor
77
80
*/
78
81
public AggregationOptions (boolean allowDiskUse , boolean explain , @ Nullable Document cursor ,
79
82
@ Nullable Collation collation ) {
80
- this (allowDiskUse , explain , cursor , collation , null );
83
+ this (allowDiskUse , explain , cursor , collation , null , null );
81
84
}
82
85
83
86
/**
@@ -89,16 +92,18 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
89
92
* aggregation.
90
93
* @param collation collation for string comparison. Can be {@literal null}.
91
94
* @param comment execution comment. Can be {@literal null}.
95
+ * @param hint can be {@literal null}, used to provide an index that would be forcibly used by query optimizer.
92
96
* @since 2.2
93
97
*/
94
98
public AggregationOptions (boolean allowDiskUse , boolean explain , @ Nullable Document cursor ,
95
- @ Nullable Collation collation , @ Nullable String comment ) {
99
+ @ Nullable Collation collation , @ Nullable String comment , @ Nullable Document hint ) {
96
100
97
101
this .allowDiskUse = allowDiskUse ;
98
102
this .explain = explain ;
99
103
this .cursor = Optional .ofNullable (cursor );
100
104
this .collation = Optional .ofNullable (collation );
101
105
this .comment = Optional .ofNullable (comment );
106
+ this .hint = Optional .ofNullable (hint );
102
107
}
103
108
104
109
/**
@@ -130,8 +135,9 @@ public static AggregationOptions fromDocument(Document document) {
130
135
Collation collation = document .containsKey (COLLATION ) ? Collation .from (document .get (COLLATION , Document .class ))
131
136
: null ;
132
137
String comment = document .getString (COMMENT );
138
+ Document hint = document .get (HINT , Document .class );
133
139
134
- AggregationOptions options = new AggregationOptions (allowDiskUse , explain , cursor , collation , comment );
140
+ AggregationOptions options = new AggregationOptions (allowDiskUse , explain , cursor , collation , comment , hint );
135
141
if (document .containsKey (MAX_TIME )) {
136
142
options .maxTime = Duration .ofMillis (document .getLong (MAX_TIME ));
137
143
}
@@ -212,6 +218,16 @@ public Optional<String> getComment() {
212
218
return comment ;
213
219
}
214
220
221
+ /**
222
+ * Get the hint used to to fulfill the aggregation.
223
+ *
224
+ * @return never {@literal null}.
225
+ */
226
+ public Optional <Document > getHint () {
227
+ return hint ;
228
+ }
229
+
230
+
215
231
/**
216
232
* @return the time limit for processing. {@link Duration#ZERO} is used for the default unbounded behavior.
217
233
* @since 3.0
@@ -248,6 +264,10 @@ Document applyAndReturnPotentiallyChangedCommand(Document command) {
248
264
result .put (EXPLAIN , explain );
249
265
}
250
266
267
+ if (result .containsKey (HINT )) {
268
+ hint .ifPresent (val -> result .append (HINT , val ));
269
+ }
270
+
251
271
if (!result .containsKey (CURSOR )) {
252
272
cursor .ifPresent (val -> result .put (CURSOR , val ));
253
273
}
@@ -277,6 +297,7 @@ public Document toDocument() {
277
297
cursor .ifPresent (val -> document .put (CURSOR , val ));
278
298
collation .ifPresent (val -> document .append (COLLATION , val .toDocument ()));
279
299
comment .ifPresent (val -> document .append (COMMENT , val ));
300
+ hint .ifPresent (val -> document .append (HINT , val ));
280
301
281
302
if (hasExecutionTimeLimit ()) {
282
303
document .append (MAX_TIME , maxTime .toMillis ());
@@ -318,6 +339,7 @@ public static class Builder {
318
339
private @ Nullable Document cursor ;
319
340
private @ Nullable Collation collation ;
320
341
private @ Nullable String comment ;
342
+ private @ Nullable Document hint ;
321
343
private @ Nullable Duration maxTime ;
322
344
private @ Nullable ResultOptions resultOptions ;
323
345
@@ -396,6 +418,19 @@ public Builder comment(@Nullable String comment) {
396
418
return this ;
397
419
}
398
420
421
+ /**
422
+ * Define a hint is used forcibly by query optimizer to to fulfill the aggregation.
423
+ *
424
+ * @param hint can be {@literal null}.
425
+ * @return this.
426
+ * @since 2.2
427
+ */
428
+ public Builder hint (@ Nullable Document hint ) {
429
+
430
+ this .hint = hint ;
431
+ return this ;
432
+ }
433
+
399
434
/**
400
435
* Set the time limit for processing.
401
436
*
@@ -431,7 +466,13 @@ public Builder skipOutput() {
431
466
*/
432
467
public AggregationOptions build () {
433
468
434
- AggregationOptions options = new AggregationOptions (allowDiskUse , explain , cursor , collation , comment );
469
+ AggregationOptions options = new AggregationOptions (
470
+ allowDiskUse ,
471
+ explain ,
472
+ cursor ,
473
+ collation ,
474
+ comment ,
475
+ hint );
435
476
if (maxTime != null ) {
436
477
options .maxTime = maxTime ;
437
478
}
0 commit comments