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.
This commit is contained in:
Mark Paluch
2019-05-15 10:35:08 +02:00
parent 207c82a50b
commit c3a5454d31
5 changed files with 24 additions and 25 deletions

View File

@@ -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));
}
/**

View File

@@ -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);
}
}
}

View File

@@ -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 {
}
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 {
return Collections.emptyList();
}
IndexDefinitionHolder holder = new IndexDefinitionHolder("", indexDefinition, root.getCollection());
IndexDefinitionHolder holder = new IndexDefinitionHolder("", indexDefinition, collection);
return Collections.singletonList(holder);
}

View File

@@ -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));
}

View File

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