Browse Source

Polishing.

See #4139
Original pull request: #4182.
pull/4212/head
Christoph Strobl 3 years ago committed by Mark Paluch
parent
commit
ff28789507
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 18
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AbstractAggregationExpression.java
  2. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/DateOperators.java
  3. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/DensifyOperation.java
  4. 113
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ObjectOperators.java
  5. 24
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SelectionOperators.java
  6. 10
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/DensifyOperationUnitTests.java

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

@ -23,9 +23,9 @@ import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import org.bson.Document; import org.bson.Document;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order; import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference; import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
@ -78,7 +78,14 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
} }
if (value instanceof Fields fields) { if (value instanceof Fields fields) {
return fields.asList().stream().map(it -> unpack(it, context)).collect(Collectors.toList());
List<Object> mapped = new ArrayList<>(fields.size());
for (Field field : fields) {
mapped.add(unpack(field, context));
}
return mapped;
} }
if (value instanceof Sort sort) { if (value instanceof Sort sort) {
@ -98,7 +105,9 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
List<Object> sourceList = (List<Object>) value; List<Object> sourceList = (List<Object>) value;
List<Object> mappedList = new ArrayList<>(sourceList.size()); List<Object> mappedList = new ArrayList<>(sourceList.size());
sourceList.stream().map((item) -> unpack(item, context)).forEach(mappedList::add); for (Object o : sourceList) {
mappedList.add(unpack(o, context));
}
return mappedList; return mappedList;
} }
@ -150,7 +159,7 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
return append(value, Expand.EXPAND_VALUES); return append(value, Expand.EXPAND_VALUES);
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked" })
protected Map<String, Object> append(String key, Object value) { protected Map<String, Object> append(String key, Object value) {
Assert.isInstanceOf(Map.class, this.value, "Value must be a type of Map"); Assert.isInstanceOf(Map.class, this.value, "Value must be a type of Map");
@ -165,6 +174,7 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
return clone; return clone;
} }
@SuppressWarnings("rawtypes")
protected Map<String, Object> appendTo(String key, Object value) { protected Map<String, Object> appendTo(String key, Object value) {
Assert.isInstanceOf(Map.class, this.value, "Value must be a type of Map"); Assert.isInstanceOf(Map.class, this.value, "Value must be a type of Map");

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/DateOperators.java

@ -826,7 +826,7 @@ public class DateOperators {
*/ */
public TsIncrement tsIncrement() { public TsIncrement tsIncrement() {
if(timezone != null && !Timezone.none().equals(timezone)) { if (timezone != null && !Timezone.none().equals(timezone)) {
throw new IllegalArgumentException("$tsIncrement does not support timezones"); throw new IllegalArgumentException("$tsIncrement does not support timezones");
} }
@ -841,7 +841,7 @@ public class DateOperators {
*/ */
public TsSecond tsSecond() { public TsSecond tsSecond() {
if(timezone != null && !Timezone.none().equals(timezone)) { if (timezone != null && !Timezone.none().equals(timezone)) {
throw new IllegalArgumentException("$tsSecond does not support timezones"); throw new IllegalArgumentException("$tsSecond does not support timezones");
} }

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/DensifyOperation.java

@ -354,6 +354,8 @@ public class DensifyOperation implements AggregationOperation {
*/ */
public DensifyOperationBuilder fullRange(Consumer<DensifyRange> consumer) { public DensifyOperationBuilder fullRange(Consumer<DensifyRange> consumer) {
Assert.notNull(consumer, "Consumer must not be null");
DensifyRange range = Range.full(); DensifyRange range = Range.full();
consumer.accept(range); consumer.accept(range);
@ -374,7 +376,7 @@ public class DensifyOperation implements AggregationOperation {
return range(range); return range(range);
} }
DensifyOperation build() { public DensifyOperation build() {
return new DensifyOperation(target.field, target.partitionBy, target.range); return new DensifyOperation(target.field, target.partitionBy, target.range);
} }
} }

113
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ObjectOperators.java

@ -18,10 +18,8 @@ package org.springframework.data.mongodb.core.aggregation;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Set;
import org.bson.Document; import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.Aggregation.SystemVariable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -135,7 +133,7 @@ public class ObjectOperators {
* @since 4.0 * @since 4.0
*/ */
public GetField getField(String fieldName) { public GetField getField(String fieldName) {
return GetField.getField(fieldName).from(value); return GetField.getField(fieldName).of(value);
} }
/** /**
@ -145,7 +143,7 @@ public class ObjectOperators {
* @since 4.0 * @since 4.0
*/ */
public SetField setField(String fieldName) { public SetField setField(String fieldName) {
return SetField.setField(fieldName).of(value); return SetField.field(fieldName).input(value);
} }
/** /**
@ -155,7 +153,7 @@ public class ObjectOperators {
* @since 4.0 * @since 4.0
*/ */
public AggregationExpression removeField(String fieldName) { public AggregationExpression removeField(String fieldName) {
return SetField.setField(fieldName).of(value).toValue(SystemVariable.REMOVE); return SetField.field(fieldName).input(value).toValue(SystemVariable.REMOVE);
} }
} }
@ -329,23 +327,49 @@ public class ObjectOperators {
super(value); super(value);
} }
/**
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given {@code fieldName}.
*
* @param fieldName must not be {@literal null}.
* @return new instance of {@link GetField}.
*/
public static GetField getField(String fieldName) { public static GetField getField(String fieldName) {
return new GetField(Collections.singletonMap("field", fieldName)); return new GetField(Collections.singletonMap("field", fieldName));
} }
/**
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given {@link Field}.
*
* @param field must not be {@literal null}.
* @return new instance of {@link GetField}.
*/
public static GetField getField(Field field) { public static GetField getField(Field field) {
return getField(field.getTarget()); return getField(field.getTarget());
} }
public GetField from(String fieldRef) { /**
return from(Fields.field(fieldRef)); * Creates new {@link GetField aggregation expression} that takes the value pointed to by given
* {@code field reference}.
*
* @param fieldRef must not be {@literal null}.
* @return new instance of {@link GetField}.
*/
public GetField of(String fieldRef) {
return of(Fields.field(fieldRef));
} }
public GetField from(AggregationExpression expression) { /**
return from((Object) expression); * Creates new {@link GetField aggregation expression} that takes the value pointed to by given
* {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link GetField}.
*/
public GetField of(AggregationExpression expression) {
return of((Object) expression);
} }
private GetField from(Object fieldRef) { private GetField of(Object fieldRef) {
return new GetField(append("input", fieldRef)); return new GetField(append("input", fieldRef));
} }
@ -367,34 +391,87 @@ public class ObjectOperators {
super(value); super(value);
} }
public static SetField setField(String fieldName) { /**
* Creates new {@link SetField aggregation expression} that takes the value pointed to by given input
* {@code fieldName}.
*
* @param fieldName must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
public static SetField field(String fieldName) {
return new SetField(Collections.singletonMap("field", fieldName)); return new SetField(Collections.singletonMap("field", fieldName));
} }
public static SetField setField(Field field) { /**
return setField(field.getTarget()); * Creates new {@link SetField aggregation expression} that takes the value pointed to by given input {@link Field}.
*
* @param field must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
public static SetField field(Field field) {
return field(field.getTarget());
} }
public SetField of(String fieldRef) { /**
return of(Fields.field(fieldRef)); * Creates new {@link GetField aggregation expression} that takes the value pointed to by given input
* {@code field reference}.
*
* @param fieldRef must not be {@literal null}.
* @return new instance of {@link GetField}.
*/
public SetField input(String fieldRef) {
return input(Fields.field(fieldRef));
} }
public SetField of(AggregationExpression expression) { /**
return of((Object) expression); * Creates new {@link SetField aggregation expression} that takes the value pointed to by given input
* {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
public SetField input(AggregationExpression expression) {
return input((Object) expression);
} }
private SetField of(Object fieldRef) { /**
* Creates new {@link SetField aggregation expression} that takes the value pointed to by given input
* {@code field reference}.
*
* @param fieldRef must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
private SetField input(Object fieldRef) {
return new SetField(append("input", fieldRef)); return new SetField(append("input", fieldRef));
} }
/**
* Creates new {@link SetField aggregation expression} providing the {@code value} using {@literal fieldReference}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
public SetField toValueOf(String fieldReference) { public SetField toValueOf(String fieldReference) {
return toValue(Fields.field(fieldReference)); return toValue(Fields.field(fieldReference));
} }
/**
* Creates new {@link SetField aggregation expression} providing the {@code value} using
* {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
public SetField toValueOf(AggregationExpression expression) { public SetField toValueOf(AggregationExpression expression) {
return toValue(expression); return toValue(expression);
} }
/**
* Creates new {@link SetField aggregation expression} providing the {@code value}.
*
* @param value
* @return new instance of {@link SetField}.
*/
public SetField toValue(Object value) { public SetField toValue(Object value) {
return new SetField(append("value", value)); return new SetField(append("value", value));
} }

24
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SelectionOperators.java

@ -261,7 +261,7 @@ public class SelectionOperators {
* Limits the number of returned elements to the given value. * Limits the number of returned elements to the given value.
* *
* @param numberOfResults * @param numberOfResults
* @return new instance of {@link Bottom}. * @return new instance of {@link First}.
*/ */
public First limit(int numberOfResults) { public First limit(int numberOfResults) {
return limit((Object) numberOfResults); return limit((Object) numberOfResults);
@ -272,7 +272,7 @@ public class SelectionOperators {
* expression}. * expression}.
* *
* @param expression must not be {@literal null}. * @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}. * @return new instance of {@link First}.
*/ */
public First limit(AggregationExpression expression) { public First limit(AggregationExpression expression) {
return limit((Object) expression); return limit((Object) expression);
@ -286,7 +286,7 @@ public class SelectionOperators {
* Define the field to serve as source. * Define the field to serve as source.
* *
* @param fieldName must not be {@literal null}. * @param fieldName must not be {@literal null}.
* @return new instance of {@link Bottom}. * @return new instance of {@link First}.
*/ */
public First of(String fieldName) { public First of(String fieldName) {
return input(fieldName); return input(fieldName);
@ -296,7 +296,7 @@ public class SelectionOperators {
* Define the expression building the value to serve as source. * Define the expression building the value to serve as source.
* *
* @param expression must not be {@literal null}. * @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}. * @return new instance of {@link First}.
*/ */
public First of(AggregationExpression expression) { public First of(AggregationExpression expression) {
return input(expression); return input(expression);
@ -306,7 +306,7 @@ public class SelectionOperators {
* Define the field to serve as source. * Define the field to serve as source.
* *
* @param fieldName must not be {@literal null}. * @param fieldName must not be {@literal null}.
* @return new instance of {@link Bottom}. * @return new instance of {@link First}.
*/ */
public First input(String fieldName) { public First input(String fieldName) {
return new First(append("input", Fields.field(fieldName))); return new First(append("input", Fields.field(fieldName)));
@ -316,7 +316,7 @@ public class SelectionOperators {
* Define the expression building the value to serve as source. * Define the expression building the value to serve as source.
* *
* @param expression must not be {@literal null}. * @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}. * @return new instance of {@link First}.
*/ */
public First input(AggregationExpression expression) { public First input(AggregationExpression expression) {
return new First(append("input", expression)); return new First(append("input", expression));
@ -355,7 +355,7 @@ public class SelectionOperators {
* Limits the number of returned elements to the given value. * Limits the number of returned elements to the given value.
* *
* @param numberOfResults * @param numberOfResults
* @return new instance of {@link Bottom}. * @return new instance of {@link Last}.
*/ */
public Last limit(int numberOfResults) { public Last limit(int numberOfResults) {
return limit((Object) numberOfResults); return limit((Object) numberOfResults);
@ -366,7 +366,7 @@ public class SelectionOperators {
* expression}. * expression}.
* *
* @param expression must not be {@literal null}. * @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}. * @return new instance of {@link Last}.
*/ */
public Last limit(AggregationExpression expression) { public Last limit(AggregationExpression expression) {
return limit((Object) expression); return limit((Object) expression);
@ -380,7 +380,7 @@ public class SelectionOperators {
* Define the field to serve as source. * Define the field to serve as source.
* *
* @param fieldName must not be {@literal null}. * @param fieldName must not be {@literal null}.
* @return new instance of {@link Bottom}. * @return new instance of {@link Last}.
*/ */
public Last of(String fieldName) { public Last of(String fieldName) {
return input(fieldName); return input(fieldName);
@ -390,7 +390,7 @@ public class SelectionOperators {
* Define the expression building the value to serve as source. * Define the expression building the value to serve as source.
* *
* @param expression must not be {@literal null}. * @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}. * @return new instance of {@link Last}.
*/ */
public Last of(AggregationExpression expression) { public Last of(AggregationExpression expression) {
return input(expression); return input(expression);
@ -400,7 +400,7 @@ public class SelectionOperators {
* Define the field to serve as source. * Define the field to serve as source.
* *
* @param fieldName must not be {@literal null}. * @param fieldName must not be {@literal null}.
* @return new instance of {@link Bottom}. * @return new instance of {@link Last}.
*/ */
public Last input(String fieldName) { public Last input(String fieldName) {
return new Last(append("input", Fields.field(fieldName))); return new Last(append("input", Fields.field(fieldName)));
@ -410,7 +410,7 @@ public class SelectionOperators {
* Define the expression building the value to serve as source. * Define the expression building the value to serve as source.
* *
* @param expression must not be {@literal null}. * @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}. * @return new instance of {@link Last}.
*/ */
public Last input(AggregationExpression expression) { public Last input(AggregationExpression expression) {
return new Last(append("input", expression)); return new Last(append("input", expression));

10
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/DensifyOperationUnitTests.java

@ -30,6 +30,8 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
/** /**
* Unit tests for {@link DensifyOperation}.
*
* @author Christoph Strobl * @author Christoph Strobl
*/ */
class DensifyOperationUnitTests { class DensifyOperationUnitTests {
@ -38,7 +40,8 @@ class DensifyOperationUnitTests {
void rendersFieldNamesAsIsForUntypedContext() { void rendersFieldNamesAsIsForUntypedContext() {
DensifyOperation densify = DensifyOperation.builder().densify("ts") DensifyOperation densify = DensifyOperation.builder().densify("ts")
.range(Range.bounded("2021-05-18T00:00:00", "2021-05-18T08:00:00").incrementBy(1).unit(DensifyUnits.HOUR)).build(); .range(Range.bounded("2021-05-18T00:00:00", "2021-05-18T08:00:00").incrementBy(1).unit(DensifyUnits.HOUR))
.build();
assertThat(densify.toDocument(contextFor(null))).isEqualTo(""" assertThat(densify.toDocument(contextFor(null))).isEqualTo("""
{ {
@ -58,7 +61,8 @@ class DensifyOperationUnitTests {
void rendersFieldNamesCorrectly() { void rendersFieldNamesCorrectly() {
DensifyOperation densify = DensifyOperation.builder().densify("ts") DensifyOperation densify = DensifyOperation.builder().densify("ts")
.range(Range.bounded("2021-05-18T00:00:00", "2021-05-18T08:00:00").incrementBy(1).unit(DensifyUnits.HOUR)).build(); .range(Range.bounded("2021-05-18T00:00:00", "2021-05-18T08:00:00").incrementBy(1).unit(DensifyUnits.HOUR))
.build();
assertThat(densify.toDocument(contextFor(Weather.class))).isEqualTo(""" assertThat(densify.toDocument(contextFor(Weather.class))).isEqualTo("""
{ {
@ -95,7 +99,7 @@ class DensifyOperationUnitTests {
} }
@Test // GH-4139 @Test // GH-4139
void rendersPartitonRangeCorrectly() { void rendersPartitionRangeCorrectly() {
DensifyOperation densify = DensifyOperation.builder().densify("alt").partitionBy("var") DensifyOperation densify = DensifyOperation.builder().densify("alt").partitionBy("var")
.partitionRange(range -> range.incrementBy(200)).build(); .partitionRange(range -> range.incrementBy(200)).build();

Loading…
Cancel
Save