Skip to content

Commit 3d8b686

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: #417.
1 parent 68db0d4 commit 3d8b686

File tree

3 files changed

+22
-58
lines changed

3 files changed

+22
-58
lines changed

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

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Filter.AsBuilder;
2929
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Let.ExpressionVariable;
3030
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
31-
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
3231
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
3332
import org.springframework.util.Assert;
3433
import org.springframework.util.ClassUtils;
@@ -3905,31 +3904,19 @@ public static AsBuilder filter(List<?> values) {
39053904
*/
39063905
@Override
39073906
public DBObject toDbObject(final AggregationOperationContext context) {
3908-
3909-
return toFilter(new ExposedFieldsAggregationOperationContext(ExposedFields.from(as), context) {
3910-
3911-
@Override
3912-
public FieldReference getReference(Field field) {
3913-
3914-
FieldReference ref = null;
3915-
try {
3916-
ref = context.getReference(field);
3917-
} catch (Exception e) {
3918-
// just ignore that one.
3919-
}
3920-
return ref != null ? ref : super.getReference(field);
3921-
}
3922-
});
3907+
return toFilter(ExposedFields.from(as), context);
39233908
}
39243909

3925-
private DBObject toFilter(AggregationOperationContext context) {
3910+
private DBObject toFilter(ExposedFields exposedFields, AggregationOperationContext context) {
39263911

39273912
DBObject filterExpression = new BasicDBObject();
3913+
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
3914+
exposedFields, context);
39283915

39293916
filterExpression.putAll(context.getMappedObject(new BasicDBObject("input", getMappedInput(context))));
39303917
filterExpression.put("as", as.getTarget());
39313918

3932-
filterExpression.putAll(context.getMappedObject(new BasicDBObject("cond", getMappedCondition(context))));
3919+
filterExpression.putAll(context.getMappedObject(new BasicDBObject("cond", getMappedCondition(operationContext))));
39333920

39343921
return new BasicDBObject("$filter", filterExpression);
39353922
}
@@ -6019,27 +6006,14 @@ public Map andApply(final AggregationExpression expression) {
60196006

60206007
@Override
60216008
public DBObject toDbObject(final AggregationOperationContext context) {
6022-
6023-
return toMap(new ExposedFieldsAggregationOperationContext(
6024-
ExposedFields.synthetic(Fields.fields(itemVariableName)), context) {
6025-
6026-
@Override
6027-
public FieldReference getReference(Field field) {
6028-
6029-
FieldReference ref = null;
6030-
try {
6031-
ref = context.getReference(field);
6032-
} catch (Exception e) {
6033-
// just ignore that one.
6034-
}
6035-
return ref != null ? ref : super.getReference(field);
6036-
}
6037-
});
6009+
return toMap(ExposedFields.synthetic(Fields.fields(itemVariableName)), context);
60386010
}
60396011

6040-
private DBObject toMap(AggregationOperationContext context) {
6012+
private DBObject toMap(ExposedFields exposedFields, AggregationOperationContext context) {
60416013

60426014
BasicDBObject map = new BasicDBObject();
6015+
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
6016+
exposedFields, context);
60436017

60446018
BasicDBObject input;
60456019
if (sourceArray instanceof Field) {
@@ -6050,7 +6024,8 @@ private DBObject toMap(AggregationOperationContext context) {
60506024

60516025
map.putAll(context.getMappedObject(input));
60526026
map.put("as", itemVariableName);
6053-
map.put("in", functionToApply.toDbObject(new NestedDelegatingExpressionAggregationOperationContext(context)));
6027+
map.put("in",
6028+
functionToApply.toDbObject(new NestedDelegatingExpressionAggregationOperationContext(operationContext)));
60546029

60556030
return new BasicDBObject("$map", map);
60566031
}
@@ -6792,22 +6767,7 @@ public interface LetBuilder {
67926767

67936768
@Override
67946769
public DBObject toDbObject(final AggregationOperationContext context) {
6795-
6796-
return toLet(new ExposedFieldsAggregationOperationContext(
6797-
ExposedFields.synthetic(Fields.fields(getVariableNames())), context) {
6798-
6799-
@Override
6800-
public FieldReference getReference(Field field) {
6801-
6802-
FieldReference ref = null;
6803-
try {
6804-
ref = context.getReference(field);
6805-
} catch (Exception e) {
6806-
// just ignore that one.
6807-
}
6808-
return ref != null ? ref : super.getReference(field);
6809-
}
6810-
});
6770+
return toLet(ExposedFields.synthetic(Fields.fields(getVariableNames())), context);
68116771
}
68126772

68136773
private String[] getVariableNames() {
@@ -6816,20 +6776,23 @@ private String[] getVariableNames() {
68166776
for (int i = 0; i < this.vars.size(); i++) {
68176777
varNames[i] = this.vars.get(i).variableName;
68186778
}
6779+
68196780
return varNames;
68206781
}
68216782

6822-
private DBObject toLet(AggregationOperationContext context) {
6783+
private DBObject toLet(ExposedFields exposedFields, AggregationOperationContext context) {
68236784

68246785
DBObject letExpression = new BasicDBObject();
6825-
68266786
DBObject mappedVars = new BasicDBObject();
6787+
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
6788+
exposedFields, context);
6789+
68276790
for (ExpressionVariable var : this.vars) {
68286791
mappedVars.putAll(getMappedVariable(var, context));
68296792
}
68306793

68316794
letExpression.put("vars", mappedVars);
6832-
letExpression.put("in", getMappedIn(context));
6795+
letExpression.put("in", getMappedIn(operationContext));
68336796

68346797
return new BasicDBObject("$let", letExpression);
68356798
}

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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ private void cleanDb() {
143143
mongoTemplate.dropCollection(MeterData.class);
144144
mongoTemplate.dropCollection(LineItem.class);
145145
mongoTemplate.dropCollection(InventoryItem.class);
146+
mongoTemplate.dropCollection(Sales.class);
147+
mongoTemplate.dropCollection(Sales2.class);
146148
}
147149

148150
/**

0 commit comments

Comments
 (0)