DATADOC-7 - Support for map-reduce operations in MongoTemplate

This commit is contained in:
Mark Pollack
2011-09-01 16:34:52 -04:00
parent 4f92690f58
commit 758ee97a8d
4 changed files with 41 additions and 4 deletions

View File

@@ -25,6 +25,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import com.mongodb.BasicDBList;
@@ -51,6 +52,8 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -135,6 +138,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
private final QueryMapper mapper;
private ApplicationEventPublisher eventPublisher;
private ResourceLoader resourceLoader;
private MongoPersistentEntityIndexCreator indexCreator;
/**
@@ -231,6 +235,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
if (mappingContext instanceof ApplicationEventPublisherAware) {
((ApplicationEventPublisherAware) mappingContext).setApplicationEventPublisher(eventPublisher);
}
resourceLoader = applicationContext;
}
/**
@@ -842,14 +847,20 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
public <T> MapReduceResults<T> mapReduce(Query query, String mapFunction, String reduceFunction,
MapReduceOptions mapReduceOptions, Class<T> entityClass) {
String mapFunc = replaceWithResourceIfNecessary(mapFunction);
String reduceFunc = replaceWithResourceIfNecessary(reduceFunction);
DBCollection inputCollection = getCollection(this.determineCollectionName(entityClass));
MapReduceCommand command = new MapReduceCommand(inputCollection, mapFunction, reduceFunction,
MapReduceCommand command = new MapReduceCommand(inputCollection, mapFunc, reduceFunc,
mapReduceOptions.getOutputCollection(), mapReduceOptions.getOutputType(), null);
DBObject commandObject = copyQuery(query, copyMapReduceOptions(mapReduceOptions, command));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Executing MapReduce on collection [" + command.getInput() + "], mapFunction [" + mapFunc
+ "], reduceFunction [" + reduceFunc + "]");
}
CommandResult commandResult = null;
try {
try {
if (command.getOutputType() == MapReduceCommand.OutputType.INLINE) {
commandResult = executeCommand(commandObject, getDb().getOptions());
} else {
@@ -872,6 +883,21 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
protected String replaceWithResourceIfNecessary(String function) {
String func = function;
if (this.resourceLoader != null) {
try {
Resource functionResource = resourceLoader.getResource(func);
if (functionResource.exists()) {
return new Scanner(functionResource.getInputStream()).useDelimiter("\\A").next();
}
} catch (Exception e) {
// ignore - could be embedded JavaScript text
}
}
return func;
}
private DBObject copyQuery(Query query, DBObject copyMapReduceOptions) {
if (query != null) {
if (query.getSkip() != 0 || query.getFieldsObject() != null) {
@@ -901,7 +927,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
}
if (mapReduceOptions.getFinalizeFunction() != null) {
command.setFinalize(mapReduceOptions.getFinalizeFunction());
command.setFinalize(this.replaceWithResourceIfNecessary(mapReduceOptions.getFinalizeFunction()));
}
if (mapReduceOptions.getOutputDatabase() != null) {
command.setOutputDB(mapReduceOptions.getOutputDatabase());

View File

@@ -144,7 +144,7 @@ public class MapReduceTests {
MapReduceResults<ValueObject> results;
if (inline) {
if (withQuery) {
results = mongoTemplate.mapReduce(new Query(), mapFunction, reduceFunction, ValueObject.class);
results = mongoTemplate.mapReduce(new Query(), "classpath:map.js", "classpath:reduce.js", ValueObject.class);
} else {
results = mongoTemplate.mapReduce(mapFunction, reduceFunction, ValueObject.class);
}

View File

@@ -0,0 +1,5 @@
function () {
for (var i = 0; i < this.x.length; i++) {
emit(this.x[i], 1);
}
}

View File

@@ -0,0 +1,6 @@
function (key, values) {
var sum = 0;
for (var i = 0; i < values.length; i++)
sum += values[i];
return sum;
}