|
|
|
|
@@ -15,7 +15,7 @@
|
|
|
|
|
*/
|
|
|
|
|
package org.springframework.data.mongodb.core.mapreduce;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
import static org.assertj.core.api.Assertions.*;
|
|
|
|
|
import static org.springframework.data.mongodb.core.mapreduce.MapReduceOptions.*;
|
|
|
|
|
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
|
|
|
|
|
|
|
|
|
@@ -46,13 +46,14 @@ import com.mongodb.client.MongoCollection;
|
|
|
|
|
* @author Mark Pollack
|
|
|
|
|
* @author Thomas Darimont
|
|
|
|
|
* @author Mark Paluch
|
|
|
|
|
* @author Christoph Strobl
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(SpringJUnit4ClassRunner.class)
|
|
|
|
|
@ContextConfiguration("classpath:infrastructure.xml")
|
|
|
|
|
public class MapReduceTests {
|
|
|
|
|
|
|
|
|
|
private String mapFunction = "function(){ for ( var i=0; i<this.x.length; i++ ){ emit( this.x[i] , 1 ); } }";
|
|
|
|
|
private String reduceFunction = "function(key,values){ var sum=0; for( var i=0; i<values.length; i++ ) sum += values[i]; return sum;}";
|
|
|
|
|
private static final String MAP_FUNCTION = "function(){ for ( var i=0; i<this.x.length; i++ ){ emit( this.x[i] , 1 ); } }";
|
|
|
|
|
private static final String REDUCE_FUNCTION = "function(key,values){ var sum=0; for( var i=0; i<values.length; i++ ) sum += values[i]; return sum;}";
|
|
|
|
|
|
|
|
|
|
@Autowired MongoTemplate template;
|
|
|
|
|
@Autowired MongoTemplate mongoTemplate;
|
|
|
|
|
@@ -68,74 +69,229 @@ public class MapReduceTests {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void cleanDb() {
|
|
|
|
|
|
|
|
|
|
template.dropCollection(template.getCollectionName(ValueObject.class));
|
|
|
|
|
template.dropCollection("jmr2");
|
|
|
|
|
template.dropCollection("jmr2_out");
|
|
|
|
|
template.dropCollection("jmr1_out");
|
|
|
|
|
template.dropCollection("jmr1");
|
|
|
|
|
template.dropCollection("jmrWithGeo");
|
|
|
|
|
template.dropCollection("mapreduceout");
|
|
|
|
|
template.getMongoDbFactory().getDb("jmr1-out-db").drop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@Test // DATADOC-7
|
|
|
|
|
@Ignore
|
|
|
|
|
public void testForDocs() {
|
|
|
|
|
|
|
|
|
|
createMapReduceData();
|
|
|
|
|
MapReduceResults<ValueObject> results = mongoTemplate.mapReduce("jmr1", mapFunction, reduceFunction,
|
|
|
|
|
MapReduceResults<ValueObject> results = mongoTemplate.mapReduce("jmr1", MAP_FUNCTION, REDUCE_FUNCTION,
|
|
|
|
|
ValueObject.class);
|
|
|
|
|
|
|
|
|
|
for (ValueObject valueObject : results) {
|
|
|
|
|
System.out.println(valueObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@Test // DATAMONGO-260
|
|
|
|
|
public void testIssue260() {
|
|
|
|
|
|
|
|
|
|
createContentAndVersionData();
|
|
|
|
|
String map = "function () { emit(this.document_id, this.version); }";
|
|
|
|
|
String reduce = "function (key, values) { return Math.max.apply(Math, values); }";
|
|
|
|
|
|
|
|
|
|
MapReduceResults<ContentAndVersion> results = mongoTemplate.mapReduce("jmr2", map, reduce,
|
|
|
|
|
new MapReduceOptions().outputCollection("jmr2_out"), ContentAndVersion.class);
|
|
|
|
|
|
|
|
|
|
int size = 0;
|
|
|
|
|
assertThat(results).hasSize(3);
|
|
|
|
|
for (ContentAndVersion cv : results) {
|
|
|
|
|
|
|
|
|
|
if ("Resume".equals(cv.getId())) {
|
|
|
|
|
assertEquals(6, cv.getValue().longValue());
|
|
|
|
|
assertThat(cv.getValue().longValue()).isEqualTo(6);
|
|
|
|
|
}
|
|
|
|
|
if ("Schema".equals(cv.getId())) {
|
|
|
|
|
assertEquals(2, cv.getValue().longValue());
|
|
|
|
|
assertThat(cv.getValue().longValue()).isEqualTo(2);
|
|
|
|
|
}
|
|
|
|
|
if ("mongoDB How-To".equals(cv.getId())) {
|
|
|
|
|
assertEquals(2, cv.getValue().longValue());
|
|
|
|
|
assertThat(cv.getValue().longValue()).isEqualTo(2);
|
|
|
|
|
}
|
|
|
|
|
size++;
|
|
|
|
|
}
|
|
|
|
|
assertEquals(3, size);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@Test // DATAMONGO-260
|
|
|
|
|
public void testIssue260Part2() {
|
|
|
|
|
|
|
|
|
|
createNumberAndVersionData();
|
|
|
|
|
String map = "function () { emit(this.number, this.version); }";
|
|
|
|
|
String reduce = "function (key, values) { return Math.max.apply(Math, values); }";
|
|
|
|
|
|
|
|
|
|
MapReduceResults<NumberAndVersion> results = mongoTemplate.mapReduce("jmr2", map, reduce,
|
|
|
|
|
new MapReduceOptions().outputCollection("jmr2_out"), NumberAndVersion.class);
|
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
|
|
for (NumberAndVersion nv : results) {
|
|
|
|
|
if ("1".equals(nv.getId())) {
|
|
|
|
|
assertEquals(2, nv.getValue().longValue());
|
|
|
|
|
assertThat(nv.getValue().longValue()).isEqualTo(2);
|
|
|
|
|
}
|
|
|
|
|
if ("2".equals(nv.getId())) {
|
|
|
|
|
assertEquals(6, nv.getValue().longValue());
|
|
|
|
|
assertThat(nv.getValue().longValue()).isEqualTo(6);
|
|
|
|
|
}
|
|
|
|
|
if ("3".equals(nv.getId())) {
|
|
|
|
|
assertEquals(2, nv.getValue().longValue());
|
|
|
|
|
assertThat(nv.getValue().longValue()).isEqualTo(2);
|
|
|
|
|
}
|
|
|
|
|
size++;
|
|
|
|
|
}
|
|
|
|
|
assertEquals(3, size);
|
|
|
|
|
|
|
|
|
|
assertThat(results).hasSize(3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test // DATADOC-7, DATAMONGO-2027
|
|
|
|
|
public void testMapReduce() {
|
|
|
|
|
|
|
|
|
|
performMapReduce(false, false);
|
|
|
|
|
|
|
|
|
|
List<ValueObject> results = mongoTemplate.find(new Query(), ValueObject.class, "jmr1_out");
|
|
|
|
|
assertMapReduceResults(copyToMap(results));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test // DATADOC-7, DATAMONGO-2027
|
|
|
|
|
public void testMapReduceInline() {
|
|
|
|
|
|
|
|
|
|
performMapReduce(true, false);
|
|
|
|
|
assertThat(template.collectionExists("jmr1_out")).isFalse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-2027
|
|
|
|
|
public void mapReduceWithOutputDatabaseShouldWorkCorrectly() {
|
|
|
|
|
|
|
|
|
|
createMapReduceData();
|
|
|
|
|
|
|
|
|
|
mongoTemplate.mapReduce("jmr1", MAP_FUNCTION, REDUCE_FUNCTION,
|
|
|
|
|
options().outputDatabase("jmr1-out-db").outputCollection("jmr1-out"), ValueObject.class);
|
|
|
|
|
|
|
|
|
|
assertThat(template.getMongoDbFactory().getDb("jmr1-out-db").listCollectionNames().into(new ArrayList<>()))
|
|
|
|
|
.contains("jmr1-out");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test // DATADOC-7
|
|
|
|
|
public void testMapReduceWithQuery() {
|
|
|
|
|
performMapReduce(false, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test // DATADOC-7
|
|
|
|
|
public void testMapReduceInlineWithScope() {
|
|
|
|
|
|
|
|
|
|
createMapReduceData();
|
|
|
|
|
|
|
|
|
|
Map<String, Object> scopeVariables = new HashMap<String, Object>();
|
|
|
|
|
scopeVariables.put("exclude", "a");
|
|
|
|
|
|
|
|
|
|
String mapWithExcludeFunction = "function(){ for ( var i=0; i<this.x.length; i++ ){ if(this.x[i] != exclude) emit( this.x[i] , 1 ); } }";
|
|
|
|
|
|
|
|
|
|
MapReduceResults<ValueObject> results = mongoTemplate.mapReduce("jmr1", mapWithExcludeFunction, REDUCE_FUNCTION,
|
|
|
|
|
new MapReduceOptions().scopeVariables(scopeVariables).outputTypeInline(), ValueObject.class);
|
|
|
|
|
|
|
|
|
|
assertThat(copyToMap(results)) //
|
|
|
|
|
.hasSize(3) //
|
|
|
|
|
.containsEntry("b", 2F) //
|
|
|
|
|
.containsEntry("c", 2F) //
|
|
|
|
|
.containsEntry("d", 1F);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test // DATADOC-7
|
|
|
|
|
public void testMapReduceExcludeQuery() {
|
|
|
|
|
|
|
|
|
|
createMapReduceData();
|
|
|
|
|
|
|
|
|
|
Query query = new Query(where("x").ne(new String[] { "a", "b" }));
|
|
|
|
|
MapReduceResults<ValueObject> results = mongoTemplate.mapReduce(query, "jmr1", MAP_FUNCTION, REDUCE_FUNCTION,
|
|
|
|
|
ValueObject.class);
|
|
|
|
|
|
|
|
|
|
assertThat(copyToMap(results)) //
|
|
|
|
|
.hasSize(3) //
|
|
|
|
|
.containsEntry("b", 1F) //
|
|
|
|
|
.containsEntry("c", 2F) //
|
|
|
|
|
.containsEntry("d", 1F);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-938
|
|
|
|
|
public void mapReduceShouldUseQueryMapper() {
|
|
|
|
|
|
|
|
|
|
MongoCollection<Document> c = mongoTemplate.getDb().getCollection("jmrWithGeo", Document.class);
|
|
|
|
|
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("a", "b")).append("loc", Arrays.asList(0D, 0D)));
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("b", "c")).append("loc", Arrays.asList(0D, 0D)));
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("c", "d")).append("loc", Arrays.asList(0D, 0D)));
|
|
|
|
|
|
|
|
|
|
Query query = new Query(where("x").ne(new String[] { "a", "b" }).and("loc")
|
|
|
|
|
.within(new Box(new double[] { 0, 0 }, new double[] { 1, 1 })));
|
|
|
|
|
|
|
|
|
|
MapReduceResults<ValueObject> results = template.mapReduce(query, "jmrWithGeo", MAP_FUNCTION, REDUCE_FUNCTION,
|
|
|
|
|
ValueObject.class);
|
|
|
|
|
|
|
|
|
|
assertThat(copyToMap(results)) //
|
|
|
|
|
.hasSize(3) //
|
|
|
|
|
.containsEntry("b", 1F) //
|
|
|
|
|
.containsEntry("c", 2F) //
|
|
|
|
|
.containsEntry("d", 1F);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void performMapReduce(boolean inline, boolean withQuery) {
|
|
|
|
|
|
|
|
|
|
createMapReduceData();
|
|
|
|
|
MapReduceResults<ValueObject> results;
|
|
|
|
|
if (inline) {
|
|
|
|
|
if (withQuery) {
|
|
|
|
|
results = mongoTemplate.mapReduce(new Query(), "jmr1", "classpath:map.js", "classpath:reduce.js",
|
|
|
|
|
ValueObject.class);
|
|
|
|
|
} else {
|
|
|
|
|
results = mongoTemplate.mapReduce("jmr1", MAP_FUNCTION, REDUCE_FUNCTION, ValueObject.class);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (withQuery) {
|
|
|
|
|
results = mongoTemplate.mapReduce(new Query(), "jmr1", MAP_FUNCTION, REDUCE_FUNCTION,
|
|
|
|
|
options().outputCollection("jmr1_out"), ValueObject.class);
|
|
|
|
|
} else {
|
|
|
|
|
results = mongoTemplate.mapReduce("jmr1", MAP_FUNCTION, REDUCE_FUNCTION,
|
|
|
|
|
new MapReduceOptions().outputCollection("jmr1_out"), ValueObject.class);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertMapReduceResults(copyToMap(results));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createMapReduceData() {
|
|
|
|
|
|
|
|
|
|
MongoCollection<Document> c = mongoTemplate.getDb().getCollection("jmr1", Document.class);
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("a", "b")));
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("b", "c")));
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("c", "d")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, Float> copyToMap(Iterable<ValueObject> results) {
|
|
|
|
|
|
|
|
|
|
List<ValueObject> valueObjects = new ArrayList<>();
|
|
|
|
|
for (ValueObject valueObject : results) {
|
|
|
|
|
valueObjects.add(valueObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, Float> m = new HashMap<>();
|
|
|
|
|
for (ValueObject vo : valueObjects) {
|
|
|
|
|
m.put(vo.getId(), vo.getValue());
|
|
|
|
|
}
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void assertMapReduceResults(Map<String, Float> map) {
|
|
|
|
|
|
|
|
|
|
assertThat(map) //
|
|
|
|
|
.hasSize(4) //
|
|
|
|
|
.containsEntry("a", 1F) //
|
|
|
|
|
.containsEntry("b", 2F) //
|
|
|
|
|
.containsEntry("c", 2F) //
|
|
|
|
|
.containsEntry("d", 1F);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createNumberAndVersionData() {
|
|
|
|
|
|
|
|
|
|
NumberAndVersion nv1 = new NumberAndVersion();
|
|
|
|
|
nv1.setNumber(1L);
|
|
|
|
|
nv1.setVersion(1L);
|
|
|
|
|
@@ -170,7 +326,7 @@ public class MapReduceTests {
|
|
|
|
|
{ "_id" : 3, "document_id" : "Resume", "author" : "Author", "content" : "...", "version" : 6 }
|
|
|
|
|
{ "_id" : 4, "document_id" : "Schema", "author" : "Someone Else", "content" : "...", "version" : 0.9 }
|
|
|
|
|
{ "_id" : 5, "document_id" : "Schema", "author" : "Someone Else", "content" : "...", "version" : 1 }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
ContentAndVersion cv1 = new ContentAndVersion();
|
|
|
|
|
cv1.setDocumentId("mongoDB How-To");
|
|
|
|
|
@@ -206,149 +362,5 @@ public class MapReduceTests {
|
|
|
|
|
cv5.setContent("...");
|
|
|
|
|
cv5.setVersion(2L);
|
|
|
|
|
template.save(cv5, "jmr2");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testMapReduce() {
|
|
|
|
|
performMapReduce(false, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testMapReduceInline() {
|
|
|
|
|
performMapReduce(true, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testMapReduceWithQuery() {
|
|
|
|
|
performMapReduce(false, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testMapReduceInlineWithScope() {
|
|
|
|
|
createMapReduceData();
|
|
|
|
|
|
|
|
|
|
Map<String, Object> scopeVariables = new HashMap<String, Object>();
|
|
|
|
|
scopeVariables.put("exclude", "a");
|
|
|
|
|
|
|
|
|
|
String mapWithExcludeFunction = "function(){ for ( var i=0; i<this.x.length; i++ ){ if(this.x[i] != exclude) emit( this.x[i] , 1 ); } }";
|
|
|
|
|
|
|
|
|
|
MapReduceResults<ValueObject> results = mongoTemplate.mapReduce("jmr1", mapWithExcludeFunction, reduceFunction,
|
|
|
|
|
new MapReduceOptions().scopeVariables(scopeVariables).outputTypeInline(), ValueObject.class);
|
|
|
|
|
Map<String, Float> m = copyToMap(results);
|
|
|
|
|
assertEquals(3, m.size());
|
|
|
|
|
assertEquals(2, m.get("b").intValue());
|
|
|
|
|
assertEquals(2, m.get("c").intValue());
|
|
|
|
|
assertEquals(1, m.get("d").intValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-2027
|
|
|
|
|
public void shouldStoreResultInCollection() {
|
|
|
|
|
|
|
|
|
|
createMapReduceData();
|
|
|
|
|
|
|
|
|
|
String mapWithExcludeFunction = "function(){ for ( var i=0; i<this.x.length; i++ ){ emit( this.x[i] , 1 ); } }";
|
|
|
|
|
|
|
|
|
|
mongoTemplate.mapReduce("jmr1", mapWithExcludeFunction, reduceFunction,
|
|
|
|
|
new MapReduceOptions().outputCollection("mapreduceout"), ValueObject.class);
|
|
|
|
|
|
|
|
|
|
List<ValueObject> results = mongoTemplate.find(new Query(), ValueObject.class, "mapreduceout");
|
|
|
|
|
|
|
|
|
|
Map<String, Float> m = copyToMap(results);
|
|
|
|
|
assertEquals(4, m.size());
|
|
|
|
|
assertEquals(1, m.get("a").intValue());
|
|
|
|
|
assertEquals(2, m.get("b").intValue());
|
|
|
|
|
assertEquals(2, m.get("c").intValue());
|
|
|
|
|
assertEquals(1, m.get("d").intValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testMapReduceExcludeQuery() {
|
|
|
|
|
createMapReduceData();
|
|
|
|
|
|
|
|
|
|
Query query = new Query(where("x").ne(new String[] { "a", "b" }));
|
|
|
|
|
MapReduceResults<ValueObject> results = mongoTemplate.mapReduce(query, "jmr1", mapFunction, reduceFunction,
|
|
|
|
|
ValueObject.class);
|
|
|
|
|
|
|
|
|
|
Map<String, Float> m = copyToMap(results);
|
|
|
|
|
assertEquals(3, m.size());
|
|
|
|
|
assertEquals(1, m.get("b").intValue());
|
|
|
|
|
assertEquals(2, m.get("c").intValue());
|
|
|
|
|
assertEquals(1, m.get("d").intValue());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-938
|
|
|
|
|
public void mapReduceShouldUseQueryMapper() {
|
|
|
|
|
|
|
|
|
|
MongoCollection<Document> c = mongoTemplate.getDb().getCollection("jmrWithGeo", Document.class);
|
|
|
|
|
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("a", "b")).append("loc", Arrays.<Double> asList(0D, 0D)));
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("b", "c")).append("loc", Arrays.<Double> asList(0D, 0D)));
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("c", "d")).append("loc", Arrays.<Double> asList(0D, 0D)));
|
|
|
|
|
|
|
|
|
|
Query query = new Query(where("x").ne(new String[] { "a", "b" }).and("loc")
|
|
|
|
|
.within(new Box(new double[] { 0, 0 }, new double[] { 1, 1 })));
|
|
|
|
|
|
|
|
|
|
MapReduceResults<ValueObject> results = template.mapReduce(query, "jmrWithGeo", mapFunction, reduceFunction,
|
|
|
|
|
ValueObject.class);
|
|
|
|
|
|
|
|
|
|
Map<String, Float> m = copyToMap(results);
|
|
|
|
|
assertEquals(3, m.size());
|
|
|
|
|
assertEquals(1, m.get("b").intValue());
|
|
|
|
|
assertEquals(2, m.get("c").intValue());
|
|
|
|
|
assertEquals(1, m.get("d").intValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void performMapReduce(boolean inline, boolean withQuery) {
|
|
|
|
|
createMapReduceData();
|
|
|
|
|
MapReduceResults<ValueObject> results;
|
|
|
|
|
if (inline) {
|
|
|
|
|
if (withQuery) {
|
|
|
|
|
results = mongoTemplate.mapReduce(new Query(), "jmr1", "classpath:map.js", "classpath:reduce.js",
|
|
|
|
|
ValueObject.class);
|
|
|
|
|
} else {
|
|
|
|
|
results = mongoTemplate.mapReduce("jmr1", mapFunction, reduceFunction, ValueObject.class);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (withQuery) {
|
|
|
|
|
results = mongoTemplate.mapReduce(new Query(), "jmr1", mapFunction, reduceFunction,
|
|
|
|
|
options().outputCollection("jmr1_out"), ValueObject.class);
|
|
|
|
|
} else {
|
|
|
|
|
results = mongoTemplate.mapReduce("jmr1", mapFunction, reduceFunction,
|
|
|
|
|
new MapReduceOptions().outputCollection("jmr1_out"), ValueObject.class);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Map<String, Float> m = copyToMap(results);
|
|
|
|
|
assertMapReduceResults(m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createMapReduceData() {
|
|
|
|
|
MongoCollection<Document> c = mongoTemplate.getDb().getCollection("jmr1", Document.class);
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("a", "b")));
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("b", "c")));
|
|
|
|
|
c.insertOne(new Document("x", Arrays.asList("c", "d")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, Float> copyToMap(Iterable<ValueObject> results) {
|
|
|
|
|
List<ValueObject> valueObjects = new ArrayList<ValueObject>();
|
|
|
|
|
for (ValueObject valueObject : results) {
|
|
|
|
|
valueObjects.add(valueObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, Float> m = new HashMap<String, Float>();
|
|
|
|
|
for (ValueObject vo : valueObjects) {
|
|
|
|
|
m.put(vo.getId(), vo.getValue());
|
|
|
|
|
}
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void assertMapReduceResults(Map<String, Float> m) {
|
|
|
|
|
assertEquals(4, m.size());
|
|
|
|
|
assertEquals(1, m.get("a").intValue());
|
|
|
|
|
assertEquals(2, m.get("b").intValue());
|
|
|
|
|
assertEquals(2, m.get("c").intValue());
|
|
|
|
|
assertEquals(1, m.get("d").intValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|