Skip to content

Commit 207b5f6

Browse files
committed
Avoid records as cache keys for performance reasons.
Fixes GH-2997.
1 parent 40edfdf commit 207b5f6

File tree

3 files changed

+124
-17
lines changed

3 files changed

+124
-17
lines changed

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

+38-1
Original file line numberDiff line numberDiff line change
@@ -487,5 +487,42 @@ public String toString() {
487487
return String.format("%s.%s", owningType.getType().getSimpleName(), toDotPath());
488488
}
489489

490-
private record Key(TypeInformation<?> type, String path) {};
490+
private static final class Key {
491+
492+
private final TypeInformation<?> type;
493+
private final String path;
494+
495+
private Key(TypeInformation<?> type, String path) {
496+
this.type = type;
497+
this.path = path;
498+
}
499+
500+
@Override
501+
public boolean equals(@Nullable Object obj) {
502+
503+
if (obj == this) {
504+
return true;
505+
}
506+
507+
if (!(obj instanceof Key that)) {
508+
return false;
509+
}
510+
511+
return Objects.equals(this.type, that.type) &&
512+
Objects.equals(this.path, that.path);
513+
}
514+
515+
@Override
516+
public int hashCode() {
517+
return Objects.hash(type, path);
518+
}
519+
520+
@Override
521+
public String toString() {
522+
523+
return "Key[" +
524+
"type=" + type + ", " +
525+
"path=" + path + ']';
526+
}
527+
}
491528
}

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)