Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.type.BasicType;

import static org.hibernate.cache.cfg.internal.ComparatorUtil.versionComparator;

/**
* @author Steve Ebersole
*/
Expand Down Expand Up @@ -40,10 +42,11 @@ public boolean isVersioned() {
}

@Override
public Comparator<?> getOwnerVersionComparator() {
public Comparator<Object> getOwnerVersionComparator() {
if ( isVersioned() ) {
final var type = (BasicType<?>) collectionDescriptor.getOwner().getVersion().getType();
return type.getJavaTypeDescriptor().getComparator();
final var type = (BasicType<?>)
collectionDescriptor.getOwner().getVersion().getType();
return versionComparator( type );
}
else {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.cache.cfg.internal;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.hibernate.type.BasicType;

import java.util.Comparator;

class ComparatorUtil {
@NonNull
static <T> Comparator<Object> versionComparator(BasicType<T> type) {
return (u, v) -> {
final var descriptor = type.getJavaTypeDescriptor();
final T x;
final T y;
try {
x = descriptor.cast( u );
y = descriptor.cast( v );
}
catch (Exception e) {
throw new IllegalArgumentException( "Cached version was not of type " + type.getName(), e );
}
return descriptor.getComparator().compare( x, y );
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
package org.hibernate.cache.cfg.internal;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.cache.cfg.spi.CollectionDataCachingConfig;
import org.hibernate.cache.cfg.spi.DomainDataCachingConfig;
import org.hibernate.cache.cfg.spi.DomainDataRegionConfig;
Expand All @@ -23,6 +26,7 @@

import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static org.hibernate.cache.cfg.internal.ComparatorUtil.versionComparator;

/**
* DomainDataRegionConfig implementation
Expand Down Expand Up @@ -96,12 +100,7 @@ public Builder addEntityConfig(PersistentClass bootEntityDescriptor, AccessType
rootEntityName,
x -> new EntityDataCachingConfigImpl(
rootEntityName,
bootEntityDescriptor.isVersioned()
? () -> {
final var type = (BasicType<?>) bootEntityDescriptor.getVersion().getType();
return type.getJavaTypeDescriptor().getComparator();
}
: null,
versionComparatorAccess( bootEntityDescriptor ),
bootEntityDescriptor.isMutable(),
accessType
)
Expand All @@ -116,6 +115,15 @@ public Builder addEntityConfig(PersistentClass bootEntityDescriptor, AccessType
return this;
}

private @Nullable Supplier<Comparator<Object>> versionComparatorAccess(PersistentClass bootEntityDescriptor) {
return bootEntityDescriptor.isVersioned()
? () -> {
final var type = (BasicType<?>)
bootEntityDescriptor.getVersion().getType();
return versionComparator( type );
}
: null;
}

// todo (6.0) : `EntityPersister` and `CollectionPersister` references here should be replaced with `EntityHierarchy` and `PersistentCollectionDescriptor`
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public class EntityDataCachingConfigImpl
extends AbstractDomainDataCachingConfig
implements EntityDataCachingConfig {
private final NavigableRole navigableRole;
private final Supplier<Comparator<?>> versionComparatorAccess;
private final Supplier<Comparator<Object>> versionComparatorAccess;
private final boolean isEntityMutable;

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

public EntityDataCachingConfigImpl(
NavigableRole rootEntityName,
Supplier<Comparator<?>> versionComparatorAccess,
Supplier<Comparator<Object>> versionComparatorAccess,
boolean isEntityMutable,
AccessType accessType) {
super( accessType );
Expand All @@ -37,7 +37,7 @@ public EntityDataCachingConfigImpl(
}

@Override
public Supplier<Comparator<?>> getVersionComparatorAccess() {
public Supplier<Comparator<Object>> getVersionComparatorAccess() {
return versionComparatorAccess;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public interface CollectionDataCachingConfig extends DomainDataCachingConfig {
/**
* The comparator to be used with the owning entity's version (if it has one).
*/
Comparator<?> getOwnerVersionComparator();
Comparator<Object> getOwnerVersionComparator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface EntityDataCachingConfig extends DomainDataCachingConfig {
* version. If the entity is not versioned, then this method
* returns {@code null}.
*/
Supplier<Comparator<?>> getVersionComparatorAccess();
Supplier<Comparator<Object>> getVersionComparatorAccess();

/**
* The list of specific subclasses of the root that are actually
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected AbstractReadWriteAccess(
super( domainDataRegion, storageAccess );
}

protected abstract Comparator<?> getVersionComparator();
protected abstract Comparator<Object> getVersionComparator();

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

/**
* Returns the enclosed value.
Expand Down Expand Up @@ -315,7 +315,7 @@ public boolean isReadable(long txTimestamp) {
}

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

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

Expand Down Expand Up @@ -389,7 +388,7 @@ public boolean isReadable(long txTimestamp) {
}

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

//noinspection unchecked
return version == null
? txTimestamp > unlockTimestamp
: versionComparator.compare( version, newVersion ) < 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author Steve Ebersole
*/
public class CollectionReadWriteAccess extends AbstractReadWriteAccess implements CollectionDataAccess {
private final Comparator<?> versionComparator;
private final Comparator<Object> versionComparator;
private final CacheKeysFactory keysFactory;

public CollectionReadWriteAccess(
Expand Down Expand Up @@ -61,7 +61,7 @@ public Object getCacheKeyId(Object cacheKey) {
}

@Override
protected Comparator<?> getVersionComparator() {
protected Comparator<Object> getVersionComparator() {
return versionComparator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class EntityReadWriteAccess extends AbstractReadWriteAccess implements EntityDataAccess {
private final CacheKeysFactory keysFactory;
private final Comparator<?> versionComparator;
private final Comparator<Object> versionComparator;

public EntityReadWriteAccess(
DomainDataRegion domainDataRegion,
Expand Down Expand Up @@ -52,7 +52,7 @@ protected AccessedDataClassification getAccessedDataClassification() {
}

@Override
protected Comparator<?> getVersionComparator() {
protected Comparator<Object> getVersionComparator() {
return versionComparator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public AccessType getAccessType() {
}

@Override
protected Comparator<?> getVersionComparator() {
protected Comparator<Object> getVersionComparator() {
// natural id has no comparator
return null;
}
Expand Down