Skip to content

Commit d297f5a

Browse files
committed
DATAMONGO-1538 - Polishing.
Use InheritingExposedFieldsAggregationOperationContext instead of anonymous context class for condition mapping. Drop aggregation input collections before tests. Minor reformatting. Original pull request: spring-projects#417.
1 parent 696e53f commit d297f5a

File tree

3 files changed

+22
-57
lines changed

3 files changed

+22
-57
lines changed

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

+18-54
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Filter.AsBuilder;
3030
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Let.ExpressionVariable;
3131
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
32-
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
3332
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
3433
import org.springframework.util.Assert;
3534
import org.springframework.util.ClassUtils;
@@ -3903,31 +3902,19 @@ public static AsBuilder filter(List<?> values) {
39033902
*/
39043903
@Override
39053904
public Document toDocument(final AggregationOperationContext context) {
3906-
3907-
return toFilter(new ExposedFieldsAggregationOperationContext(ExposedFields.from(as), context) {
3908-
3909-
@Override
3910-
public FieldReference getReference(Field field) {
3911-
3912-
FieldReference ref = null;
3913-
try {
3914-
ref = context.getReference(field);
3915-
} catch (Exception e) {
3916-
// just ignore that one.
3917-
}
3918-
return ref != null ? ref : super.getReference(field);
3919-
}
3920-
});
3905+
return toFilter(ExposedFields.from(as), context);
39213906
}
39223907

3923-
private Document toFilter(AggregationOperationContext context) {
3908+
private Document toFilter(ExposedFields exposedFields, AggregationOperationContext context) {
39243909

39253910
Document filterExpression = new Document();
3911+
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
3912+
exposedFields, context);
39263913

39273914
filterExpression.putAll(context.getMappedObject(new Document("input", getMappedInput(context))));
39283915
filterExpression.put("as", as.getTarget());
39293916

3930-
filterExpression.putAll(context.getMappedObject(new Document("cond", getMappedCondition(context))));
3917+
filterExpression.putAll(context.getMappedObject(new Document("cond", getMappedCondition(operationContext))));
39313918

39323919
return new Document("$filter", filterExpression);
39333920
}
@@ -6017,27 +6004,14 @@ public Map andApply(final AggregationExpression expression) {
60176004

60186005
@Override
60196006
public Document toDocument(final AggregationOperationContext context) {
6020-
6021-
return toMap(new ExposedFieldsAggregationOperationContext(
6022-
ExposedFields.synthetic(Fields.fields(itemVariableName)), context) {
6023-
6024-
@Override
6025-
public FieldReference getReference(Field field) {
6026-
6027-
FieldReference ref = null;
6028-
try {
6029-
ref = context.getReference(field);
6030-
} catch (Exception e) {
6031-
// just ignore that one.
6032-
}
6033-
return ref != null ? ref : super.getReference(field);
6034-
}
6035-
});
6007+
return toMap(ExposedFields.synthetic(Fields.fields(itemVariableName)), context);
60366008
}
60376009

6038-
private Document toMap(AggregationOperationContext context) {
6010+
private Document toMap(ExposedFields exposedFields, AggregationOperationContext context) {
60396011

60406012
Document map = new Document();
6013+
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
6014+
exposedFields, context);
60416015

60426016
Document input;
60436017
if (sourceArray instanceof Field) {
@@ -6048,7 +6022,8 @@ private Document toMap(AggregationOperationContext context) {
60486022

60496023
map.putAll(context.getMappedObject(input));
60506024
map.put("as", itemVariableName);
6051-
map.put("in", functionToApply.toDocument(new NestedDelegatingExpressionAggregationOperationContext(context)));
6025+
map.put("in",
6026+
functionToApply.toDocument(new NestedDelegatingExpressionAggregationOperationContext(operationContext)));
60526027

60536028
return new Document("$map", map);
60546029
}
@@ -6790,22 +6765,7 @@ public interface LetBuilder {
67906765

67916766
@Override
67926767
public Document toDocument(final AggregationOperationContext context) {
6793-
6794-
return toLet(new ExposedFieldsAggregationOperationContext(
6795-
ExposedFields.synthetic(Fields.fields(getVariableNames())), context) {
6796-
6797-
@Override
6798-
public FieldReference getReference(Field field) {
6799-
6800-
FieldReference ref = null;
6801-
try {
6802-
ref = context.getReference(field);
6803-
} catch (Exception e) {
6804-
// just ignore that one.
6805-
}
6806-
return ref != null ? ref : super.getReference(field);
6807-
}
6808-
});
6768+
return toLet(ExposedFields.synthetic(Fields.fields(getVariableNames())), context);
68096769
}
68106770

68116771
private String[] getVariableNames() {
@@ -6814,20 +6774,24 @@ private String[] getVariableNames() {
68146774
for (int i = 0; i < this.vars.size(); i++) {
68156775
varNames[i] = this.vars.get(i).variableName;
68166776
}
6777+
68176778
return varNames;
68186779
}
68196780

6820-
private Document toLet(AggregationOperationContext context) {
6781+
private Document toLet(ExposedFields exposedFields, AggregationOperationContext context) {
68216782

68226783
Document letExpression = new Document();
68236784

68246785
Document mappedVars = new Document();
6786+
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
6787+
exposedFields, context);
6788+
68256789
for (ExpressionVariable var : this.vars) {
68266790
mappedVars.putAll(getMappedVariable(var, context));
68276791
}
68286792

68296793
letExpression.put("vars", mappedVars);
6830-
letExpression.put("in", getMappedIn(context));
6794+
letExpression.put("in", getMappedIn(operationContext));
68316795

68326796
return new Document("$let", letExpression);
68336797
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.data.mongodb.core.aggregation;
1817

1918
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
20-
import org.springframework.util.Assert;
2119

2220
/**
2321
* {@link ExposedFieldsAggregationOperationContext} that inherits fields from its parent
2422
* {@link AggregationOperationContext}.
2523
*
2624
* @author Mark Paluch
25+
* @since 1.9
2726
*/
2827
class InheritingExposedFieldsAggregationOperationContext extends ExposedFieldsAggregationOperationContext {
2928

@@ -40,7 +39,7 @@ public InheritingExposedFieldsAggregationOperationContext(ExposedFields exposedF
4039
AggregationOperationContext previousContext) {
4140

4241
super(exposedFields, previousContext);
43-
Assert.notNull(previousContext, "PreviousContext must not be null!");
42+
4443
this.previousContext = previousContext;
4544
}
4645

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

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ private void cleanDb() {
138138
mongoTemplate.dropCollection(MeterData.class);
139139
mongoTemplate.dropCollection(LineItem.class);
140140
mongoTemplate.dropCollection(InventoryItem.class);
141+
mongoTemplate.dropCollection(Sales.class);
142+
mongoTemplate.dropCollection(Sales2.class);
141143
}
142144

143145
/**

0 commit comments

Comments
 (0)