Browse Source

Polishing.

Introduce has…() and getRequired…() methods for comment and max time limit to remove code duplications.

See #4374
Original pull request: #4378
3.4.x
Mark Paluch 3 years ago
parent
commit
7778077a9b
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  2. 18
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java
  3. 13
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java
  4. 70
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Meta.java
  5. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AggregationUtils.java
  6. 3
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryMethodUnitTests.java

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

@ -1918,7 +1918,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
if (query.getLimit() > 0 && mapReduceOptions != null && mapReduceOptions.getLimit() == null) { if (query.getLimit() > 0 && mapReduceOptions != null && mapReduceOptions.getLimit() == null) {
mapReduce = mapReduce.limit(query.getLimit()); mapReduce = mapReduce.limit(query.getLimit());
} }
if (query.getMeta().getMaxTimeMsec() != null) { if (query.getMeta().hasMaxTime()) {
mapReduce = mapReduce.maxTime(query.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS); mapReduce = mapReduce.maxTime(query.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS);
} }
@ -3411,12 +3411,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
if (meta.hasValues()) { if (meta.hasValues()) {
if (StringUtils.hasText(meta.getComment())) { if (meta.hasComment()) {
cursorToUse = cursorToUse.comment(meta.getComment()); cursorToUse = cursorToUse.comment(meta.getRequiredComment());
} }
if (meta.getMaxTimeMsec() != null) { if (meta.hasMaxTime()) {
cursorToUse = cursorToUse.maxTime(meta.getMaxTimeMsec(), TimeUnit.MILLISECONDS); cursorToUse = cursorToUse.maxTime(meta.getRequiredMaxTimeMsec(), TimeUnit.MILLISECONDS);
} }
if (meta.getCursorBatchSize() != null) { if (meta.getCursorBatchSize() != null) {

18
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java

@ -53,6 +53,7 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.mapping.ShardKey; import org.springframework.data.mongodb.core.mapping.ShardKey;
import org.springframework.data.mongodb.core.query.BasicQuery; import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Collation; import org.springframework.data.mongodb.core.query.Collation;
import org.springframework.data.mongodb.core.query.Meta;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.UpdateDefinition; import org.springframework.data.mongodb.core.query.UpdateDefinition;
import org.springframework.data.mongodb.core.query.UpdateDefinition.ArrayFilter; import org.springframework.data.mongodb.core.query.UpdateDefinition.ArrayFilter;
@ -67,7 +68,6 @@ import com.mongodb.client.model.CountOptions;
import com.mongodb.client.model.DeleteOptions; import com.mongodb.client.model.DeleteOptions;
import com.mongodb.client.model.ReplaceOptions; import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.model.UpdateOptions; import com.mongodb.client.model.UpdateOptions;
import org.springframework.util.StringUtils;
/** /**
* {@link QueryOperations} centralizes common operations required before an operation is actually ready to be executed. * {@link QueryOperations} centralizes common operations required before an operation is actually ready to be executed.
@ -390,7 +390,7 @@ class QueryOperations {
for (Entry<String, Object> entry : fields.entrySet()) { for (Entry<String, Object> entry : fields.entrySet()) {
if (entry.getValue() instanceof MongoExpression) { if (entry.getValue()instanceof MongoExpression) {
AggregationOperationContext ctx = entity == null ? Aggregation.DEFAULT_CONTEXT AggregationOperationContext ctx = entity == null ? Aggregation.DEFAULT_CONTEXT
: new RelaxedTypeBasedAggregationOperationContext(entity.getType(), mappingContext, queryMapper); : new RelaxedTypeBasedAggregationOperationContext(entity.getType(), mappingContext, queryMapper);
@ -566,16 +566,20 @@ class QueryOperations {
if (query.getLimit() > 0) { if (query.getLimit() > 0) {
options.limit(query.getLimit()); options.limit(query.getLimit());
} }
if (query.getSkip() > 0) { if (query.getSkip() > 0) {
options.skip((int) query.getSkip()); options.skip((int) query.getSkip());
} }
if(query.getMeta().hasValues()) { Meta meta = query.getMeta();
if(query.getMeta().getMaxTimeMsec() != null && query.getMeta().getMaxTimeMsec() > 0) { if (meta.hasValues()) {
options.maxTime(query.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS);
if (meta.hasMaxTime()) {
options.maxTime(meta.getRequiredMaxTimeMsec(), TimeUnit.MILLISECONDS);
} }
if(StringUtils.hasText(query.getMeta().getComment())) {
options.comment(query.getMeta().getComment()); if (meta.hasComment()) {
options.comment(meta.getComment());
} }
} }

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

@ -2305,8 +2305,9 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
publisher.sort(mappedSort); publisher.sort(mappedSort);
} }
if (filterQuery.getMeta().getMaxTimeMsec() != null) { Meta meta = filterQuery.getMeta();
publisher.maxTime(filterQuery.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS); if (meta.hasMaxTime()) {
publisher.maxTime(meta.getRequiredMaxTimeMsec(), TimeUnit.MILLISECONDS);
} }
if (filterQuery.getLimit() > 0 || (options.getLimit() != null)) { if (filterQuery.getLimit() > 0 || (options.getLimit() != null)) {
@ -3454,12 +3455,12 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
if (meta.hasValues()) { if (meta.hasValues()) {
if (StringUtils.hasText(meta.getComment())) { if (meta.hasComment()) {
findPublisherToUse = findPublisherToUse.comment(meta.getComment()); findPublisherToUse = findPublisherToUse.comment(meta.getRequiredComment());
} }
if (meta.getMaxTimeMsec() != null) { if (meta.hasMaxTime()) {
findPublisherToUse = findPublisherToUse.maxTime(meta.getMaxTimeMsec(), TimeUnit.MILLISECONDS); findPublisherToUse = findPublisherToUse.maxTime(meta.getRequiredMaxTimeMsec(), TimeUnit.MILLISECONDS);
} }
if (meta.getCursorBatchSize() != null) { if (meta.getCursorBatchSize() != null) {

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

@ -70,6 +70,19 @@ public class Meta {
this.allowDiskUse = source.allowDiskUse; this.allowDiskUse = source.allowDiskUse;
} }
/**
* Return whether the maximum time limit for processing operations is set.
*
* @return {@code true} if set; {@code false} otherwise.
* @since 3.4.12
*/
public boolean hasMaxTime() {
Long maxTimeMsec = getMaxTimeMsec();
return maxTimeMsec != null && maxTimeMsec > 0;
}
/** /**
* @return {@literal null} if not set. * @return {@literal null} if not set.
*/ */
@ -78,6 +91,26 @@ public class Meta {
return getValue(MetaKey.MAX_TIME_MS.key); return getValue(MetaKey.MAX_TIME_MS.key);
} }
/**
* Returns the required maximum time limit in milliseconds or throws {@link IllegalStateException} if the maximum time
* limit is not set.
*
* @return the maximum time limit in milliseconds for processing operations.
* @throws IllegalStateException if the maximum time limit is not set
* @see #hasMaxTime()
* @since 3.4.12
*/
public Long getRequiredMaxTimeMsec() {
Long maxTimeMsec = getMaxTimeMsec();
if (maxTimeMsec == null) {
throw new IllegalStateException("Maximum time limit in milliseconds not set");
}
return maxTimeMsec;
}
/** /**
* Set the maximum time limit in milliseconds for processing operations. * Set the maximum time limit in milliseconds for processing operations.
* *
@ -112,12 +145,13 @@ public class Meta {
} }
/** /**
* Add a comment to the query that is propagated to the profile log. * Return whether the comment is set.
* *
* @param comment * @return {@code true} if set; {@code false} otherwise.
* @since 3.4.12
*/ */
public void setComment(String comment) { public boolean hasComment() {
setValue(MetaKey.COMMENT.key, comment); return StringUtils.hasText(getComment());
} }
/** /**
@ -128,6 +162,34 @@ public class Meta {
return getValue(MetaKey.COMMENT.key); return getValue(MetaKey.COMMENT.key);
} }
/**
* Returns the required comment or throws {@link IllegalStateException} if the comment is not set.
*
* @return the comment.
* @throws IllegalStateException if the comment is not set
* @see #hasComment()
* @since 3.4.12
*/
public String getRequiredComment() {
String comment = getComment();
if (comment == null) {
throw new IllegalStateException("Comment not set");
}
return comment;
}
/**
* Add a comment to the query that is propagated to the profile log.
*
* @param comment
*/
public void setComment(String comment) {
setValue(MetaKey.COMMENT.key, comment);
}
/** /**
* @return {@literal null} if not set. * @return {@literal null} if not set.
* @since 2.1 * @since 2.1

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

@ -36,7 +36,6 @@ import org.springframework.expression.ExpressionParser;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/** /**
* Internal utility class to help avoid duplicate code required in both the reactive and the sync {@link Aggregation} * Internal utility class to help avoid duplicate code required in both the reactive and the sync {@link Aggregation}
@ -83,7 +82,7 @@ abstract class AggregationUtils {
Meta meta = queryMethod.getQueryMetaAttributes(); Meta meta = queryMethod.getQueryMetaAttributes();
if (StringUtils.hasText(meta.getComment())) { if (meta.hasComment()) {
builder.comment(meta.getComment()); builder.comment(meta.getComment());
} }
@ -91,8 +90,8 @@ abstract class AggregationUtils {
builder.cursorBatchSize(meta.getCursorBatchSize()); builder.cursorBatchSize(meta.getCursorBatchSize());
} }
if (meta.getMaxTimeMsec() != null && meta.getMaxTimeMsec() > 0) { if (meta.hasMaxTime()) {
builder.maxTime(Duration.ofMillis(meta.getMaxTimeMsec())); builder.maxTime(Duration.ofMillis(meta.getRequiredMaxTimeMsec()));
} }
if (meta.getAllowDiskUse() != null) { if (meta.getAllowDiskUse() != null) {

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

@ -272,7 +272,8 @@ public class MongoQueryMethodUnitTests {
void queryCreationForUpdateMethodFailsOnInvalidReturnType() throws Exception { void queryCreationForUpdateMethodFailsOnInvalidReturnType() throws Exception {
assertThatExceptionOfType(IllegalStateException.class) // assertThatExceptionOfType(IllegalStateException.class) //
.isThrownBy(() -> queryMethod(InvalidUpdateMethodRepo.class, "findAndIncrementVisitsByFirstname", String.class).verify()) // .isThrownBy(() -> queryMethod(InvalidUpdateMethodRepo.class, "findAndIncrementVisitsByFirstname", String.class)
.verify()) //
.withMessageContaining("Update") // .withMessageContaining("Update") //
.withMessageContaining("numeric") // .withMessageContaining("numeric") //
.withMessageContaining("findAndIncrementVisitsByFirstname"); .withMessageContaining("findAndIncrementVisitsByFirstname");

Loading…
Cancel
Save