Skip to content

Commit

Permalink
HHH-7866 Avoid redundant log level checking in StandardQueryCache
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne authored and Brett Meyer committed Jan 3, 2013
1 parent bcf73bd commit 5cf1b46
Showing 1 changed file with 25 additions and 22 deletions.
Expand Up @@ -61,7 +61,7 @@ public class StandardQueryCache implements QueryCache {
StandardQueryCache.class.getName()
);

private static final boolean tracing = LOG.isTraceEnabled();
private static final boolean tracing = LOG.isTraceEnabled();

private QueryResultsRegion cacheRegion;
private UpdateTimestampsCache updateTimestampsCache;
Expand All @@ -88,13 +88,13 @@ public StandardQueryCache(
this.updateTimestampsCache = updateTimestampsCache;
}

@SuppressWarnings({ "UnnecessaryBoxing", "unchecked" })
@SuppressWarnings({ "unchecked" })
public boolean put(
QueryKey key,
Type[] returnTypes,
List result,
boolean isNaturalKeyLookup,
SessionImplementor session) throws HibernateException {
final QueryKey key,
final Type[] returnTypes,
final List result,
final boolean isNaturalKeyLookup,
final SessionImplementor session) throws HibernateException {
if ( isNaturalKeyLookup && result.isEmpty() ) {
return false;
}
Expand All @@ -103,7 +103,9 @@ public boolean put(
LOG.debugf( "Caching query results in region: %s; timestamp=%s", cacheRegion.getName(), ts );

List cacheable = new ArrayList( result.size() + 1 );
logCachedResultDetails( key, null, returnTypes, cacheable );
if ( tracing ) {
logCachedResultDetails( key, null, returnTypes, cacheable );
}
cacheable.add( ts );
final boolean singleResult = returnTypes.length == 1;
for ( Object aResult : result ) {
Expand All @@ -113,7 +115,7 @@ public boolean put(
null
) : TypeHelper.disassemble( (Object[]) aResult, returnTypes, null, session, null );
cacheable.add( cacheItem );
logCachedResultRowDetails( returnTypes, aResult );
if ( tracing ) logCachedResultRowDetails( returnTypes, aResult );
}

cacheRegion.put( key, cacheable );
Expand All @@ -122,28 +124,29 @@ public boolean put(

@SuppressWarnings({ "unchecked" })
public List get(
QueryKey key,
Type[] returnTypes,
boolean isNaturalKeyLookup,
Set spaces,
SessionImplementor session) throws HibernateException {
LOG.debugf( "Checking cached query results in region: %s", cacheRegion.getName() );
final QueryKey key,
final Type[] returnTypes,
final boolean isNaturalKeyLookup,
final Set spaces,
final SessionImplementor session) throws HibernateException {
final boolean debugEnabled = LOG.isDebugEnabled();
if ( debugEnabled ) LOG.debugf( "Checking cached query results in region: %s", cacheRegion.getName() );

List cacheable = (List) cacheRegion.get( key );
logCachedResultDetails( key, spaces, returnTypes, cacheable );
if ( tracing ) logCachedResultDetails( key, spaces, returnTypes, cacheable );

if ( cacheable == null ) {
LOG.debug( "Query results were not found in cache" );
if ( debugEnabled ) LOG.debug( "Query results were not found in cache" );
return null;
}

Long timestamp = (Long) cacheable.get( 0 );
if ( !isNaturalKeyLookup && !isUpToDate( spaces, timestamp ) ) {
LOG.debug( "Cached query results were not up-to-date" );
if ( debugEnabled ) LOG.debug( "Cached query results were not up-to-date" );
return null;
}

LOG.debug( "Returning cached query results" );
if ( debugEnabled ) LOG.debug( "Returning cached query results" );
final boolean singleResult = returnTypes.length == 1;
for ( int i = 1; i < cacheable.size(); i++ ) {
if ( singleResult ) {
Expand All @@ -164,7 +167,7 @@ public List get(
TypeHelper.assemble( (Serializable[]) cacheable.get( i ), returnTypes, session, null )
);
}
logCachedResultRowDetails( returnTypes, result.get( i - 1 ) );
if ( tracing ) logCachedResultRowDetails( returnTypes, result.get( i - 1 ) );
}
catch ( RuntimeException ex ) {
if ( isNaturalKeyLookup &&
Expand All @@ -174,7 +177,7 @@ public List get(
// the uoe could occur while resolving
// associations, leaving the PC in an
// inconsistent state
LOG.debug( "Unable to reassemble cached result set" );
if ( debugEnabled ) LOG.debug( "Unable to reassemble cached result set" );
cacheRegion.evict( key );
return null;
}
Expand All @@ -184,7 +187,7 @@ public List get(
return result;
}

protected boolean isUpToDate(Set spaces, Long timestamp) {
protected boolean isUpToDate(final Set spaces, final Long timestamp) {
LOG.debugf( "Checking query spaces are up-to-date: %s", spaces );
return updateTimestampsCache.isUpToDate( spaces, timestamp );
}
Expand Down

0 comments on commit 5cf1b46

Please sign in to comment.