Skip to content

Commit 5015b34

Browse files
mp911deodrotbohm
authored andcommitted
DATACMNS-1101 - Add caching for annotation-based property lookup.
1 parent 0493d13 commit 5015b34

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implement
7272

7373
private final Map<String, P> propertyCache;
7474
private final Map<Class<? extends Annotation>, Optional<Annotation>> annotationCache;
75+
private final Map<Class<? extends Annotation>, Optional<P>> propertyAnnotationCache;
7576

7677
private P idProperty;
7778
private P versionProperty;
@@ -109,6 +110,7 @@ public BasicPersistentEntity(TypeInformation<T> information, Comparator<P> compa
109110

110111
this.propertyCache = new HashMap<>();
111112
this.annotationCache = new HashMap<>();
113+
this.propertyAnnotationCache = new HashMap<>();
112114
this.propertyAccessorFactory = BeanWrapperPropertyAccessorFactory.INSTANCE;
113115
this.typeAlias = Lazy.of(() -> getAliasFromAnnotation(getType()));
114116
}
@@ -265,14 +267,7 @@ public void addAssociation(Association<P> association) {
265267
* @see org.springframework.data.mapping.PersistentEntity#getPersistentProperty(java.lang.String)
266268
*/
267269
public P getPersistentProperty(String name) {
268-
269-
P property = propertyCache.get(name);
270-
271-
if (property != null) {
272-
return property;
273-
}
274-
275-
return null;
270+
return propertyCache.get(name);
276271
}
277272

278273
/*
@@ -284,12 +279,23 @@ public P getPersistentProperty(Class<? extends Annotation> annotationType) {
284279

285280
Assert.notNull(annotationType, "Annotation type must not be null!");
286281

287-
Optional<P> property = properties.stream()//
288-
.filter(it -> it.isAnnotationPresent(annotationType))//
282+
return propertyAnnotationCache.computeIfAbsent(annotationType, this::doFindPersistentProperty).orElse(null);
283+
}
284+
285+
private Optional<P> doFindPersistentProperty(Class<? extends Annotation> annotationType) {
286+
287+
Optional<P> property = properties.stream() //
288+
.filter(it -> it.isAnnotationPresent(annotationType)) //
289289
.findAny();
290290

291-
return property.orElseGet(() -> associations.stream().map(Association::getInverse)//
292-
.filter(it -> it.isAnnotationPresent(annotationType)).findAny().orElse(null));
291+
if (property.isPresent()) {
292+
293+
return property;
294+
}
295+
296+
return associations.stream() //
297+
.map(Association::getInverse) //
298+
.filter(it -> it.isAnnotationPresent(annotationType)).findAny();
293299
}
294300

295301
/*

0 commit comments

Comments
 (0)