Skip to content

Commit 5c79ff3

Browse files
mp911dechristophstrobl
authored andcommitted
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
1 parent 33fa79b commit 5c79ff3

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexResolver.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,33 @@
1616
package org.springframework.data.mongodb.core.index;
1717

1818
import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder;
19+
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
20+
import org.springframework.data.util.ClassTypeInformation;
1921
import org.springframework.data.util.TypeInformation;
22+
import org.springframework.util.Assert;
2023

2124
/**
2225
* {@link IndexResolver} finds those {@link IndexDefinition}s to be created for a given class.
2326
*
2427
* @author Christoph Strobl
2528
* @author Thomas Darimont
29+
* @author Mark Paluch
2630
* @since 1.5
2731
*/
28-
interface IndexResolver {
32+
public interface IndexResolver {
33+
34+
/**
35+
* Creates a new {@link IndexResolver} given {@link MongoMappingContext}.
36+
*
37+
* @param mappingContext must not be {@literal null}.
38+
* @return the new {@link IndexResolver}.
39+
*/
40+
static IndexResolver create(MongoMappingContext mappingContext) {
41+
42+
Assert.notNull(mappingContext, "MongoMappingContext must not be null!");
43+
44+
return new MongoPersistentEntityIndexResolver(mappingContext);
45+
}
2946

3047
/**
3148
* Find and create {@link IndexDefinition}s for properties of given {@link TypeInformation}. {@link IndexDefinition}s
@@ -36,4 +53,16 @@ interface IndexResolver {
3653
*/
3754
Iterable<? extends IndexDefinitionHolder> resolveIndexFor(TypeInformation<?> typeInformation);
3855

56+
/**
57+
* Find and create {@link IndexDefinition}s for properties of given {@link TypeInformation}. {@link IndexDefinition}s
58+
* are created for properties and types with {@link Indexed}, {@link CompoundIndexes} or {@link GeoSpatialIndexed}.
59+
*
60+
* @param entityType
61+
* @return Empty {@link Iterable} in case no {@link IndexDefinition} could be resolved for type.
62+
* @see 2.2
63+
*/
64+
default Iterable<? extends IndexDefinitionHolder> resolveIndexFor(Class<?> entityType) {
65+
return resolveIndexFor(ClassTypeInformation.from(entityType));
66+
}
67+
3968
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
6969
*/
7070
public MongoPersistentEntityIndexCreator(MongoMappingContext mappingContext,
7171
IndexOperationsProvider indexOperationsProvider) {
72-
this(mappingContext, indexOperationsProvider, new MongoPersistentEntityIndexResolver(mappingContext));
72+
this(mappingContext, indexOperationsProvider, IndexResolver.create(mappingContext));
7373
}
7474

7575
/**

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/ReactiveMongoPersistentEntityIndexCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class ReactiveMongoPersistentEntityIndexCreator {
6363
*/
6464
public ReactiveMongoPersistentEntityIndexCreator(MongoMappingContext mappingContext,
6565
ReactiveIndexOperationsProvider operationsProvider) {
66-
this(mappingContext, operationsProvider, new MongoPersistentEntityIndexResolver(mappingContext));
66+
this(mappingContext, operationsProvider, IndexResolver.create(mappingContext));
6767
}
6868

6969
/**

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public class MongoPersistentEntityIndexResolverUnitTests {
6464
* Test resolution of {@link Indexed}.
6565
*
6666
* @author Christoph Strobl
67+
* @author Mark Paluch
6768
*/
6869
public static class IndexResolutionTests {
6970

@@ -87,7 +88,17 @@ public void indexPathOnLevelOneIsResolvedCorrectly() {
8788
}
8889

8990
@Test // DATAMONGO-899
90-
public void depplyNestedIndexPathIsResolvedCorrectly() {
91+
public void shouldResolveIndexViaClass() {
92+
93+
MongoMappingContext mappingContext = new MongoMappingContext();
94+
IndexResolver indexResolver = IndexResolver.create(mappingContext);
95+
Iterable<? extends IndexDefinitionHolder> definitions = indexResolver.resolveIndexFor(IndexOnLevelOne.class);
96+
97+
assertThat(definitions.iterator().hasNext(), is(true));
98+
}
99+
100+
@Test // DATAMONGO-899
101+
public void deeplyNestedIndexPathIsResolvedCorrectly() {
91102

92103
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(IndexOnLevelTwo.class);
93104

0 commit comments

Comments
 (0)