}
String outDB = getStringFieldValue(Item.mrOutDB);
DBObject query = ((DocBuilderField) getBoundUnit(Item.mrQuery)).getDBObject();
int limit = getIntFieldValue(Item.mrLimit);
final MapReduceCommand cmd = new MapReduceCommand(col, map, reduce, out, type, query);
DBObject sort = ((DocBuilderField) getBoundUnit(Item.mrSort)).getDBObject();
if (sort != null) {
cmd.setSort(sort);
}
if (!outDB.isEmpty()) {
cmd.setOutputDB(outDB);
}
if (!finalize.isEmpty()) {
cmd.setFinalize(finalize);
}
if (limit > 0) {
cmd.setLimit(limit);
}
if (getBooleanFieldValue(Item.mrJSMode)) {
cmd.addExtraOption("jsMode", true);
}
final BasicDBObject cmdobj = (BasicDBObject) cmd.toDBObject();
if (getBooleanFieldValue(Item.mrOutSharded)) {
((BasicDBObject) cmdobj.get("out")).put("sharded", true);
}
if (getBooleanFieldValue(Item.mrNonAtomic)) {
((BasicDBObject) cmdobj.get("out")).put("nonAtomic", true);
}
new DbJob() {
MapReduceOutput output;
@Override
public Object doRun() {
// output = col.mapReduce(cmd);
// if type in inline, then query options like slaveOk is fine
CommandResult res = null;
if (type == MapReduceCommand.OutputType.INLINE) {
res = col.getDB().command(cmdobj, col.getOptions());
return res;
}
res = col.getDB().command(cmdobj);
res.throwOnError();
output = new MapReduceOutput(col, cmdobj, res);
return output;
}
@Override
public void wrapUp(Object res) {
if (output != null) {
if (cmd.getOutputType() == OutputType.INLINE) {
res = output.results();
} else {
// spawn a find
doFind(output.getOutputCollection(), null);
res = output.getRaw();