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.
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user