diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexInfo.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexInfo.java index 1ee4d0281..332a25d10 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexInfo.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexInfo.java @@ -15,9 +15,12 @@ */ package org.springframework.data.mongodb.core.index; +import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; +import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; public class IndexInfo { @@ -47,6 +50,24 @@ public class IndexInfo { return this.indexFields; } + /** + * Returns whether the index is covering exactly the fields given independently of the order. + * + * @param keys must not be {@literal null}. + * @return + */ + public boolean isIndexForFields(Collection keys) { + + Assert.notNull(keys); + List indexKeys = new ArrayList(indexFields.size()); + + for (IndexField field : indexFields) { + indexKeys.add(field.getKey()); + } + + return indexKeys.containsAll(keys); + } + public String getName() { return name; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexInfoUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexInfoUnitTests.java new file mode 100644 index 000000000..de1156ba8 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexInfoUnitTests.java @@ -0,0 +1,42 @@ +/* + * Copyright 2012 the original author or authors. + * + * 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.mongodb.core.index; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Test; +import org.springframework.data.mongodb.core.query.Order; + +/** + * Unit tests for {@link IndexInfo}. + * + * @author Oliver Gierke + */ +public class IndexInfoUnitTests { + + @Test + public void isIndexForFieldsCorrectly() { + + IndexField fooField = IndexField.create("foo", Order.ASCENDING); + IndexField barField = IndexField.create("bar", Order.DESCENDING); + + IndexInfo info = new IndexInfo(Arrays.asList(fooField, barField), "myIndex", false, false, false); + assertThat(info.isIndexForFields(Arrays.asList("foo", "bar")), is(true)); + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/RepositoryIndexCreationIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/RepositoryIndexCreationIntegrationTests.java index b9892012d..bd2f523ef 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/RepositoryIndexCreationIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/RepositoryIndexCreationIntegrationTests.java @@ -15,10 +15,9 @@ */ package org.springframework.data.mongodb.repository; -import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.junit.After; @@ -28,6 +27,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.data.mongodb.core.CollectionCallback; import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.index.IndexInfo; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -70,25 +70,21 @@ public class RepositoryIndexCreationIntegrationTests { @Test public void testname() { - operations.execute(Person.class, new CollectionCallback() { - public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { - List indexInfo = collection.getIndexInfo(); + List indexInfo = operations.indexOps(Person.class).getIndexInfo(); - assertThat(indexInfo.isEmpty(), is(false)); - assertThat(indexInfo.size(), is(greaterThan(2))); - assertThat(getIndexNamesFrom(indexInfo), hasItems("findByLastname", "findByFirstnameNotIn")); - - return null; - } - }); + assertHasIndexForField(indexInfo, "lastname"); + assertHasIndexForField(indexInfo, "firstname"); } - private static List getIndexNamesFrom(List indexes) { - List result = new ArrayList(); - for (DBObject dbObject : indexes) { - result.add(dbObject.get("name").toString()); + private static void assertHasIndexForField(List indexInfo, String... fields) { + + for (IndexInfo info : indexInfo) { + if (info.isIndexForFields(Arrays.asList(fields))) { + return; + } } - return result; + + fail(String.format("Did not find index for field(s) %s in %s!", fields, indexInfo)); } }