Skip to content

Commit 0cf2538

Browse files
committed
Avoid records as cache keys for performance reasons.
Fixes GH-2997.
1 parent 6e22ffd commit 0cf2538

File tree

3 files changed

+126
-20
lines changed

3 files changed

+126
-20
lines changed

src/main/java/org/springframework/data/mapping/PropertyPath.java

+40-4
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public class PropertyPath implements Streamable<PropertyPath> {
9595
}
9696

9797
this.owningType = owningType;
98-
this.name = property.path();
99-
this.typeInformation = property.type();
98+
this.name = property.path;
99+
this.typeInformation = property.type;
100100
this.isCollection = this.typeInformation.isCollectionLike();
101101
this.actualTypeInformation = this.typeInformation.getActualType() == null ? this.typeInformation
102102
: this.typeInformation.getRequiredActualType();
@@ -499,6 +499,42 @@ public String toString() {
499499
return String.format("%s.%s", owningType.getType().getSimpleName(), toDotPath());
500500
}
501501

502-
private record Property(TypeInformation<?> type, String path) {
503-
};
502+
private static final class Property {
503+
504+
private final TypeInformation<?> type;
505+
private final String path;
506+
507+
private Property(TypeInformation<?> type, String path) {
508+
this.type = type;
509+
this.path = path;
510+
}
511+
512+
@Override
513+
public boolean equals(@Nullable Object obj) {
514+
515+
if (obj == this) {
516+
return true;
517+
}
518+
519+
if (!(obj instanceof Property that)) {
520+
return false;
521+
}
522+
523+
return Objects.equals(this.type, that.type) &&
524+
Objects.equals(this.path, that.path);
525+
}
526+
527+
@Override
528+
public int hashCode() {
529+
return Objects.hash(type, path);
530+
}
531+
532+
@Override
533+
public String toString() {
534+
535+
return "Key[" +
536+
"type=" + type + ", " +
537+
"path=" + path + ']';
538+
}
539+
}
504540
}

src/main/java/org/springframework/data/mapping/callback/EntityCallbackDiscoverer.java

+41-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.LinkedHashSet;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.Objects;
2627
import java.util.Set;
2728
import java.util.concurrent.ConcurrentHashMap;
2829
import java.util.function.BiFunction;
@@ -331,7 +332,7 @@ void discoverEntityCallbacks(BeanFactory beanFactory) {
331332
*
332333
* @author Oliver Drotbohm
333334
*/
334-
private record EntityCallbackAdapter<T> (EntityCallback<T> delegate,
335+
private record EntityCallbackAdapter<T>(EntityCallback<T> delegate,
335336
ResolvableType type) implements EntityCallback<T> {
336337

337338
boolean supports(ResolvableType callbackType, ResolvableType entityType) {
@@ -342,17 +343,52 @@ boolean supports(ResolvableType callbackType, ResolvableType entityType) {
342343
/**
343344
* Cache key for {@link EntityCallback}, based on event type and source type.
344345
*/
345-
private record CallbackCacheKey(ResolvableType callbackType,
346-
@Nullable Class<?> entityType) implements Comparable<CallbackCacheKey> {
346+
private static final class CallbackCacheKey implements Comparable<CallbackCacheKey> {
347347

348348
private static final Comparator<CallbackCacheKey> COMPARATOR = Comparators.<CallbackCacheKey> nullsHigh() //
349349
.thenComparing(it -> it.callbackType.toString()) //
350350
.thenComparing(it -> it.entityType.getName());
351351

352+
private final ResolvableType callbackType;
353+
private final Class<?> entityType;
354+
355+
private CallbackCacheKey(ResolvableType callbackType, Class<?> entityType) {
356+
357+
this.callbackType = callbackType;
358+
this.entityType = entityType;
359+
}
360+
352361
@Override
353-
public int compareTo(CallbackCacheKey other) {
362+
public int compareTo(@Nullable CallbackCacheKey other) {
354363
return COMPARATOR.compare(this, other);
355364
}
356-
}
357365

366+
@Override
367+
public boolean equals(@Nullable Object obj) {
368+
369+
if (obj == this) {
370+
return true;
371+
}
372+
373+
if (!(obj instanceof CallbackCacheKey that)) {
374+
return false;
375+
}
376+
377+
return Objects.equals(this.callbackType, that.callbackType)
378+
&& Objects.equals(this.entityType, that.entityType);
379+
}
380+
381+
@Override
382+
public int hashCode() {
383+
return Objects.hash(callbackType, entityType);
384+
}
385+
386+
@Override
387+
public String toString() {
388+
389+
return "CallbackCacheKey[" +
390+
"callbackType=" + callbackType + ", " +
391+
"entityType=" + entityType + ']';
392+
}
393+
}
358394
}

src/main/java/org/springframework/data/mapping/context/PersistentPropertyPathFactory.java

+45-11
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,7 @@
1515
*/
1616
package org.springframework.data.mapping.context;
1717

18-
import java.util.ArrayList;
19-
import java.util.Collection;
20-
import java.util.Collections;
21-
import java.util.Comparator;
22-
import java.util.HashSet;
23-
import java.util.Iterator;
24-
import java.util.List;
25-
import java.util.Map;
26-
import java.util.Optional;
27-
import java.util.Set;
18+
import java.util.*;
2819
import java.util.function.Function;
2920
import java.util.function.Predicate;
3021
import java.util.stream.Collectors;
@@ -279,12 +270,55 @@ private Collection<PersistentPropertyPath<P>> from(E entity, Predicate<? super P
279270
return properties;
280271
}
281272

282-
record TypeAndPath(TypeInformation<?> type, String path) {
273+
static final class TypeAndPath {
274+
275+
private final TypeInformation<?> type;
276+
private final String path;
277+
278+
private TypeAndPath(TypeInformation<?> type, String path) {
279+
this.type = type;
280+
this.path = path;
281+
}
283282

284283
public static TypeAndPath of(TypeInformation<?> type, String path) {
285284
return new TypeAndPath(type, path);
286285
}
287286

287+
public TypeInformation<?> type() {
288+
return type;
289+
}
290+
291+
public String path() {
292+
return path;
293+
}
294+
295+
@Override
296+
public boolean equals(@Nullable Object obj) {
297+
298+
if (obj == this) {
299+
return true;
300+
}
301+
302+
if (!(obj instanceof TypeAndPath that)) {
303+
return false;
304+
}
305+
306+
return Objects.equals(this.type, that.type)
307+
&& Objects.equals(this.path, that.path);
308+
}
309+
310+
@Override
311+
public int hashCode() {
312+
return Objects.hash(type, path);
313+
}
314+
315+
@Override
316+
public String toString() {
317+
318+
return "TypeAndPath[" +
319+
"type=" + type + ", " +
320+
"path=" + path + ']';
321+
}
288322
}
289323

290324
static class DefaultPersistentPropertyPaths<T, P extends PersistentProperty<P>>

0 commit comments

Comments
 (0)