Skip to content

Commit 6a9163d

Browse files
committed
Polishing.
Introduce has…() and getRequired…() methods for comment and max time limit to remove code duplications. See #4374 Original pull request: #4378
1 parent a21fca7 commit 6a9163d

File tree

6 files changed

+100
-32
lines changed

6 files changed

+100
-32
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -1818,7 +1818,7 @@ public <T> List<T> mapReduce(Query query, Class<?> domainType, String inputColle
18181818
if (query.getLimit() > 0 && mapReduceOptions != null && mapReduceOptions.getLimit() == null) {
18191819
mapReduce = mapReduce.limit(query.getLimit());
18201820
}
1821-
if (query.getMeta().getMaxTimeMsec() != null) {
1821+
if (query.getMeta().hasMaxTime()) {
18221822
mapReduce = mapReduce.maxTime(query.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS);
18231823
}
18241824

@@ -3159,12 +3159,12 @@ public FindIterable<Document> prepare(FindIterable<Document> iterable) {
31593159

31603160
if (meta.hasValues()) {
31613161

3162-
if (StringUtils.hasText(meta.getComment())) {
3163-
cursorToUse = cursorToUse.comment(meta.getComment());
3162+
if (meta.hasComment()) {
3163+
cursorToUse = cursorToUse.comment(meta.getRequiredComment());
31643164
}
31653165

3166-
if (meta.getMaxTimeMsec() != null) {
3167-
cursorToUse = cursorToUse.maxTime(meta.getMaxTimeMsec(), TimeUnit.MILLISECONDS);
3166+
if (meta.hasMaxTime()) {
3167+
cursorToUse = cursorToUse.maxTime(meta.getRequiredMaxTimeMsec(), TimeUnit.MILLISECONDS);
31683168
}
31693169

31703170
if (meta.getCursorBatchSize() != null) {

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.springframework.data.mongodb.core.mapping.ShardKey;
5454
import org.springframework.data.mongodb.core.query.BasicQuery;
5555
import org.springframework.data.mongodb.core.query.Collation;
56+
import org.springframework.data.mongodb.core.query.Meta;
5657
import org.springframework.data.mongodb.core.query.Query;
5758
import org.springframework.data.mongodb.core.query.UpdateDefinition;
5859
import org.springframework.data.mongodb.core.query.UpdateDefinition.ArrayFilter;
@@ -67,7 +68,6 @@
6768
import com.mongodb.client.model.DeleteOptions;
6869
import com.mongodb.client.model.ReplaceOptions;
6970
import com.mongodb.client.model.UpdateOptions;
70-
import org.springframework.util.StringUtils;
7171

7272
/**
7373
* {@link QueryOperations} centralizes common operations required before an operation is actually ready to be executed.
@@ -390,7 +390,7 @@ private Document evaluateFields(@Nullable MongoPersistentEntity<?> entity) {
390390

391391
for (Entry<String, Object> entry : fields.entrySet()) {
392392

393-
if (entry.getValue() instanceof MongoExpression) {
393+
if (entry.getValue()instanceof MongoExpression) {
394394

395395
AggregationOperationContext ctx = entity == null ? Aggregation.DEFAULT_CONTEXT
396396
: new RelaxedTypeBasedAggregationOperationContext(entity.getType(), mappingContext, queryMapper);
@@ -566,16 +566,20 @@ CountOptions getCountOptions(@Nullable Class<?> domainType, @Nullable Consumer<C
566566
if (query.getLimit() > 0) {
567567
options.limit(query.getLimit());
568568
}
569+
569570
if (query.getSkip() > 0) {
570571
options.skip((int) query.getSkip());
571572
}
572573

573-
if(query.getMeta().hasValues()) {
574-
if(query.getMeta().getMaxTimeMsec() != null && query.getMeta().getMaxTimeMsec() > 0) {
575-
options.maxTime(query.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS);
574+
Meta meta = query.getMeta();
575+
if (meta.hasValues()) {
576+
577+
if (meta.hasMaxTime()) {
578+
options.maxTime(meta.getRequiredMaxTimeMsec(), TimeUnit.MILLISECONDS);
576579
}
577-
if(StringUtils.hasText(query.getMeta().getComment())) {
578-
options.comment(query.getMeta().getComment());
580+
581+
if (meta.hasComment()) {
582+
options.comment(meta.getComment());
579583
}
580584
}
581585

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -1971,8 +1971,9 @@ public <T> Flux<T> mapReduce(Query filterQuery, Class<?> domainType, String inpu
19711971
publisher.sort(mappedSort);
19721972
}
19731973

1974-
if (filterQuery.getMeta().getMaxTimeMsec() != null) {
1975-
publisher.maxTime(filterQuery.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS);
1974+
Meta meta = filterQuery.getMeta();
1975+
if (meta.hasMaxTime()) {
1976+
publisher.maxTime(meta.getRequiredMaxTimeMsec(), TimeUnit.MILLISECONDS);
19761977
}
19771978

19781979
if (filterQuery.getLimit() > 0 || (options.getLimit() != null)) {
@@ -3062,12 +3063,12 @@ public FindPublisher<Document> prepare(FindPublisher<Document> findPublisher) {
30623063

30633064
if (meta.hasValues()) {
30643065

3065-
if (StringUtils.hasText(meta.getComment())) {
3066-
findPublisherToUse = findPublisherToUse.comment(meta.getComment());
3066+
if (meta.hasComment()) {
3067+
findPublisherToUse = findPublisherToUse.comment(meta.getRequiredComment());
30673068
}
30683069

3069-
if (meta.getMaxTimeMsec() != null) {
3070-
findPublisherToUse = findPublisherToUse.maxTime(meta.getMaxTimeMsec(), TimeUnit.MILLISECONDS);
3070+
if (meta.hasMaxTime()) {
3071+
findPublisherToUse = findPublisherToUse.maxTime(meta.getRequiredMaxTimeMsec(), TimeUnit.MILLISECONDS);
30713072
}
30723073

30733074
if (meta.getCursorBatchSize() != null) {

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

+66-4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ public Meta() {}
6969
this.allowDiskUse = source.allowDiskUse;
7070
}
7171

72+
/**
73+
* Return whether the maximum time limit for processing operations is set.
74+
*
75+
* @return {@code true} if set; {@code false} otherwise.
76+
* @since 4.0.6
77+
*/
78+
public boolean hasMaxTime() {
79+
80+
Long maxTimeMsec = getMaxTimeMsec();
81+
82+
return maxTimeMsec != null && maxTimeMsec > 0;
83+
}
84+
7285
/**
7386
* @return {@literal null} if not set.
7487
*/
@@ -77,6 +90,26 @@ public Long getMaxTimeMsec() {
7790
return getValue(MetaKey.MAX_TIME_MS.key);
7891
}
7992

93+
/**
94+
* Returns the required maximum time limit in milliseconds or throws {@link IllegalStateException} if the maximum time
95+
* limit is not set.
96+
*
97+
* @return the maximum time limit in milliseconds for processing operations.
98+
* @throws IllegalStateException if the maximum time limit is not set
99+
* @see #hasMaxTime()
100+
* @since 4.0.6
101+
*/
102+
public Long getRequiredMaxTimeMsec() {
103+
104+
Long maxTimeMsec = getMaxTimeMsec();
105+
106+
if (maxTimeMsec == null) {
107+
throw new IllegalStateException("Maximum time limit in milliseconds not set");
108+
}
109+
110+
return maxTimeMsec;
111+
}
112+
80113
/**
81114
* Set the maximum time limit in milliseconds for processing operations.
82115
*
@@ -99,12 +132,13 @@ public void setMaxTime(Duration timeout) {
99132
}
100133

101134
/**
102-
* Add a comment to the query that is propagated to the profile log.
135+
* Return whether the comment is set.
103136
*
104-
* @param comment
137+
* @return {@code true} if set; {@code false} otherwise.
138+
* @since 4.0.6
105139
*/
106-
public void setComment(String comment) {
107-
setValue(MetaKey.COMMENT.key, comment);
140+
public boolean hasComment() {
141+
return StringUtils.hasText(getComment());
108142
}
109143

110144
/**
@@ -115,6 +149,34 @@ public String getComment() {
115149
return getValue(MetaKey.COMMENT.key);
116150
}
117151

152+
/**
153+
* Returns the required comment or throws {@link IllegalStateException} if the comment is not set.
154+
*
155+
* @return the comment.
156+
* @throws IllegalStateException if the comment is not set
157+
* @see #hasComment()
158+
* @since 4.0.6
159+
*/
160+
public String getRequiredComment() {
161+
162+
String comment = getComment();
163+
164+
if (comment == null) {
165+
throw new IllegalStateException("Comment not set");
166+
}
167+
168+
return comment;
169+
}
170+
171+
/**
172+
* Add a comment to the query that is propagated to the profile log.
173+
*
174+
* @param comment
175+
*/
176+
public void setComment(String comment) {
177+
setValue(MetaKey.COMMENT.key, comment);
178+
}
179+
118180
/**
119181
* @return {@literal null} if not set.
120182
* @since 2.1

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AggregationUtils.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.data.mongodb.repository.query;
1717

1818
import java.time.Duration;
19-
import java.util.List;
2019
import java.util.Map;
2120
import java.util.function.IntUnaryOperator;
2221
import java.util.function.LongUnaryOperator;
@@ -25,7 +24,6 @@
2524
import org.springframework.data.domain.Pageable;
2625
import org.springframework.data.domain.Sort.Order;
2726
import org.springframework.data.mongodb.core.aggregation.Aggregation;
28-
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
2927
import org.springframework.data.mongodb.core.aggregation.AggregationOptions;
3028
import org.springframework.data.mongodb.core.aggregation.AggregationPipeline;
3129
import org.springframework.data.mongodb.core.convert.MongoConverter;
@@ -37,7 +35,6 @@
3735
import org.springframework.lang.Nullable;
3836
import org.springframework.util.ClassUtils;
3937
import org.springframework.util.ObjectUtils;
40-
import org.springframework.util.StringUtils;
4138

4239
/**
4340
* Internal utility class to help avoid duplicate code required in both the reactive and the sync {@link Aggregation}
@@ -84,16 +81,16 @@ static AggregationOptions.Builder applyMeta(AggregationOptions.Builder builder,
8481

8582
Meta meta = queryMethod.getQueryMetaAttributes();
8683

87-
if (StringUtils.hasText(meta.getComment())) {
84+
if (meta.hasComment()) {
8885
builder.comment(meta.getComment());
8986
}
9087

9188
if (meta.getCursorBatchSize() != null) {
9289
builder.cursorBatchSize(meta.getCursorBatchSize());
9390
}
9491

95-
if (meta.getMaxTimeMsec() != null && meta.getMaxTimeMsec() > 0) {
96-
builder.maxTime(Duration.ofMillis(meta.getMaxTimeMsec()));
92+
if (meta.hasMaxTime()) {
93+
builder.maxTime(Duration.ofMillis(meta.getRequiredMaxTimeMsec()));
9794
}
9895

9996
if (meta.getAllowDiskUse() != null) {

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryMethodUnitTests.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ void queryCreationFailsOnInvalidUpdate() throws Exception {
274274
void queryCreationForUpdateMethodFailsOnInvalidReturnType() throws Exception {
275275

276276
assertThatExceptionOfType(IllegalStateException.class) //
277-
.isThrownBy(() -> queryMethod(InvalidUpdateMethodRepo.class, "findAndIncrementVisitsByFirstname", String.class).verify()) //
277+
.isThrownBy(() -> queryMethod(InvalidUpdateMethodRepo.class, "findAndIncrementVisitsByFirstname", String.class)
278+
.verify()) //
278279
.withMessageContaining("Update") //
279280
.withMessageContaining("numeric") //
280281
.withMessageContaining("findAndIncrementVisitsByFirstname");
@@ -283,7 +284,8 @@ void queryCreationForUpdateMethodFailsOnInvalidReturnType() throws Exception {
283284
@Test // GH-3002
284285
void readsCollationFromAtCollationAnnotation() throws Exception {
285286

286-
MongoQueryMethod method = queryMethod(PersonRepository.class, "findWithCollationFromAtCollationByFirstname", String.class);
287+
MongoQueryMethod method = queryMethod(PersonRepository.class, "findWithCollationFromAtCollationByFirstname",
288+
String.class);
287289

288290
assertThat(method.hasAnnotatedCollation()).isTrue();
289291
assertThat(method.getAnnotatedCollation()).isEqualTo("en_US");
@@ -292,7 +294,8 @@ void readsCollationFromAtCollationAnnotation() throws Exception {
292294
@Test // GH-3002
293295
void readsCollationFromAtQueryAnnotation() throws Exception {
294296

295-
MongoQueryMethod method = queryMethod(PersonRepository.class, "findWithCollationFromAtQueryByFirstname", String.class);
297+
MongoQueryMethod method = queryMethod(PersonRepository.class, "findWithCollationFromAtQueryByFirstname",
298+
String.class);
296299

297300
assertThat(method.hasAnnotatedCollation()).isTrue();
298301
assertThat(method.getAnnotatedCollation()).isEqualTo("en_US");
@@ -301,7 +304,8 @@ void readsCollationFromAtQueryAnnotation() throws Exception {
301304
@Test // GH-3002
302305
void annotatedCollationClashSelectsAtCollationAnnotationValue() throws Exception {
303306

304-
MongoQueryMethod method = queryMethod(PersonRepository.class, "findWithMultipleCollationsFromAtQueryAndAtCollationByFirstname", String.class);
307+
MongoQueryMethod method = queryMethod(PersonRepository.class,
308+
"findWithMultipleCollationsFromAtQueryAndAtCollationByFirstname", String.class);
305309

306310
assertThat(method.hasAnnotatedCollation()).isTrue();
307311
assertThat(method.getAnnotatedCollation()).isEqualTo("de_AT");

0 commit comments

Comments
 (0)