Skip to content

Commit

Permalink
Revisit internal caching arrangements.
Browse files Browse the repository at this point in the history
Introduce caching and reduce allocations on hot code paths.

Closes: #4818
Original Pull Request: #4819
  • Loading branch information
mp911de authored and christophstrobl committed Nov 4, 2024
1 parent 5419414 commit 1075a25
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public DocumentTypeAliasAccessor(@Nullable String typeKey) {
this.typeKey = typeKey;
}

@Override
public Alias readAliasFrom(Bson source) {

if (source instanceof List) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -130,6 +131,23 @@ public class MappingMongoConverter extends AbstractMongoConverter
private static final String INCOMPATIBLE_TYPES = "Cannot convert %1$s of type %2$s into an instance of %3$s; Implement a custom Converter<%2$s, %3$s> and register it with the CustomConversions; Parent object was: %4$s";
private static final String INVALID_TYPE_TO_READ = "Expected to read Document %s into type %s but didn't find a PersistentEntity for the latter";

private static final BiPredicate<MongoPersistentEntity<?>, MongoPersistentProperty> PROPERTY_FILTER = (e,
property) -> {

if (property.isIdProperty()) {
return false;
}

if (e.isCreatorArgument(property)) {
return false;
}

if (!property.isReadable()) {
return false;
}
return true;
};

public static final TypeInformation<Bson> BSON = TypeInformation.of(Bson.class);

protected static final Log LOGGER = LogFactory.getLog(MappingMongoConverter.class);
Expand Down Expand Up @@ -368,7 +386,7 @@ private <R> R doReadProjection(ConversionContext context, Bson bson, EntityProje
evaluator, spELContext);

readProperties(context, entity, convertingAccessor, documentAccessor, valueProvider, evaluator,
Predicates.isTrue());
(mongoPersistentProperties, mongoPersistentProperty) -> true);
return (R) projectionFactory.createProjection(mappedType.getType(), accessor.getBean());
}

Expand Down Expand Up @@ -518,26 +536,42 @@ private ParameterValueProvider<MongoPersistentProperty> getParameterProvider(Con
parameterProvider);
}

class EvaluatingDocumentAccessor extends DocumentAccessor implements ValueExpressionEvaluator {

/**
* Creates a new {@link DocumentAccessor} for the given {@link Document}.
*
* @param document must be a {@link Document} effectively, must not be {@literal null}.
*/
public EvaluatingDocumentAccessor(Bson document) {
super(document);
}

@Override
public <T> T evaluate(String expression) {
return expressionEvaluatorFactory.create(getDocument()).evaluate(expression);
}
}

private <S> S read(ConversionContext context, MongoPersistentEntity<S> entity, Document bson) {

S existing = context.findContextualEntity(entity, bson);
if (existing != null) {
return existing;
}

ValueExpressionEvaluator evaluator = expressionEvaluatorFactory.create(bson);
DocumentAccessor documentAccessor = new DocumentAccessor(bson);

EvaluatingDocumentAccessor documentAccessor = new EvaluatingDocumentAccessor(bson);
InstanceCreatorMetadata<MongoPersistentProperty> instanceCreatorMetadata = entity.getInstanceCreatorMetadata();

ParameterValueProvider<MongoPersistentProperty> provider = instanceCreatorMetadata != null
&& instanceCreatorMetadata.hasParameters() ? getParameterProvider(context, entity, documentAccessor, evaluator)
&& instanceCreatorMetadata.hasParameters()
? getParameterProvider(context, entity, documentAccessor, documentAccessor)
: NoOpParameterValueProvider.INSTANCE;

EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
S instance = instantiator.createInstance(entity, provider);

return populateProperties(context, entity, documentAccessor, evaluator, instance);
return populateProperties(context, entity, documentAccessor, documentAccessor, instance);
}

private <S> S populateProperties(ConversionContext context, MongoPersistentEntity<S> entity,
Expand All @@ -559,9 +593,7 @@ private <S> S populateProperties(ConversionContext context, MongoPersistentEntit
MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(contextToUse, documentAccessor,
evaluator, spELContext);

Predicate<MongoPersistentProperty> propertyFilter = isIdentifier(entity).or(isConstructorArgument(entity))
.or(Predicates.negate(PersistentProperty::isReadable)).negate();
readProperties(contextToUse, entity, accessor, documentAccessor, valueProvider, evaluator, propertyFilter);
readProperties(contextToUse, entity, accessor, documentAccessor, valueProvider, evaluator, PROPERTY_FILTER);

return accessor.getBean();
}
Expand Down Expand Up @@ -606,13 +638,13 @@ private Object readIdValue(ConversionContext context, ValueExpressionEvaluator e
private void readProperties(ConversionContext context, MongoPersistentEntity<?> entity,
PersistentPropertyAccessor<?> accessor, DocumentAccessor documentAccessor,
MongoDbPropertyValueProvider valueProvider, ValueExpressionEvaluator evaluator,
Predicate<MongoPersistentProperty> propertyFilter) {
BiPredicate<MongoPersistentEntity<?>, MongoPersistentProperty> propertyFilter) {

DbRefResolverCallback callback = null;

for (MongoPersistentProperty prop : entity) {

if (!propertyFilter.test(prop)) {
if (!propertyFilter.test(entity, prop)) {
continue;
}

Expand Down Expand Up @@ -1943,10 +1975,6 @@ static class MongoDbPropertyValueProvider implements PropertyValueProvider<Mongo
MongoDbPropertyValueProvider(ConversionContext context, DocumentAccessor accessor,
ValueExpressionEvaluator evaluator, SpELContext spELContext) {

Assert.notNull(context, "ConversionContext must no be null");
Assert.notNull(accessor, "DocumentAccessor must no be null");
Assert.notNull(evaluator, "ValueExpressionEvaluator must not be null");

this.context = context;
this.accessor = accessor;
this.evaluator = evaluator;
Expand Down Expand Up @@ -2359,9 +2387,6 @@ protected static class DefaultConversionContext implements ConversionContext {
public <S extends Object> S convert(Object source, TypeInformation<? extends S> typeHint,
ConversionContext context) {

Assert.notNull(source, "Source must not be null");
Assert.notNull(typeHint, "TypeInformation must not be null");

if (conversions.hasCustomReadTarget(source.getClass(), typeHint.getType())) {
return (S) elementConverter.convert(source, typeHint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
public class CachingMongoPersistentProperty extends BasicMongoPersistentProperty {

private final Lazy<Boolean> isEntity = Lazy.of(super::isEntity);
private final Lazy<Boolean> isUnwrapped = Lazy.of(super::isUnwrapped);
private final Lazy<Boolean> isIdProperty = Lazy.of(super::isIdProperty);
private final Lazy<Boolean> isAssociation = Lazy.of(super::isAssociation);
private final Lazy<DBRef> dbref = Lazy.of(super::getDBRef);
Expand Down Expand Up @@ -58,6 +60,16 @@ public CachingMongoPersistentProperty(Property property, MongoPersistentEntity<?
super(property, owner, simpleTypeHolder, fieldNamingStrategy);
}

@Override
public boolean isEntity() {
return isEntity.get();
}

@Override
public boolean isUnwrapped() {
return isUnwrapped.get();
}

@Override
public boolean isIdProperty() {
return isIdProperty.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
* @author Christoph Strobl
* @since 4.2
*/
public record FieldName(String name, Type type) {
public record FieldName(String name, Type type, String[] parts) {

public FieldName(String name, Type type) {
this(name, type, name.split("\\."));
}

private static final String ID_KEY = "_id";

Expand Down Expand Up @@ -65,7 +69,7 @@ public String[] parts() {
return new String[] { name };
}

return name.split("\\.");
return parts;
}

/**
Expand Down

0 comments on commit 1075a25

Please sign in to comment.