Search in sources :

Example 1 with MapReduceStatistics

use of com.mongodb.internal.operation.MapReduceStatistics in project mongo-java-driver by mongodb.

the class MapReducePublisherImplTest method shouldBuildTheExpectedMapReduceToCollectionOperation.

@DisplayName("Should build the expected MapReduceToCollectionOperation")
@Test
void shouldBuildTheExpectedMapReduceToCollectionOperation() {
    MapReduceStatistics stats = Mockito.mock(MapReduceStatistics.class);
    TestOperationExecutor executor = createOperationExecutor(asList(stats, stats));
    com.mongodb.reactivestreams.client.MapReducePublisher<Document> publisher = new MapReducePublisherImpl<>(null, createMongoOperationPublisher(executor), MAP_FUNCTION, REDUCE_FUNCTION).collectionName(NAMESPACE.getCollectionName());
    MapReduceToCollectionOperation expectedOperation = new MapReduceToCollectionOperation(NAMESPACE, new BsonJavaScript(MAP_FUNCTION), new BsonJavaScript(REDUCE_FUNCTION), NAMESPACE.getCollectionName(), WriteConcern.ACKNOWLEDGED).verbose(true);
    // default input should be as expected
    Flux.from(publisher.toCollection()).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getWriteOperation());
    // Should apply settings
    publisher.batchSize(100).bypassDocumentValidation(true).collation(COLLATION).filter(new Document("filter", 1)).finalizeFunction(FINALIZE_FUNCTION).limit(999).maxTime(10, SECONDS).scope(new Document("scope", 1)).sort(Sorts.ascending("sort")).verbose(false);
    expectedOperation.collation(COLLATION).bypassDocumentValidation(true).filter(BsonDocument.parse("{filter: 1}")).finalizeFunction(new BsonJavaScript(FINALIZE_FUNCTION)).limit(999).maxTime(10, SECONDS).maxTime(10, SECONDS).scope(new BsonDocument("scope", new BsonInt32(1))).sort(new BsonDocument("sort", new BsonInt32(1))).verbose(false);
    Flux.from(publisher.toCollection()).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getWriteOperation());
}
Also used : BsonInt32(org.bson.BsonInt32) MapReduceStatistics(com.mongodb.internal.operation.MapReduceStatistics) BsonDocument(org.bson.BsonDocument) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MapReduceToCollectionOperation(com.mongodb.internal.operation.MapReduceToCollectionOperation) BsonJavaScript(org.bson.BsonJavaScript) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with MapReduceStatistics

use of com.mongodb.internal.operation.MapReduceStatistics in project mongo-java-driver by mongodb.

the class DBCollection method mapReduce.

/**
 * Allows you to run map-reduce aggregation operations over a collection.
 *
 * @param command specifies the details of the Map Reduce operation to perform
 * @return a MapReduceOutput containing the results of the map reduce operation
 * @mongodb.driver.manual core/map-reduce/ Map-Reduce
 */
public MapReduceOutput mapReduce(final MapReduceCommand command) {
    ReadPreference readPreference = command.getReadPreference() == null ? getReadPreference() : command.getReadPreference();
    Map<String, Object> scope = command.getScope();
    Boolean jsMode = command.getJsMode();
    if (command.getOutputType() == MapReduceCommand.OutputType.INLINE) {
        MapReduceWithInlineResultsOperation<DBObject> operation = new MapReduceWithInlineResultsOperation<DBObject>(getNamespace(), new BsonJavaScript(command.getMap()), new BsonJavaScript(command.getReduce()), getDefaultDBObjectCodec()).filter(wrapAllowNull(command.getQuery())).limit(command.getLimit()).maxTime(command.getMaxTime(MILLISECONDS), MILLISECONDS).jsMode(jsMode == null ? false : jsMode).sort(wrapAllowNull(command.getSort())).verbose(command.isVerbose()).collation(command.getCollation());
        if (scope != null) {
            operation.scope(wrap(new BasicDBObject(scope)));
        }
        if (command.getFinalize() != null) {
            operation.finalizeFunction(new BsonJavaScript(command.getFinalize()));
        }
        MapReduceBatchCursor<DBObject> executionResult = executor.execute(operation, readPreference, getReadConcern());
        return new MapReduceOutput(command.toDBObject(), executionResult);
    } else {
        String action;
        switch(command.getOutputType()) {
            case REPLACE:
                action = "replace";
                break;
            case MERGE:
                action = "merge";
                break;
            case REDUCE:
                action = "reduce";
                break;
            default:
                throw new IllegalArgumentException("Unexpected output type");
        }
        MapReduceToCollectionOperation operation = new MapReduceToCollectionOperation(getNamespace(), new BsonJavaScript(command.getMap()), new BsonJavaScript(command.getReduce()), command.getOutputTarget(), getWriteConcern()).filter(wrapAllowNull(command.getQuery())).limit(command.getLimit()).maxTime(command.getMaxTime(MILLISECONDS), MILLISECONDS).jsMode(jsMode == null ? false : jsMode).sort(wrapAllowNull(command.getSort())).verbose(command.isVerbose()).action(action).databaseName(command.getOutputDB()).bypassDocumentValidation(command.getBypassDocumentValidation()).collation(command.getCollation());
        if (scope != null) {
            operation.scope(wrap(new BasicDBObject(scope)));
        }
        if (command.getFinalize() != null) {
            operation.finalizeFunction(new BsonJavaScript(command.getFinalize()));
        }
        try {
            MapReduceStatistics mapReduceStatistics = executor.execute(operation, getReadConcern());
            DBCollection mapReduceOutputCollection = getMapReduceOutputCollection(command);
            DBCursor executionResult = mapReduceOutputCollection.find();
            return new MapReduceOutput(command.toDBObject(), executionResult, mapReduceStatistics, mapReduceOutputCollection);
        } catch (MongoWriteConcernException e) {
            throw createWriteConcernException(e);
        }
    }
}
Also used : MapReduceStatistics(com.mongodb.internal.operation.MapReduceStatistics) BsonString(org.bson.BsonString) BsonJavaScript(org.bson.BsonJavaScript) MapReduceToCollectionOperation(com.mongodb.internal.operation.MapReduceToCollectionOperation)

Aggregations

MapReduceStatistics (com.mongodb.internal.operation.MapReduceStatistics)2 MapReduceToCollectionOperation (com.mongodb.internal.operation.MapReduceToCollectionOperation)2 BsonJavaScript (org.bson.BsonJavaScript)2 BsonDocument (org.bson.BsonDocument)1 BsonInt32 (org.bson.BsonInt32)1 BsonString (org.bson.BsonString)1 Document (org.bson.Document)1 DisplayName (org.junit.jupiter.api.DisplayName)1 Test (org.junit.jupiter.api.Test)1