From 4eaef300cb8628a0caf9656ac6cee37996b92b0c Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 21 Aug 2014 13:35:26 +0200 Subject: [PATCH] DATAMONGO-1025 - Fix creation of nested named index. Deprecated collection attribute for @Indexed, @CompoundIndex, @GeoSpatialIndexed. Removed deprecated attribute `expireAfterSeconds` from @CompoundIndex. Original pull request: #219. --- .../mongodb/core/index/CompoundIndex.java | 13 ++------ .../mongodb/core/index/GeoSpatialIndexed.java | 3 ++ .../data/mongodb/core/index/Indexed.java | 5 ++- .../MongoPersistentEntityIndexResolver.java | 10 ------ ...ersistentEntityIndexResolverUnitTests.java | 32 +++---------------- 5 files changed, 15 insertions(+), 48 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java index eb31e7a96..2b1807607 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java @@ -129,7 +129,10 @@ public @interface CompoundIndex { * stored in. * * @return + * @deprecated The collection name is derived from the domain type. Fixing the collection via this attribute might + * result in broken definitions. Will be removed in 1.7. */ + @Deprecated String collection() default ""; /** @@ -140,14 +143,4 @@ public @interface CompoundIndex { */ boolean background() default false; - /** - * Configures the number of seconds after which the collection should expire. Defaults to -1 for no expiry. - * - * @deprecated TTL cannot be defined for {@link CompoundIndex} having more than one field as key. Will be removed in - * 1.6. - * @see http://docs.mongodb.org/manual/tutorial/expire-data/ - * @return - */ - @Deprecated - int expireAfterSeconds() default -1; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeoSpatialIndexed.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeoSpatialIndexed.java index 7b4c09c79..0f13634ff 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeoSpatialIndexed.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeoSpatialIndexed.java @@ -85,7 +85,10 @@ public @interface GeoSpatialIndexed { * Name of the collection in which to create the index. * * @return + * @deprecated The collection name is derived from the domain type. Fixing the collection via this attribute might + * result in broken definitions. Will be removed in 1.7. */ + @Deprecated String collection() default ""; /** diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java index a907d4770..58f7c65b6 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java @@ -108,10 +108,13 @@ public @interface Indexed { boolean useGeneratedName() default false; /** - * Colleciton name for index to be created on. + * Collection name for index to be created on. * * @return + * @deprecated The collection name is derived from the domain type. Fixing the collection via this attribute might + * result in broken definitions. Will be removed in 1.7. */ + @Deprecated String collection() default ""; /** diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java index 947a56b9a..6881f658e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java @@ -320,16 +320,6 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { indexDefinition.background(); } - int ttl = index.expireAfterSeconds(); - - if (ttl >= 0) { - if (indexDefinition.getIndexKeys().keySet().size() > 1) { - LOGGER.warn("TTL is not supported for compound index with more than one key. TTL={} will be ignored.", ttl); - } else { - indexDefinition.expire(ttl, TimeUnit.SECONDS); - } - } - String collection = StringUtils.hasText(index.collection()) ? index.collection() : fallbackCollection; return new IndexDefinitionHolder(dotPath, indexDefinition, collection); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java index 2af355571..48f14d99f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java @@ -374,22 +374,6 @@ public class MongoPersistentEntityIndexResolverUnitTests { assertIndexPathAndCollection(new String[] { "foo", "bar" }, "CompoundIndexOnLevelZero", indexDefinitions.get(0)); } - /** - * @see DATAMONGO-963 - */ - @Test - public void compoundIndexShouldIncludeTTLWhenConsistingOfOnlyOneKey() { - - List indexDefinitions = prepareMappingContextAndResolveIndexForType(CompoundIndexWithOnlyOneKeyAndTTL.class); - - IndexDefinition indexDefinition = indexDefinitions.get(0).getIndexDefinition(); - assertThat( - indexDefinition.getIndexOptions(), - equalTo(new BasicDBObjectBuilder().add("unique", true).add("dropDups", true).add("sparse", true) - .add("background", true).add("expireAfterSeconds", 10L).get())); - assertThat(indexDefinition.getIndexKeys(), equalTo(new BasicDBObjectBuilder().add("foo", 1).get())); - } - @Document(collection = "CompoundIndexOnLevelOne") static class CompoundIndexOnLevelOne { @@ -404,16 +388,16 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Document(collection = "CompoundIndexOnLevelZero") @CompoundIndexes({ @CompoundIndex(name = "compound_index", def = "{'foo': 1, 'bar': -1}", background = true, - dropDups = true, expireAfterSeconds = 10, sparse = true, unique = true) }) + dropDups = true, sparse = true, unique = true) }) static class CompoundIndexOnLevelZero {} - @CompoundIndexes({ @CompoundIndex(name = "compound_index", background = true, dropDups = true, - expireAfterSeconds = 10, sparse = true, unique = true) }) + @CompoundIndexes({ @CompoundIndex(name = "compound_index", background = true, dropDups = true, sparse = true, + unique = true) }) static class CompoundIndexOnLevelZeroWithEmptyIndexDef {} @Document(collection = "CompoundIndexOnLevelZero") @CompoundIndex(name = "compound_index", def = "{'foo': 1, 'bar': -1}", background = true, dropDups = true, - expireAfterSeconds = 10, sparse = true, unique = true) + sparse = true, unique = true) static class SingleCompoundIndex {} static class IndexDefinedOnSuperClass extends CompoundIndexOnLevelZero { @@ -422,17 +406,11 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Document(collection = "ComountIndexWithAutogeneratedName") @CompoundIndexes({ @CompoundIndex(useGeneratedName = true, def = "{'foo': 1, 'bar': -1}", background = true, - dropDups = true, expireAfterSeconds = 10, sparse = true, unique = true) }) + dropDups = true, sparse = true, unique = true) }) static class ComountIndexWithAutogeneratedName { } - @Document(collection = "CompoundIndexWithOnlyOneKeyAndTTL") - @CompoundIndex(def = "{'foo': 1}", background = true, dropDups = true, expireAfterSeconds = 10, sparse = true, - unique = true) - static class CompoundIndexWithOnlyOneKeyAndTTL { - - } } public static class TextIndexedResolutionTests {