HHH-7179 impl natural id region in hibernate-infinispan

This commit is contained in:
Strong Liu 2012-03-15 23:22:46 +08:00 committed by Galder Zamarreño
parent 9c207b1128
commit 2afa747ef9
4 changed files with 221 additions and 6 deletions

View File

@ -22,6 +22,10 @@ import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import org.hibernate.cache.infinispan.impl.BaseRegion;
import org.hibernate.cache.infinispan.naturalid.NaturalIdRegionImpl;
import org.hibernate.cache.infinispan.util.CacheCommandFactory;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.infinispan.collection.CollectionRegionImpl;
import org.hibernate.cache.infinispan.entity.EntityRegionImpl;
@ -92,6 +96,15 @@ public class InfinispanRegionFactory implements RegionFactory {
*/
public static final String INFINISPAN_USE_SYNCHRONIZATION_PROP = "hibernate.cache.infinispan.use_synchronization";
private static final String NATURAL_ID_KEY = "naturalid";
/**
* Name of the configuration that should be used for natural id caches.
*
* @see #DEF_ENTITY_RESOURCE
*/
public static final String NATURAL_ID_CACHE_RESOURCE_PROP = PREFIX + NATURAL_ID_KEY + CONFIG_SUFFIX;
private static final String ENTITY_KEY = "entity";
/**
@ -203,11 +216,24 @@ public class InfinispanRegionFactory implements RegionFactory {
return region;
}
@Override
public NaturalIdRegion buildNaturalIdRegion(String regionName, Properties properties, CacheDataDescription metadata)
throws CacheException {
throw new UnsupportedOperationException(); //TODO
}
@Override
public NaturalIdRegion buildNaturalIdRegion(String regionName, Properties properties, CacheDataDescription metadata)
throws CacheException {
if ( log.isDebugEnabled() ) {
log.debug( "Building natural id cache region [" + regionName + "]" );
}
AdvancedCache cache = getCache( regionName, NATURAL_ID_KEY, properties );
CacheAdapter cacheAdapter = CacheAdapterImpl.newInstance( cache );
NaturalIdRegionImpl region = new NaturalIdRegionImpl(
cacheAdapter,
regionName,
metadata,
transactionManager,
this
);
startRegion( region, regionName );
return region;
}
/**
* {@inheritDoc}

View File

@ -0,0 +1,37 @@
package org.hibernate.cache.infinispan.naturalid;
import javax.transaction.TransactionManager;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.infinispan.access.PutFromLoadValidator;
import org.hibernate.cache.infinispan.impl.BaseTransactionalDataRegion;
import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.spi.NaturalIdRegion;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
/**
* @author Strong Liu <stliu@hibernate.org>
*/
public class NaturalIdRegionImpl extends BaseTransactionalDataRegion implements NaturalIdRegion {
public NaturalIdRegionImpl(CacheAdapter cacheAdapter,
String name, CacheDataDescription metadata,
TransactionManager transactionManager, RegionFactory factory) {
super( cacheAdapter, name, metadata, transactionManager, factory );
}
@Override
public NaturalIdRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
if (AccessType.READ_ONLY.equals(accessType)) {
return new ReadOnlyAccess(this);
} else if (AccessType.TRANSACTIONAL.equals(accessType)) {
return new TransactionalAccess(this);
}
throw new CacheException("Unsupported access type [" + accessType.getExternalName() + "]");
}
public PutFromLoadValidator getPutFromLoadValidator() {
return new PutFromLoadValidator(transactionManager);
}
}

View File

@ -0,0 +1,48 @@
package org.hibernate.cache.infinispan.naturalid;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.spi.access.SoftLock;
/**
* @author Strong Liu <stliu@hibernate.org>
*/
class ReadOnlyAccess extends TransactionalAccess {
private static final Log log = LogFactory.getLog( ReadOnlyAccess.class );
ReadOnlyAccess(NaturalIdRegionImpl naturalIdRegion) {
super( naturalIdRegion );
}
@Override
public SoftLock lockItem(Object key, Object version) throws CacheException {
throw new UnsupportedOperationException( "Illegal attempt to edit read only item" );
}
@Override
public SoftLock lockRegion() throws CacheException {
throw new UnsupportedOperationException( "Illegal attempt to edit read only item" );
}
@Override
public void unlockItem(Object key, SoftLock lock) throws CacheException {
log.error( "Illegal attempt to edit read only item" );
}
@Override
public void unlockRegion(SoftLock lock) throws CacheException {
log.error( "Illegal attempt to edit read only item" );
}
@Override
public boolean update(Object key, Object value) throws CacheException {
throw new UnsupportedOperationException( "Illegal attempt to edit read only item" );
}
@Override
public boolean afterUpdate(Object key, Object value, SoftLock lock) throws CacheException {
throw new UnsupportedOperationException( "Illegal attempt to edit read only item" );
}
}

View File

@ -0,0 +1,104 @@
package org.hibernate.cache.infinispan.naturalid;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.infinispan.access.TransactionalAccessDelegate;
import org.hibernate.cache.spi.EntityRegion;
import org.hibernate.cache.spi.NaturalIdRegion;
import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
import org.hibernate.cache.spi.access.SoftLock;
/**
* @author Strong Liu <stliu@hibernate.org>
*/
class TransactionalAccess implements NaturalIdRegionAccessStrategy {
private final NaturalIdRegionImpl region;
private final TransactionalAccessDelegate delegate;
TransactionalAccess(NaturalIdRegionImpl region) {
this.region = region;
this.delegate = new TransactionalAccessDelegate( region, region.getPutFromLoadValidator() );
}
@Override
public boolean afterInsert(Object key, Object value) throws CacheException {
return false;
}
@Override
public boolean insert(Object key, Object value) throws CacheException {
return false;
}
@Override
public boolean update(Object key, Object value) throws CacheException {
return false;
}
@Override
public boolean afterUpdate(Object key, Object value, SoftLock lock) throws CacheException {
return false;
}
@Override
public NaturalIdRegion getRegion() {
return region;
}
@Override
public void evict(Object key) throws CacheException {
delegate.evict( key );
}
@Override
public void evictAll() throws CacheException {
delegate.evictAll();
}
@Override
public Object get(Object key, long txTimestamp) throws CacheException {
return delegate.get( key, txTimestamp );
}
@Override
public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version) throws CacheException {
return delegate.putFromLoad( key, value, txTimestamp, version );
}
@Override
public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride)
throws CacheException {
return delegate.putFromLoad( key, value, txTimestamp, version, minimalPutOverride );
}
@Override
public void remove(Object key) throws CacheException {
delegate.remove( key );
}
@Override
public void removeAll() throws CacheException {
delegate.removeAll();
}
@Override
public SoftLock lockItem(Object key, Object version) throws CacheException {
return null;
}
@Override
public SoftLock lockRegion() throws CacheException {
return null;
}
@Override
public void unlockItem(Object key, SoftLock lock) throws CacheException {
}
@Override
public void unlockRegion(SoftLock lock) throws CacheException {
}
}