Skip to content

Commit 034bd8d

Browse files
Switch from boolean to String for allowDiskUse flag.
1 parent 24e0f54 commit 034bd8d

File tree

7 files changed

+99
-21
lines changed

7 files changed

+99
-21
lines changed

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

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.data.mongodb.core.ReadConcernAware;
2424
import org.springframework.data.mongodb.core.ReadPreferenceAware;
2525
import org.springframework.data.mongodb.core.query.Collation;
26+
import org.springframework.data.mongodb.core.query.DiskUse;
2627
import org.springframework.data.mongodb.util.BsonUtils;
2728
import org.springframework.lang.Contract;
2829
import org.springframework.util.Assert;
@@ -60,7 +61,7 @@ public class AggregationOptions implements ReadConcernAware, ReadPreferenceAware
6061
private static final String MAX_TIME = "maxTimeMS";
6162
private static final String HINT = "hint";
6263

63-
private final Optional<Boolean> allowDiskUse;
64+
private final DiskUse diskUse;
6465
private final boolean explain;
6566
private final Optional<Document> cursor;
6667
private final Optional<Collation> collation;
@@ -85,6 +86,15 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
8586
this(allowDiskUse, explain, cursor, null);
8687
}
8788

89+
public AggregationOptions(DiskUse diskUse, boolean explain, @Nullable Document cursor) {
90+
this(diskUse, explain, cursor, null);
91+
}
92+
93+
public AggregationOptions(DiskUse allowDiskUse, boolean explain, @Nullable Document cursor,
94+
@Nullable Collation collation) {
95+
this(allowDiskUse, explain, cursor, collation, null);
96+
}
97+
8898
/**
8999
* Creates a new {@link AggregationOptions}.
90100
*
@@ -97,7 +107,7 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
97107
*/
98108
public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Document cursor,
99109
@Nullable Collation collation) {
100-
this(allowDiskUse, explain, cursor, collation, null, null);
110+
this(DiskUse.of(allowDiskUse), explain, cursor, collation, null, null);
101111
}
102112

103113
/**
@@ -113,13 +123,18 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
113123
*/
114124
public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Document cursor,
115125
@Nullable Collation collation, @Nullable String comment) {
126+
this(allowDiskUse ? DiskUse.ALLOW : DiskUse.DENY, explain, cursor, collation, comment, null);
127+
}
128+
129+
public AggregationOptions(DiskUse allowDiskUse, boolean explain, @Nullable Document cursor,
130+
@Nullable Collation collation, @Nullable String comment) {
116131
this(allowDiskUse, explain, cursor, collation, comment, null);
117132
}
118133

119134
/**
120135
* Creates a new {@link AggregationOptions}.
121136
*
122-
* @param allowDiskUse whether to off-load intensive sort-operations to disk.
137+
* @param diskUse whether to off-load intensive sort-operations to disk.
123138
* @param explain whether to get the execution plan for the aggregation instead of the actual results.
124139
* @param cursor can be {@literal null}, used to pass additional options (such as {@code batchSize}) to the
125140
* aggregation.
@@ -128,10 +143,10 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
128143
* @param hint can be {@literal null}, used to provide an index that would be forcibly used by query optimizer.
129144
* @since 3.1
130145
*/
131-
private AggregationOptions(@Nullable Boolean allowDiskUse, boolean explain, @Nullable Document cursor,
146+
private AggregationOptions(DiskUse diskUse, boolean explain, @Nullable Document cursor,
132147
@Nullable Collation collation, @Nullable String comment, @Nullable Object hint) {
133148

134-
this.allowDiskUse = Optional.ofNullable(allowDiskUse);
149+
this.diskUse = diskUse;
135150
this.explain = explain;
136151
this.cursor = Optional.ofNullable(cursor);
137152
this.collation = Optional.ofNullable(collation);
@@ -172,7 +187,7 @@ public static AggregationOptions fromDocument(Document document) {
172187
String comment = document.getString(COMMENT);
173188
Document hint = document.get(HINT, Document.class);
174189

175-
AggregationOptions options = new AggregationOptions(allowDiskUse, explain, cursor, collation, comment, hint);
190+
AggregationOptions options = new AggregationOptions(DiskUse.of(allowDiskUse) , explain, cursor, collation, comment, hint);
176191
if (document.containsKey(MAX_TIME)) {
177192
options.maxTime = Duration.ofMillis(document.getLong(MAX_TIME));
178193
}
@@ -196,7 +211,7 @@ public static Builder builder() {
196211
* @return {@literal true} if enabled; {@literal false} otherwise (or if not set).
197212
*/
198213
public boolean isAllowDiskUse() {
199-
return allowDiskUse.orElse(false);
214+
return diskUse.equals(DiskUse.ALLOW);
200215
}
201216

202217
/**
@@ -206,7 +221,7 @@ public boolean isAllowDiskUse() {
206221
* @since 4.2.5
207222
*/
208223
public boolean isAllowDiskUseSet() {
209-
return allowDiskUse.isPresent();
224+
return !diskUse.equals(DiskUse.DEFAULT);
210225
}
211226

212227
/**
@@ -427,7 +442,7 @@ static Document createCursor(int cursorBatchSize) {
427442
*/
428443
public static class Builder {
429444

430-
private @Nullable Boolean allowDiskUse;
445+
private @Nullable DiskUse diskUse = DiskUse.DEFAULT;
431446
private boolean explain;
432447
private @Nullable Document cursor;
433448
private @Nullable Collation collation;
@@ -447,8 +462,19 @@ public static class Builder {
447462
*/
448463
@Contract("_ -> this")
449464
public Builder allowDiskUse(boolean allowDiskUse) {
465+
return diskUse(DiskUse.of(allowDiskUse));
466+
}
467+
468+
/**
469+
* Defines whether to off-load intensive sort-operations to disk.
470+
*
471+
* @param diskUse use {@literal true} to allow disk use during the aggregation.
472+
* @return this.
473+
*/
474+
@Contract("_ -> this")
475+
public Builder diskUse(DiskUse diskUse) {
450476

451-
this.allowDiskUse = allowDiskUse;
477+
this.diskUse = diskUse;
452478
return this;
453479
}
454480

@@ -655,7 +681,7 @@ public Builder noMapping() {
655681
@Contract("-> new")
656682
public AggregationOptions build() {
657683

658-
AggregationOptions options = new AggregationOptions(allowDiskUse, explain, cursor, collation, comment, hint);
684+
AggregationOptions options = new AggregationOptions(diskUse, explain, cursor, collation, comment, hint);
659685
if (maxTime != null) {
660686
options.maxTime = maxTime;
661687
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2025-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.query;
17+
18+
import org.jspecify.annotations.Nullable;
19+
import org.springframework.util.StringUtils;
20+
21+
/**
22+
* @author Christoph Strobl
23+
*/
24+
public enum DiskUse {
25+
26+
DEFAULT, ALLOW, DENY;
27+
28+
public static DiskUse of(@Nullable Boolean value) {
29+
return value != null ? (value ? ALLOW : DENY) : DEFAULT;
30+
}
31+
32+
public static DiskUse of(String value) {
33+
if (!StringUtils.hasText(value)) {
34+
return DEFAULT;
35+
}
36+
if (value.toLowerCase().equalsIgnoreCase("true")) {
37+
return ALLOW;
38+
}
39+
return valueOf(value.toUpperCase());
40+
}
41+
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private enum MetaKey {
5151
private Map<String, Object> values = Collections.emptyMap();
5252
private Set<CursorOption> flags = Collections.emptySet();
5353
private @Nullable Integer cursorBatchSize;
54-
private @Nullable Boolean allowDiskUse;
54+
private DiskUse diskUse = DiskUse.DEFAULT;
5555

5656
public Meta() {}
5757

@@ -66,7 +66,7 @@ public Meta() {}
6666
this.values = new LinkedHashMap<>(source.values);
6767
this.flags = new LinkedHashSet<>(source.flags);
6868
this.cursorBatchSize = source.cursorBatchSize;
69-
this.allowDiskUse = source.allowDiskUse;
69+
this.diskUse = source.diskUse;
7070
}
7171

7272
/**
@@ -230,7 +230,7 @@ public Set<CursorOption> getFlags() {
230230
*/
231231
@Nullable
232232
public Boolean getAllowDiskUse() {
233-
return allowDiskUse;
233+
return diskUse.equals(DiskUse.DEFAULT) ? null : diskUse.equals(DiskUse.ALLOW);
234234
}
235235

236236
/**
@@ -244,14 +244,18 @@ public Boolean getAllowDiskUse() {
244244
* @since 3.0
245245
*/
246246
public void setAllowDiskUse(@Nullable Boolean allowDiskUse) {
247-
this.allowDiskUse = allowDiskUse;
247+
setDiskUse(DiskUse.of(allowDiskUse));
248+
}
249+
250+
public void setDiskUse(DiskUse diskUse) {
251+
this.diskUse = diskUse;
248252
}
249253

250254
/**
251255
* @return
252256
*/
253257
public boolean hasValues() {
254-
return !this.values.isEmpty() || !this.flags.isEmpty() || this.cursorBatchSize != null || this.allowDiskUse != null;
258+
return !this.values.isEmpty() || !this.flags.isEmpty() || this.cursorBatchSize != null || !this.diskUse.equals(DiskUse.DEFAULT);
255259
}
256260

257261
/**

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,13 @@ public Query comment(String comment) {
582582
*/
583583
@Contract("_ -> this")
584584
public Query allowDiskUse(boolean allowDiskUse) {
585+
return diskUse(DiskUse.of(allowDiskUse));
586+
}
587+
588+
@Contract("_ -> this")
589+
public Query diskUse(DiskUse diskUse) {
585590

586-
meta.setAllowDiskUse(allowDiskUse);
591+
meta.setDiskUse(diskUse);
587592
return this;
588593
}
589594

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Meta.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@
7474
* @since 3.0
7575
* @see Aggregation
7676
*/
77-
boolean allowDiskUse() default false;
77+
String allowDiskUse() default "";
7878

7979
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.data.mongodb.core.annotation.Collation;
2828
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2929
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
30+
import org.springframework.data.mongodb.core.query.DiskUse;
3031
import org.springframework.data.mongodb.core.query.UpdateDefinition;
3132
import org.springframework.data.mongodb.repository.Aggregation;
3233
import org.springframework.data.mongodb.repository.Hint;
@@ -272,8 +273,9 @@ public org.springframework.data.mongodb.core.query.Meta getQueryMetaAttributes()
272273
}
273274
}
274275

275-
if (meta.allowDiskUse()) {
276-
metaAttributes.setAllowDiskUse(meta.allowDiskUse());
276+
DiskUse diskUse = DiskUse.of(meta.allowDiskUse());
277+
if (!diskUse.equals(DiskUse.DEFAULT)) {
278+
metaAttributes.setAllowDiskUse(diskUse.equals(DiskUse.ALLOW));
277279
}
278280

279281
return metaAttributes;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StringBasedAggregationUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ private Class<?> targetTypeOf(AggregationInvocation invocation) {
349349

350350
private interface SampleRepository extends Repository<Person, Long> {
351351

352-
@Meta(cursorBatchSize = 42, comment = "expensive-aggregation", allowDiskUse = true, maxExecutionTimeMs = 100)
352+
@Meta(cursorBatchSize = 42, comment = "expensive-aggregation", allowDiskUse = "true", maxExecutionTimeMs = 100)
353353
@Aggregation({ RAW_GROUP_BY_LASTNAME_STRING, RAW_SORT_STRING })
354354
PersonAggregate plainStringAggregation();
355355

0 commit comments

Comments
 (0)