Skip to content

Commit 8fdad82

Browse files
committed
Do not set AggregationOptions.allowDiskUse by default.
With MongoDB changing its default, we now no longer set allowDiskUse to false if the value is not configured.
1 parent f664dc0 commit 8fdad82

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -2191,8 +2191,11 @@ protected <O> AggregationResults<O> doAggregate(Aggregation aggregation, String
21912191
.getCollation());
21922192

21932193
AggregateIterable<Document> aggregateIterable = delegate.prepare(collection).aggregate(pipeline, Document.class) //
2194-
.collation(collation.map(Collation::toMongoCollation).orElse(null)) //
2195-
.allowDiskUse(options.isAllowDiskUse());
2194+
.collation(collation.map(Collation::toMongoCollation).orElse(null));
2195+
2196+
if (options.isAllowDiskUseSet()) {
2197+
aggregateIterable = aggregateIterable.allowDiskUse(options.isAllowDiskUse());
2198+
}
21962199

21972200
if (options.getCursorBatchSize() != null) {
21982201
aggregateIterable = aggregateIterable.batchSize(options.getCursorBatchSize());
@@ -2255,8 +2258,11 @@ protected <O> Stream<O> aggregateStream(Aggregation aggregation, String collecti
22552258

22562259
CollectionPreparerDelegate delegate = CollectionPreparerDelegate.of(options);
22572260

2258-
AggregateIterable<Document> cursor = delegate.prepare(collection).aggregate(pipeline, Document.class) //
2259-
.allowDiskUse(options.isAllowDiskUse());
2261+
AggregateIterable<Document> cursor = delegate.prepare(collection).aggregate(pipeline, Document.class);
2262+
2263+
if (options.isAllowDiskUseSet()) {
2264+
cursor = cursor.allowDiskUse(options.isAllowDiskUse());
2265+
}
22602266

22612267
if (options.getCursorBatchSize() != null) {
22622268
cursor = cursor.batchSize(options.getCursorBatchSize());

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1027,8 +1027,11 @@ private <O> Flux<O> aggregateAndMap(MongoCollection<Document> collection, List<D
10271027
@Nullable Class<?> inputType) {
10281028

10291029
ReactiveCollectionPreparerDelegate collectionPreparer = ReactiveCollectionPreparerDelegate.of(options);
1030-
AggregatePublisher<Document> cursor = collectionPreparer.prepare(collection).aggregate(pipeline, Document.class)
1031-
.allowDiskUse(options.isAllowDiskUse());
1030+
AggregatePublisher<Document> cursor = collectionPreparer.prepare(collection).aggregate(pipeline, Document.class);
1031+
1032+
if (options.isAllowDiskUseSet()) {
1033+
cursor = cursor.allowDiskUse(options.isAllowDiskUse());
1034+
}
10321035

10331036
if (options.getCursorBatchSize() != null) {
10341037
cursor = cursor.batchSize(options.getCursorBatchSize());

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

+24-12
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class AggregationOptions implements ReadConcernAware, ReadPreferenceAware
5555
private static final String MAX_TIME = "maxTimeMS";
5656
private static final String HINT = "hint";
5757

58-
private final boolean allowDiskUse;
58+
private final Optional<Boolean> allowDiskUse;
5959
private final boolean explain;
6060
private final Optional<Document> cursor;
6161
private final Optional<Collation> collation;
@@ -123,10 +123,10 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
123123
* @param hint can be {@literal null}, used to provide an index that would be forcibly used by query optimizer.
124124
* @since 3.1
125125
*/
126-
private AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Document cursor,
126+
private AggregationOptions(@Nullable Boolean allowDiskUse, boolean explain, @Nullable Document cursor,
127127
@Nullable Collation collation, @Nullable String comment, @Nullable Object hint) {
128128

129-
this.allowDiskUse = allowDiskUse;
129+
this.allowDiskUse = Optional.ofNullable(allowDiskUse);
130130
this.explain = explain;
131131
this.cursor = Optional.ofNullable(cursor);
132132
this.collation = Optional.ofNullable(collation);
@@ -159,7 +159,7 @@ public static AggregationOptions fromDocument(Document document) {
159159

160160
Assert.notNull(document, "Document must not be null");
161161

162-
boolean allowDiskUse = document.getBoolean(ALLOW_DISK_USE, false);
162+
Boolean allowDiskUse = document.get(ALLOW_DISK_USE, Boolean.class);
163163
boolean explain = document.getBoolean(EXPLAIN, false);
164164
Document cursor = document.get(CURSOR, Document.class);
165165
Collation collation = document.containsKey(COLLATION) ? Collation.from(document.get(COLLATION, Document.class))
@@ -185,13 +185,23 @@ public static Builder builder() {
185185
}
186186

187187
/**
188-
* Enables writing to temporary files. When set to true, aggregation stages can write data to the _tmp subdirectory in
189-
* the dbPath directory.
188+
* Enables writing to temporary files. When set to {@literal true}, aggregation stages can write data to the
189+
* {@code _tmp} subdirectory in the {@code dbPath} directory.
190190
*
191-
* @return {@literal true} if enabled.
191+
* @return {@literal true} if enabled; {@literal false} otherwise (or if not set).
192192
*/
193193
public boolean isAllowDiskUse() {
194-
return allowDiskUse;
194+
return allowDiskUse.orElse(false);
195+
}
196+
197+
/**
198+
* Return whether {@link #isAllowDiskUse} is configured.
199+
*
200+
* @return {@literal true} if is {@code allowDiskUse} is configured, {@literal false} otherwise.
201+
* @since 4.2.5
202+
*/
203+
public boolean isAllowDiskUseSet() {
204+
return allowDiskUse.isPresent();
195205
}
196206

197207
/**
@@ -335,8 +345,8 @@ Document applyAndReturnPotentiallyChangedCommand(Document command) {
335345

336346
Document result = new Document(command);
337347

338-
if (allowDiskUse && !result.containsKey(ALLOW_DISK_USE)) {
339-
result.put(ALLOW_DISK_USE, allowDiskUse);
348+
if (isAllowDiskUseSet() && !result.containsKey(ALLOW_DISK_USE)) {
349+
result.put(ALLOW_DISK_USE, isAllowDiskUse());
340350
}
341351

342352
if (explain && !result.containsKey(EXPLAIN)) {
@@ -370,7 +380,9 @@ Document applyAndReturnPotentiallyChangedCommand(Document command) {
370380
public Document toDocument() {
371381

372382
Document document = new Document();
373-
document.put(ALLOW_DISK_USE, allowDiskUse);
383+
if (isAllowDiskUseSet()) {
384+
document.put(ALLOW_DISK_USE, isAllowDiskUse());
385+
}
374386
document.put(EXPLAIN, explain);
375387

376388
cursor.ifPresent(val -> document.put(CURSOR, val));
@@ -410,7 +422,7 @@ static Document createCursor(int cursorBatchSize) {
410422
*/
411423
public static class Builder {
412424

413-
private boolean allowDiskUse;
425+
private Boolean allowDiskUse;
414426
private boolean explain;
415427
private @Nullable Document cursor;
416428
private @Nullable Collation collation;

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

+16
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ void shouldInitializeFromDocument() {
7777
assertThat(aggregationOptions.getHintObject()).contains(dummyHint);
7878
}
7979

80+
@Test // GH-4664
81+
void omitsAllowDiskUseByDefault() {
82+
83+
aggregationOptions = AggregationOptions.fromDocument(new Document());
84+
85+
assertThat(aggregationOptions.isAllowDiskUse()).isFalse();
86+
assertThat(aggregationOptions.isAllowDiskUseSet()).isFalse();
87+
88+
assertThat(aggregationOptions.toDocument()).doesNotContainKey("allowDiskUse");
89+
90+
Document empty = new Document();
91+
aggregationOptions.applyAndReturnPotentiallyChangedCommand(empty);
92+
93+
assertThat(empty).doesNotContainKey("allowDiskUse");
94+
}
95+
8096
@Test // DATAMONGO-960, DATAMONGO-2153, DATAMONGO-1836
8197
void aggregationOptionsToString() {
8298

0 commit comments

Comments
 (0)