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.
This commit is contained in:
Mark Paluch
2016-12-06 15:26:35 +01:00
parent 696e53ff60
commit d297f5a253
3 changed files with 22 additions and 57 deletions

View File

@@ -29,7 +29,6 @@ import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Filter.AsBuilder;
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Let.ExpressionVariable;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -3903,31 +3902,19 @@ public interface AggregationExpressions {
*/
@Override
public Document toDocument(final AggregationOperationContext context) {
return toFilter(new ExposedFieldsAggregationOperationContext(ExposedFields.from(as), context) {
@Override
public FieldReference getReference(Field field) {
FieldReference ref = null;
try {
ref = context.getReference(field);
} catch (Exception e) {
// just ignore that one.
}
return ref != null ? ref : super.getReference(field);
}
});
return toFilter(ExposedFields.from(as), context);
}
private Document toFilter(AggregationOperationContext context) {
private Document toFilter(ExposedFields exposedFields, AggregationOperationContext context) {
Document filterExpression = new Document();
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
exposedFields, context);
filterExpression.putAll(context.getMappedObject(new Document("input", getMappedInput(context))));
filterExpression.put("as", as.getTarget());
filterExpression.putAll(context.getMappedObject(new Document("cond", getMappedCondition(context))));
filterExpression.putAll(context.getMappedObject(new Document("cond", getMappedCondition(operationContext))));
return new Document("$filter", filterExpression);
}
@@ -6017,27 +6004,14 @@ public interface AggregationExpressions {
@Override
public Document toDocument(final AggregationOperationContext context) {
return toMap(new ExposedFieldsAggregationOperationContext(
ExposedFields.synthetic(Fields.fields(itemVariableName)), context) {
@Override
public FieldReference getReference(Field field) {
FieldReference ref = null;
try {
ref = context.getReference(field);
} catch (Exception e) {
// just ignore that one.
}
return ref != null ? ref : super.getReference(field);
}
});
return toMap(ExposedFields.synthetic(Fields.fields(itemVariableName)), context);
}
private Document toMap(AggregationOperationContext context) {
private Document toMap(ExposedFields exposedFields, AggregationOperationContext context) {
Document map = new Document();
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
exposedFields, context);
Document input;
if (sourceArray instanceof Field) {
@@ -6048,7 +6022,8 @@ public interface AggregationExpressions {
map.putAll(context.getMappedObject(input));
map.put("as", itemVariableName);
map.put("in", functionToApply.toDocument(new NestedDelegatingExpressionAggregationOperationContext(context)));
map.put("in",
functionToApply.toDocument(new NestedDelegatingExpressionAggregationOperationContext(operationContext)));
return new Document("$map", map);
}
@@ -6790,22 +6765,7 @@ public interface AggregationExpressions {
@Override
public Document toDocument(final AggregationOperationContext context) {
return toLet(new ExposedFieldsAggregationOperationContext(
ExposedFields.synthetic(Fields.fields(getVariableNames())), context) {
@Override
public FieldReference getReference(Field field) {
FieldReference ref = null;
try {
ref = context.getReference(field);
} catch (Exception e) {
// just ignore that one.
}
return ref != null ? ref : super.getReference(field);
}
});
return toLet(ExposedFields.synthetic(Fields.fields(getVariableNames())), context);
}
private String[] getVariableNames() {
@@ -6814,20 +6774,24 @@ public interface AggregationExpressions {
for (int i = 0; i < this.vars.size(); i++) {
varNames[i] = this.vars.get(i).variableName;
}
return varNames;
}
private Document toLet(AggregationOperationContext context) {
private Document toLet(ExposedFields exposedFields, AggregationOperationContext context) {
Document letExpression = new Document();
Document mappedVars = new Document();
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
exposedFields, context);
for (ExpressionVariable var : this.vars) {
mappedVars.putAll(getMappedVariable(var, context));
}
letExpression.put("vars", mappedVars);
letExpression.put("in", getMappedIn(context));
letExpression.put("in", getMappedIn(operationContext));
return new Document("$let", letExpression);
}

View File

@@ -13,17 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.aggregation;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.util.Assert;
/**
* {@link ExposedFieldsAggregationOperationContext} that inherits fields from its parent
* {@link AggregationOperationContext}.
*
* @author Mark Paluch
* @since 1.9
*/
class InheritingExposedFieldsAggregationOperationContext extends ExposedFieldsAggregationOperationContext {
@@ -40,7 +39,7 @@ class InheritingExposedFieldsAggregationOperationContext extends ExposedFieldsAg
AggregationOperationContext previousContext) {
super(exposedFields, previousContext);
Assert.notNull(previousContext, "PreviousContext must not be null!");
this.previousContext = previousContext;
}

View File

@@ -138,6 +138,8 @@ public class AggregationTests {
mongoTemplate.dropCollection(MeterData.class);
mongoTemplate.dropCollection(LineItem.class);
mongoTemplate.dropCollection(InventoryItem.class);
mongoTemplate.dropCollection(Sales.class);
mongoTemplate.dropCollection(Sales2.class);
}
/**