DATAMONGO-2188 - Expose IndexResolver.

We now provide IndexResolver to resolve and derive index definitions from Mongo entities.

MongoMappingContext mappingContext = new MongoMappingContext();
IndexResolver indexResolver = IndexResolver.create(mappingContext);
Iterable<? extends IndexDefinitionHolder> definitions = indexResolver.resolveIndexFor(MyEntity.class);

Original Pull Request: #636
This commit is contained in:
Mark Paluch
2019-01-16 14:52:52 +01:00
committed by Christoph Strobl
parent 33fa79b29f
commit 5c79ff387e
4 changed files with 44 additions and 4 deletions

View File

@@ -16,16 +16,33 @@
package org.springframework.data.mongodb.core.index;
import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
/**
* {@link IndexResolver} finds those {@link IndexDefinition}s to be created for a given class.
*
* @author Christoph Strobl
* @author Thomas Darimont
* @author Mark Paluch
* @since 1.5
*/
interface IndexResolver {
public interface IndexResolver {
/**
* Creates a new {@link IndexResolver} given {@link MongoMappingContext}.
*
* @param mappingContext must not be {@literal null}.
* @return the new {@link IndexResolver}.
*/
static IndexResolver create(MongoMappingContext mappingContext) {
Assert.notNull(mappingContext, "MongoMappingContext must not be null!");
return new MongoPersistentEntityIndexResolver(mappingContext);
}
/**
* Find and create {@link IndexDefinition}s for properties of given {@link TypeInformation}. {@link IndexDefinition}s
@@ -36,4 +53,16 @@ interface IndexResolver {
*/
Iterable<? extends IndexDefinitionHolder> resolveIndexFor(TypeInformation<?> typeInformation);
/**
* Find and create {@link IndexDefinition}s for properties of given {@link TypeInformation}. {@link IndexDefinition}s
* are created for properties and types with {@link Indexed}, {@link CompoundIndexes} or {@link GeoSpatialIndexed}.
*
* @param entityType
* @return Empty {@link Iterable} in case no {@link IndexDefinition} could be resolved for type.
* @see 2.2
*/
default Iterable<? extends IndexDefinitionHolder> resolveIndexFor(Class<?> entityType) {
return resolveIndexFor(ClassTypeInformation.from(entityType));
}
}

View File

@@ -69,7 +69,7 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
*/
public MongoPersistentEntityIndexCreator(MongoMappingContext mappingContext,
IndexOperationsProvider indexOperationsProvider) {
this(mappingContext, indexOperationsProvider, new MongoPersistentEntityIndexResolver(mappingContext));
this(mappingContext, indexOperationsProvider, IndexResolver.create(mappingContext));
}
/**

View File

@@ -63,7 +63,7 @@ public class ReactiveMongoPersistentEntityIndexCreator {
*/
public ReactiveMongoPersistentEntityIndexCreator(MongoMappingContext mappingContext,
ReactiveIndexOperationsProvider operationsProvider) {
this(mappingContext, operationsProvider, new MongoPersistentEntityIndexResolver(mappingContext));
this(mappingContext, operationsProvider, IndexResolver.create(mappingContext));
}
/**

View File

@@ -64,6 +64,7 @@ public class MongoPersistentEntityIndexResolverUnitTests {
* Test resolution of {@link Indexed}.
*
* @author Christoph Strobl
* @author Mark Paluch
*/
public static class IndexResolutionTests {
@@ -87,7 +88,17 @@ public class MongoPersistentEntityIndexResolverUnitTests {
}
@Test // DATAMONGO-899
public void depplyNestedIndexPathIsResolvedCorrectly() {
public void shouldResolveIndexViaClass() {
MongoMappingContext mappingContext = new MongoMappingContext();
IndexResolver indexResolver = IndexResolver.create(mappingContext);
Iterable<? extends IndexDefinitionHolder> definitions = indexResolver.resolveIndexFor(IndexOnLevelOne.class);
assertThat(definitions.iterator().hasNext(), is(true));
}
@Test // DATAMONGO-899
public void deeplyNestedIndexPathIsResolvedCorrectly() {
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(IndexOnLevelTwo.class);