diff --git a/pom.xml b/pom.xml index a401249d79..7d0c2a5fa9 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 4.0.0-SNAPSHOT + 4.0.x-GH-4145-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml index c28a240d2c..9cac0aa4f7 100644 --- a/spring-data-mongodb-benchmarks/pom.xml +++ b/spring-data-mongodb-benchmarks/pom.xml @@ -7,7 +7,7 @@ org.springframework.data spring-data-mongodb-parent - 4.0.0-SNAPSHOT + 4.0.x-GH-4145-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 0412911f82..66fe35dc07 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -15,7 +15,7 @@ org.springframework.data spring-data-mongodb-parent - 4.0.0-SNAPSHOT + 4.0.x-GH-4145-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index cb8c76ade4..6db56edd39 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 4.0.0-SNAPSHOT + 4.0.x-GH-4145-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AbstractAggregationExpression.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AbstractAggregationExpression.java index 15092d6706..bb46ee3959 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AbstractAggregationExpression.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AbstractAggregationExpression.java @@ -24,7 +24,6 @@ import java.util.Map; import org.bson.Document; -import org.springframework.data.mongodb.core.aggregation.Aggregation.SystemVariable; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java index 89d769066e..ebee95678c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java @@ -35,7 +35,6 @@ import org.springframework.data.mongodb.core.query.CriteriaDefinition; import org.springframework.data.mongodb.core.query.NearQuery; import org.springframework.data.mongodb.core.query.SerializationUtils; -import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** @@ -766,48 +765,4 @@ public Document toDocument(String inputCollectionName, AggregationOperationConte public String toString() { return SerializationUtils.serializeToJsonSafely(toDocument("__collection__", DEFAULT_CONTEXT)); } - - /** - * Describes the system variables available in MongoDB aggregation framework pipeline expressions. - * - * @author Thomas Darimont - * @author Christoph Strobl - * @see Aggregation Variables. - */ - enum SystemVariable { - - ROOT, CURRENT, REMOVE; - - private static final String PREFIX = "$$"; - - /** - * Return {@literal true} if the given {@code fieldRef} denotes a well-known system variable, {@literal false} - * otherwise. - * - * @param fieldRef may be {@literal null}. - * @return {@literal true} if the given field refers to a {@link SystemVariable}. - */ - public static boolean isReferingToSystemVariable(@Nullable String fieldRef) { - - if (fieldRef == null || !fieldRef.startsWith(PREFIX) || fieldRef.length() <= 2) { - return false; - } - - int indexOfFirstDot = fieldRef.indexOf('.'); - String candidate = fieldRef.substring(2, indexOfFirstDot == -1 ? fieldRef.length() : indexOfFirstDot); - - for (SystemVariable value : values()) { - if (value.name().equals(candidate)) { - return true; - } - } - - return false; - } - - @Override - public String toString() { - return PREFIX.concat(name()); - } - } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java index 6478e3ec8c..500863a0a9 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java @@ -245,7 +245,7 @@ public AggregationField(String name, @Nullable String target) { private static String cleanUp(String source) { - if (Aggregation.SystemVariable.isReferingToSystemVariable(source)) { + if (SystemVariable.isReferingToSystemVariable(source)) { return source; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java index 4773028c12..d7a8887f33 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java @@ -510,7 +510,7 @@ public Object getValue(AggregationOperationContext context) { return value; } - if (Aggregation.SystemVariable.isReferingToSystemVariable(reference)) { + if (SystemVariable.isReferingToSystemVariable(reference)) { return reference; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java index 07fcfe3d26..6d96e51c9c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java @@ -1395,7 +1395,7 @@ private Object renderFieldValue(AggregationOperationContext context) { // implicit reference or explicit include? if (value == null || Boolean.TRUE.equals(value)) { - if (Aggregation.SystemVariable.isReferingToSystemVariable(field.getTarget())) { + if (SystemVariable.isReferingToSystemVariable(field.getTarget())) { return field.getTarget(); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SystemVariable.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SystemVariable.java new file mode 100644 index 0000000000..524d23926c --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SystemVariable.java @@ -0,0 +1,116 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core.aggregation; + +import org.springframework.lang.Nullable; + +/** + * Describes the system variables available in MongoDB aggregation framework pipeline expressions. + * + * @author Thomas Darimont + * @author Christoph Strobl + * @see Aggregation Variables. + */ +public enum SystemVariable { + + /** + * Variable for the current datetime. + * + * @since 4.0 + */ + NOW, + + /** + * Variable for the current timestamp. + * + * @since 4.0 + */ + CLUSTER_TIME, + + /** + * Variable that references the root document. + */ + ROOT, + + /** + * Variable that references the start of the field path being processed. + */ + CURRENT, + + /** + * Variable that evaluates to a missing value. + */ + REMOVE, + + /** + * One of the allowed results of a {@literal $redact} expression + * + * @since 4.0 + */ + DESCEND, + + /** + * One of the allowed results of a {@literal $redact} expression + * + * @since 4.0 + */ + PRUNE, + /** + * One of the allowed results of a {@literal $redact} expression + * + * @since 4.0 + */ + KEEP, + + /** + * A variable that stores the metadata results of an Atlas Search query. + * + * @since 4.0 + */ + SEARCH_META; + + private static final String PREFIX = "$$"; + + /** + * Return {@literal true} if the given {@code fieldRef} denotes a well-known system variable, {@literal false} + * otherwise. + * + * @param fieldRef may be {@literal null}. + * @return {@literal true} if the given field refers to a {@link SystemVariable}. + */ + public static boolean isReferingToSystemVariable(@Nullable String fieldRef) { + + if (fieldRef == null || !fieldRef.startsWith(PREFIX) || fieldRef.length() <= 2) { + return false; + } + + int indexOfFirstDot = fieldRef.indexOf('.'); + String candidate = fieldRef.substring(2, indexOfFirstDot == -1 ? fieldRef.length() : indexOfFirstDot); + + for (SystemVariable value : values()) { + if (value.name().equals(candidate)) { + return true; + } + } + + return false; + } + + @Override + public String toString() { + return PREFIX.concat(name()); + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ObjectOperatorsUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ObjectOperatorsUnitTests.java index 1c26ec6cd8..f8f94f8889 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ObjectOperatorsUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ObjectOperatorsUnitTests.java @@ -19,7 +19,6 @@ import org.bson.Document; import org.junit.jupiter.api.Test; -import org.springframework.data.mongodb.core.aggregation.Aggregation.SystemVariable; import org.springframework.data.mongodb.core.aggregation.ObjectOperators.MergeObjects; /**