Skip to content

Commit af4785d

Browse files
Update documentation and make sure it's covered in AOT code generation.
1 parent 034bd8d commit af4785d

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

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

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,60 @@
1919
import org.springframework.util.StringUtils;
2020

2121
/**
22+
* Disk use indicates if the MongoDB server is allowed to write temporary files to disk during query/aggregation
23+
* execution. MongoDB 6.0 server (and later) default for {@literal allowDiskUseByDefault} is {@literal true} on server
24+
* side.
25+
*
2226
* @author Christoph Strobl
27+
* @since 5.0
2328
*/
2429
public enum DiskUse {
2530

26-
DEFAULT, ALLOW, DENY;
31+
/**
32+
* Go with the server default value and do not specify any override.
33+
*/
34+
DEFAULT,
35+
36+
/**
37+
* Override server default value and explicitly allow disk writes.
38+
*/
39+
ALLOW,
40+
41+
/**
42+
* Override server default value and explicitly deny disk writes.
43+
*/
44+
DENY;
2745

46+
/**
47+
* Obtain the {@link DiskUse} corresponding to the given Boolean flag. {@literal null} is considered {@link #DEFAULT},
48+
* {@literal true} as {@link #ALLOW}, {@literal false} as {@link #DENY}.
49+
*
50+
* @param value can be {@literal null}.
51+
* @return the {@link DiskUse} corresponding to the given value.
52+
*/
2853
public static DiskUse of(@Nullable Boolean value) {
2954
return value != null ? (value ? ALLOW : DENY) : DEFAULT;
3055
}
3156

32-
public static DiskUse of(String value) {
57+
/**
58+
* Obtain the {@link DiskUse} referred to by the given value. Considers {@literal null} or empty Strings as
59+
* {@link #DEFAULT}, {@literal true} as {@link #ALLOW}, {@literal false} as {@link #DENY} and delegates other values
60+
* to {@link #valueOf(String)}.
61+
*
62+
* @param value can be {@literal null}.
63+
* @return the {@link DiskUse} corresponding to the given value.
64+
* @throws IllegalArgumentException if not matching {@link DiskUse} found.
65+
*/
66+
public static DiskUse of(@Nullable String value) {
67+
3368
if (!StringUtils.hasText(value)) {
3469
return DEFAULT;
3570
}
36-
if (value.toLowerCase().equalsIgnoreCase("true")) {
37-
return ALLOW;
38-
}
39-
return valueOf(value.toUpperCase());
71+
72+
return switch (value) {
73+
case "true" -> ALLOW;
74+
case "false" -> DENY;
75+
default -> valueOf(value.toUpperCase());
76+
};
4077
}
4178
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,16 @@
6969

7070
/**
7171
* When set to {@literal true}, aggregation stages can write data to disk.
72+
* Valid arguments are:
73+
* <dl>
74+
* <dt>""</dt><dd>DiskUse#DEFAULT</dd>
75+
* <dt>true|allow</dt><dd>DiskUse#ALLOW</dd>
76+
* <dt>false|deny</dt><dd>DiskUse#DENY</dd>
77+
* </dl>
7278
*
7379
* @return {@literal false} by default.
7480
* @since 3.0
75-
* @see Aggregation
81+
* @see org.springframework.data.mongodb.core.query.DiskUse
7682
*/
7783
String allowDiskUse() default "";
7884

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.data.mongodb.core.MongoOperations;
2727
import org.springframework.data.mongodb.core.annotation.Collation;
2828
import org.springframework.data.mongodb.core.query.BasicQuery;
29+
import org.springframework.data.mongodb.core.query.DiskUse;
2930
import org.springframework.data.mongodb.repository.Hint;
3031
import org.springframework.data.mongodb.repository.Meta;
3132
import org.springframework.data.mongodb.repository.query.MongoQueryExecution.PagedExecution;
@@ -292,6 +293,12 @@ CodeBlock build() {
292293
if (StringUtils.hasText(comment)) {
293294
builder.addStatement("$L.comment($S)", queryVariableName, comment);
294295
}
296+
297+
String allowDiskUse = metaAnnotation.getString("allowDiskUse");
298+
if (StringUtils.hasText(allowDiskUse)) {
299+
DiskUse diskUse = DiskUse.of(allowDiskUse);
300+
builder.addStatement("$L.diskUse($T.$L)", queryVariableName, DiskUse.class, diskUse.name());
301+
}
295302
}
296303

297304
MergedAnnotation<Collation> collationAnnotation = context.getAnnotation(Collation.class);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/aot/MongoRepositoryContributorUnitTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.aot;
1717

18-
import static org.mockito.Mockito.*;
19-
import static org.springframework.data.mongodb.test.util.Assertions.*;
18+
import static org.mockito.Mockito.mock;
19+
import static org.springframework.data.mongodb.test.util.Assertions.assertThat;
2020

2121
import example.aot.User;
2222
import example.aot.UserRepository;
@@ -25,7 +25,6 @@
2525
import java.nio.charset.StandardCharsets;
2626

2727
import org.junit.jupiter.api.Test;
28-
2928
import org.springframework.aot.generate.GeneratedFiles;
3029
import org.springframework.aot.test.generate.TestGenerationContext;
3130
import org.springframework.beans.factory.annotation.Autowired;
@@ -42,8 +41,8 @@
4241
* Unit tests for the {@link UserRepository} fragment sources via {@link MongoRepositoryContributor}.
4342
*
4443
* @author Mark Paluch
44+
* @author Christoph Strobl
4545
*/
46-
4746
@SpringJUnitConfig(classes = MongoRepositoryContributorUnitTests.MongoRepositoryContributorConfiguration.class)
4847
class MongoRepositoryContributorUnitTests {
4948

@@ -63,7 +62,7 @@ MongoOperations mongoOperations() {
6362

6463
@Autowired TestGenerationContext generationContext;
6564

66-
@Test // GH-4970
65+
@Test // GH-4970, GH-4667
6766
void shouldConsiderMetaAnnotation() throws IOException {
6867

6968
InputStreamSource aotFragment = generationContext.getGeneratedFiles().getGeneratedFile(GeneratedFiles.Kind.SOURCE,
@@ -74,14 +73,15 @@ void shouldConsiderMetaAnnotation() throws IOException {
7473
assertThat(content).contains("filterQuery.maxTimeMsec(555)");
7574
assertThat(content).contains("filterQuery.cursorBatchSize(1234)");
7675
assertThat(content).contains("filterQuery.comment(\"foo\")");
76+
assertThat(content).contains("filterQuery.diskUse(DiskUse.DENY)");
7777
}
7878

7979
interface MetaUserRepository extends CrudRepository<User, String> {
8080

8181
@Meta
8282
User findAllByLastname(String lastname);
8383

84-
@Meta(cursorBatchSize = 1234, comment = "foo", maxExecutionTimeMs = 555)
84+
@Meta(cursorBatchSize = 1234, comment = "foo", maxExecutionTimeMs = 555, allowDiskUse = "false")
8585
User findWithMetaAllByLastname(String lastname);
8686
}
8787

0 commit comments

Comments
 (0)