Remove duplication of IndexDefinition classes.
This commit is contained in:
@@ -22,7 +22,8 @@ import com.mongodb.CommandResult;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.WriteResult;
|
||||
import org.springframework.data.document.mongodb.query.IndexDefinition;
|
||||
|
||||
import org.springframework.data.document.mongodb.index.IndexDefinition;
|
||||
import org.springframework.data.document.mongodb.query.Query;
|
||||
import org.springframework.data.document.mongodb.query.Update;
|
||||
|
||||
|
||||
@@ -56,13 +56,13 @@ import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoP
|
||||
import org.springframework.data.document.mongodb.convert.MappingMongoConverter;
|
||||
import org.springframework.data.document.mongodb.convert.MongoConverter;
|
||||
import org.springframework.data.document.mongodb.convert.SimpleMongoConverter;
|
||||
import org.springframework.data.document.mongodb.index.IndexDefinition;
|
||||
import org.springframework.data.document.mongodb.mapping.event.AfterConvertEvent;
|
||||
import org.springframework.data.document.mongodb.mapping.event.AfterLoadEvent;
|
||||
import org.springframework.data.document.mongodb.mapping.event.AfterSaveEvent;
|
||||
import org.springframework.data.document.mongodb.mapping.event.BeforeConvertEvent;
|
||||
import org.springframework.data.document.mongodb.mapping.event.BeforeSaveEvent;
|
||||
import org.springframework.data.document.mongodb.mapping.event.MongoMappingEvent;
|
||||
import org.springframework.data.document.mongodb.query.IndexDefinition;
|
||||
import org.springframework.data.document.mongodb.query.Query;
|
||||
import org.springframework.data.document.mongodb.query.Update;
|
||||
import org.springframework.jca.cci.core.ConnectionCallback;
|
||||
@@ -472,9 +472,9 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
||||
DBObject indexOptions = indexDefinition.getIndexOptions();
|
||||
if (indexOptions != null) {
|
||||
collection.ensureIndex(indexDefinition.getIndexObject(), indexOptions);
|
||||
collection.ensureIndex(indexDefinition.getIndexKeys(), indexOptions);
|
||||
} else {
|
||||
collection.ensureIndex(indexDefinition.getIndexObject());
|
||||
collection.ensureIndex(indexDefinition.getIndexKeys());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -16,13 +16,15 @@
|
||||
|
||||
package org.springframework.data.document.mongodb.index;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
public interface IndexDefinition<T> {
|
||||
public interface IndexDefinition {
|
||||
|
||||
T getIndexDefinition();
|
||||
DBObject getIndexKeys();
|
||||
|
||||
T getIndexOptions();
|
||||
DBObject getIndexOptions();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.document.mongodb.mapping.index;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.util.JSON;
|
||||
import org.springframework.data.document.mongodb.index.IndexDefinition;
|
||||
import org.springframework.data.document.mongodb.index.IndexDirection;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
public class MongoIndexDefinition implements IndexDefinition<DBObject> {
|
||||
|
||||
private String collection = null;
|
||||
private String name = null;
|
||||
private IndexDirection direction = IndexDirection.ASCENDING;
|
||||
private boolean unique = false;
|
||||
private boolean dropDups = true;
|
||||
private boolean sparse = false;
|
||||
private String definition = null;
|
||||
|
||||
public MongoIndexDefinition() {
|
||||
}
|
||||
|
||||
public String getCollection() {
|
||||
return collection;
|
||||
}
|
||||
|
||||
public void setCollection(String collection) {
|
||||
this.collection = collection;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public IndexDirection getDirection() {
|
||||
return direction;
|
||||
}
|
||||
|
||||
public void setDirection(IndexDirection direction) {
|
||||
this.direction = direction;
|
||||
}
|
||||
|
||||
public boolean isUnique() {
|
||||
return unique;
|
||||
}
|
||||
|
||||
public void setUnique(boolean unique) {
|
||||
this.unique = unique;
|
||||
}
|
||||
|
||||
public boolean isDropDups() {
|
||||
return dropDups;
|
||||
}
|
||||
|
||||
public void setDropDups(boolean dropDups) {
|
||||
this.dropDups = dropDups;
|
||||
}
|
||||
|
||||
public boolean isSparse() {
|
||||
return sparse;
|
||||
}
|
||||
|
||||
public void setSparse(boolean sparse) {
|
||||
this.sparse = sparse;
|
||||
}
|
||||
|
||||
public String getDefinition() {
|
||||
return definition;
|
||||
}
|
||||
|
||||
public void setDefinition(String definition) {
|
||||
this.definition = definition;
|
||||
}
|
||||
|
||||
public DBObject getIndexDefinition() {
|
||||
DBObject dbo;
|
||||
if (null != definition) {
|
||||
dbo = (DBObject) JSON.parse(definition);
|
||||
} else {
|
||||
dbo = new BasicDBObject();
|
||||
dbo.put(name, (direction == IndexDirection.ASCENDING ? 1 : -1));
|
||||
}
|
||||
return dbo;
|
||||
}
|
||||
|
||||
public DBObject getIndexOptions() {
|
||||
DBObject dbo = new BasicDBObject();
|
||||
dbo.put("dropDups", dropDups);
|
||||
dbo.put("sparse", sparse);
|
||||
dbo.put("unique", unique);
|
||||
return dbo;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.document.mongodb.query;
|
||||
|
||||
import org.springframework.data.document.mongodb.index.IndexDefinition;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
@@ -51,7 +53,7 @@ public class GeospatialIndex implements IndexDefinition {
|
||||
return this;
|
||||
}
|
||||
|
||||
public DBObject getIndexObject() {
|
||||
public DBObject getIndexKeys() {
|
||||
DBObject dbo = new BasicDBObject();
|
||||
dbo.put(keyField, "2d");
|
||||
return dbo;
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.data.document.mongodb.query;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.data.document.mongodb.index.IndexDefinition;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
@@ -36,6 +38,8 @@ public class Index implements IndexDefinition {
|
||||
private boolean unique = false;
|
||||
|
||||
private boolean dropDuplicates = false;
|
||||
|
||||
private boolean sparse = false;
|
||||
|
||||
public Index() {
|
||||
}
|
||||
@@ -58,6 +62,11 @@ public class Index implements IndexDefinition {
|
||||
this.unique = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Index sparse() {
|
||||
this.sparse = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Index unique(Duplicates duplicates) {
|
||||
if (duplicates == Duplicates.DROP) {
|
||||
@@ -66,7 +75,7 @@ public class Index implements IndexDefinition {
|
||||
return unique();
|
||||
}
|
||||
|
||||
public DBObject getIndexObject() {
|
||||
public DBObject getIndexKeys() {
|
||||
DBObject dbo = new BasicDBObject();
|
||||
for (String k : fieldSpec.keySet()) {
|
||||
dbo.put(k, (fieldSpec.get(k).equals(Order.ASCENDING) ? 1 : -1));
|
||||
@@ -86,13 +95,16 @@ public class Index implements IndexDefinition {
|
||||
dbo.put("unique", true);
|
||||
}
|
||||
if (dropDuplicates) {
|
||||
dbo.put("drop_dups", true);
|
||||
dbo.put("dropDups", true);
|
||||
}
|
||||
if (sparse) {
|
||||
dbo.put("sparse", true);
|
||||
}
|
||||
return dbo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Index: %s - Options: %s", getIndexObject(), getIndexOptions());
|
||||
return String.format("Index: %s - Options: %s", getIndexKeys(), getIndexOptions());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package org.springframework.data.document.mongodb.query;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
public interface IndexDefinition {
|
||||
|
||||
DBObject getIndexObject();
|
||||
|
||||
DBObject getIndexOptions();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.springframework.data.document.mongodb;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:geospatial.xml")
|
||||
public class GeoSpatialTests {
|
||||
|
||||
|
||||
@Autowired
|
||||
MongoTemplate template;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
template.dropCollection(template.getDefaultCollectionName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void geoIndex() {
|
||||
assertThat(template, notNullValue());
|
||||
}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ public class MongoTemplateTests {
|
||||
if ("age_-1".equals(ix.get("name"))) {
|
||||
indexKey = ix.get("key").toString();
|
||||
unique = (Boolean) ix.get("unique");
|
||||
dropDupes = (Boolean) ix.get("drop_dups");
|
||||
dropDupes = (Boolean) ix.get("dropDups");
|
||||
}
|
||||
}
|
||||
assertThat(indexKey, is("{ \"age\" : -1}"));
|
||||
|
||||
@@ -24,20 +24,20 @@ public class IndexTests {
|
||||
@Test
|
||||
public void testWithAscendingIndex() {
|
||||
Index i = new Index().on("name", Order.ASCENDING);
|
||||
Assert.assertEquals("{ \"name\" : 1}", i.getIndexObject().toString());
|
||||
Assert.assertEquals("{ \"name\" : 1}", i.getIndexKeys().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithDescendingIndex() {
|
||||
Index i = new Index().on("name", Order.DESCENDING);
|
||||
Assert.assertEquals("{ \"name\" : -1}", i.getIndexObject().toString());
|
||||
Assert.assertEquals("{ \"name\" : -1}", i.getIndexKeys().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamedMultiFieldUniqueIndex() {
|
||||
Index i = new Index().on("name", Order.ASCENDING).on("age", Order.DESCENDING);
|
||||
i.named("test").unique();
|
||||
Assert.assertEquals("{ \"age\" : -1 , \"name\" : 1}", i.getIndexObject().toString());
|
||||
Assert.assertEquals("{ \"age\" : -1 , \"name\" : 1}", i.getIndexKeys().toString());
|
||||
Assert.assertEquals("{ \"name\" : \"test\" , \"unique\" : true}", i.getIndexOptions().toString());
|
||||
}
|
||||
|
||||
@@ -45,14 +45,22 @@ public class IndexTests {
|
||||
public void testWithDropDuplicates() {
|
||||
Index i = new Index().on("name", Order.ASCENDING);
|
||||
i.unique(Duplicates.DROP);
|
||||
Assert.assertEquals("{ \"name\" : 1}", i.getIndexObject().toString());
|
||||
Assert.assertEquals("{ \"unique\" : true , \"drop_dups\" : true}", i.getIndexOptions().toString());
|
||||
Assert.assertEquals("{ \"name\" : 1}", i.getIndexKeys().toString());
|
||||
Assert.assertEquals("{ \"unique\" : true , \"dropDups\" : true}", i.getIndexOptions().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithSparse() {
|
||||
Index i = new Index().on("name", Order.ASCENDING);
|
||||
i.sparse().unique();
|
||||
Assert.assertEquals("{ \"name\" : 1}", i.getIndexKeys().toString());
|
||||
Assert.assertEquals("{ \"unique\" : true , \"sparse\" : true}", i.getIndexOptions().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeospatialIndex() {
|
||||
GeospatialIndex i = new GeospatialIndex("location").withMin(0);
|
||||
Assert.assertEquals("{ \"location\" : \"2d\"}", i.getIndexObject().toString());
|
||||
Assert.assertEquals("{ \"location\" : \"2d\"}", i.getIndexKeys().toString());
|
||||
Assert.assertEquals("{ \"min\" : 0}", i.getIndexOptions().toString());
|
||||
}
|
||||
|
||||
|
||||
21
spring-data-mongodb/src/test/resources/geospatial.xml
Normal file
21
spring-data-mongodb/src/test/resources/geospatial.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
|
||||
|
||||
<mongo:mongo host="localhost" port="27017"/>
|
||||
<mongo:mapping-converter base-package="org.springframework.data.document.mongodb.mapping"
|
||||
autowire="true"/>
|
||||
|
||||
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
|
||||
<constructor-arg ref="mongo"/>
|
||||
<constructor-arg value="geospatial"/>
|
||||
<constructor-arg value="newyork"/>
|
||||
<constructor-arg ref="mappingConverter"/>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.data.document.mongodb.mapping.MappingEventsListener"/>
|
||||
|
||||
</beans>
|
||||
@@ -16,6 +16,4 @@
|
||||
<constructor-arg ref="mappingConverter"/>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.data.document.mongodb.mapping.MappingEventsListener"/>
|
||||
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user