DATAMONGO-2267 - Fix eager collection resolution in Object path.
We now lazily read the collection of an entity as it potentially requires a more expensive SpEL evaluation that might not have been required in fist place. Original pull request: #746.
This commit is contained in:
committed by
Mark Paluch
parent
c6293e0ebd
commit
3ec426352f
@@ -17,8 +17,10 @@ package org.springframework.data.mongodb.core.convert;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
||||||
|
import org.springframework.data.util.Lazy;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
@@ -46,14 +48,14 @@ class ObjectPath {
|
|||||||
private final @Nullable ObjectPath parent;
|
private final @Nullable ObjectPath parent;
|
||||||
private final @Nullable Object object;
|
private final @Nullable Object object;
|
||||||
private final @Nullable Object idValue;
|
private final @Nullable Object idValue;
|
||||||
private final String collection;
|
private final Lazy<String> collection;
|
||||||
|
|
||||||
private ObjectPath() {
|
private ObjectPath() {
|
||||||
|
|
||||||
this.parent = null;
|
this.parent = null;
|
||||||
this.object = null;
|
this.object = null;
|
||||||
this.idValue = null;
|
this.idValue = null;
|
||||||
this.collection = "";
|
this.collection = Lazy.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,7 +66,7 @@ class ObjectPath {
|
|||||||
* @param idValue
|
* @param idValue
|
||||||
* @param collection
|
* @param collection
|
||||||
*/
|
*/
|
||||||
private ObjectPath(ObjectPath parent, Object object, @Nullable Object idValue, String collection) {
|
private ObjectPath(ObjectPath parent, Object object, @Nullable Object idValue, Lazy<String> collection) {
|
||||||
|
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.object = object;
|
this.object = object;
|
||||||
@@ -85,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, entity.getCollection());
|
return new ObjectPath(this, object, id, Lazy.of(() -> entity.getCollection()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -175,7 +177,7 @@ class ObjectPath {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getCollection() {
|
private String getCollection() {
|
||||||
return collection;
|
return collection.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
package org.springframework.data.mongodb.core.convert;
|
package org.springframework.data.mongodb.core.convert;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -81,6 +82,19 @@ public class ObjectPathUnitTests {
|
|||||||
assertThat(path.getPathItem("id-1", "one", ValueInterface.class)).isNotNull();
|
assertThat(path.getPathItem("id-1", "one", ValueInterface.class)).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // DATAMONGO-2267
|
||||||
|
public void collectionLookupShouldBeLazy/* because we may need to resolve SpEL which can be pretty expensive */() {
|
||||||
|
|
||||||
|
MongoPersistentEntity<EntityOne> spied = spy(one);
|
||||||
|
ObjectPath path = ObjectPath.ROOT.push(new EntityThree(), spied, "id-1");
|
||||||
|
|
||||||
|
verify(spied, never()).getCollection();
|
||||||
|
|
||||||
|
path.getPathItem("id-1", "foo", EntityTwo.class);
|
||||||
|
|
||||||
|
verify(spied).getCollection();
|
||||||
|
}
|
||||||
|
|
||||||
@Document("one")
|
@Document("one")
|
||||||
static class EntityOne {
|
static class EntityOne {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user