DATAMONGO-360 - Beautify IndexInfo API.
Added a method to IndexInfo to allow asking the object whether it is an index with a given collection of fields. Refactored RepositoryIndexCreationIntegrationTest to use that API to be more safe against potential duplicate index definitions.
This commit is contained in:
@@ -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<String> keys) {
|
||||
|
||||
Assert.notNull(keys);
|
||||
List<String> indexKeys = new ArrayList<String>(indexFields.size());
|
||||
|
||||
for (IndexField field : indexFields) {
|
||||
indexKeys.add(field.getKey());
|
||||
}
|
||||
|
||||
return indexKeys.containsAll(keys);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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<Void>() {
|
||||
|
||||
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
||||
List<DBObject> indexInfo = collection.getIndexInfo();
|
||||
List<IndexInfo> 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<String> getIndexNamesFrom(List<DBObject> indexes) {
|
||||
List<String> result = new ArrayList<String>();
|
||||
for (DBObject dbObject : indexes) {
|
||||
result.add(dbObject.get("name").toString());
|
||||
private static void assertHasIndexForField(List<IndexInfo> 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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user