From 613d085bb71ac3e43130cc1d511f6f2ab8922796 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 18 Jun 2020 15:06:04 +0200 Subject: [PATCH] DATAMONGO-1569 - Polishing. Update Javadoc and avoid unrelated index creation during tests due to class path scanning. Original Pull Request: #738 --- .../mongodb/core/index/CompoundIndex.java | 12 +- .../MongoPersistentEntityIndexResolver.java | 4 +- ...ersistentEntityIndexResolverUnitTests.java | 179 +++++++++--------- 3 files changed, 98 insertions(+), 97 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 b6808dd8f..373992c99 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 @@ -96,7 +96,8 @@ public @interface CompoundIndex { boolean unique() default false; /** - * If set to true index will skip over any document that is missing the indexed field. + * If set to true index will skip over any document that is missing the indexed field.
+ * Must not be used with {@link #partialFilter()}. * * @return {@literal false} by default. * @see + * Must not be used with {@link #sparse() sparse = true}. * - * @return + * @return empty by default. * @see https://docs.mongodb.com/manual/core/index-partial/ + * @since 3.1 */ - String partial() default ""; + String partialFilter() 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 aa3578bad..6d3b2b612 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 @@ -382,8 +382,8 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { indexDefinition.background(); } - if (StringUtils.hasText(index.partial())) { - indexDefinition.partial(PartialIndexFilter.of(org.bson.Document.parse(index.partial()))); + if (StringUtils.hasText(index.partialFilter())) { + indexDefinition.partial(evaluatePartialFilter(index.partialFilter(), entity)); } 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 fbddbdbb3..996e380f8 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 @@ -282,22 +282,22 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document("Zero") - static class IndexOnLevelZero { + class IndexOnLevelZero { @Indexed String indexedProperty; } @Document("One") - static class IndexOnLevelOne { + class IndexOnLevelOne { IndexOnLevelZero zero; } @Document("Two") - static class IndexOnLevelTwo { + class IndexOnLevelTwo { IndexOnLevelOne one; } @Document("WithOptionsOnIndexedProperty") - static class WithOptionsOnIndexedProperty { + class WithOptionsOnIndexedProperty { @Indexed(background = true, direction = IndexDirection.DESCENDING, dropDups = true, expireAfterSeconds = 10, sparse = true, unique = true) // @@ -305,23 +305,23 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class IndexOnLevelOneWithExplicitlyNamedField { + class IndexOnLevelOneWithExplicitlyNamedField { @Field("customZero") IndexOnLevelZeroWithExplicityNamedField zero; } - static class IndexOnLevelZeroWithExplicityNamedField { + class IndexOnLevelZeroWithExplicityNamedField { @Indexed @Field("customFieldName") String namedProperty; } @Document - static class WrapperOfWithDbRef { + class WrapperOfWithDbRef { WithDbRef nested; } @Document - static class WithDbRef { + class WithDbRef { @Indexed // @DBRef // @@ -329,12 +329,12 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document("no-index") - static class NoIndex { + class NoIndex { @Id String id; } @Document - static class IndexedDocumentWithComposedAnnotations { + class IndexedDocumentWithComposedAnnotations { @Id String id; @CustomIndexedAnnotation String fieldWithDifferentIndexName; @@ -372,7 +372,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class WithExpireAfterAsPlainString { + class WithExpireAfterAsPlainString { @Indexed(expireAfter = "10m") String withTimeout; } @@ -382,12 +382,12 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class WithExpireAfterAsExpression { + class WithExpireAfterAsExpression { @Indexed(expireAfter = "#{10 + 1 + 's'}") String withTimeout; } @Document - static class WithExpireAfterAsExpressionResultingInDuration { + class WithExpireAfterAsExpressionResultingInDuration { @Indexed(expireAfter = "#{T(java.time.Duration).ofSeconds(100)}") String withTimeout; } @@ -402,7 +402,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class WithIndexNameAsExpression { + class WithIndexNameAsExpression { @Indexed(name = "#{'my' + 1 + 'st'}") String spelIndexName; } @@ -419,7 +419,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class IndexOnMetaAnnotatedField { + class IndexOnMetaAnnotatedField { @Field("_name") @IndexedFieldAnnotation String lastname; } @@ -496,29 +496,29 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document("Zero") - static class GeoSpatialIndexOnLevelZero { + class GeoSpatialIndexOnLevelZero { @GeoSpatialIndexed Point geoIndexedProperty; } @Document("One") - static class GeoSpatialIndexOnLevelOne { + class GeoSpatialIndexOnLevelOne { GeoSpatialIndexOnLevelZero zero; } @Document("Two") - static class GeoSpatialIndexOnLevelTwo { + class GeoSpatialIndexOnLevelTwo { GeoSpatialIndexOnLevelOne one; } @Document("WithOptionsOnGeoSpatialIndexProperty") - static class WithOptionsOnGeoSpatialIndexProperty { + class WithOptionsOnGeoSpatialIndexProperty { @GeoSpatialIndexed(bits = 2, max = 100, min = 1, type = GeoSpatialIndexType.GEO_2D) // Point location; } @Document("WithComposedAnnotation") - static class GeoSpatialIndexedDocumentWithComposedAnnotation { + class GeoSpatialIndexedDocumentWithComposedAnnotation { @ComposedGeoSpatialIndexed // Point location; @@ -543,7 +543,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class GeoIndexWithNameAsExpression { + class GeoIndexWithNameAsExpression { @GeoSpatialIndexed(name = "#{'my' + 1 + 'st'}") Point spelIndexName; } @@ -690,18 +690,17 @@ public class MongoPersistentEntityIndexResolverUnitTests { assertThat(indexDefinitions.get(0).getIndexOptions()).containsEntry("name", "compound_index_with_partial") .containsEntry("unique", true).containsEntry("background", true); assertThat(indexDefinitions.get(0).getIndexOptions()).containsEntry("partialFilterExpression", - new org.bson.Document().append("bar", new org.bson.Document().append("$exists", true))); + org.bson.Document.parse("{'value': {'$exists': true}}")); } - @Document("CompoundIndexOnLevelOne") - static class CompoundIndexOnLevelOne { + class CompoundIndexOnLevelOne { CompoundIndexOnLevelZero zero; } @Document("CompoundIndexOnLevelZeroWithEmptyIndexDef") - static class CompoundIndexOnLevelOneWithEmptyIndexDefinition { + class CompoundIndexOnLevelOneWithEmptyIndexDefinition { CompoundIndexOnLevelZeroWithEmptyIndexDef zero; } @@ -709,26 +708,26 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Document("CompoundIndexOnLevelZero") @CompoundIndexes({ @CompoundIndex(name = "compound_index", def = "{'foo': 1, 'bar': -1}", background = true, sparse = true, unique = true) }) - static class CompoundIndexOnLevelZero {} + class CompoundIndexOnLevelZero {} @CompoundIndexes({ @CompoundIndex(name = "compound_index", background = true, sparse = true, unique = true) }) - static class CompoundIndexOnLevelZeroWithEmptyIndexDef {} + class CompoundIndexOnLevelZeroWithEmptyIndexDef {} @Document("CompoundIndexOnLevelZero") @CompoundIndex(name = "compound_index", def = "{'foo': 1, 'bar': -1}", background = true, sparse = true, unique = true) - static class SingleCompoundIndex {} + class SingleCompoundIndex {} - static class IndexDefinedOnSuperClass extends CompoundIndexOnLevelZero {} + class IndexDefinedOnSuperClass extends CompoundIndexOnLevelZero {} @Document("ComountIndexWithAutogeneratedName") @CompoundIndexes({ @CompoundIndex(useGeneratedName = true, def = "{'foo': 1, 'bar': -1}", background = true, sparse = true, unique = true) }) - static class ComountIndexWithAutogeneratedName {} + class ComountIndexWithAutogeneratedName {} @Document("WithComposedAnnotation") @ComposedCompoundIndex - static class CompoundIndexDocumentWithComposedAnnotation {} + class CompoundIndexDocumentWithComposedAnnotation {} @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE }) @@ -754,21 +753,21 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Document @CompoundIndex(name = "#{'cmp' + 2 + 'name'}", def = "{'foo': 1, 'bar': -1}") - static class CompoundIndexWithNameExpression {} + class CompoundIndexWithNameExpression {} @Document @CompoundIndex(def = "#{T(org.bson.Document).parse(\"{ 'foo': 1, 'bar': -1 }\")}") - static class CompoundIndexWithDefExpression {} + class CompoundIndexWithDefExpression {} @Document @CompoundIndex(name = "cmp-idx-one", def = "{'firstname': 1, 'lastname': -1}") @CompoundIndex(name = "cmp-idx-two", def = "{'address.city': -1, 'address.street': 1}") - static class RepeatedCompoundIndex {} + class RepeatedCompoundIndex {} @Document("SingleCompoundIndexWithPartialFilter") @CompoundIndex(name = "compound_index_with_partial", def = "{'foo': 1, 'bar': -1}", background = true, - unique = true, partial = "{'bar': {$exists: true}}") - static class SingleCompoundIndexWithPartialFilter {} + unique = true, partialFilter = "{'value': {'$exists': true}}") + class SingleCompoundIndexWithPartialFilter {} } public static class TextIndexedResolutionTests { @@ -904,7 +903,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class TextIndexOnSinglePropertyInRoot { + class TextIndexOnSinglePropertyInRoot { String foo; @@ -912,13 +911,13 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document(collation = "de_AT") - static class TextIndexWithCollation { + class TextIndexWithCollation { @TextIndexed String foo; } @Document - static class TextIndexOnMultiplePropertiesInRoot { + class TextIndexOnMultiplePropertiesInRoot { @TextIndexed String foo; @@ -926,48 +925,48 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class TextIndexOnNestedRoot { + class TextIndexOnNestedRoot { String bar; @TextIndexed TextIndexOnNested nested; } - static class TextIndexOnNested { + class TextIndexOnNested { String foo; } @Document - static class TextIndexOnNestedWithWeightRoot { + class TextIndexOnNestedWithWeightRoot { @TextIndexed(weight = 5) TextIndexOnNested nested; } @Document - static class TextIndexOnNestedWithMostSpecificValueRoot { + class TextIndexOnNestedWithMostSpecificValueRoot { @TextIndexed(weight = 5) TextIndexOnNestedWithMostSpecificValue nested; } - static class TextIndexOnNestedWithMostSpecificValue { + class TextIndexOnNestedWithMostSpecificValue { String foo; @TextIndexed(weight = 10) String bar; } @Document(language = "spanish") - static class DocumentWithDefaultLanguage { + class DocumentWithDefaultLanguage { @TextIndexed String foo; } @Document - static class DocumentWithLanguageOverrideOnNestedElement { + class DocumentWithLanguageOverrideOnNestedElement { DocumentWithLanguageOverride nested; } @Document - static class DocumentWithLanguageOverride { + class DocumentWithLanguageOverride { @TextIndexed String foo; @@ -975,19 +974,19 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class DocumentWithNoTextIndexPropertyButReservedFieldLanguage { + class DocumentWithNoTextIndexPropertyButReservedFieldLanguage { String language; } @Document - static class DocumentWithNoTextIndexPropertyButReservedFieldLanguageAnnotated { + class DocumentWithNoTextIndexPropertyButReservedFieldLanguageAnnotated { @Field("language") String lang; } @Document - static class DocumentWithOverlappingLanguageProps { + class DocumentWithOverlappingLanguageProps { @TextIndexed String foo; String language; @@ -995,7 +994,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class TextIndexedDocumentWithComposedAnnotation { + class TextIndexedDocumentWithComposedAnnotation { @ComposedTextIndexedAnnotation String foo; String lang; @@ -1284,104 +1283,104 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class MixedIndexRoot { + class MixedIndexRoot { @Indexed String first; NestedGeoIndex nestedGeo; } - static class NestedGeoIndex { + class NestedGeoIndex { @GeoSpatialIndexed Point location; } @Document - static class Outer { + class Outer { @DBRef Inner inner; } @Document - static class Inner { + class Inner { @Indexed Outer outer; } @Document - static class CycleLevelZero { + class CycleLevelZero { @Indexed String indexedProperty; CycleLevelZero cyclicReference; } @Document - static class CycleOnLevelOne { + class CycleOnLevelOne { CycleOnLevelOneReferenced reference; } - static class CycleOnLevelOneReferenced { + class CycleOnLevelOneReferenced { @Indexed String indexedProperty; CycleOnLevelOne cyclicReference; } @Document - public static class CycleStartingInBetween { + static class CycleStartingInBetween { CycleOnLevelOne referenceToCycleStart; } @Document - static class NoCycleButIdenticallyNamedProperties { + class NoCycleButIdenticallyNamedProperties { @Indexed String foo; NoCycleButIdenticallyNamedPropertiesNested reference; } - static class NoCycleButIdenticallyNamedPropertiesNested { + class NoCycleButIdenticallyNamedPropertiesNested { @Indexed String foo; NoCycleButIndenticallNamedPropertiesDeeplyNested deep; } - static class NoCycleButIndenticallNamedPropertiesDeeplyNested { + class NoCycleButIndenticallNamedPropertiesDeeplyNested { @Indexed String foo; } @Document("rules") - static class NoCycleManyPathsToDeepValueObject { + class NoCycleManyPathsToDeepValueObject { private NoCycleLevel3 l3; private NoCycleLevel2 l2; } - static class NoCycleLevel2 { + class NoCycleLevel2 { private NoCycleLevel3 l3; } - static class NoCycleLevel3 { + class NoCycleLevel3 { private ValueObject valueObject; } - static class ValueObject { + class ValueObject { @Indexed private String value; } @Document - static class SimilarityHolingBean { + class SimilarityHolingBean { @Indexed @Field("norm") String normalProperty; @Field("similarityL") private List listOfSimilarilyNamedEntities = null; } - static class SimilaritySibling { + class SimilaritySibling { @Field("similarity") private String similarThoughNotEqualNamedProperty; } @Document - static class MultipleObjectsOfSameType { + class MultipleObjectsOfSameType { SelfCyclingViaCollectionType cycleOne; @@ -1389,7 +1388,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class SelfCyclingViaCollectionType { + class SelfCyclingViaCollectionType { List cyclic; @@ -1397,55 +1396,55 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Document @CompoundIndex(name = "c_index", def = "{ foo:1, bar:1 }") - static class DocumentWithNamedCompoundIndex { + class DocumentWithNamedCompoundIndex { String property; } @Document - static class DocumentWithNamedIndex { + class DocumentWithNamedIndex { @Indexed(name = "property_index") String property; } - static class TypeWithNamedIndex { + class TypeWithNamedIndex { @Indexed(name = "property_index") String property; } @Document - static class DocumentWithNestedDocumentHavingNamedCompoundIndex { + class DocumentWithNestedDocumentHavingNamedCompoundIndex { DocumentWithNamedCompoundIndex propertyOfTypeHavingNamedCompoundIndex; } @CompoundIndex(name = "c_index", def = "{ foo:1, bar:1 }") - static class TypeWithNamedCompoundIndex { + class TypeWithNamedCompoundIndex { String property; } @Document - static class DocumentWithNestedTypeHavingNamedCompoundIndex { + class DocumentWithNestedTypeHavingNamedCompoundIndex { TypeWithNamedCompoundIndex propertyOfTypeHavingNamedCompoundIndex; } @Document - static class DocumentWithNestedDocumentHavingNamedIndex { + class DocumentWithNestedDocumentHavingNamedIndex { DocumentWithNamedIndex propertyOfTypeHavingNamedIndex; } @Document - static class DocumentWithNestedTypeHavingNamedIndex { + class DocumentWithNestedTypeHavingNamedIndex { TypeWithNamedIndex propertyOfTypeHavingNamedIndex; } @Document - public class MultiplePropertiesOfSameTypeWithMatchingStartLetters { + class MultiplePropertiesOfSameTypeWithMatchingStartLetters { - public class NameComponent { + class NameComponent { @Indexed String component; } @@ -1455,9 +1454,9 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - public class MultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty { + class MultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty { - public class NameComponent { + class NameComponent { @Indexed String nameLast; @Indexed String name; @@ -1467,39 +1466,39 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - public static class OuterDocumentReferingToIndexedPropertyViaDifferentNonCyclingPaths { + static class OuterDocumentReferingToIndexedPropertyViaDifferentNonCyclingPaths { NoCycleButIndenticallNamedPropertiesDeeplyNested path1; AlternatePathToNoCycleButIndenticallNamedPropertiesDeeplyNestedDocument path2; } - public static class AlternatePathToNoCycleButIndenticallNamedPropertiesDeeplyNestedDocument { + static class AlternatePathToNoCycleButIndenticallNamedPropertiesDeeplyNestedDocument { NoCycleButIndenticallNamedPropertiesDeeplyNested propertyWithIndexedStructure; } - static class GenericEntityWrapper { + class GenericEntityWrapper { T entity; } @Document - static class EntityWithGenericTypeWrapperAsElement { + class EntityWithGenericTypeWrapperAsElement { List> listWithGeneircTypeElement; } @Document - static class WithHashedIndexOnId { + class WithHashedIndexOnId { @HashIndexed @Id String id; } @Document - static class WithHashedIndex { + class WithHashedIndex { @HashIndexed String value; } @Document - static class WithHashedIndexAndIndex { + class WithHashedIndexAndIndex { @Indexed // @HashIndexed // @@ -1507,7 +1506,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { } @Document - static class WithComposedHashedIndexAndIndex { + class WithComposedHashedIndexAndIndex { @ComposedHashIndexed(name = "idx-name") String value; }