DATAMONGO-2403 - Polishing.

Use handle(…) to skip values instead of flatMap(…) to reduce overhead.

Original pull request: #804.
This commit is contained in:
Mark Paluch
2019-11-08 13:47:11 +01:00
parent b014fe4c7c
commit 0e0b45597b
3 changed files with 17 additions and 9 deletions

View File

@@ -36,6 +36,7 @@ import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider; import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
import org.springframework.data.repository.query.RepositoryQuery; import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.ResultProcessor; import org.springframework.data.repository.query.ResultProcessor;
import org.springframework.data.util.TypeInformation;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -117,17 +118,18 @@ public abstract class AbstractReactiveMongoQuery implements RepositoryQuery {
private Object execute(MongoParameterAccessor parameterAccessor) { private Object execute(MongoParameterAccessor parameterAccessor) {
ConvertingParameterAccessor convertingParamterAccessor = new ConvertingParameterAccessor(operations.getConverter(), ConvertingParameterAccessor accessor = new ConvertingParameterAccessor(operations.getConverter(),
parameterAccessor); parameterAccessor);
ResultProcessor processor = method.getResultProcessor().withDynamicProjection(convertingParamterAccessor); TypeInformation<?> returnType = method.getReturnType();
ResultProcessor processor = method.getResultProcessor().withDynamicProjection(accessor);
Class<?> typeToRead = processor.getReturnedType().getTypeToRead(); Class<?> typeToRead = processor.getReturnedType().getTypeToRead();
if(typeToRead == null && method.getReturnType().getComponentType() != null) { if (typeToRead == null && returnType.getComponentType() != null) {
typeToRead = method.getReturnType().getComponentType().getType(); typeToRead = returnType.getComponentType().getType();
} }
return doExecute(method, processor, convertingParamterAccessor, typeToRead); return doExecute(method, processor, accessor, typeToRead);
} }
/** /**

View File

@@ -16,11 +16,11 @@
package org.springframework.data.mongodb.repository.query; package org.springframework.data.mongodb.repository.query;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List; import java.util.List;
import org.bson.Document; import org.bson.Document;
import org.springframework.data.mongodb.core.ReactiveMongoOperations; import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation; import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
@@ -95,8 +95,14 @@ public class ReactiveStringBasedAggregation extends AbstractReactiveMongoQuery {
Flux<?> flux = reactiveMongoOperations.aggregate(aggregation, targetType); Flux<?> flux = reactiveMongoOperations.aggregate(aggregation, targetType);
if (isSimpleReturnType && !isRawReturnType) { if (isSimpleReturnType && !isRawReturnType) {
flux = flux.flatMap( flux = flux.handle((it, sink) -> {
it -> Mono.justOrEmpty(AggregationUtils.extractSimpleTypeResult((Document) it, typeToRead, mongoConverter)));
Object result = AggregationUtils.extractSimpleTypeResult((Document) it, typeToRead, mongoConverter);
if (result != null) {
sink.next(result);
}
});
} }
if (method.isCollectionQuery()) { if (method.isCollectionQuery()) {

View File

@@ -546,7 +546,7 @@ public class ReactiveMongoRepositoryTests {
} }
@Test // DATAMONGO-2403 @Test // DATAMONGO-2403
public void annotatedAggregationExtractingSimpleValueEmitsEmptyMonoForEmptyDocument() { public void annotatedAggregationExtractingSimpleValueIsEmptyForEmptyDocument() {
Person p = new Person("project-on-lastanme", null); Person p = new Person("project-on-lastanme", null);
repository.save(p).then().as(StepVerifier::create).verifyComplete(); repository.save(p).then().as(StepVerifier::create).verifyComplete();