From c3a5454d315b2178e9e0064afb7ea04cbc26b85d Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 15 May 2019 10:35:08 +0200 Subject: [PATCH] DATAMONGO-2267 - Polishing. Reuse collection name for index creation instead of resolving the collection for every index. Switch lambda to method reference. Original pull request: #746. --- .../data/mongodb/core/convert/ObjectPath.java | 2 +- .../MongoPersistentEntityIndexCreator.java | 6 +++-- .../MongoPersistentEntityIndexResolver.java | 13 +++++----- ...tiveMongoPersistentEntityIndexCreator.java | 4 +++- .../core/convert/ObjectPathUnitTests.java | 24 +++++++------------ 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ObjectPath.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ObjectPath.java index 76c066e10..672f5bd11 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ObjectPath.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ObjectPath.java @@ -87,7 +87,7 @@ class ObjectPath { Assert.notNull(object, "Object must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); - return new ObjectPath(this, object, id, Lazy.of(() -> entity.getCollection())); + return new ObjectPath(this, object, id, Lazy.of(entity::getCollection)); } /** diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java index 9df870638..9f9c2323b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java @@ -134,16 +134,18 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener entity) { if (entity.isAnnotationPresent(Document.class)) { + + String collection = entity.getCollection(); + for (IndexDefinition indexDefinition : indexResolver.resolveIndexFor(entity.getTypeInformation())) { JustOnceLogger.logWarnIndexCreationConfigurationChange(this.getClass().getName()); IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder ? (IndexDefinitionHolder) indexDefinition - : new IndexDefinitionHolder("", indexDefinition, entity.getCollection()); + : new IndexDefinitionHolder("", indexDefinition, collection); createIndex(indexToCreate); - } } } 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 041241c0f..73edce0f4 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 @@ -118,14 +118,15 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { Document document = root.findAnnotation(Document.class); Assert.notNull(document, "Given entity is not collection root."); - final List indexInformation = new ArrayList<>(); - indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", root.getCollection(), root)); - indexInformation.addAll(potentiallyCreateTextIndexDefinition(root)); + List indexInformation = new ArrayList<>(); + String collection = root.getCollection(); + indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", collection, root)); + indexInformation.addAll(potentiallyCreateTextIndexDefinition(root, collection)); root.doWithProperties((PropertyHandler) property -> this .potentiallyAddIndexForProperty(root, property, indexInformation, new CycleGuard())); - indexInformation.addAll(resolveIndexesForDbrefs("", root.getCollection(), root)); + indexInformation.addAll(resolveIndexesForDbrefs("", collection, root)); return indexInformation; } @@ -225,7 +226,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { } private Collection potentiallyCreateTextIndexDefinition( - MongoPersistentEntity root) { + MongoPersistentEntity root, String collection) { String name = root.getType().getSimpleName() + "_TextIndex"; if (name.getBytes().length > 127) { @@ -261,7 +262,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { return Collections.emptyList(); } - IndexDefinitionHolder holder = new IndexDefinitionHolder("", indexDefinition, root.getCollection()); + IndexDefinitionHolder holder = new IndexDefinitionHolder("", indexDefinition, collection); return Collections.singletonList(holder); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/ReactiveMongoPersistentEntityIndexCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/ReactiveMongoPersistentEntityIndexCreator.java index 1bc09f729..29cdf656d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/ReactiveMongoPersistentEntityIndexCreator.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/ReactiveMongoPersistentEntityIndexCreator.java @@ -125,11 +125,13 @@ public class ReactiveMongoPersistentEntityIndexCreator { List> publishers = new ArrayList<>(); if (entity.isAnnotationPresent(Document.class)) { + + String collection = entity.getCollection(); for (IndexDefinition indexDefinition : indexResolver.resolveIndexFor(entity.getTypeInformation())) { IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder ? (IndexDefinitionHolder) indexDefinition - : new IndexDefinitionHolder("", indexDefinition, entity.getCollection()); + : new IndexDefinitionHolder("", indexDefinition, collection); publishers.add(createIndex(indexToCreate)); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java index 84a9c1040..6dd2cf873 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java @@ -26,6 +26,8 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.util.ClassTypeInformation; /** + * Unit tests for {@link ObjectPath}. + * * @author Christoph Strobl */ public class ObjectPathUnitTests { @@ -37,9 +39,9 @@ public class ObjectPathUnitTests { @Before public void setUp() { - one = new BasicMongoPersistentEntity(ClassTypeInformation.from(EntityOne.class)); - two = new BasicMongoPersistentEntity(ClassTypeInformation.from(EntityTwo.class)); - three = new BasicMongoPersistentEntity(ClassTypeInformation.from(EntityThree.class)); + one = new BasicMongoPersistentEntity<>(ClassTypeInformation.from(EntityOne.class)); + two = new BasicMongoPersistentEntity<>(ClassTypeInformation.from(EntityTwo.class)); + three = new BasicMongoPersistentEntity<>(ClassTypeInformation.from(EntityThree.class)); } @Test // DATAMONGO-1703 @@ -96,20 +98,12 @@ public class ObjectPathUnitTests { } @Document("one") - static class EntityOne { + static class EntityOne {} - } + static class EntityTwo extends EntityOne {} - static class EntityTwo extends EntityOne { - - } - - interface ValueInterface { - - } + interface ValueInterface {} @Document("three") - static class EntityThree implements ValueInterface { - - } + static class EntityThree implements ValueInterface {} }