Search in sources :

Example 1 with MapReduceCommand

use of com.mongodb.MapReduceCommand in project morphia by mongodb.

the class DatastoreImpl method mapReduce.

@Override
@Deprecated
public <T> MapreduceResults<T> mapReduce(final MapreduceType type, final Query query, final Class<T> outputType, final MapReduceCommand baseCommand) {
    Assert.parametersNotNull("map", baseCommand.getMap());
    Assert.parameterNotEmpty("map", baseCommand.getMap());
    Assert.parametersNotNull("reduce", baseCommand.getReduce());
    Assert.parameterNotEmpty("reduce", baseCommand.getReduce());
    if (query.getOffset() != 0 || query.getFieldsObject() != null) {
        throw new QueryException("mapReduce does not allow the offset/retrievedFields query options.");
    }
    final OutputType outType = type.toOutputType();
    final DBCollection dbColl = query.getCollection();
    final MapReduceCommand cmd = new MapReduceCommand(dbColl, baseCommand.getMap(), baseCommand.getReduce(), baseCommand.getOutputTarget(), outType, query.getQueryObject());
    cmd.setFinalize(baseCommand.getFinalize());
    cmd.setScope(baseCommand.getScope());
    if (query.getLimit() > 0) {
        cmd.setLimit(query.getLimit());
    }
    if (query.getSortObject() != null) {
        cmd.setSort(query.getSortObject());
    }
    if (LOG.isTraceEnabled()) {
        LOG.info("Executing " + cmd.toString());
    }
    final EntityCache cache = createCache();
    MapreduceResults<T> results = new MapreduceResults<T>(dbColl.mapReduce(baseCommand));
    results.setType(type);
    if (MapreduceType.INLINE.equals(type)) {
        results.setInlineRequiredOptions(this, outputType, getMapper(), cache);
    } else {
        results.setQuery(newQuery(outputType, getDB().getCollection(results.getOutputCollectionName())));
    }
    return results;
}
Also used : DBCollection(com.mongodb.DBCollection) QueryException(org.mongodb.morphia.query.QueryException) EntityCache(org.mongodb.morphia.mapping.cache.EntityCache) MapReduceCommand(com.mongodb.MapReduceCommand) OutputType(com.mongodb.MapReduceCommand.OutputType)

Example 2 with MapReduceCommand

use of com.mongodb.MapReduceCommand in project morphia by mongodb.

the class MapReduceOptions method toCommand.

@SuppressWarnings("deprecation")
MapReduceCommand toCommand(final Mapper mapper) {
    if (query.getOffset() != 0 || query.getFieldsObject() != null) {
        throw new QueryException("mapReduce does not allow the offset/retrievedFields query ");
    }
    final DBCollection dbColl = inputCollection != null ? getQuery().getCollection().getDB().getCollection(inputCollection) : query.getCollection();
    final String target = outputCollection != null ? outputCollection : mapper.getMappedClass(resultType).getCollectionName();
    final MapReduceCommand command = new MapReduceCommand(dbColl, map, reduce, target, outputType, query.getQueryObject());
    command.setBypassDocumentValidation(bypassDocumentValidation);
    command.setCollation(collation);
    command.setFinalize(finalize);
    command.setJsMode(jsMode);
    command.setLimit(limit);
    command.setMaxTime(maxTimeMS, TimeUnit.MILLISECONDS);
    command.setOutputDB(outputDB);
    command.setReadPreference(readPreference);
    command.setScope(scope);
    command.setSort(query.getSortObject());
    command.setVerbose(verbose);
    return command;
}
Also used : DBCollection(com.mongodb.DBCollection) QueryException(org.mongodb.morphia.query.QueryException) MapReduceCommand(com.mongodb.MapReduceCommand)

Example 3 with MapReduceCommand

use of com.mongodb.MapReduceCommand in project morphia by mongodb.

the class MapReduceOptionsTest method mapReduceCommand.

@Test
@SuppressWarnings("deprecation")
public void mapReduceCommand() {
    Query<FacebookUser> query = getDs().find(FacebookUser.class);
    MapReduceOptions<FacebookUser> options = new MapReduceOptions<FacebookUser>().bypassDocumentValidation(true).collation(Collation.builder().locale("en").build()).finalize("i'm a finalize function").jsMode(true).limit(42).map("i'm a map function").maxTimeMS(42000).outputCollection("output collection").outputDB("output db").outputType(OutputType.INLINE).query(query).readPreference(ReadPreference.primaryPreferred()).reduce("i'm a reduce function").scope(new Document("key", "value").append("key2", "value2")).verbose(true);
    MapReduceCommand command = options.toCommand(getMorphia().getMapper());
    assertTrue(command.getBypassDocumentValidation());
    assertEquals(Collation.builder().locale("en").build(), command.getCollation());
    assertTrue(command.getJsMode());
    assertEquals(42, command.getLimit());
    assertEquals("i'm a map function", command.getMap());
    assertEquals(42000, command.getMaxTime(TimeUnit.MILLISECONDS));
    assertEquals("output collection", command.getOutputTarget());
    assertEquals("output db", command.getOutputDB());
    assertEquals(query.getQueryObject(), command.getQuery());
    assertEquals(query.getSortObject(), command.getSort());
    assertEquals(ReadPreference.primaryPreferred(), command.getReadPreference());
    assertEquals("i'm a map function", command.getMap());
    assertEquals("i'm a reduce function", command.getReduce());
    assertEquals("i'm a finalize function", command.getFinalize());
    assertEquals(new Document("key", "value").append("key2", "value2"), command.getScope());
    assertTrue(command.isVerbose());
}
Also used : MapReduceCommand(com.mongodb.MapReduceCommand) Document(org.bson.Document) FacebookUser(org.mongodb.morphia.TestDatastore.FacebookUser) Test(org.junit.Test)

Example 4 with MapReduceCommand

use of com.mongodb.MapReduceCommand in project morphia by mongodb.

the class DatastoreImpl method mapReduce.

@Override
@Deprecated
public <T> MapreduceResults<T> mapReduce(final MapreduceType type, final Query query, final String map, final String reduce, final String finalize, final Map<String, Object> scopeFields, final Class<T> outputType) {
    final DBCollection dbColl = query.getCollection();
    final String outColl = mapper.getCollectionName(outputType);
    final MapReduceCommand cmd = new MapReduceCommand(dbColl, map, reduce, outColl, type.toOutputType(), query.getQueryObject());
    if (query.getLimit() > 0) {
        cmd.setLimit(query.getLimit());
    }
    if (query.getSortObject() != null) {
        cmd.setSort(query.getSortObject());
    }
    if (finalize != null && finalize.length() != 0) {
        cmd.setFinalize(finalize);
    }
    if (scopeFields != null && !scopeFields.isEmpty()) {
        cmd.setScope(scopeFields);
    }
    return mapReduce(type, query, outputType, cmd);
}
Also used : DBCollection(com.mongodb.DBCollection) MapReduceCommand(com.mongodb.MapReduceCommand)

Aggregations

MapReduceCommand (com.mongodb.MapReduceCommand)4 DBCollection (com.mongodb.DBCollection)3 QueryException (org.mongodb.morphia.query.QueryException)2 OutputType (com.mongodb.MapReduceCommand.OutputType)1 Document (org.bson.Document)1 Test (org.junit.Test)1 FacebookUser (org.mongodb.morphia.TestDatastore.FacebookUser)1 EntityCache (org.mongodb.morphia.mapping.cache.EntityCache)1