Skip to content

Commit 2dfaeba

Browse files
committed
HHH-19999 migrate to Comparator<Object> in the caching APIs
1 parent 8ca24d9 commit 2dfaeba

10 files changed

+63
-25
lines changed

hibernate-core/src/main/java/org/hibernate/cache/cfg/internal/CollectionDataCachingConfigImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.hibernate.metamodel.model.domain.NavigableRole;
1313
import org.hibernate.type.BasicType;
1414

15+
import static org.hibernate.cache.cfg.internal.ComparatorUtil.versionComparator;
16+
1517
/**
1618
* @author Steve Ebersole
1719
*/
@@ -40,10 +42,11 @@ public boolean isVersioned() {
4042
}
4143

4244
@Override
43-
public Comparator<?> getOwnerVersionComparator() {
45+
public Comparator<Object> getOwnerVersionComparator() {
4446
if ( isVersioned() ) {
45-
final var type = (BasicType<?>) collectionDescriptor.getOwner().getVersion().getType();
46-
return type.getJavaTypeDescriptor().getComparator();
47+
final var type = (BasicType<?>)
48+
collectionDescriptor.getOwner().getVersion().getType();
49+
return versionComparator( type );
4750
}
4851
else {
4952
return null;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.cache.cfg.internal;
6+
7+
import org.checkerframework.checker.nullness.qual.NonNull;
8+
import org.hibernate.type.BasicType;
9+
10+
import java.util.Comparator;
11+
12+
class ComparatorUtil {
13+
@NonNull
14+
static <T> Comparator<Object> versionComparator(BasicType<T> type) {
15+
return (u, v) -> {
16+
final var descriptor = type.getJavaTypeDescriptor();
17+
final T x;
18+
final T y;
19+
try {
20+
x = descriptor.cast( u );
21+
y = descriptor.cast( v );
22+
}
23+
catch (Exception e) {
24+
throw new IllegalArgumentException( "Cached version was not of type " + type.getName(), e );
25+
}
26+
return descriptor.getComparator().compare( x, y );
27+
};
28+
}
29+
}

hibernate-core/src/main/java/org/hibernate/cache/cfg/internal/DomainDataRegionConfigImpl.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
package org.hibernate.cache.cfg.internal;
66

77
import java.util.ArrayList;
8+
import java.util.Comparator;
89
import java.util.HashMap;
910
import java.util.List;
1011
import java.util.Map;
12+
import java.util.function.Supplier;
1113

14+
import org.checkerframework.checker.nullness.qual.Nullable;
1215
import org.hibernate.cache.cfg.spi.CollectionDataCachingConfig;
1316
import org.hibernate.cache.cfg.spi.DomainDataCachingConfig;
1417
import org.hibernate.cache.cfg.spi.DomainDataRegionConfig;
@@ -23,6 +26,7 @@
2326

2427
import static java.util.Collections.emptyList;
2528
import static java.util.Collections.unmodifiableList;
29+
import static org.hibernate.cache.cfg.internal.ComparatorUtil.versionComparator;
2630

2731
/**
2832
* DomainDataRegionConfig implementation
@@ -96,12 +100,7 @@ public Builder addEntityConfig(PersistentClass bootEntityDescriptor, AccessType
96100
rootEntityName,
97101
x -> new EntityDataCachingConfigImpl(
98102
rootEntityName,
99-
bootEntityDescriptor.isVersioned()
100-
? () -> {
101-
final var type = (BasicType<?>) bootEntityDescriptor.getVersion().getType();
102-
return type.getJavaTypeDescriptor().getComparator();
103-
}
104-
: null,
103+
versionComparatorAccess( bootEntityDescriptor ),
105104
bootEntityDescriptor.isMutable(),
106105
accessType
107106
)
@@ -116,6 +115,15 @@ public Builder addEntityConfig(PersistentClass bootEntityDescriptor, AccessType
116115
return this;
117116
}
118117

118+
private @Nullable Supplier<Comparator<Object>> versionComparatorAccess(PersistentClass bootEntityDescriptor) {
119+
return bootEntityDescriptor.isVersioned()
120+
? () -> {
121+
final var type = (BasicType<?>)
122+
bootEntityDescriptor.getVersion().getType();
123+
return versionComparator( type );
124+
}
125+
: null;
126+
}
119127

120128
// todo (6.0) : `EntityPersister` and `CollectionPersister` references here should be replaced with `EntityHierarchy` and `PersistentCollectionDescriptor`
121129
//

hibernate-core/src/main/java/org/hibernate/cache/cfg/internal/EntityDataCachingConfigImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public class EntityDataCachingConfigImpl
2020
extends AbstractDomainDataCachingConfig
2121
implements EntityDataCachingConfig {
2222
private final NavigableRole navigableRole;
23-
private final Supplier<Comparator<?>> versionComparatorAccess;
23+
private final Supplier<Comparator<Object>> versionComparatorAccess;
2424
private final boolean isEntityMutable;
2525

2626
private final Set<NavigableRole> cachedTypes = new HashSet<>();
2727

2828
public EntityDataCachingConfigImpl(
2929
NavigableRole rootEntityName,
30-
Supplier<Comparator<?>> versionComparatorAccess,
30+
Supplier<Comparator<Object>> versionComparatorAccess,
3131
boolean isEntityMutable,
3232
AccessType accessType) {
3333
super( accessType );
@@ -37,7 +37,7 @@ public EntityDataCachingConfigImpl(
3737
}
3838

3939
@Override
40-
public Supplier<Comparator<?>> getVersionComparatorAccess() {
40+
public Supplier<Comparator<Object>> getVersionComparatorAccess() {
4141
return versionComparatorAccess;
4242
}
4343

hibernate-core/src/main/java/org/hibernate/cache/cfg/spi/CollectionDataCachingConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ public interface CollectionDataCachingConfig extends DomainDataCachingConfig {
1616
/**
1717
* The comparator to be used with the owning entity's version (if it has one).
1818
*/
19-
Comparator<?> getOwnerVersionComparator();
19+
Comparator<Object> getOwnerVersionComparator();
2020
}

hibernate-core/src/main/java/org/hibernate/cache/cfg/spi/EntityDataCachingConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface EntityDataCachingConfig extends DomainDataCachingConfig {
3232
* version. If the entity is not versioned, then this method
3333
* returns {@code null}.
3434
*/
35-
Supplier<Comparator<?>> getVersionComparatorAccess();
35+
Supplier<Comparator<Object>> getVersionComparatorAccess();
3636

3737
/**
3838
* The list of specific subclasses of the root that are actually

hibernate-core/src/main/java/org/hibernate/cache/spi/support/AbstractReadWriteAccess.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected AbstractReadWriteAccess(
3737
super( domainDataRegion, storageAccess );
3838
}
3939

40-
protected abstract Comparator<?> getVersionComparator();
40+
protected abstract Comparator<Object> getVersionComparator();
4141

4242
protected UUID uuid() {
4343
return uuid;
@@ -262,7 +262,7 @@ public interface Lockable {
262262
* Returns <code>true</code> if the enclosed value can be replaced with one of the given version by a
263263
* transaction started at the given time.
264264
*/
265-
boolean isWriteable(long txTimestamp, Object version, Comparator versionComparator);
265+
boolean isWriteable(long txTimestamp, Object version, Comparator<Object> versionComparator);
266266

267267
/**
268268
* Returns the enclosed value.
@@ -315,7 +315,7 @@ public boolean isReadable(long txTimestamp) {
315315
}
316316

317317
@Override
318-
public boolean isWriteable(long txTimestamp, Object newVersion, Comparator versionComparator) {
318+
public boolean isWriteable(long txTimestamp, Object newVersion, Comparator<Object> versionComparator) {
319319
if ( L2CACHE_LOGGER.isTraceEnabled() ) {
320320
L2CACHE_LOGGER.tracef(
321321
"Checking writeability of read-write cache item [timestamp='%s', version='%s'] : txTimestamp='%s', newVersion='%s'",
@@ -326,7 +326,6 @@ public boolean isWriteable(long txTimestamp, Object newVersion, Comparator versi
326326
);
327327
}
328328

329-
//noinspection unchecked
330329
return version != null && versionComparator.compare( version, newVersion ) < 0;
331330
}
332331

@@ -389,7 +388,7 @@ public boolean isReadable(long txTimestamp) {
389388
}
390389

391390
@Override
392-
public boolean isWriteable(long txTimestamp, Object newVersion, Comparator versionComparator) {
391+
public boolean isWriteable(long txTimestamp, Object newVersion, Comparator<Object> versionComparator) {
393392
if ( L2CACHE_LOGGER.isTraceEnabled() ) {
394393
L2CACHE_LOGGER.tracef(
395394
"Checking writeability of read-write cache lock [timeout='%s', lockId='%s', version='%s', sourceUuid=%s, multiplicity='%s', unlockTimestamp='%s'] : txTimestamp='%s', newVersion='%s'",
@@ -413,7 +412,6 @@ public boolean isWriteable(long txTimestamp, Object newVersion, Comparator versi
413412
return false;
414413
}
415414

416-
//noinspection unchecked
417415
return version == null
418416
? txTimestamp > unlockTimestamp
419417
: versionComparator.compare( version, newVersion ) < 0;

hibernate-core/src/main/java/org/hibernate/cache/spi/support/CollectionReadWriteAccess.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @author Steve Ebersole
2424
*/
2525
public class CollectionReadWriteAccess extends AbstractReadWriteAccess implements CollectionDataAccess {
26-
private final Comparator<?> versionComparator;
26+
private final Comparator<Object> versionComparator;
2727
private final CacheKeysFactory keysFactory;
2828

2929
public CollectionReadWriteAccess(
@@ -61,7 +61,7 @@ public Object getCacheKeyId(Object cacheKey) {
6161
}
6262

6363
@Override
64-
protected Comparator<?> getVersionComparator() {
64+
protected Comparator<Object> getVersionComparator() {
6565
return versionComparator;
6666
}
6767

hibernate-core/src/main/java/org/hibernate/cache/spi/support/EntityReadWriteAccess.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
public class EntityReadWriteAccess extends AbstractReadWriteAccess implements EntityDataAccess {
2626
private final CacheKeysFactory keysFactory;
27-
private final Comparator<?> versionComparator;
27+
private final Comparator<Object> versionComparator;
2828

2929
public EntityReadWriteAccess(
3030
DomainDataRegion domainDataRegion,
@@ -52,7 +52,7 @@ protected AccessedDataClassification getAccessedDataClassification() {
5252
}
5353

5454
@Override
55-
protected Comparator<?> getVersionComparator() {
55+
protected Comparator<Object> getVersionComparator() {
5656
return versionComparator;
5757
}
5858

hibernate-core/src/main/java/org/hibernate/cache/spi/support/NaturalIdReadWriteAccess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public AccessType getAccessType() {
4444
}
4545

4646
@Override
47-
protected Comparator<?> getVersionComparator() {
47+
protected Comparator<Object> getVersionComparator() {
4848
// natural id has no comparator
4949
return null;
5050
}

0 commit comments

Comments
 (0)