diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableAggregationOperationSupport.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableAggregationOperationSupport.java index e6867a89d..f0c7e6267 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableAggregationOperationSupport.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableAggregationOperationSupport.java @@ -35,22 +35,10 @@ import org.springframework.util.StringUtils; * @author Mark Paluch * @since 2.0 */ +@RequiredArgsConstructor class ExecutableAggregationOperationSupport implements ExecutableAggregationOperation { - private final MongoTemplate template; - - /** - * Create new instance of {@link ExecutableAggregationOperationSupport}. - * - * @param template must not be {@literal null}. - * @throws IllegalArgumentException if template is {@literal null}. - */ - ExecutableAggregationOperationSupport(MongoTemplate template) { - - Assert.notNull(template, "Template must not be null!"); - - this.template = template; - } + private final @NonNull MongoTemplate template; /* * (non-Javadoc) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableFindOperationSupport.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableFindOperationSupport.java index 1987a9cce..cbfbf4d0f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableFindOperationSupport.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableFindOperationSupport.java @@ -45,24 +45,12 @@ import com.mongodb.client.FindIterable; * @author Mark Paluch * @since 2.0 */ +@RequiredArgsConstructor class ExecutableFindOperationSupport implements ExecutableFindOperation { private static final Query ALL_QUERY = new Query(); - private final MongoTemplate template; - - /** - * Create new {@link ExecutableFindOperationSupport}. - * - * @param template must not be {@literal null}. - * @throws IllegalArgumentException if template is {@literal null}. - */ - ExecutableFindOperationSupport(MongoTemplate template) { - - Assert.notNull(template, "Template must not be null!"); - - this.template = template; - } + private final @NonNull MongoTemplate template; /* * (non-Javadoc) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableInsertOperationSupport.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableInsertOperationSupport.java index aee2fed70..512af9efb 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableInsertOperationSupport.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableInsertOperationSupport.java @@ -37,22 +37,10 @@ import com.mongodb.bulk.BulkWriteResult; * @author Mark Paluch * @since 2.0 */ +@RequiredArgsConstructor class ExecutableInsertOperationSupport implements ExecutableInsertOperation { - private final MongoTemplate template; - - /** - * Create new {@link ExecutableInsertOperationSupport}. - * - * @param template must not be {@literal null}. - * @throws IllegalArgumentException if template is {@literal null}. - */ - ExecutableInsertOperationSupport(MongoTemplate template) { - - Assert.notNull(template, "Template must not be null!"); - - this.template = template; - } + private final @NonNull MongoTemplate template; /* * (non-Javadoc) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableMapReduceOperationSupport.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableMapReduceOperationSupport.java index 3e7defa53..989d7c4c2 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableMapReduceOperationSupport.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableMapReduceOperationSupport.java @@ -17,6 +17,8 @@ package org.springframework.data.mongodb.core; import java.util.List; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions; import org.springframework.data.mongodb.core.query.Query; import org.springframework.lang.Nullable; @@ -29,23 +31,12 @@ import org.springframework.util.StringUtils; * @author Christoph Strobl * @since 2.1 */ +@RequiredArgsConstructor class ExecutableMapReduceOperationSupport implements ExecutableMapReduceOperation { private static final Query ALL_QUERY = new Query(); - private final MongoTemplate template; - - /** - * Create new {@link ExecutableMapReduceOperationSupport}. - * - * @param template must not be {@literal null}. - * @throws IllegalArgumentException if template is {@literal null}. - */ - ExecutableMapReduceOperationSupport(MongoTemplate template) { - - Assert.notNull(template, "Template must not be null!"); - this.template = template; - } + private final @NonNull MongoTemplate template; /* * (non-Javascript) @@ -56,7 +47,7 @@ class ExecutableMapReduceOperationSupport implements ExecutableMapReduceOperatio Assert.notNull(domainType, "DomainType must not be null!"); - return new ExecutableMapReduceSupport(template, domainType, domainType, null, ALL_QUERY, null, null, null); + return new ExecutableMapReduceSupport<>(template, domainType, domainType, null, ALL_QUERY, null, null, null); } /** @@ -76,8 +67,8 @@ class ExecutableMapReduceOperationSupport implements ExecutableMapReduceOperatio private final @Nullable String reduceFunction; private final @Nullable MapReduceOptions options; - ExecutableMapReduceSupport(MongoTemplate template, Class domainType, Class returnType, String collection, - Query query, @Nullable String mapFunction, @Nullable String reduceFunction, MapReduceOptions options) { + ExecutableMapReduceSupport(MongoTemplate template, Class domainType, Class returnType, @Nullable String collection, + Query query, @Nullable String mapFunction, @Nullable String reduceFunction, @Nullable MapReduceOptions options) { this.template = template; this.domainType = domainType; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableRemoveOperationSupport.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableRemoveOperationSupport.java index cba5343de..a27108eee 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableRemoveOperationSupport.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableRemoveOperationSupport.java @@ -36,24 +36,12 @@ import com.mongodb.client.result.DeleteResult; * @author Mark Paluch * @since 2.0 */ +@RequiredArgsConstructor class ExecutableRemoveOperationSupport implements ExecutableRemoveOperation { private static final Query ALL_QUERY = new Query(); - private final MongoTemplate tempate; - - /** - * Create new {@link ExecutableRemoveOperationSupport}. - * - * @param template must not be {@literal null}. - * @throws IllegalArgumentException if template is {@literal null}. - */ - ExecutableRemoveOperationSupport(MongoTemplate template) { - - Assert.notNull(template, "Template must not be null!"); - - this.tempate = template; - } + private final @NonNull MongoTemplate tempate; /* * (non-Javadoc) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableUpdateOperationSupport.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableUpdateOperationSupport.java index e42a0dc44..fb3041ca8 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableUpdateOperationSupport.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableUpdateOperationSupport.java @@ -35,23 +35,12 @@ import com.mongodb.client.result.UpdateResult; * @author Mark Paluch * @since 2.0 */ +@RequiredArgsConstructor class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation { private static final Query ALL_QUERY = new Query(); - private final MongoTemplate template; - - /** - * Creates new {@link ExecutableUpdateOperationSupport}. - * - * @param template must not be {@literal null}. - */ - ExecutableUpdateOperationSupport(MongoTemplate template) { - - Assert.notNull(template, "Template must not be null!"); - - this.template = template; - } + private final @NonNull MongoTemplate template; /* * (non-Javadoc) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 421ebffa2..0932c2409 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -1816,7 +1816,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, public MapReduceResults mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, @Nullable MapReduceOptions mapReduceOptions, Class entityClass) { - return new MapReduceResults( + return new MapReduceResults<>( mapReduce(query, entityClass, inputCollectionName, mapFunction, reduceFunction, mapReduceOptions, entityClass), new Document()); } @@ -1829,38 +1829,35 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, * @param reduceFunction * @param mapReduceOptions * @param resultType - * @param * @return * @since 2.1 */ public List mapReduce(Query query, Class domainType, String inputCollectionName, String mapFunction, String reduceFunction, @Nullable MapReduceOptions mapReduceOptions, Class resultType) { - Assert.notNull(query, "Query must not be null!"); - Assert.notNull(inputCollectionName, "InputCollectionName must not be null!"); - Assert.notNull(resultType, "EntityClass must not be null!"); - Assert.notNull(reduceFunction, "ReduceFunction must not be null!"); - Assert.notNull(mapFunction, "MapFunction must not be null!"); + Assert.notNull(domainType, "Domain type must not be null!"); + Assert.notNull(inputCollectionName, "Input collection name must not be null!"); + Assert.notNull(resultType, "Result type must not be null!"); + Assert.notNull(mapFunction, "Map function must not be null!"); + Assert.notNull(reduceFunction, "Reduce function must not be null!"); String mapFunc = replaceWithResourceIfNecessary(mapFunction); String reduceFunc = replaceWithResourceIfNecessary(reduceFunction); MongoCollection inputCollection = getAndPrepareCollection(doGetDatabase(), inputCollectionName); // MapReduceOp - MapReduceIterable result = inputCollection.mapReduce(mapFunc, reduceFunc, Document.class); - if (query != null && result != null) { + MapReduceIterable mapReduce = inputCollection.mapReduce(mapFunc, reduceFunc, Document.class); - if (query.getLimit() > 0 && mapReduceOptions.getLimit() == null) { - result = result.limit(query.getLimit()); - } - if (query.getMeta() != null && query.getMeta().getMaxTimeMsec() != null) { - result = result.maxTime(query.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS); - } - result = result.sort(getMappedSortObject(query, domainType)); - - result = result - .filter(queryMapper.getMappedObject(query.getQueryObject(), mappingContext.getPersistentEntity(domainType))); + if (query.getLimit() > 0 && mapReduceOptions != null && mapReduceOptions.getLimit() == null) { + mapReduce = mapReduce.limit(query.getLimit()); } + if (query.getMeta().getMaxTimeMsec() != null) { + mapReduce = mapReduce.maxTime(query.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS); + } + mapReduce = mapReduce.sort(getMappedSortObject(query, domainType)); + + mapReduce = mapReduce + .filter(queryMapper.getMappedObject(query.getQueryObject(), mappingContext.getPersistentEntity(domainType))); Optional collation = query.getCollation(); @@ -1876,28 +1873,28 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, } if (!CollectionUtils.isEmpty(mapReduceOptions.getScopeVariables())) { - result = result.scope(new Document(mapReduceOptions.getScopeVariables())); + mapReduce = mapReduce.scope(new Document(mapReduceOptions.getScopeVariables())); } - if (mapReduceOptions.getLimit() != null && mapReduceOptions.getLimit().intValue() > 0) { - result = result.limit(mapReduceOptions.getLimit()); + if (mapReduceOptions.getLimit() != null && mapReduceOptions.getLimit() > 0) { + mapReduce = mapReduce.limit(mapReduceOptions.getLimit()); } if (mapReduceOptions.getFinalizeFunction().filter(StringUtils::hasText).isPresent()) { - result = result.finalizeFunction(mapReduceOptions.getFinalizeFunction().get()); + mapReduce = mapReduce.finalizeFunction(mapReduceOptions.getFinalizeFunction().get()); } if (mapReduceOptions.getJavaScriptMode() != null) { - result = result.jsMode(mapReduceOptions.getJavaScriptMode()); + mapReduce = mapReduce.jsMode(mapReduceOptions.getJavaScriptMode()); } if (mapReduceOptions.getOutputSharded().isPresent()) { - result = result.sharded(mapReduceOptions.getOutputSharded().get()); + mapReduce = mapReduce.sharded(mapReduceOptions.getOutputSharded().get()); } } - result = collation.map(Collation::toMongoCollation).map(result::collation).orElse(result); + mapReduce = collation.map(Collation::toMongoCollation).map(mapReduce::collation).orElse(mapReduce); - List mappedResults = new ArrayList(); - DocumentCallback callback = new ReadDocumentCallback(mongoConverter, resultType, inputCollectionName); + List mappedResults = new ArrayList<>(); + DocumentCallback callback = new ReadDocumentCallback<>(mongoConverter, resultType, inputCollectionName); - for (Document document : result) { + for (Document document : mapReduce) { mappedResults.add(callback.doWith(document)); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMapReduceOperationSupport.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMapReduceOperationSupport.java index c1a48c4a4..036424125 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMapReduceOperationSupport.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMapReduceOperationSupport.java @@ -15,6 +15,8 @@ */ package org.springframework.data.mongodb.core; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; import reactor.core.publisher.Flux; import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions; @@ -29,23 +31,12 @@ import org.springframework.util.StringUtils; * @author Christoph Strobl * @since 2.1 */ +@RequiredArgsConstructor class ReactiveMapReduceOperationSupport implements ReactiveMapReduceOperation { private static final Query ALL_QUERY = new Query(); - private final ReactiveMongoTemplate template; - - /** - * Create new {@link ReactiveMapReduceOperationSupport}. - * - * @param template must not be {@literal null}. - * @throws IllegalArgumentException if template is {@literal null}. - */ - ReactiveMapReduceOperationSupport(ReactiveMongoTemplate template) { - - Assert.notNull(template, "Template must not be null!"); - this.template = template; - } + private final @NonNull ReactiveMongoTemplate template; /* * (non-Javascript) @@ -56,7 +47,7 @@ class ReactiveMapReduceOperationSupport implements ReactiveMapReduceOperation { Assert.notNull(domainType, "DomainType must not be null!"); - return new ReactiveMapReduceSupport(template, domainType, domainType, null, ALL_QUERY, null, null, null); + return new ReactiveMapReduceSupport<>(template, domainType, domainType, null, ALL_QUERY, null, null, null); } /** @@ -77,8 +68,8 @@ class ReactiveMapReduceOperationSupport implements ReactiveMapReduceOperation { private final @Nullable MapReduceOptions options; ReactiveMapReduceSupport(ReactiveMongoTemplate template, Class domainType, Class returnType, - String collection, Query query, @Nullable String mapFunction, @Nullable String reduceFunction, - MapReduceOptions options) { + @Nullable String collection, Query query, @Nullable String mapFunction, @Nullable String reduceFunction, + @Nullable MapReduceOptions options) { this.template = template; this.domainType = domainType; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java index e2fd433ea..6a64a0476 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java @@ -1975,9 +1975,10 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati Assert.notNull(domainType, "Domain type must not be null!"); Assert.hasText(inputCollectionName, "Input collection name must not be null or empty!"); Assert.notNull(resultType, "Result type must not be null!"); - Assert.notNull(options, "MapReduceOptions must not be null!"); Assert.notNull(mapFunction, "Map function must not be null!"); Assert.notNull(reduceFunction, "Reduce function must not be null!"); + Assert.notNull(options, "MapReduceOptions must not be null!"); + assertLocalFunctionNames(mapFunction, reduceFunction); return createFlux(inputCollectionName, collection -> { diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensionsTests.kt index 8b97e1007..630e4d953 100644 --- a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensionsTests.kt +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensionsTests.kt @@ -35,7 +35,6 @@ class ExecutableMapReduceOperationExtensionsTests { @Mock(answer = Answers.RETURNS_MOCKS) lateinit var operationWithProjection: ExecutableMapReduceOperation.MapReduceWithProjection - @Test // DATAMONGO-1929 fun `ExecutableMapReduceOperation#mapReduce(KClass) extension should call its Java counterpart`() { diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensionsTests.kt index c057b3034..55a8dd8a6 100644 --- a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensionsTests.kt +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensionsTests.kt @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.mongodb.core import com.nhaarman.mockito_kotlin.verify