Browse Source

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.
pull/749/head
Mark Paluch 7 years ago
parent
commit
c3a5454d31
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ObjectPath.java
  2. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java
  3. 13
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java
  4. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/ReactiveMongoPersistentEntityIndexCreator.java
  5. 24
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java

2
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ObjectPath.java

@ -87,7 +87,7 @@ class ObjectPath { @@ -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));
}
/**

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

@ -134,16 +134,18 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma @@ -134,16 +134,18 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
private void checkForAndCreateIndexes(MongoPersistentEntity<?> 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);
}
}
}

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

@ -118,14 +118,15 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { @@ -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<IndexDefinitionHolder> indexInformation = new ArrayList<>();
indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", root.getCollection(), root));
indexInformation.addAll(potentiallyCreateTextIndexDefinition(root));
List<IndexDefinitionHolder> indexInformation = new ArrayList<>();
String collection = root.getCollection();
indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", collection, root));
indexInformation.addAll(potentiallyCreateTextIndexDefinition(root, collection));
root.doWithProperties((PropertyHandler<MongoPersistentProperty>) 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 { @@ -225,7 +226,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
}
private Collection<? extends IndexDefinitionHolder> 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 { @@ -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);
}

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

@ -125,11 +125,13 @@ public class ReactiveMongoPersistentEntityIndexCreator { @@ -125,11 +125,13 @@ public class ReactiveMongoPersistentEntityIndexCreator {
List<Mono<?>> 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));
}

24
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; @@ -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 { @@ -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 { @@ -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 {}
}

Loading…
Cancel
Save