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 3ec426352f
commit bede55714c
3 changed files with 21 additions and 24 deletions

View File

@@ -87,7 +87,7 @@ class ObjectPath {
Assert.notNull(object, "Object must not be null!"); Assert.notNull(object, "Object must not be null!");
Assert.notNull(entity, "MongoPersistentEntity 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

@@ -105,14 +105,15 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
Document document = root.findAnnotation(Document.class); Document document = root.findAnnotation(Document.class);
Assert.notNull(document, "Given entity is not collection root."); Assert.notNull(document, "Given entity is not collection root.");
final List<IndexDefinitionHolder> indexInformation = new ArrayList<>(); List<IndexDefinitionHolder> indexInformation = new ArrayList<>();
indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", root.getCollection(), root)); String collection = root.getCollection();
indexInformation.addAll(potentiallyCreateTextIndexDefinition(root)); indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", collection, root));
indexInformation.addAll(potentiallyCreateTextIndexDefinition(root, collection));
root.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> this root.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> this
.potentiallyAddIndexForProperty(root, property, indexInformation, new CycleGuard())); .potentiallyAddIndexForProperty(root, property, indexInformation, new CycleGuard()));
indexInformation.addAll(resolveIndexesForDbrefs("", root.getCollection(), root)); indexInformation.addAll(resolveIndexesForDbrefs("", collection, root));
return indexInformation; return indexInformation;
} }
@@ -121,13 +122,15 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
List<IndexDefinitionHolder> indexes, CycleGuard guard) { List<IndexDefinitionHolder> indexes, CycleGuard guard) {
try { try {
String collection = root.getCollection();
if (persistentProperty.isEntity()) { if (persistentProperty.isEntity()) {
indexes.addAll(resolveIndexForClass(persistentProperty.getTypeInformation().getActualType(), indexes.addAll(resolveIndexForClass(persistentProperty.getTypeInformation().getActualType(),
persistentProperty.getFieldName(), Path.of(persistentProperty), root.getCollection(), guard)); persistentProperty.getFieldName(), Path.of(persistentProperty), collection, guard));
} }
IndexDefinitionHolder indexDefinitionHolder = createIndexDefinitionHolderForProperty( IndexDefinitionHolder indexDefinitionHolder = createIndexDefinitionHolderForProperty(
persistentProperty.getFieldName(), root.getCollection(), persistentProperty); persistentProperty.getFieldName(), collection, persistentProperty);
if (indexDefinitionHolder != null) { if (indexDefinitionHolder != null) {
indexes.add(indexDefinitionHolder); indexes.add(indexDefinitionHolder);
} }
@@ -212,7 +215,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
} }
private Collection<? extends IndexDefinitionHolder> potentiallyCreateTextIndexDefinition( private Collection<? extends IndexDefinitionHolder> potentiallyCreateTextIndexDefinition(
MongoPersistentEntity<?> root) { MongoPersistentEntity<?> root, String collection) {
String name = root.getType().getSimpleName() + "_TextIndex"; String name = root.getType().getSimpleName() + "_TextIndex";
if (name.getBytes().length > 127) { if (name.getBytes().length > 127) {
@@ -248,7 +251,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
return Collections.emptyList(); return Collections.emptyList();
} }
IndexDefinitionHolder holder = new IndexDefinitionHolder("", indexDefinition, root.getCollection()); IndexDefinitionHolder holder = new IndexDefinitionHolder("", indexDefinition, collection);
return Collections.singletonList(holder); return Collections.singletonList(holder);
} }

View File

@@ -26,6 +26,8 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.ClassTypeInformation;
/** /**
* Unit tests for {@link ObjectPath}.
*
* @author Christoph Strobl * @author Christoph Strobl
*/ */
public class ObjectPathUnitTests { public class ObjectPathUnitTests {
@@ -37,9 +39,9 @@ public class ObjectPathUnitTests {
@Before @Before
public void setUp() { public void setUp() {
one = new BasicMongoPersistentEntity(ClassTypeInformation.from(EntityOne.class)); one = new BasicMongoPersistentEntity<>(ClassTypeInformation.from(EntityOne.class));
two = new BasicMongoPersistentEntity(ClassTypeInformation.from(EntityTwo.class)); two = new BasicMongoPersistentEntity<>(ClassTypeInformation.from(EntityTwo.class));
three = new BasicMongoPersistentEntity(ClassTypeInformation.from(EntityThree.class)); three = new BasicMongoPersistentEntity<>(ClassTypeInformation.from(EntityThree.class));
} }
@Test // DATAMONGO-1703 @Test // DATAMONGO-1703
@@ -96,20 +98,12 @@ public class ObjectPathUnitTests {
} }
@Document("one") @Document("one")
static class EntityOne { static class EntityOne {}
} static class EntityTwo extends EntityOne {}
static class EntityTwo extends EntityOne { interface ValueInterface {}
}
interface ValueInterface {
}
@Document("three") @Document("three")
static class EntityThree implements ValueInterface { static class EntityThree implements ValueInterface {}
}
} }