Skip to content

Commit

Permalink
HHH-9840 Refactor org.hibernate.cache.spi.CacheKey into an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed Jul 1, 2015
1 parent ea82d09 commit 9ac0a34
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 81 deletions.
@@ -0,0 +1,104 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.cache.internal;

import java.io.Serializable;

import org.hibernate.cache.spi.CacheKey;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.type.Type;

/**
* Allows multiple entity classes / collection roles to be stored in the same cache region. Also allows for composite
* keys which do not properly implement equals()/hashCode().
*
* This was named org.hibernate.cache.spi.CacheKey in Hibernate until version 5.
* Temporarily maintained as a reference while all components catch up with the refactoring to interface.
*
* @author Gavin King
* @author Steve Ebersole
*/
@Deprecated
public class OldCacheKeyImplementation implements CacheKey, Serializable {
private final Serializable key;
private final Type type;
private final String entityOrRoleName;
private final String tenantId;
private final int hashCode;

/**
* Construct a new key for a collection or entity instance.
* Note that an entity name should always be the root entity
* name, not a subclass entity name.
*
* @param id The identifier associated with the cached data
* @param type The Hibernate type mapping
* @param entityOrRoleName The entity or collection-role name.
* @param tenantId The tenant identifier associated this data.
* @param factory The session factory for which we are caching
*/
public OldCacheKeyImplementation(
final Serializable id,
final Type type,
final String entityOrRoleName,
final String tenantId,
final SessionFactoryImplementor factory) {
this.key = id;
this.type = type;
this.entityOrRoleName = entityOrRoleName;
this.tenantId = tenantId;
this.hashCode = calculateHashCode( type, factory );
}

private int calculateHashCode(Type type, SessionFactoryImplementor factory) {
int result = type.getHashCode( key, factory );
result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0);
return result;
}

public Serializable getKey() {
return key;
}

public String getEntityOrRoleName() {
return entityOrRoleName;
}

public String getTenantId() {
return tenantId;
}

@Override
public boolean equals(Object other) {
if ( other == null ) {
return false;
}
if ( this == other ) {
return true;
}
if ( hashCode != other.hashCode() || !( other instanceof OldCacheKeyImplementation ) ) {
//hashCode is part of this check since it is pre-calculated and hash must match for equals to be true
return false;
}
final OldCacheKeyImplementation that = (OldCacheKeyImplementation) other;
return EqualsHelper.equals( entityOrRoleName, that.entityOrRoleName )
&& type.isEqual( key, that.key )
&& EqualsHelper.equals( tenantId, that.tenantId );
}

@Override
public int hashCode() {
return hashCode;
}

@Override
public String toString() {
// Used to be required for OSCache
return entityOrRoleName + '#' + key.toString();
}
}
83 changes: 5 additions & 78 deletions hibernate-core/src/main/java/org/hibernate/cache/spi/CacheKey.java
Expand Up @@ -8,92 +8,19 @@


import java.io.Serializable; import java.io.Serializable;


import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.type.Type;

/** /**
* Allows multiple entity classes / collection roles to be stored in the same cache region. Also allows for composite * Allows multiple entity classes / collection roles to be stored in the same cache region. Also allows for composite
* keys which do not properly implement equals()/hashCode(). * keys which do not properly implement equals()/hashCode().
* *
* @author Gavin King * @author Gavin King
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class CacheKey implements Serializable { public interface CacheKey {
private final Serializable key;
private final Type type; public Serializable getKey();
private final String entityOrRoleName;
private final String tenantId;
private final int hashCode;

/**
* Construct a new key for a collection or entity instance.
* Note that an entity name should always be the root entity
* name, not a subclass entity name.
*
* @param id The identifier associated with the cached data
* @param type The Hibernate type mapping
* @param entityOrRoleName The entity or collection-role name.
* @param tenantId The tenant identifier associated this data.
* @param factory The session factory for which we are caching
*/
public CacheKey(
final Serializable id,
final Type type,
final String entityOrRoleName,
final String tenantId,
final SessionFactoryImplementor factory) {
this.key = id;
this.type = type;
this.entityOrRoleName = entityOrRoleName;
this.tenantId = tenantId;
this.hashCode = calculateHashCode( type, factory );
}

private int calculateHashCode(Type type, SessionFactoryImplementor factory) {
int result = type.getHashCode( key, factory );
result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0);
return result;
}

public Serializable getKey() {
return key;
}

public String getEntityOrRoleName() {
return entityOrRoleName;
}

public String getTenantId() {
return tenantId;
}


@Override public String getEntityOrRoleName();
public boolean equals(Object other) {
if ( other == null ) {
return false;
}
if ( this == other ) {
return true;
}
if ( hashCode != other.hashCode() || !( other instanceof CacheKey ) ) {
//hashCode is part of this check since it is pre-calculated and hash must match for equals to be true
return false;
}
final CacheKey that = (CacheKey) other;
return EqualsHelper.equals( entityOrRoleName, that.entityOrRoleName )
&& type.isEqual( key, that.key )
&& EqualsHelper.equals( tenantId, that.tenantId );
}


@Override public String getTenantId();
public int hashCode() {
return hashCode;
}


@Override
public String toString() {
// Used to be required for OSCache
return entityOrRoleName + '#' + key.toString();
}
} }
Expand Up @@ -26,6 +26,7 @@
import org.hibernate.SharedSessionContract; import org.hibernate.SharedSessionContract;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.internal.OldCacheKeyImplementation;
import org.hibernate.cache.spi.CacheKey; import org.hibernate.cache.spi.CacheKey;
import org.hibernate.engine.jdbc.LobCreationContext; import org.hibernate.engine.jdbc.LobCreationContext;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
Expand Down Expand Up @@ -338,7 +339,7 @@ public EntityKey generateEntityKey(Serializable id, EntityPersister persister) {


@Override @Override
public CacheKey generateCacheKey(Serializable id, Type type, String entityOrRoleName) { public CacheKey generateCacheKey(Serializable id, Type type, String entityOrRoleName) {
return new CacheKey( id, type, entityOrRoleName, getTenantIdentifier(), getFactory() ); return new OldCacheKeyImplementation( id, type, entityOrRoleName, getTenantIdentifier(), getFactory() );
} }


private transient JdbcConnectionAccess jdbcConnectionAccess; private transient JdbcConnectionAccess jdbcConnectionAccess;
Expand Down
Expand Up @@ -14,6 +14,7 @@


import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.internal.OldCacheKeyImplementation;
import org.hibernate.cache.spi.CacheKey; import org.hibernate.cache.spi.CacheKey;
import org.hibernate.cache.spi.QueryCache; import org.hibernate.cache.spi.QueryCache;
import org.hibernate.cache.spi.Region; import org.hibernate.cache.spi.Region;
Expand Down Expand Up @@ -97,7 +98,7 @@ public void evictEntity(String entityName, Serializable identifier) {
} }


private CacheKey buildCacheKey(Serializable identifier, EntityPersister p) { private CacheKey buildCacheKey(Serializable identifier, EntityPersister p) {
return new CacheKey( return new OldCacheKeyImplementation(
identifier, identifier,
p.getIdentifierType(), p.getIdentifierType(),
p.getRootEntityName(), p.getRootEntityName(),
Expand Down Expand Up @@ -175,7 +176,7 @@ public void evictCollection(String role, Serializable ownerIdentifier) {
} }


private CacheKey buildCacheKey(Serializable ownerIdentifier, CollectionPersister p) { private CacheKey buildCacheKey(Serializable ownerIdentifier, CollectionPersister p) {
return new CacheKey( return new OldCacheKeyImplementation(
ownerIdentifier, ownerIdentifier,
p.getKeyType(), p.getKeyType(),
p.getRole(), p.getRole(),
Expand Down

0 comments on commit 9ac0a34

Please sign in to comment.