Browse Source

DATAMONGO-378 - Fixed potential ClassCastException for MapReduceResults and upcoming MongoDB release.

The type of the value returned for the total field of the timing map in map-reduce results has changed from Integer to Long as of MongoDB version 2.1.0 apparently. Changed MapReduceResults to accommodate either Integer or Long types.
1.0.x
Oliver Gierke 14 years ago
parent
commit
11f0c515b0
  1. 16
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceResults.java
  2. 13
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceResultsUnitTests.java

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

@ -88,13 +88,25 @@ public class MapReduceResults<T> implements Iterable<T> { @@ -88,13 +88,25 @@ public class MapReduceResults<T> implements Iterable<T> {
}
if (timing.get("mapTime") != null && timing.get("emitLoop") != null && timing.get("total") != null) {
return new MapReduceTiming((Long) timing.get("mapTime"), (Integer) timing.get("emitLoop"),
(Integer) timing.get("total"));
return new MapReduceTiming(getAsLong(timing, "mapTime"), getAsLong(timing, "emitLoop"),
getAsLong(timing, "total"));
}
return new MapReduceTiming(-1, -1, -1);
}
/**
* Returns the value of the source's field with the given key as {@link Long}.
*
* @param source
* @param key
* @return
*/
private Long getAsLong(DBObject source, String key) {
Object raw = source.get(key);
return raw instanceof Long ? (Long) raw : (Integer) raw;
}
/**
* Parses the raw {@link DBObject} result into a {@link MapReduceCounts} value object.
*

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

@ -56,4 +56,17 @@ public class MapReduceResultsUnitTests { @@ -56,4 +56,17 @@ public class MapReduceResultsUnitTests {
assertThat(results.getOutputCollection(), is("FOO"));
}
/**
* @see DATAMONGO-378
*/
@Test
public void handlesLongTotalInResult() {
DBObject inner = new BasicDBObject("total", 1L);
inner.put("mapTime", 1L);
inner.put("emitLoop", 1);
DBObject source = new BasicDBObject("timing", inner);
new MapReduceResults<Object>(Collections.emptyList(), source);
}
}

Loading…
Cancel
Save