From bccd302b50da57519194ec5fc4c12834146675d7 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Sat, 12 Feb 2011 16:23:12 -0500 Subject: [PATCH] added IndexSpecification to support creating GeospatialIndex --- .../data/document/mongodb/MongoOperations.java | 12 ++++++------ .../data/document/mongodb/MongoTemplate.java | 14 +++++++------- .../document/mongodb/query/GeospatialIndex.java | 2 +- .../data/document/mongodb/query/Index.java | 2 +- .../document/mongodb/query/IndexSpecification.java | 11 +++++++++++ .../data/document/mongodb/query/IndexTests.java | 8 ++++++++ 6 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/IndexSpecification.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java index f615ac63f..a018faff4 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java @@ -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 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. diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java index f67a259b1..81ad54f26 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java @@ -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() { 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; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/GeospatialIndex.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/GeospatialIndex.java index 0e74185ef..4b67817b6 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/GeospatialIndex.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/GeospatialIndex.java @@ -19,7 +19,7 @@ import com.mongodb.BasicDBObject; import com.mongodb.DBObject; -public class GeospatialIndex { +public class GeospatialIndex implements IndexSpecification { private String keyField; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Index.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Index.java index 8c76b8303..8d94c02ed 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Index.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Index.java @@ -22,7 +22,7 @@ import com.mongodb.BasicDBObject; import com.mongodb.DBObject; -public class Index { +public class Index implements IndexSpecification { public enum Duplicates { RETAIN, diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/IndexSpecification.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/IndexSpecification.java new file mode 100644 index 000000000..197b9f978 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/IndexSpecification.java @@ -0,0 +1,11 @@ +package org.springframework.data.document.mongodb.query; + +import com.mongodb.DBObject; + +public interface IndexSpecification { + + DBObject getIndexObject(); + + DBObject getIndexOptions(); + +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/IndexTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/IndexTests.java index 8c88db8d5..bb6d2694f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/IndexTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/IndexTests.java @@ -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()); + } + }