Skip to content

Commit 5795a50

Browse files
committed
DATAMONGO-1836 - Polishing.
Revert constructor change of AggregationOptions to not break existing code. Update since tags. Reformat code. Align visibility of AggregationOptionsTests with JUnit 5 rules. Update documentation. Original pull request: #878.
1 parent 22bd3e6 commit 5795a50

File tree

6 files changed

+39
-35
lines changed

6 files changed

+39
-35
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

-2
Original file line numberDiff line numberDiff line change
@@ -2149,7 +2149,6 @@ protected <O> AggregationResults<O> doAggregate(Aggregation aggregation, String
21492149
}
21502150

21512151
options.getComment().ifPresent(aggregateIterable::comment);
2152-
21532152
options.getHint().ifPresent(aggregateIterable::hint);
21542153

21552154
if (options.hasExecutionTimeLimit()) {
@@ -2209,7 +2208,6 @@ protected <O> CloseableIterator<O> aggregateStream(Aggregation aggregation, Stri
22092208
}
22102209

22112210
options.getComment().ifPresent(cursor::comment);
2212-
22132211
options.getHint().ifPresent(cursor::hint);
22142212

22152213
Class<?> domainType = aggregation instanceof TypedAggregation ? ((TypedAggregation) aggregation).getInputType()

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java

-1
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,6 @@ private <O> Flux<O> aggregateAndMap(MongoCollection<Document> collection, List<D
10241024
}
10251025

10261026
options.getComment().ifPresent(cursor::comment);
1027-
10281027
options.getHint().ifPresent(cursor::hint);
10291028

10301029
Optionals.firstNonEmpty(options::getCollation, () -> operations.forType(inputType).getCollation()) //

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOptions.java

+24-14
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, Document cursor
7474
* @param allowDiskUse whether to off-load intensive sort-operations to disk.
7575
* @param explain whether to get the execution plan for the aggregation instead of the actual results.
7676
* @param cursor can be {@literal null}, used to pass additional options (such as {@code batchSize}) to the
77-
* aggregation.
77+
* aggregation.
7878
* @param collation collation for string comparison. Can be {@literal null}.
7979
* @since 2.0
8080
*/
@@ -89,13 +89,29 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
8989
* @param allowDiskUse whether to off-load intensive sort-operations to disk.
9090
* @param explain whether to get the execution plan for the aggregation instead of the actual results.
9191
* @param cursor can be {@literal null}, used to pass additional options (such as {@code batchSize}) to the
92-
* aggregation.
92+
* aggregation.
9393
* @param collation collation for string comparison. Can be {@literal null}.
9494
* @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.
9695
* @since 2.2
9796
*/
9897
public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Document cursor,
98+
@Nullable Collation collation, @Nullable String comment) {
99+
this(allowDiskUse, explain, cursor, collation, comment, null);
100+
}
101+
102+
/**
103+
* Creates a new {@link AggregationOptions}.
104+
*
105+
* @param allowDiskUse whether to off-load intensive sort-operations to disk.
106+
* @param explain whether to get the execution plan for the aggregation instead of the actual results.
107+
* @param cursor can be {@literal null}, used to pass additional options (such as {@code batchSize}) to the
108+
* aggregation.
109+
* @param collation collation for string comparison. Can be {@literal null}.
110+
* @param comment execution comment. Can be {@literal null}.
111+
* @param hint can be {@literal null}, used to provide an index that would be forcibly used by query optimizer.
112+
* @since 3.1
113+
*/
114+
private AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Document cursor,
99115
@Nullable Collation collation, @Nullable String comment, @Nullable Document hint) {
100116

101117
this.allowDiskUse = allowDiskUse;
@@ -222,12 +238,12 @@ public Optional<String> getComment() {
222238
* Get the hint used to to fulfill the aggregation.
223239
*
224240
* @return never {@literal null}.
241+
* @since 3.1
225242
*/
226243
public Optional<Document> getHint() {
227244
return hint;
228245
}
229246

230-
231247
/**
232248
* @return the time limit for processing. {@link Duration#ZERO} is used for the default unbounded behavior.
233249
* @since 3.0
@@ -419,11 +435,11 @@ public Builder comment(@Nullable String comment) {
419435
}
420436

421437
/**
422-
* Define a hint is used forcibly by query optimizer to to fulfill the aggregation.
438+
* Define a hint that is used by query optimizer to to fulfill the aggregation.
423439
*
424440
* @param hint can be {@literal null}.
425441
* @return this.
426-
* @since 2.2
442+
* @since 3.1
427443
*/
428444
public Builder hint(@Nullable Document hint) {
429445

@@ -435,7 +451,7 @@ public Builder hint(@Nullable Document hint) {
435451
* Set the time limit for processing.
436452
*
437453
* @param maxTime {@link Duration#ZERO} is used for the default unbounded behavior. {@link Duration#isNegative()
438-
* Negative} values will be ignored.
454+
* Negative} values will be ignored.
439455
* @return this.
440456
* @since 3.0
441457
*/
@@ -466,13 +482,7 @@ public Builder skipOutput() {
466482
*/
467483
public AggregationOptions build() {
468484

469-
AggregationOptions options = new AggregationOptions(
470-
allowDiskUse,
471-
explain,
472-
cursor,
473-
collation,
474-
comment,
475-
hint);
485+
AggregationOptions options = new AggregationOptions(allowDiskUse, explain, cursor, collation, comment, hint);
476486
if (maxTime != null) {
477487
options.maxTime = maxTime;
478488
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ void aggregateShouldHonorOptionsComment() {
631631

632632
@Test // DATAMONGO-1836
633633
void aggregateShouldHonorOptionsHint() {
634+
634635
Document hint = new Document("dummyHint", 1);
635636
AggregationOptions options = AggregationOptions.builder().hint(hint).build();
636637

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationOptionsTests.java

+13-18
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
* @author Yadhukrishna S Pai
3232
* @since 1.6
3333
*/
34-
public class AggregationOptionsTests {
34+
class AggregationOptionsTests {
3535

3636
private final Document dummyHint = new Document("dummyField", 1);
3737
AggregationOptions aggregationOptions;
3838

3939
@BeforeEach
40-
public void setup() {
40+
void setup() {
4141
aggregationOptions = newAggregationOptions().explain(true) //
4242
.cursorBatchSize(1) //
4343
.allowDiskUse(true) //
@@ -47,16 +47,16 @@ public void setup() {
4747
}
4848

4949
@Test // DATAMONGO-960, DATAMONGO-1836
50-
public void aggregationOptionsBuilderShouldSetOptionsAccordingly() {
50+
void aggregationOptionsBuilderShouldSetOptionsAccordingly() {
5151

5252
assertThat(aggregationOptions.isAllowDiskUse()).isTrue();
5353
assertThat(aggregationOptions.isExplain()).isTrue();
54-
assertThat(aggregationOptions.getCursor().get()).isEqualTo(new Document("batchSize", 1));
55-
assertThat(aggregationOptions.getHint().get()).isEqualTo(dummyHint);
54+
assertThat(aggregationOptions.getCursor()).contains(new Document("batchSize", 1));
55+
assertThat(aggregationOptions.getHint()).contains(dummyHint);
5656
}
5757

5858
@Test // DATAMONGO-1637, DATAMONGO-2153, DATAMONGO-1836
59-
public void shouldInitializeFromDocument() {
59+
void shouldInitializeFromDocument() {
6060

6161
Document document = new Document();
6262
document.put("cursor", new Document("batchSize", 1));
@@ -69,22 +69,17 @@ public void shouldInitializeFromDocument() {
6969

7070
assertThat(aggregationOptions.isAllowDiskUse()).isTrue();
7171
assertThat(aggregationOptions.isExplain()).isTrue();
72-
assertThat(aggregationOptions.getCursor().get()).isEqualTo(new Document("batchSize", 1));
72+
assertThat(aggregationOptions.getCursor()).contains(new Document("batchSize", 1));
7373
assertThat(aggregationOptions.getCursorBatchSize()).isEqualTo(1);
74-
assertThat(aggregationOptions.getComment().get()).isEqualTo("hola!");
75-
assertThat(aggregationOptions.getHint().get()).isEqualTo(dummyHint);
74+
assertThat(aggregationOptions.getComment()).contains("hola!");
75+
assertThat(aggregationOptions.getHint()).contains(dummyHint);
7676
}
7777

7878
@Test // DATAMONGO-960, DATAMONGO-2153, DATAMONGO-1836
79-
public void aggregationOptionsToString() {
79+
void aggregationOptionsToString() {
8080

81-
assertThat(aggregationOptions.toDocument()).isEqualTo(Document.parse(
82-
"{ " +
83-
"\"allowDiskUse\" : true , " +
84-
"\"explain\" : true , " +
85-
"\"cursor\" : { \"batchSize\" : 1}, " +
86-
"\"comment\": \"hola!\", " +
87-
"\"hint\" : { \"dummyField\" : 1}" +
88-
"}"));
81+
assertThat(aggregationOptions.toDocument()).isEqualTo(Document
82+
.parse("{ " + "\"allowDiskUse\" : true , " + "\"explain\" : true , " + "\"cursor\" : { \"batchSize\" : 1}, "
83+
+ "\"comment\": \"hola!\", " + "\"hint\" : { \"dummyField\" : 1}" + "}"));
8984
}
9085
}

src/main/asciidoc/new-features.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
* <<mongo.auditing,Reactive auditing>> enabled through `@EnableReactiveMongoAuditing`. `@EnableMongoAuditing` no longer registers `ReactiveAuditingEntityCallback`.
88
* Reactive SpEL support in `@Query` and `@Aggregation` query methods.
9+
* Aggregation hints via `AggregationOptions.builder().hint(bson).build()`.
910

1011
[[new-features.3.0]]
1112
== What's New in Spring Data MongoDB 3.0

0 commit comments

Comments
 (0)