added IndexSpecification to support creating GeospatialIndex
This commit is contained in:
@@ -18,7 +18,7 @@ package org.springframework.data.document.mongodb;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.document.mongodb.query.Index;
|
||||
import org.springframework.data.document.mongodb.query.IndexSpecification;
|
||||
import org.springframework.data.document.mongodb.query.Query;
|
||||
import org.springframework.data.document.mongodb.query.Update;
|
||||
|
||||
@@ -207,21 +207,21 @@ public interface MongoOperations {
|
||||
MongoReader<T> reader);
|
||||
|
||||
/**
|
||||
* Ensure that an index for the specified {@link Index} specification exists for the default collection.
|
||||
* If not is will be created.
|
||||
* Ensure that an index for the provided {@link IndexSpecification} exists for the default collection.
|
||||
* If not it will be created.
|
||||
*
|
||||
* @param index
|
||||
*/
|
||||
void ensureIndex(Index index);
|
||||
void ensureIndex(IndexSpecification indexSpecification);
|
||||
|
||||
/**
|
||||
* Ensure that an index for the specified {@link Index} specification exists. If not is will be
|
||||
* Ensure that an index for the provided {@link IndexSpecification} exists. If not it will be
|
||||
* created.
|
||||
*
|
||||
* @param collectionName
|
||||
* @param index
|
||||
*/
|
||||
void ensureIndex(String collectionName, Index index);
|
||||
void ensureIndex(String collectionName, IndexSpecification indexSpecification);
|
||||
|
||||
/**
|
||||
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
|
||||
|
||||
@@ -30,7 +30,7 @@ import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
|
||||
import org.springframework.data.document.mongodb.query.Index;
|
||||
import org.springframework.data.document.mongodb.query.IndexSpecification;
|
||||
import org.springframework.data.document.mongodb.query.Query;
|
||||
import org.springframework.data.document.mongodb.query.Update;
|
||||
import org.springframework.jca.cci.core.ConnectionCallback;
|
||||
@@ -409,19 +409,19 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
||||
|
||||
// Indexing methods
|
||||
|
||||
public void ensureIndex(Index index) {
|
||||
ensureIndex(getDefaultCollectionName(), index);
|
||||
public void ensureIndex(IndexSpecification indexSpecification) {
|
||||
ensureIndex(getDefaultCollectionName(), indexSpecification);
|
||||
}
|
||||
|
||||
public void ensureIndex(String collectionName, final Index index) {
|
||||
public void ensureIndex(String collectionName, final IndexSpecification indexSpecification) {
|
||||
execute(new CollectionCallback<Object>() {
|
||||
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
||||
DBObject indexOptions = index.getIndexOptions();
|
||||
DBObject indexOptions = indexSpecification.getIndexOptions();
|
||||
if (indexOptions != null) {
|
||||
collection.ensureIndex(index.getIndexObject(), indexOptions);
|
||||
collection.ensureIndex(indexSpecification.getIndexObject(), indexOptions);
|
||||
}
|
||||
else {
|
||||
collection.ensureIndex(index.getIndexObject());
|
||||
collection.ensureIndex(indexSpecification.getIndexObject());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
|
||||
public class GeospatialIndex {
|
||||
public class GeospatialIndex implements IndexSpecification {
|
||||
|
||||
private String keyField;
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
|
||||
public class Index {
|
||||
public class Index implements IndexSpecification {
|
||||
|
||||
public enum Duplicates {
|
||||
RETAIN,
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.springframework.data.document.mongodb.query;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
public interface IndexSpecification {
|
||||
|
||||
DBObject getIndexObject();
|
||||
|
||||
DBObject getIndexOptions();
|
||||
|
||||
}
|
||||
@@ -48,4 +48,12 @@ public class IndexTests {
|
||||
Assert.assertEquals("{ \"name\" : 1}", i.getIndexObject().toString());
|
||||
Assert.assertEquals("{ \"unique\" : true , \"drop_dups\" : true}", i.getIndexOptions().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeospatialIndex() {
|
||||
GeospatialIndex i = new GeospatialIndex("location").withMin(0);
|
||||
Assert.assertEquals("{ \"location\" : \"2d\"}", i.getIndexObject().toString());
|
||||
Assert.assertEquals("{ \"min\" : 0}", i.getIndexOptions().toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user