HHH-7431 Don't pre-calc toString

Remove toString pre-calculation to avoid excess memory usage.
This commit is contained in:
Eric Dalquist 2012-07-05 21:32:40 -05:00
parent ffa9cf5768
commit a92d402cc5
2 changed files with 24 additions and 18 deletions

View File

@ -75,7 +75,11 @@ public class CacheKey implements Serializable {
@Override
public boolean equals(Object other) {
if ( !(other instanceof CacheKey) ) {
if ( this == other ) {
return true;
}
if ( !(other instanceof CacheKey) || hashCode != other.hashCode() ) {
//hashCode is part of this check since it is pre-calculated and hash must match for equals to be true
return false;
}
CacheKey that = (CacheKey) other;

View File

@ -43,7 +43,6 @@ public class NaturalIdCacheKey implements Serializable {
private final String entityName;
private final String tenantId;
private final int hashCode;
private final String toString;
/**
* Construct a new key for a caching natural identifier resolutions into the second level cache.
@ -61,8 +60,7 @@ public class NaturalIdCacheKey implements Serializable {
this.entityName = persister.getRootEntityName();
this.tenantId = session.getTenantIdentifier();
final Serializable[] disassembledNaturalId = new Serializable[naturalIdValues.length];
final StringBuilder toStringBuilder = new StringBuilder( entityName ).append( "##NaturalId[" );
this.naturalIdValues = new Serializable[naturalIdValues.length];
final SessionFactoryImplementor factory = session.getFactory();
final int[] naturalIdPropertyIndexes = persister.getNaturalIdentifierProperties();
@ -78,18 +76,10 @@ public class NaturalIdCacheKey implements Serializable {
result = prime * result + (value != null ? type.getHashCode( value, factory ) : 0);
disassembledNaturalId[i] = type.disassemble( value, session, null );
toStringBuilder.append( type.toLoggableString( value, factory ) );
if (i + 1 < naturalIdValues.length) {
toStringBuilder.append( ", " );
}
this.naturalIdValues[i] = type.disassemble( value, session, null );
}
toStringBuilder.append( "]" );
this.naturalIdValues = disassembledNaturalId;
this.hashCode = result;
this.toString = toStringBuilder.toString();
}
@SuppressWarnings( {"UnusedDeclaration"})
@ -109,7 +99,18 @@ public class NaturalIdCacheKey implements Serializable {
@Override
public String toString() {
return this.toString;
//Complex toString is needed as naturalIds for entities are not simply based on a single value like primary keys
//the only sane way to differentiate the keys is to included the disassembled values in the string.
final StringBuilder toStringBuilder = new StringBuilder( entityName ).append( "##NaturalId[" );
for ( int i = 0; i < naturalIdValues.length; i++ ) {
toStringBuilder.append( naturalIdValues[i] );
if (i + 1 < naturalIdValues.length) {
toStringBuilder.append( ", " );
}
}
toStringBuilder.append( "]" );
return toStringBuilder.toString();
}
@Override
@ -122,14 +123,15 @@ public class NaturalIdCacheKey implements Serializable {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
if ( !(o instanceof NaturalIdCacheKey) || hashCode != o.hashCode() ) {
//hashCode is part of this check since it is pre-calculated and hash must match for equals to be true
return false;
}
final NaturalIdCacheKey other = (NaturalIdCacheKey) o;
return entityName.equals( other.entityName )
return entityName.equals( other.entityName )
&& EqualsHelper.equals( tenantId, other.tenantId )
&& Arrays.equals( naturalIdValues, other.naturalIdValues );
&& Arrays.deepEquals( this.naturalIdValues, other.naturalIdValues );
}
}