Browse Source

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.

Closes: #4664
Original Pull Request: #4666
pull/4668/head
Mark Paluch 2 years ago committed by Christoph Strobl
parent
commit
52ba939bd7
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 14
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  2. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java
  3. 36
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOptions.java
  4. 16
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationOptionsTests.java

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

@ -2191,8 +2191,11 @@ public class MongoTemplate
.getCollation()); .getCollation());
AggregateIterable<Document> aggregateIterable = delegate.prepare(collection).aggregate(pipeline, Document.class) // AggregateIterable<Document> aggregateIterable = delegate.prepare(collection).aggregate(pipeline, Document.class) //
.collation(collation.map(Collation::toMongoCollation).orElse(null)) // .collation(collation.map(Collation::toMongoCollation).orElse(null));
.allowDiskUse(options.isAllowDiskUse());
if (options.isAllowDiskUseSet()) {
aggregateIterable = aggregateIterable.allowDiskUse(options.isAllowDiskUse());
}
if (options.getCursorBatchSize() != null) { if (options.getCursorBatchSize() != null) {
aggregateIterable = aggregateIterable.batchSize(options.getCursorBatchSize()); aggregateIterable = aggregateIterable.batchSize(options.getCursorBatchSize());
@ -2255,8 +2258,11 @@ public class MongoTemplate
CollectionPreparerDelegate delegate = CollectionPreparerDelegate.of(options); CollectionPreparerDelegate delegate = CollectionPreparerDelegate.of(options);
AggregateIterable<Document> cursor = delegate.prepare(collection).aggregate(pipeline, Document.class) // AggregateIterable<Document> cursor = delegate.prepare(collection).aggregate(pipeline, Document.class);
.allowDiskUse(options.isAllowDiskUse());
if (options.isAllowDiskUseSet()) {
cursor = cursor.allowDiskUse(options.isAllowDiskUse());
}
if (options.getCursorBatchSize() != null) { if (options.getCursorBatchSize() != null) {
cursor = cursor.batchSize(options.getCursorBatchSize()); cursor = cursor.batchSize(options.getCursorBatchSize());

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

@ -1027,8 +1027,11 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
@Nullable Class<?> inputType) { @Nullable Class<?> inputType) {
ReactiveCollectionPreparerDelegate collectionPreparer = ReactiveCollectionPreparerDelegate.of(options); ReactiveCollectionPreparerDelegate collectionPreparer = ReactiveCollectionPreparerDelegate.of(options);
AggregatePublisher<Document> cursor = collectionPreparer.prepare(collection).aggregate(pipeline, Document.class) AggregatePublisher<Document> cursor = collectionPreparer.prepare(collection).aggregate(pipeline, Document.class);
.allowDiskUse(options.isAllowDiskUse());
if (options.isAllowDiskUseSet()) {
cursor = cursor.allowDiskUse(options.isAllowDiskUse());
}
if (options.getCursorBatchSize() != null) { if (options.getCursorBatchSize() != null) {
cursor = cursor.batchSize(options.getCursorBatchSize()); cursor = cursor.batchSize(options.getCursorBatchSize());

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

@ -55,7 +55,7 @@ public class AggregationOptions implements ReadConcernAware, ReadPreferenceAware
private static final String MAX_TIME = "maxTimeMS"; private static final String MAX_TIME = "maxTimeMS";
private static final String HINT = "hint"; private static final String HINT = "hint";
private final boolean allowDiskUse; private final Optional<Boolean> allowDiskUse;
private final boolean explain; private final boolean explain;
private final Optional<Document> cursor; private final Optional<Document> cursor;
private final Optional<Collation> collation; private final Optional<Collation> collation;
@ -123,10 +123,10 @@ public class AggregationOptions implements ReadConcernAware, ReadPreferenceAware
* @param hint can be {@literal null}, used to provide an index that would be forcibly used by query optimizer. * @param hint can be {@literal null}, used to provide an index that would be forcibly used by query optimizer.
* @since 3.1 * @since 3.1
*/ */
private AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Document cursor, private AggregationOptions(@Nullable Boolean allowDiskUse, boolean explain, @Nullable Document cursor,
@Nullable Collation collation, @Nullable String comment, @Nullable Object hint) { @Nullable Collation collation, @Nullable String comment, @Nullable Object hint) {
this.allowDiskUse = allowDiskUse; this.allowDiskUse = Optional.ofNullable(allowDiskUse);
this.explain = explain; this.explain = explain;
this.cursor = Optional.ofNullable(cursor); this.cursor = Optional.ofNullable(cursor);
this.collation = Optional.ofNullable(collation); this.collation = Optional.ofNullable(collation);
@ -159,7 +159,7 @@ public class AggregationOptions implements ReadConcernAware, ReadPreferenceAware
Assert.notNull(document, "Document must not be null"); Assert.notNull(document, "Document must not be null");
boolean allowDiskUse = document.getBoolean(ALLOW_DISK_USE, false); Boolean allowDiskUse = document.get(ALLOW_DISK_USE, Boolean.class);
boolean explain = document.getBoolean(EXPLAIN, false); boolean explain = document.getBoolean(EXPLAIN, false);
Document cursor = document.get(CURSOR, Document.class); Document cursor = document.get(CURSOR, Document.class);
Collation collation = document.containsKey(COLLATION) ? Collation.from(document.get(COLLATION, Document.class)) Collation collation = document.containsKey(COLLATION) ? Collation.from(document.get(COLLATION, Document.class))
@ -185,13 +185,23 @@ public class AggregationOptions implements ReadConcernAware, ReadPreferenceAware
} }
/** /**
* Enables writing to temporary files. When set to true, aggregation stages can write data to the _tmp subdirectory in * Enables writing to temporary files. When set to {@literal true}, aggregation stages can write data to the
* the dbPath directory. * {@code _tmp} subdirectory in the {@code dbPath} directory.
* *
* @return {@literal true} if enabled. * @return {@literal true} if enabled; {@literal false} otherwise (or if not set).
*/ */
public boolean isAllowDiskUse() { public boolean isAllowDiskUse() {
return allowDiskUse; return allowDiskUse.orElse(false);
}
/**
* Return whether {@link #isAllowDiskUse} is configured.
*
* @return {@literal true} if is {@code allowDiskUse} is configured, {@literal false} otherwise.
* @since 4.2.5
*/
public boolean isAllowDiskUseSet() {
return allowDiskUse.isPresent();
} }
/** /**
@ -335,8 +345,8 @@ public class AggregationOptions implements ReadConcernAware, ReadPreferenceAware
Document result = new Document(command); Document result = new Document(command);
if (allowDiskUse && !result.containsKey(ALLOW_DISK_USE)) { if (isAllowDiskUseSet() && !result.containsKey(ALLOW_DISK_USE)) {
result.put(ALLOW_DISK_USE, allowDiskUse); result.put(ALLOW_DISK_USE, isAllowDiskUse());
} }
if (explain && !result.containsKey(EXPLAIN)) { if (explain && !result.containsKey(EXPLAIN)) {
@ -370,7 +380,9 @@ public class AggregationOptions implements ReadConcernAware, ReadPreferenceAware
public Document toDocument() { public Document toDocument() {
Document document = new Document(); Document document = new Document();
document.put(ALLOW_DISK_USE, allowDiskUse); if (isAllowDiskUseSet()) {
document.put(ALLOW_DISK_USE, isAllowDiskUse());
}
document.put(EXPLAIN, explain); document.put(EXPLAIN, explain);
cursor.ifPresent(val -> document.put(CURSOR, val)); cursor.ifPresent(val -> document.put(CURSOR, val));
@ -410,7 +422,7 @@ public class AggregationOptions implements ReadConcernAware, ReadPreferenceAware
*/ */
public static class Builder { public static class Builder {
private boolean allowDiskUse; private Boolean allowDiskUse;
private boolean explain; private boolean explain;
private @Nullable Document cursor; private @Nullable Document cursor;
private @Nullable Collation collation; private @Nullable Collation collation;

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

@ -77,6 +77,22 @@ class AggregationOptionsTests {
assertThat(aggregationOptions.getHintObject()).contains(dummyHint); assertThat(aggregationOptions.getHintObject()).contains(dummyHint);
} }
@Test // GH-4664
void omitsAllowDiskUseByDefault() {
aggregationOptions = AggregationOptions.fromDocument(new Document());
assertThat(aggregationOptions.isAllowDiskUse()).isFalse();
assertThat(aggregationOptions.isAllowDiskUseSet()).isFalse();
assertThat(aggregationOptions.toDocument()).doesNotContainKey("allowDiskUse");
Document empty = new Document();
aggregationOptions.applyAndReturnPotentiallyChangedCommand(empty);
assertThat(empty).doesNotContainKey("allowDiskUse");
}
@Test // DATAMONGO-960, DATAMONGO-2153, DATAMONGO-1836 @Test // DATAMONGO-960, DATAMONGO-2153, DATAMONGO-1836
void aggregationOptionsToString() { void aggregationOptionsToString() {

Loading…
Cancel
Save