Skip to content

Commit b80c81f

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1008 - DefaultIndexOperations no considers 2dsphere, too.
We now also check for 2dsphere when inspecting index keys and create an geo IndexField in that case. Original pull request: #210.
1 parent 005d21c commit b80c81f

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.data.mongodb.core.index.IndexField;
2626
import org.springframework.data.mongodb.core.index.IndexInfo;
2727
import org.springframework.util.Assert;
28+
import org.springframework.util.ObjectUtils;
2829

2930
import com.mongodb.DBCollection;
3031
import com.mongodb.DBObject;
@@ -140,7 +141,7 @@ private List<IndexInfo> getIndexData(List<DBObject> dbObjectList) {
140141

141142
Object value = keyDbObject.get(key);
142143

143-
if ("2d".equals(value)) {
144+
if (isGeoIndex(value)) {
144145
indexFields.add(IndexField.geo(key));
145146
} else {
146147

@@ -167,4 +168,8 @@ private List<IndexInfo> getIndexData(List<DBObject> dbObjectList) {
167168
}
168169
});
169170
}
171+
172+
private boolean isGeoIndex(Object indexType) {
173+
return ObjectUtils.nullSafeEquals("2d", indexType) || ObjectUtils.nullSafeEquals("2dsphere", indexType);
174+
}
170175
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core;
17+
18+
import static org.hamcrest.core.Is.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.data.mongodb.core.index.IndexInfo;
26+
import org.springframework.test.context.ContextConfiguration;
27+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
28+
import org.springframework.util.ClassUtils;
29+
import org.springframework.util.ObjectUtils;
30+
31+
import com.mongodb.BasicDBObject;
32+
import com.mongodb.DBCollection;
33+
import com.mongodb.DBObject;
34+
35+
/**
36+
* @author Christoph Strobl
37+
*/
38+
@RunWith(SpringJUnit4ClassRunner.class)
39+
@ContextConfiguration("classpath:infrastructure.xml")
40+
public class DefaultIndexOperationTests {
41+
42+
private static final String COLLECTION_NAME = ClassUtils.getShortNameAsProperty(DefaultIndexOperationTests.class);
43+
private static final DBObject GEO_SPHERE_2D = new BasicDBObject("loaction", "2dsphere");
44+
45+
@Autowired MongoTemplate template;
46+
private DefaultIndexOperations indexOps;
47+
private DBCollection collection;
48+
49+
@Before
50+
public void setUp() {
51+
52+
this.collection = this.template.getDb().getCollection(COLLECTION_NAME);
53+
dropAllIndexes();
54+
55+
indexOps = new DefaultIndexOperations(template, COLLECTION_NAME);
56+
}
57+
58+
/**
59+
* @see DATAMONGO-1008
60+
*/
61+
@Test
62+
public void getIndexInfoShouldBeAbleToRead2dsphereIndex() {
63+
64+
createIndex(GEO_SPHERE_2D);
65+
66+
IndexInfo info = findAndReturnIndexInfo(GEO_SPHERE_2D);
67+
assertThat(info.getIndexFields().get(0).isGeo(), is(true));
68+
}
69+
70+
private IndexInfo findAndReturnIndexInfo(DBObject keys) {
71+
return findAndReturnIndexInfo(indexOps.getIndexInfo(), keys);
72+
}
73+
74+
@SuppressWarnings("deprecation")
75+
private IndexInfo findAndReturnIndexInfo(Iterable<IndexInfo> candidates, DBObject keys) {
76+
return findAndReturnIndexInfo(candidates, DBCollection.genIndexName(keys));
77+
}
78+
79+
private IndexInfo findAndReturnIndexInfo(Iterable<IndexInfo> candidates, String name) {
80+
81+
for (IndexInfo info : candidates) {
82+
if (ObjectUtils.nullSafeEquals(name, info.getName())) {
83+
return info;
84+
}
85+
}
86+
throw new AssertionError(String.format("Index with %s was not found", name));
87+
}
88+
89+
private void createIndex(DBObject keys) {
90+
template.getDb().getCollection(COLLECTION_NAME).createIndex(keys);
91+
}
92+
93+
private void dropAllIndexes() {
94+
this.collection.dropIndexes();
95+
}
96+
97+
}

0 commit comments

Comments
 (0)