From bede55714c5fc156ba8b5c1dccf77c5d7c46d463 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 +- .../MongoPersistentEntityIndexResolver.java | 19 ++++++++------- .../core/convert/ObjectPathUnitTests.java | 24 +++++++------------ 3 files changed, 21 insertions(+), 24 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/MongoPersistentEntityIndexResolver.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java index af56d06b0..98a9eebd7 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 @@ -105,14 +105,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; } @@ -121,13 +122,15 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { List indexes, CycleGuard guard) { try { + String collection = root.getCollection(); + if (persistentProperty.isEntity()) { indexes.addAll(resolveIndexForClass(persistentProperty.getTypeInformation().getActualType(), - persistentProperty.getFieldName(), Path.of(persistentProperty), root.getCollection(), guard)); + persistentProperty.getFieldName(), Path.of(persistentProperty), collection, guard)); } IndexDefinitionHolder indexDefinitionHolder = createIndexDefinitionHolderForProperty( - persistentProperty.getFieldName(), root.getCollection(), persistentProperty); + persistentProperty.getFieldName(), collection, persistentProperty); if (indexDefinitionHolder != null) { indexes.add(indexDefinitionHolder); } @@ -212,7 +215,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) { @@ -248,7 +251,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/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 {} }