Skip to content

Commit 76e7d29

Browse files
committed
DATAMONGO-2188 - Deprecate auto-index creation.
1 parent e7333a9 commit 76e7d29

File tree

7 files changed

+35
-19
lines changed

7 files changed

+35
-19
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,24 @@
5555

5656
/**
5757
* @return
58-
* @see <a href="https://docs.mongodb.org/manual/core/index-unique/">https://docs.mongodb.org/manual/core/index-unique/</a>
58+
* @see <a href=
59+
* "https://docs.mongodb.org/manual/core/index-unique/">https://docs.mongodb.org/manual/core/index-unique/</a>
5960
*/
6061
boolean unique() default false;
6162

6263
/**
6364
* If set to true index will skip over any document that is missing the indexed field.
6465
*
6566
* @return
66-
* @see <a href="https://docs.mongodb.org/manual/core/index-sparse/">https://docs.mongodb.org/manual/core/index-sparse/</a>
67+
* @see <a href=
68+
* "https://docs.mongodb.org/manual/core/index-sparse/">https://docs.mongodb.org/manual/core/index-sparse/</a>
6769
*/
6870
boolean sparse() default false;
6971

7072
/**
7173
* @return
72-
* @see <a href="https://docs.mongodb.org/manual/core/index-creation/#index-creation-duplicate-dropping">https://docs.mongodb.org/manual/core/index-creation/#index-creation-duplicate-dropping</a>
74+
* @see <a href=
75+
* "https://docs.mongodb.org/manual/core/index-creation/#index-creation-duplicate-dropping">https://docs.mongodb.org/manual/core/index-creation/#index-creation-duplicate-dropping</a>
7376
* @deprecated since 2.1. No longer supported by MongoDB as of server version 3.0.
7477
*/
7578
@Deprecated
@@ -131,7 +134,8 @@
131134
* If {@literal true} the index will be created in the background.
132135
*
133136
* @return
134-
* @see <a href="https://docs.mongodb.org/manual/core/indexes/#background-construction">https://docs.mongodb.org/manual/core/indexes/#background-construction</a>
137+
* @see <a href=
138+
* "https://docs.mongodb.org/manual/core/indexes/#background-construction">https://docs.mongodb.org/manual/core/indexes/#background-construction</a>
135139
*/
136140
boolean background() default false;
137141

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
interface IndexResolver {
2929

3030
/**
31-
* Find and create {@link IndexDefinition}s for properties of given {@link TypeInformation}. {@link IndexDefinition}s are created
32-
* for properties and types with {@link Indexed}, {@link CompoundIndexes} or {@link GeoSpatialIndexed}.
31+
* Find and create {@link IndexDefinition}s for properties of given {@link TypeInformation}. {@link IndexDefinition}s
32+
* are created for properties and types with {@link Indexed}, {@link CompoundIndexes} or {@link GeoSpatialIndexed}.
3333
*
3434
* @param typeInformation
3535
* @return Empty {@link Iterable} in case no {@link IndexDefinition} could be resolved for type.

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@
3131
* @author Christoph Strobl
3232
* @author Jordi Llach
3333
*/
34-
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD})
34+
@Target({ ElementType.ANNOTATION_TYPE, ElementType.FIELD })
3535
@Retention(RetentionPolicy.RUNTIME)
3636
public @interface Indexed {
3737

3838
/**
3939
* If set to true reject all documents that contain a duplicate value for the indexed field.
4040
*
4141
* @return
42-
* @see <a href="https://docs.mongodb.org/manual/core/index-unique/">https://docs.mongodb.org/manual/core/index-unique/</a>
42+
* @see <a href=
43+
* "https://docs.mongodb.org/manual/core/index-unique/">https://docs.mongodb.org/manual/core/index-unique/</a>
4344
*/
4445
boolean unique() default false;
4546

@@ -49,13 +50,15 @@
4950
* If set to true index will skip over any document that is missing the indexed field.
5051
*
5152
* @return
52-
* @see <a href="https://docs.mongodb.org/manual/core/index-sparse/">https://docs.mongodb.org/manual/core/index-sparse/</a>
53+
* @see <a href=
54+
* "https://docs.mongodb.org/manual/core/index-sparse/">https://docs.mongodb.org/manual/core/index-sparse/</a>
5355
*/
5456
boolean sparse() default false;
5557

5658
/**
5759
* @return
58-
* @see <a href="https://docs.mongodb.org/manual/core/index-creation/#index-creation-duplicate-dropping">https://docs.mongodb.org/manual/core/index-creation/#index-creation-duplicate-dropping</a>
60+
* @see <a href=
61+
* "https://docs.mongodb.org/manual/core/index-creation/#index-creation-duplicate-dropping">https://docs.mongodb.org/manual/core/index-creation/#index-creation-duplicate-dropping</a>
5962
* @deprecated since 2.1. No longer supported by MongoDB as of server version 3.0.
6063
*/
6164
@Deprecated
@@ -115,15 +118,17 @@
115118
* If {@literal true} the index will be created in the background.
116119
*
117120
* @return
118-
* @see <a href="https://docs.mongodb.org/manual/core/indexes/#background-construction">https://docs.mongodb.org/manual/core/indexes/#background-construction</a>
121+
* @see <a href=
122+
* "https://docs.mongodb.org/manual/core/indexes/#background-construction">https://docs.mongodb.org/manual/core/indexes/#background-construction</a>
119123
*/
120124
boolean background() default false;
121125

122126
/**
123127
* Configures the number of seconds after which the collection should expire. Defaults to -1 for no expiry.
124128
*
125129
* @return
126-
* @see <a href="https://docs.mongodb.org/manual/tutorial/expire-data/">https://docs.mongodb.org/manual/tutorial/expire-data/</a>
130+
* @see <a href=
131+
* "https://docs.mongodb.org/manual/tutorial/expire-data/">https://docs.mongodb.org/manual/tutorial/expire-data/</a>
127132
*/
128133
int expireAfterSeconds() default -1;
129134
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
* @author Jon Brisbin
3737
* @author Oliver Gierke
3838
* @author Mark Paluch
39+
* @deprecated since 2.2. Use {@link IndexOperations} to define and create indexes.
3940
*/
41+
@Deprecated
4042
public class MongoMappingEventPublisher implements ApplicationEventPublisher {
4143

4244
private final ApplicationListener<MappingContextEvent<?, ?>> indexCreator;

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
6363
/**
6464
* Creates a new {@link MongoPersistentEntityIndexCreator} for the given {@link MongoMappingContext} and
6565
* {@link MongoDbFactory}.
66-
* @param mappingContext must not be {@literal null}.
66+
*
67+
* @param mappingContext must not be {@literal null}.
6768
* @param indexOperationsProvider must not be {@literal null}.
6869
*/
69-
public MongoPersistentEntityIndexCreator(MongoMappingContext mappingContext, IndexOperationsProvider indexOperationsProvider) {
70+
public MongoPersistentEntityIndexCreator(MongoMappingContext mappingContext,
71+
IndexOperationsProvider indexOperationsProvider) {
7072
this(mappingContext, indexOperationsProvider, new MongoPersistentEntityIndexResolver(mappingContext));
7173
}
7274

@@ -78,8 +80,8 @@ public MongoPersistentEntityIndexCreator(MongoMappingContext mappingContext, Ind
7880
* @param mongoDbFactory must not be {@literal null}.
7981
* @param indexResolver must not be {@literal null}.
8082
*/
81-
public MongoPersistentEntityIndexCreator(MongoMappingContext mappingContext, IndexOperationsProvider indexOperationsProvider,
82-
IndexResolver indexResolver) {
83+
public MongoPersistentEntityIndexCreator(MongoMappingContext mappingContext,
84+
IndexOperationsProvider indexOperationsProvider, IndexResolver indexResolver) {
8385

8486
Assert.notNull(mappingContext, "MongoMappingContext must not be null!");
8587
Assert.notNull(indexOperationsProvider, "IndexOperationsProvider must not be null!");
@@ -146,8 +148,8 @@ void createIndex(IndexDefinitionHolder indexDefinition) {
146148

147149
} catch (UncategorizedMongoDbException ex) {
148150

149-
if (ex.getCause() instanceof MongoException &&
150-
MongoDbErrorCodes.isDataIntegrityViolationCode(((MongoException) ex.getCause()).getCode())) {
151+
if (ex.getCause() instanceof MongoException
152+
&& MongoDbErrorCodes.isDataIntegrityViolationCode(((MongoException) ex.getCause()).getCode())) {
151153

152154
IndexInfo existingIndex = fetchIndexInformation(indexDefinition);
153155
String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'.";

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/IndexEnsuringQueryCreationListener.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
* @author Oliver Gierke
4343
* @author Mark Paluch
4444
* @author Christoph Strobl
45+
* @deprecated since 2.2. Use {@link org.springframework.data.mongodb.core.index.IndexOperations} to define and create
46+
* indexes.
4547
*/
48+
@Deprecated
4649
class IndexEnsuringQueryCreationListener implements QueryCreationListener<PartTreeMongoQuery> {
4750

4851
private static final Set<Type> GEOSPATIAL_TYPES = new HashSet<Type>(Arrays.asList(Type.NEAR, Type.WITHIN));

src/main/asciidoc/reference/mapping.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public class Person {
378378

379379
IMPORTANT: The `@Id` annotation tells the mapper which property you want to use for the MongoDB `_id` property, and the `@Indexed` annotation tells the mapping framework to call `createIndex(…)` on that property of your document, making searches faster.
380380

381-
IMPORTANT: Automatic index creation is only done for types annotated with `@Document`.
381+
IMPORTANT: Automatic index creation is deprecated since version 2.2 because controlling the actual time of index creation is rather difficult. Index creation can be part of the application startup, happen during runtime or an out of band process. Therefore, Spring Data MongoDB backs off entirely and recommends index creation to happen either out of band or as part of the application startup using `IndexOperations`. Automatic index creation is still available and is is only done for types annotated with `@Document`.
382382

383383
[[mapping-usage-annotations]]
384384
=== Mapping Annotation Overview

0 commit comments

Comments
 (0)