Browse Source

DATAMONGO-1092 - Ensure compatibility with MongoDB 2.8.0.rc0 and java driver 2.13.0-rc0.

We updated GroupByResults to allow working with changed data types returned for count and keys and fixed assertion on error message for duplicate keys.
Using java-driver 2.12.x when connecting to an 2.8.0.rc-0 instance is likely to cause trouble with authentication. This is the intended behavior.

2.8.0-rc0 throws error when removing elements from a collection that does not yet exist, which is different to what 2.6.x does.

The java-driver 2.13.0-rc0 works perfectly fine with a 2.6.x Server instance.
We deprecated Index.Duplicates#DROP since it has been removed in MongoDB 2.8

Original pull request: #246.
pull/248/head
Christoph Strobl 11 years ago committed by Thomas Darimont
parent
commit
461e7d05d7
  1. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java
  2. 17
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/GroupByResults.java
  3. 29
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java
  4. 10
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/GroupByTests.java

12
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java

@ -35,7 +35,17 @@ import com.mongodb.DBObject;
public class Index implements IndexDefinition { public class Index implements IndexDefinition {
public enum Duplicates { public enum Duplicates {
RETAIN, DROP RETAIN, //
/**
* Dropping Duplicates was removed in MongoDB Server 2.8.0-rc0.
* <p>
* See https://jira.mongodb.org/browse/SERVER-14710
*
* @deprecated since 1.7.
*/
@Deprecated//
DROP
} }
private final Map<String, Direction> fieldSpec = new LinkedHashMap<String, Direction>(); private final Map<String, Direction> fieldSpec = new LinkedHashMap<String, Direction>();

17
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/GroupByResults.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2011 the original author or authors. * Copyright 2011 - 2014 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,7 +26,8 @@ import com.mongodb.DBObject;
* Collects the results of executing a group operation. * Collects the results of executing a group operation.
* *
* @author Mark Pollack * @author Mark Pollack
* @param <T> The class in which the results are mapped onto, accessible via an interator. * @author Christoph Strobl
* @param <T> The class in which the results are mapped onto, accessible via an {@link Iterator}.
*/ */
public class GroupByResults<T> implements Iterable<T> { public class GroupByResults<T> implements Iterable<T> {
@ -38,6 +39,7 @@ public class GroupByResults<T> implements Iterable<T> {
private String serverUsed; private String serverUsed;
public GroupByResults(List<T> mappedResults, DBObject rawResults) { public GroupByResults(List<T> mappedResults, DBObject rawResults) {
Assert.notNull(mappedResults); Assert.notNull(mappedResults);
Assert.notNull(rawResults); Assert.notNull(rawResults);
this.mappedResults = mappedResults; this.mappedResults = mappedResults;
@ -68,21 +70,24 @@ public class GroupByResults<T> implements Iterable<T> {
} }
private void parseCount() { private void parseCount() {
Object object = rawResults.get("count"); Object object = rawResults.get("count");
if (object instanceof Double) { if (object instanceof Number) {
count = (Double) object; count = ((Number) object).doubleValue();
} }
} }
private void parseKeys() { private void parseKeys() {
Object object = rawResults.get("keys"); Object object = rawResults.get("keys");
if (object instanceof Integer) { if (object instanceof Number) {
keys = (Integer) object; keys = ((Number) object).intValue();
} }
} }
private void parseServerUsed() { private void parseServerUsed() {
// "serverUsed" : "127.0.0.1:27017" // "serverUsed" : "127.0.0.1:27017"
Object object = rawResults.get("serverUsed"); Object object = rawResults.get("serverUsed");
if (object instanceof String) { if (object instanceof String) {

29
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

@ -105,6 +105,8 @@ public class MongoTemplateTests {
private static final org.springframework.data.util.Version TWO_DOT_FOUR = org.springframework.data.util.Version private static final org.springframework.data.util.Version TWO_DOT_FOUR = org.springframework.data.util.Version
.parse("2.4"); .parse("2.4");
private static final org.springframework.data.util.Version TWO_DOT_EIGHT = org.springframework.data.util.Version
.parse("2.8");
@Autowired MongoTemplate template; @Autowired MongoTemplate template;
@Autowired MongoDbFactory factory; @Autowired MongoDbFactory factory;
@ -115,7 +117,6 @@ public class MongoTemplateTests {
@Rule public ExpectedException thrown = ExpectedException.none(); @Rule public ExpectedException thrown = ExpectedException.none();
@Autowired @Autowired
@SuppressWarnings("unchecked")
public void setMongo(Mongo mongo) throws Exception { public void setMongo(Mongo mongo) throws Exception {
CustomConversions conversions = new CustomConversions(Arrays.asList(DateToDateTimeConverter.INSTANCE, CustomConversions conversions = new CustomConversions(Arrays.asList(DateToDateTimeConverter.INSTANCE,
@ -235,7 +236,7 @@ public class MongoTemplateTests {
template.insert(person); template.insert(person);
fail("Expected DataIntegrityViolationException!"); fail("Expected DataIntegrityViolationException!");
} catch (DataIntegrityViolationException e) { } catch (DataIntegrityViolationException e) {
assertThat(e.getMessage(), containsString("E11000 duplicate key error index: database.person.$_id_ dup key:")); assertThat(e.getMessage(), containsString("E11000 duplicate key error index: database.person.$_id_"));
} }
} }
@ -289,8 +290,7 @@ public class MongoTemplateTests {
template.save(person); template.save(person);
fail("Expected DataIntegrityViolationException!"); fail("Expected DataIntegrityViolationException!");
} catch (DataIntegrityViolationException e) { } catch (DataIntegrityViolationException e) {
assertThat(e.getMessage(), assertThat(e.getMessage(), containsString("E11000 duplicate key error index: database.person.$firstName_-1"));
containsString("E11000 duplicate key error index: database.person.$firstName_-1 dup key:"));
} }
} }
@ -318,6 +318,7 @@ public class MongoTemplateTests {
} }
@Test @Test
@SuppressWarnings("deprecation")
public void testEnsureIndex() throws Exception { public void testEnsureIndex() throws Exception {
Person p1 = new Person("Oliver"); Person p1 = new Person("Oliver");
@ -339,19 +340,29 @@ public class MongoTemplateTests {
if ("age_-1".equals(ix.get("name"))) { if ("age_-1".equals(ix.get("name"))) {
indexKey = ix.get("key").toString(); indexKey = ix.get("key").toString();
unique = (Boolean) ix.get("unique"); unique = (Boolean) ix.get("unique");
dropDupes = (Boolean) ix.get("dropDups"); if (mongoVersion.isLessThan(TWO_DOT_EIGHT)) {
dropDupes = (Boolean) ix.get("dropDups");
assertThat(dropDupes, is(true));
} else {
assertThat(ix.get("dropDups"), is(nullValue()));
}
} }
} }
assertThat(indexKey, is("{ \"age\" : -1}")); assertThat(indexKey, is("{ \"age\" : -1}"));
assertThat(unique, is(true)); assertThat(unique, is(true));
assertThat(dropDupes, is(true));
List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo(); List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo();
assertThat(indexInfoList.size(), is(2)); assertThat(indexInfoList.size(), is(2));
IndexInfo ii = indexInfoList.get(1); IndexInfo ii = indexInfoList.get(1);
assertThat(ii.isUnique(), is(true)); assertThat(ii.isUnique(), is(true));
assertThat(ii.isDropDuplicates(), is(true));
if (mongoVersion.isLessThan(TWO_DOT_EIGHT)) {
assertThat(ii.isDropDuplicates(), is(true));
} else {
assertThat(ii.isDropDuplicates(), is(false));
}
assertThat(ii.isSparse(), is(false)); assertThat(ii.isSparse(), is(false));
List<IndexField> indexFields = ii.getIndexFields(); List<IndexField> indexFields = ii.getIndexFields();
@ -677,8 +688,8 @@ public class MongoTemplateTests {
Query q = new Query(Criteria.where("text").regex("^Hello.*")); Query q = new Query(Criteria.where("text").regex("^Hello.*"));
Message found1 = template.findAndRemove(q, Message.class); Message found1 = template.findAndRemove(q, Message.class);
Message found2 = template.findAndRemove(q, Message.class); Message found2 = template.findAndRemove(q, Message.class);
// Message notFound = template.findAndRemove(q, Message.class);
DBObject notFound = template.getCollection("").findAndRemove(q.getQueryObject()); Message notFound = template.findAndRemove(q, Message.class);
assertThat(found1, notNullValue()); assertThat(found1, notNullValue());
assertThat(found2, notNullValue()); assertThat(found2, notNullValue());
assertThat(notFound, nullValue()); assertThat(notFound, nullValue());

10
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/GroupByTests.java

@ -49,6 +49,7 @@ import com.mongodb.Mongo;
* *
* @author Mark Pollack * @author Mark Pollack
* @author Oliver Gierke * @author Oliver Gierke
* @author Christoph Strobl
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:infrastructure.xml") @ContextConfiguration("classpath:infrastructure.xml")
@ -60,7 +61,6 @@ public class GroupByTests {
MongoTemplate mongoTemplate; MongoTemplate mongoTemplate;
@Autowired @Autowired
@SuppressWarnings("unchecked")
public void setMongo(Mongo mongo) throws Exception { public void setMongo(Mongo mongo) throws Exception {
MongoMappingContext mappingContext = new MongoMappingContext(); MongoMappingContext mappingContext = new MongoMappingContext();
@ -116,7 +116,7 @@ public class GroupByTests {
} }
@Test @Test
public void SimpleGroup() { public void simpleGroupFunction() {
createGroupByData(); createGroupByData();
GroupByResults<XObject> results = mongoTemplate.group( GroupByResults<XObject> results = mongoTemplate.group(
@ -128,7 +128,7 @@ public class GroupByTests {
} }
@Test @Test
public void SimpleGroupWithKeyFunction() { public void simpleGroupWithKeyFunction() {
createGroupByData(); createGroupByData();
GroupByResults<XObject> results = mongoTemplate.group( GroupByResults<XObject> results = mongoTemplate.group(
@ -140,7 +140,7 @@ public class GroupByTests {
} }
@Test @Test
public void SimpleGroupWithFunctionsAsResources() { public void simpleGroupWithFunctionsAsResources() {
createGroupByData(); createGroupByData();
GroupByResults<XObject> results = mongoTemplate.group( GroupByResults<XObject> results = mongoTemplate.group(
@ -152,7 +152,7 @@ public class GroupByTests {
} }
@Test @Test
public void SimpleGroupWithQueryAndFunctionsAsResources() { public void simpleGroupWithQueryAndFunctionsAsResources() {
createGroupByData(); createGroupByData();
GroupByResults<XObject> results = mongoTemplate.group( GroupByResults<XObject> results = mongoTemplate.group(

Loading…
Cancel
Save