Skip to content

Commit

Permalink
HHH-8961 Reduce allocation cost of org.hibernate.cache.spi.CacheKey i…
Browse files Browse the repository at this point in the history
…nstances

Remove tenantId field from CacheKey: use a different type when tenants are needed.
Also remove the Type as we should be able to rely on the entityOrRoleName String.
  • Loading branch information
Sanne authored and brmeyer committed Feb 18, 2014
1 parent af5804a commit f69e1a6
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 23 deletions.
@@ -0,0 +1,54 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.cache.internal;

import java.io.Serializable;

import org.hibernate.cache.spi.CacheKey;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.type.Type;

/**
* Variation of CacheKey to be used when multi-tenancy is applied.
* The tenantId field was not simply added to the superclass because of performance considerations:
* CacheKey instances are have a very high allocation frequency and this field would enlarge the
* size by 50%.
*
* @author Sanne Grinovero
*/
public class TenantAwareCacheKey extends CacheKey {

private final String tenantId;

public TenantAwareCacheKey(Serializable id, Type type, String entityOrRoleName, SessionFactoryImplementor factory, String tenantId) {
super( id, type, entityOrRoleName, factory, tenantId );
this.tenantId = tenantId;
}

@Override
public String getTenantId() {
return tenantId;
}

}
46 changes: 26 additions & 20 deletions hibernate-core/src/main/java/org/hibernate/cache/spi/CacheKey.java
Expand Up @@ -33,14 +33,20 @@
* 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().
*
* Note on performance: this class is allocated very heavily!
* Make sure the allocation cost stays as low as possible:
* if you need to add fields for not-so-common use cases,
* it's probably better to create an ad-hoc implementation
* extending this one.
*
* @author Gavin King
* @author Steve Ebersole
* @author Sanne Grinovero
*/
public class CacheKey implements Serializable {

private final Serializable key;
private final Type type;
private final String entityOrRoleName;
private final String tenantId;
private final int hashCode;

/**
Expand All @@ -58,16 +64,25 @@ public CacheKey(
final Serializable id,
final Type type,
final String entityOrRoleName,
final String tenantId,
final SessionFactoryImplementor factory) {
this( id, type, entityOrRoleName, factory, null );
}

/**
* Used by subclasses: need to specify a tenantId
*/
protected CacheKey(
final Serializable id,
final Type type,
final String entityOrRoleName,
final SessionFactoryImplementor factory,
final String tenantId) {
this.key = id;
this.type = type;
this.entityOrRoleName = entityOrRoleName;
this.tenantId = tenantId;
this.hashCode = calculateHashCode( type, factory );
this.hashCode = calculateHashCode( type, factory, tenantId );
}

private int calculateHashCode(Type type, SessionFactoryImplementor factory) {
private int calculateHashCode(Type type, SessionFactoryImplementor factory, String tenantId) {
int result = type.getHashCode( key, factory );
result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0);
return result;
Expand All @@ -77,12 +92,8 @@ public Serializable getKey() {
return key;
}

public String getEntityOrRoleName() {
return entityOrRoleName;
}

public String getTenantId() {
return tenantId;
return null;
}

@Override
Expand All @@ -97,20 +108,15 @@ public boolean equals(Object other) {
//hashCode is part of this check since it is pre-calculated and hash must match for equals to be true
return false;
}
//Warning: this equals implementation needs to work correctly also for the subclass
final CacheKey that = (CacheKey) other;
return EqualsHelper.equals( entityOrRoleName, that.entityOrRoleName )
&& type.isEqual( key, that.key )
&& EqualsHelper.equals( tenantId, that.tenantId );
return EqualsHelper.equals( getTenantId(), that.getTenantId() )
&& type.isEqual( key, that.key );
}

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

@Override
public String toString() {
// Used to be required for OSCache
return entityOrRoleName + '#' + key.toString();
}
}
Expand Up @@ -38,6 +38,7 @@
import org.hibernate.SessionEventListener;
import org.hibernate.SessionException;
import org.hibernate.SharedSessionContract;
import org.hibernate.cache.internal.TenantAwareCacheKey;
import org.hibernate.cache.spi.CacheKey;
import org.hibernate.engine.jdbc.LobCreationContext;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
Expand Down Expand Up @@ -329,7 +330,13 @@ public EntityKey generateEntityKey(Serializable id, EntityPersister persister) {

@Override
public CacheKey generateCacheKey(Serializable id, Type type, String entityOrRoleName) {
return new CacheKey( id, type, entityOrRoleName, getTenantIdentifier(), getFactory() );
final String tenantIdentifier = getTenantIdentifier();
if ( tenantIdentifier == null ) {
return new CacheKey( id, type, entityOrRoleName, getFactory() );
}
else {
return new TenantAwareCacheKey( id, type, entityOrRoleName, getFactory(), tenantIdentifier );
}
}

private transient JdbcConnectionAccess jdbcConnectionAccess;
Expand Down
Expand Up @@ -119,11 +119,11 @@ public void evictEntity(String entityName, Serializable identifier) {
}

private CacheKey buildCacheKey(Serializable identifier, EntityPersister p) {
// have to assume non tenancy
return new CacheKey(
identifier,
p.getIdentifierType(),
p.getRootEntityName(),
null, // have to assume non tenancy
sessionFactory
);
}
Expand Down Expand Up @@ -197,11 +197,11 @@ public void evictCollection(String role, Serializable ownerIdentifier) {
}

private CacheKey buildCacheKey(Serializable ownerIdentifier, CollectionPersister p) {
// have to assume non tenancy
return new CacheKey(
ownerIdentifier,
p.getKeyType(),
p.getRole(),
null, // have to assume non tenancy
sessionFactory
);
}
Expand Down

0 comments on commit f69e1a6

Please sign in to comment.