Skip to content

Commit 7778077

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 ec55e28 commit 7778077

File tree

6 files changed

+94
-27
lines changed

6 files changed

+94
-27
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
@@ -1918,7 +1918,7 @@ public <T> List<T> mapReduce(Query query, Class<?> domainType, String inputColle
19181918
if (query.getLimit() > 0 && mapReduceOptions != null && mapReduceOptions.getLimit() == null) {
19191919
mapReduce = mapReduce.limit(query.getLimit());
19201920
}
1921-
if (query.getMeta().getMaxTimeMsec() != null) {
1921+
if (query.getMeta().hasMaxTime()) {
19221922
mapReduce = mapReduce.maxTime(query.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS);
19231923
}
19241924

@@ -3411,12 +3411,12 @@ public FindIterable<Document> prepare(FindIterable<Document> iterable) {
34113411

34123412
if (meta.hasValues()) {
34133413

3414-
if (StringUtils.hasText(meta.getComment())) {
3415-
cursorToUse = cursorToUse.comment(meta.getComment());
3414+
if (meta.hasComment()) {
3415+
cursorToUse = cursorToUse.comment(meta.getRequiredComment());
34163416
}
34173417

3418-
if (meta.getMaxTimeMsec() != null) {
3419-
cursorToUse = cursorToUse.maxTime(meta.getMaxTimeMsec(), TimeUnit.MILLISECONDS);
3418+
if (meta.hasMaxTime()) {
3419+
cursorToUse = cursorToUse.maxTime(meta.getRequiredMaxTimeMsec(), TimeUnit.MILLISECONDS);
34203420
}
34213421

34223422
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
@@ -2305,8 +2305,9 @@ public <T> Flux<T> mapReduce(Query filterQuery, Class<?> domainType, String inpu
23052305
publisher.sort(mappedSort);
23062306
}
23072307

2308-
if (filterQuery.getMeta().getMaxTimeMsec() != null) {
2309-
publisher.maxTime(filterQuery.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS);
2308+
Meta meta = filterQuery.getMeta();
2309+
if (meta.hasMaxTime()) {
2310+
publisher.maxTime(meta.getRequiredMaxTimeMsec(), TimeUnit.MILLISECONDS);
23102311
}
23112312

23122313
if (filterQuery.getLimit() > 0 || (options.getLimit() != null)) {
@@ -3454,12 +3455,12 @@ public FindPublisher<Document> prepare(FindPublisher<Document> findPublisher) {
34543455

34553456
if (meta.hasValues()) {
34563457

3457-
if (StringUtils.hasText(meta.getComment())) {
3458-
findPublisherToUse = findPublisherToUse.comment(meta.getComment());
3458+
if (meta.hasComment()) {
3459+
findPublisherToUse = findPublisherToUse.comment(meta.getRequiredComment());
34593460
}
34603461

3461-
if (meta.getMaxTimeMsec() != null) {
3462-
findPublisherToUse = findPublisherToUse.maxTime(meta.getMaxTimeMsec(), TimeUnit.MILLISECONDS);
3462+
if (meta.hasMaxTime()) {
3463+
findPublisherToUse = findPublisherToUse.maxTime(meta.getRequiredMaxTimeMsec(), TimeUnit.MILLISECONDS);
34633464
}
34643465

34653466
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
@@ -70,6 +70,19 @@ public Meta() {}
7070
this.allowDiskUse = source.allowDiskUse;
7171
}
7272

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

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

114147
/**
115-
* Add a comment to the query that is propagated to the profile log.
148+
* Return whether the comment is set.
116149
*
117-
* @param comment
150+
* @return {@code true} if set; {@code false} otherwise.
151+
* @since 3.4.12
118152
*/
119-
public void setComment(String comment) {
120-
setValue(MetaKey.COMMENT.key, comment);
153+
public boolean hasComment() {
154+
return StringUtils.hasText(getComment());
121155
}
122156

123157
/**
@@ -128,6 +162,34 @@ public String getComment() {
128162
return getValue(MetaKey.COMMENT.key);
129163
}
130164

165+
/**
166+
* Returns the required comment or throws {@link IllegalStateException} if the comment is not set.
167+
*
168+
* @return the comment.
169+
* @throws IllegalStateException if the comment is not set
170+
* @see #hasComment()
171+
* @since 3.4.12
172+
*/
173+
public String getRequiredComment() {
174+
175+
String comment = getComment();
176+
177+
if (comment == null) {
178+
throw new IllegalStateException("Comment not set");
179+
}
180+
181+
return comment;
182+
}
183+
184+
/**
185+
* Add a comment to the query that is propagated to the profile log.
186+
*
187+
* @param comment
188+
*/
189+
public void setComment(String comment) {
190+
setValue(MetaKey.COMMENT.key, comment);
191+
}
192+
131193
/**
132194
* @return {@literal null} if not set.
133195
* @since 2.1

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.springframework.lang.Nullable;
3737
import org.springframework.util.ClassUtils;
3838
import org.springframework.util.ObjectUtils;
39-
import org.springframework.util.StringUtils;
4039

4140
/**
4241
* Internal utility class to help avoid duplicate code required in both the reactive and the sync {@link Aggregation}
@@ -83,16 +82,16 @@ static AggregationOptions.Builder applyMeta(AggregationOptions.Builder builder,
8382

8483
Meta meta = queryMethod.getQueryMetaAttributes();
8584

86-
if (StringUtils.hasText(meta.getComment())) {
85+
if (meta.hasComment()) {
8786
builder.comment(meta.getComment());
8887
}
8988

9089
if (meta.getCursorBatchSize() != null) {
9190
builder.cursorBatchSize(meta.getCursorBatchSize());
9291
}
9392

94-
if (meta.getMaxTimeMsec() != null && meta.getMaxTimeMsec() > 0) {
95-
builder.maxTime(Duration.ofMillis(meta.getMaxTimeMsec()));
93+
if (meta.hasMaxTime()) {
94+
builder.maxTime(Duration.ofMillis(meta.getRequiredMaxTimeMsec()));
9695
}
9796

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ void queryCreationFailsOnInvalidUpdate() throws Exception {
272272
void queryCreationForUpdateMethodFailsOnInvalidReturnType() throws Exception {
273273

274274
assertThatExceptionOfType(IllegalStateException.class) //
275-
.isThrownBy(() -> queryMethod(InvalidUpdateMethodRepo.class, "findAndIncrementVisitsByFirstname", String.class).verify()) //
275+
.isThrownBy(() -> queryMethod(InvalidUpdateMethodRepo.class, "findAndIncrementVisitsByFirstname", String.class)
276+
.verify()) //
276277
.withMessageContaining("Update") //
277278
.withMessageContaining("numeric") //
278279
.withMessageContaining("findAndIncrementVisitsByFirstname");

0 commit comments

Comments
 (0)