From 5015b34a58480efa4cb81ffd22f220ab7b3d57b7 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 29 Jun 2017 13:18:57 +0200 Subject: [PATCH] DATACMNS-1101 - Add caching for annotation-based property lookup. --- .../mapping/model/BasicPersistentEntity.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java index 751b6cbf0..e0c78e339 100644 --- a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java @@ -72,6 +72,7 @@ public class BasicPersistentEntity> implement private final Map propertyCache; private final Map, Optional> annotationCache; + private final Map, Optional

> propertyAnnotationCache; private P idProperty; private P versionProperty; @@ -109,6 +110,7 @@ public class BasicPersistentEntity> implement this.propertyCache = new HashMap<>(); this.annotationCache = new HashMap<>(); + this.propertyAnnotationCache = new HashMap<>(); this.propertyAccessorFactory = BeanWrapperPropertyAccessorFactory.INSTANCE; this.typeAlias = Lazy.of(() -> getAliasFromAnnotation(getType())); } @@ -265,14 +267,7 @@ public class BasicPersistentEntity> implement * @see org.springframework.data.mapping.PersistentEntity#getPersistentProperty(java.lang.String) */ public P getPersistentProperty(String name) { - - P property = propertyCache.get(name); - - if (property != null) { - return property; - } - - return null; + return propertyCache.get(name); } /* @@ -284,12 +279,23 @@ public class BasicPersistentEntity> implement Assert.notNull(annotationType, "Annotation type must not be null!"); - Optional

property = properties.stream()// - .filter(it -> it.isAnnotationPresent(annotationType))// + return propertyAnnotationCache.computeIfAbsent(annotationType, this::doFindPersistentProperty).orElse(null); + } + + private Optional

doFindPersistentProperty(Class annotationType) { + + Optional

property = properties.stream() // + .filter(it -> it.isAnnotationPresent(annotationType)) // .findAny(); - return property.orElseGet(() -> associations.stream().map(Association::getInverse)// - .filter(it -> it.isAnnotationPresent(annotationType)).findAny().orElse(null)); + if (property.isPresent()) { + + return property; + } + + return associations.stream() // + .map(Association::getInverse) // + .filter(it -> it.isAnnotationPresent(annotationType)).findAny(); } /*