Search in sources :

Example 1 with MapReduceResults

use of org.springframework.data.mongodb.core.mapreduce.MapReduceResults in project spring-data-mongodb by spring-projects.

the class MongoTemplate method mapReduce.

@Override
public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, @Nullable MapReduceOptions mapReduceOptions, Class<T> entityClass) {
    Assert.notNull(query, "Query must not be null!");
    Assert.notNull(inputCollectionName, "InputCollectionName must not be null!");
    Assert.notNull(entityClass, "EntityClass must not be null!");
    Assert.notNull(reduceFunction, "ReduceFunction must not be null!");
    Assert.notNull(mapFunction, "MapFunction must not be null!");
    String mapFunc = replaceWithResourceIfNecessary(mapFunction);
    String reduceFunc = replaceWithResourceIfNecessary(reduceFunction);
    MongoCollection<Document> inputCollection = getAndPrepareCollection(doGetDatabase(), inputCollectionName);
    // MapReduceOp
    MapReduceIterable<Document> result = inputCollection.mapReduce(mapFunc, reduceFunc, Document.class);
    if (query != null && result != null) {
        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, entityClass));
        result = result.filter(queryMapper.getMappedObject(query.getQueryObject(), Optional.empty()));
    }
    Optional<Collation> collation = query.getCollation();
    if (mapReduceOptions != null) {
        Optionals.ifAllPresent(collation, mapReduceOptions.getCollation(), (l, r) -> {
            throw new IllegalArgumentException("Both Query and MapReduceOptions define a collation. Please provide the collation only via one of the two.");
        });
        if (mapReduceOptions.getCollation().isPresent()) {
            collation = mapReduceOptions.getCollation();
        }
        if (!CollectionUtils.isEmpty(mapReduceOptions.getScopeVariables())) {
            result = result.scope(new Document(mapReduceOptions.getScopeVariables()));
        }
        if (mapReduceOptions.getLimit() != null && mapReduceOptions.getLimit().intValue() > 0) {
            result = result.limit(mapReduceOptions.getLimit());
        }
        if (mapReduceOptions.getFinalizeFunction().filter(StringUtils::hasText).isPresent()) {
            result = result.finalizeFunction(mapReduceOptions.getFinalizeFunction().get());
        }
        if (mapReduceOptions.getJavaScriptMode() != null) {
            result = result.jsMode(mapReduceOptions.getJavaScriptMode());
        }
        if (mapReduceOptions.getOutputSharded().isPresent()) {
            result = result.sharded(mapReduceOptions.getOutputSharded().get());
        }
    }
    result = collation.map(Collation::toMongoCollation).map(result::collation).orElse(result);
    List<T> mappedResults = new ArrayList<T>();
    DocumentCallback<T> callback = new ReadDocumentCallback<T>(mongoConverter, entityClass, inputCollectionName);
    for (Document document : result) {
        mappedResults.add(callback.doWith(document));
    }
    return new MapReduceResults<T>(mappedResults, new Document());
}
Also used : MapReduceResults(org.springframework.data.mongodb.core.mapreduce.MapReduceResults) Document(org.bson.Document) Collation(org.springframework.data.mongodb.core.query.Collation)

Aggregations

Document (org.bson.Document)1 MapReduceResults (org.springframework.data.mongodb.core.mapreduce.MapReduceResults)1 Collation (org.springframework.data.mongodb.core.query.Collation)1