package org.hibernate.cache.internal;
import java.io.Serializable;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.TimestampsRegion;
import org.hibernate.cache.spi.TimestampsCache;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.jboss.logging.Logger;
public class TimestampsCacheEnabledImpl implements TimestampsCache {
private static final Logger log = Logger.getLogger( TimestampsCacheEnabledImpl.class );
private static final boolean DEBUG_ENABLED = log.isDebugEnabled();
private final TimestampsRegion timestampsRegion;
public TimestampsCacheEnabledImpl(TimestampsRegion timestampsRegion) {
this.timestampsRegion = timestampsRegion;
}
@Override
public TimestampsRegion getRegion() {
return timestampsRegion;
}
@Override
public void preInvalidate(
String[] spaces,
SharedSessionContractImplementor session) {
final SessionFactoryImplementor factory = session.getFactory();
final RegionFactory regionFactory = factory.getCache().getRegionFactory();
final boolean stats = factory.getStatistics().isStatisticsEnabled();
final Long ts = regionFactory.nextTimestamp() + regionFactory.getTimeout();
for ( Serializable space : spaces ) {
if ( DEBUG_ENABLED ) {
log.debugf( "Pre-invalidating space [%s], timestamp: %s", space, ts );
}
try {
session.getEventListenerManager().cachePutStart();
timestampsRegion.putIntoCache( space, ts, session );
}
finally {
session.getEventListenerManager().cachePutEnd();
}
if ( stats ) {
factory.getStatistics().updateTimestampsCachePut();
}
}
}
@Override
public void invalidate(
String[] spaces,
SharedSessionContractImplementor session) {
final boolean stats = session.getFactory().getStatistics().isStatisticsEnabled();
final Long ts = session.getFactory().getCache().getRegionFactory().nextTimestamp();
for (Serializable space : spaces) {
if ( DEBUG_ENABLED ) {
log.debugf( "Invalidating space [%s], timestamp: %s", space, ts );
}
try {
session.getEventListenerManager().cachePutStart();
timestampsRegion.putIntoCache( space, ts, session );
}
finally {
session.getEventListenerManager().cachePutEnd();
if ( stats ) {
session.getFactory().getStatistics().updateTimestampsCachePut();
}
}
}
}
@Override
public boolean isUpToDate(
String[] spaces,
Long timestamp,
SharedSessionContractImplementor session) {
final boolean stats = session.getFactory().getStatistics().isStatisticsEnabled();
for ( Serializable space : spaces ) {
final Long lastUpdate = getLastUpdateTimestampForSpace( space, session );
if ( lastUpdate == null ) {
if ( stats ) {
session.getFactory().getStatistics().updateTimestampsCacheMiss();
}
}
else {
if ( DEBUG_ENABLED ) {
log.debugf(
"[%s] last update timestamp: %s",
space,
lastUpdate + ", result set timestamp: " + timestamp
);
}
if ( stats ) {
session.getFactory().getStatistics().updateTimestampsCacheHit();
}
if ( lastUpdate >= timestamp ) {
return false;
}
}
}
return true;
}
private Long getLastUpdateTimestampForSpace(Serializable space, SharedSessionContractImplementor session) {
Long ts = null;
try {
session.getEventListenerManager().cacheGetStart();
ts = (Long) timestampsRegion.getFromCache( space, session );
}
finally {
session.getEventListenerManager().cacheGetEnd( ts != null );
}
return ts;
}
}