Browse Source

DATAMONGO-428 - Fixed parsing of output collection for complex MapReduce result.

The raw result for a map-reduce operation might contain a complex element containing the output collection in case the original request configured an output database as option. Adapted the parsing of the output collection to accommodate both scenarios (plain String value as well as DBObject wrapper).
1.0.x
Oliver Gierke 14 years ago
parent
commit
b02e81c481
  1. 91
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceResults.java
  2. 59
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceResultsUnitTests.java

91
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceResults.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2011 the original author or authors. * Copyright 2011-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,33 +26,39 @@ import com.mongodb.DBObject;
* Collects the results of performing a MapReduce operations. * Collects the results of performing a MapReduce operations.
* *
* @author Mark Pollack * @author Mark Pollack
* * @author Oliver Gierke
* @param <T> The class in which the results are mapped onto, accessible via an interator. * @param <T> The class in which the results are mapped onto, accessible via an iterator.
*/ */
public class MapReduceResults<T> implements Iterable<T> { public class MapReduceResults<T> implements Iterable<T> {
private final List<T> mappedResults; private final List<T> mappedResults;
private final DBObject rawResults;
private final String outputCollection;
private final MapReduceTiming mapReduceTiming;
private final MapReduceCounts mapReduceCounts;
private DBObject rawResults; /**
* Creates a new {@link MapReduceResults} from the given mapped results and the raw one.
private MapReduceTiming mapReduceTiming; *
* @param mappedResults must not be {@literal null}.
private MapReduceCounts mapReduceCounts; * @param rawResults must not be {@literal null}.
*/
private String outputCollection;
public MapReduceResults(List<T> mappedResults, DBObject rawResults) { public MapReduceResults(List<T> mappedResults, DBObject rawResults) {
Assert.notNull(mappedResults); Assert.notNull(mappedResults);
Assert.notNull(rawResults); Assert.notNull(rawResults);
this.mappedResults = mappedResults; this.mappedResults = mappedResults;
this.rawResults = rawResults; this.rawResults = rawResults;
parseTiming(rawResults); this.mapReduceTiming = parseTiming(rawResults);
parseCounts(rawResults); this.mapReduceCounts = parseCounts(rawResults);
if (rawResults.get("result") != null) { this.outputCollection = parseOutputCollection(rawResults);
this.outputCollection = (String) rawResults.get("result");
}
} }
/*
* (non-Javadoc)
* @see java.lang.Iterable#iterator()
*/
public Iterator<T> iterator() { public Iterator<T> iterator() {
return mappedResults.iterator(); return mappedResults.iterator();
} }
@ -73,28 +79,59 @@ public class MapReduceResults<T> implements Iterable<T> {
return rawResults; return rawResults;
} }
protected void parseTiming(DBObject rawResults) { private MapReduceTiming parseTiming(DBObject rawResults) {
DBObject timing = (DBObject) rawResults.get("timing"); DBObject timing = (DBObject) rawResults.get("timing");
if (timing != null) {
if (timing == null) {
return new MapReduceTiming(-1, -1, -1);
}
if (timing.get("mapTime") != null && timing.get("emitLoop") != null && timing.get("total") != null) { if (timing.get("mapTime") != null && timing.get("emitLoop") != null && timing.get("total") != null) {
mapReduceTiming = new MapReduceTiming((Long) timing.get("mapTime"), (Integer) timing.get("emitLoop"), return new MapReduceTiming((Long) timing.get("mapTime"), (Integer) timing.get("emitLoop"),
(Integer) timing.get("total")); (Integer) timing.get("total"));
} }
} else {
mapReduceTiming = new MapReduceTiming(-1, -1, -1); return new MapReduceTiming(-1, -1, -1);
}
} }
protected void parseCounts(DBObject rawResults) { /**
* Parses the raw {@link DBObject} result into a {@link MapReduceCounts} value object.
*
* @param rawResults
* @return
*/
private MapReduceCounts parseCounts(DBObject rawResults) {
DBObject counts = (DBObject) rawResults.get("counts"); DBObject counts = (DBObject) rawResults.get("counts");
if (counts != null) {
if (counts == null) {
return new MapReduceCounts(-1, -1, -1);
}
if (counts.get("input") != null && counts.get("emit") != null && counts.get("output") != null) { if (counts.get("input") != null && counts.get("emit") != null && counts.get("output") != null) {
mapReduceCounts = new MapReduceCounts((Integer) counts.get("input"), (Integer) counts.get("emit"), return new MapReduceCounts((Integer) counts.get("input"), (Integer) counts.get("emit"),
(Integer) counts.get("output")); (Integer) counts.get("output"));
} }
} else {
mapReduceCounts = new MapReduceCounts(-1, -1, -1); return new MapReduceCounts(-1, -1, -1);
} }
/**
* Parses the output collection from the raw {@link DBObject} result.
*
* @param rawResults
* @return
*/
private String parseOutputCollection(DBObject rawResults) {
Object resultField = rawResults.get("result");
if (resultField == null) {
return null;
} }
return resultField instanceof DBObject ? ((DBObject) resultField).get("collection").toString() : resultField
.toString();
}
} }

59
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceResultsUnitTests.java

@ -0,0 +1,59 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.mapreduce;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.Collections;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
* Unit tests for {@link MapReduceResults}.
*
* @author Oliver Gierke
*/
public class MapReduceResultsUnitTests {
/**
* @see DATAMONGO-428
*/
@Test
public void resolvesOutputCollectionForPlainResult() {
DBObject rawResult = new BasicDBObject("result", "FOO");
MapReduceResults<Object> results = new MapReduceResults<Object>(Collections.emptyList(), rawResult);
assertThat(results.getOutputCollection(), is("FOO"));
}
/**
* @see DATAMONGO-428
*/
@Test
public void resolvesOutputCollectionForDBObjectResult() {
DBObject rawResult = new BasicDBObject("result", new BasicDBObject("collection", "FOO"));
MapReduceResults<Object> results = new MapReduceResults<Object>(Collections.emptyList(), rawResult);
assertThat(results.getOutputCollection(), is("FOO"));
}
}
Loading…
Cancel
Save