Example usage for com.mongodb MapReduceCommand setFinalize

List of usage examples for com.mongodb MapReduceCommand setFinalize

Introduction

In this page you can find the example usage for com.mongodb MapReduceCommand setFinalize.

Prototype

public void setFinalize(@Nullable final String finalize) 

Source Link

Document

Sets the Finalize JS Function

Usage

From source file:org.basex.modules.nosql.MongoDB.java

License:BSD License

/**
 * Mapreduce all functions in xquery's Map like :{"map":"function(){..}"
 * , "reduce":"function(){}"}.//  w  w  w  .j a  v a  2 s .  c  om
 * @param handler database handler
 * @param col collection name
 * @param options all options of Mapreduce including "map" in key.
 * @return Item
 * @throws Exception exception
 */
public Item mapreduce(final Str handler, final Str col, final Map options) throws Exception {
    if (options == null) {
        throw MongoDBErrors.generalExceptionError("Map optoins are empty");
    }
    final DB db = getDbHandler(handler);
    final DBCollection collection = db.getCollection(itemToString(col));
    String out = null;
    String outType = null;
    String map = null;
    String reduce = null;
    DBObject query = null;
    DBObject sort = null;
    int limit = 0;
    String finalalize = null;
    OutputType op = MapReduceCommand.OutputType.INLINE;
    for (Item k : options.keys()) {
        String key = (String) k.toJava();
        Value val = options.get(k, null);
        String value = (String) val.toJava();
        if (key.toLowerCase().equals(MAP)) {
            map = value;
        } else if (key.toLowerCase().equals(REDUCE)) {
            reduce = value;
        } else if (key.toLowerCase().equals(OUTPUTS)) {
            out = value;
        } else if (key.toLowerCase().equals(OUTPUTTYPE)) {
            outType = value;
        } else if (key.toLowerCase().equals(LIMIT)) {
            if (val.seqType().instanceOf(SeqType.ITR_OM)) {
                long l = ((Item) val).itr(null);
                limit = (int) l;
            } else {
                throw MongoDBErrors.generalExceptionError(" Expected integer Value");
            }
        } else if (key.toLowerCase().equals(SORT)) {
            sort = getDbObjectFromItem(Str.get(value));
        } else if (key.toLowerCase().equals(QUERY)) {
            query = getDbObjectFromItem(Str.get(value));
        } else if (key.toLowerCase().equals(FINALIZE)) {
            finalalize = value;
        }
    }
    if (out != null && outType != null) {
        if (outType.toUpperCase().equals("REPLACE")) {
            op = MapReduceCommand.OutputType.REPLACE;
        } else if (outType.toUpperCase().equals("MERGE")) {
            op = MapReduceCommand.OutputType.MERGE;
        } else if (outType.toUpperCase().equals("REDUCE")) {
            op = MapReduceCommand.OutputType.REDUCE;
        }
    } else if (out != null) {
        op = MapReduceCommand.OutputType.REPLACE;
    }
    if (map == null) {
        throw MongoDBErrors.generalExceptionError("Map function cannot be empty");
    }
    db.requestStart();
    try {
        MapReduceCommand cmd = new MapReduceCommand(collection, map, reduce, out, op, query);
        if (finalalize != null) {
            cmd.setFinalize(finalalize);
        }
        if (limit != 0) {
            cmd.setLimit(limit);
        }
        if (sort != null) {
            cmd.setSort(sort);
        }
        final MapReduceOutput outcmd = collection.mapReduce(cmd);
        return returnResult(handler, Str.get(JSON.serialize(outcmd.results())));
    } catch (MongoException e) {
        throw MongoDBErrors.generalExceptionError(e);
    } finally {
        db.requestDone();
    }
}

From source file:org.mongodb.morphia.MapReduceOptions.java

License:Apache License

@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 ");
    }/*from   w  w  w .jav a  2s  . c  o  m*/

    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;
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

License:Apache License

private void copyMapReduceOptionsToCommand(Query query, MapReduceOptions mapReduceOptions,
        MapReduceCommand mapReduceCommand) {

    if (query != null) {
        if (query.getSkip() != 0 || query.getFieldsObject() != null) {
            throw new InvalidDataAccessApiUsageException(
                    "Can not use skip or field specification with map reduce operations");
        }//from w w  w .  java2s .co  m
        if (query.getLimit() > 0 && mapReduceOptions.getLimit() == null) {
            mapReduceCommand.setLimit(query.getLimit());
        }
        if (query.getSortObject() != null) {
            mapReduceCommand.setSort(queryMapper.getMappedObject(query.getSortObject(), null));
        }
    }

    if (mapReduceOptions.getLimit() != null && mapReduceOptions.getLimit().intValue() > 0) {
        mapReduceCommand.setLimit(mapReduceOptions.getLimit());
    }

    if (mapReduceOptions.getJavaScriptMode() != null) {
        mapReduceCommand.setJsMode(true);
    }
    if (!mapReduceOptions.getExtraOptions().isEmpty()) {
        for (Map.Entry<String, Object> entry : mapReduceOptions.getExtraOptions().entrySet()) {
            ReflectiveMapReduceInvoker.addExtraOption(mapReduceCommand, entry.getKey(), entry.getValue());
        }
    }
    if (mapReduceOptions.getFinalizeFunction() != null) {
        mapReduceCommand
                .setFinalize(this.replaceWithResourceIfNecessary(mapReduceOptions.getFinalizeFunction()));
    }
    if (mapReduceOptions.getOutputDatabase() != null) {
        mapReduceCommand.setOutputDB(mapReduceOptions.getOutputDatabase());
    }
    if (!mapReduceOptions.getScopeVariables().isEmpty()) {
        mapReduceCommand.setScope(mapReduceOptions.getScopeVariables());
    }
}