FileDocCategorySizeDatePackage
OptimisticTreeCache.javaAPI DocHibernate 3.2.510303Thu Jul 13 16:38:42 BST 2006org.hibernate.cache

OptimisticTreeCache

public class OptimisticTreeCache extends Object implements OptimisticCache
Represents a particular region within the given JBossCache TreeCache utilizing TreeCache's optimistic locking capabilities.
see
OptimisticTreeCacheProvider for more details
author
Steve Ebersole

Fields Summary
private static final Log
log
private static final String
ITEM
private org.jboss.cache.TreeCache
cache
private final String
regionName
private final org.jboss.cache.Fqn
regionFqn
private OptimisticCacheSource
source
Constructors Summary
public OptimisticTreeCache(org.jboss.cache.TreeCache cache, String regionName)


	    
	  
		this.cache = cache;
		this.regionName = regionName;
		this.regionFqn = Fqn.fromString( regionName.replace( '.", '/" ) );
	
Methods Summary
public voidclear()

		try {
			Option option = new Option();
			option.setDataVersion( NonLockingDataVersion.INSTANCE );
			cache.remove( regionFqn, option );
		}
		catch (Exception e) {
			throw new CacheException(e);
		}
	
public voiddestroy()

		try {
			Option option = new Option();
			option.setCacheModeLocal( true );
			option.setFailSilently( true );
			option.setDataVersion( NonLockingDataVersion.INSTANCE );
			cache.remove( regionFqn, option );
		}
		catch( Exception e ) {
			throw new CacheException( e );
		}
	
public java.lang.Objectget(java.lang.Object key)

		try {
			Option option = new Option();
			option.setFailSilently( true );
//			option.setDataVersion( NonLockingDataVersion.INSTANCE );
			return cache.get( new Fqn( regionFqn, key ), ITEM, option );
		}
		catch (Exception e) {
			throw new CacheException(e);
		}
	
public longgetElementCountInMemory()

		try {
			Set children = cache.getChildrenNames( regionFqn );
			return children == null ? 0 : children.size();
		}
		catch (Exception e) {
			throw new CacheException(e);
		}
	
public longgetElementCountOnDisk()

		return 0;
	
public java.lang.StringgetRegionName()

		return regionName;
	
public longgetSizeInMemory()

		return -1;
	
public intgetTimeout()

		return 600; //60 seconds
	
public voidlock(java.lang.Object key)

		throw new UnsupportedOperationException( "TreeCache is a fully transactional cache" + regionName );
	
public longnextTimestamp()

		return System.currentTimeMillis() / 100;
	
public voidput(java.lang.Object key, java.lang.Object value)

		try {
			log.trace( "performing put() into region [" + regionName + "]" );
			// do the put outside the scope of the JTA txn
			Option option = new Option();
			option.setFailSilently( true );
			option.setDataVersion( NonLockingDataVersion.INSTANCE );
			cache.put( new Fqn( regionFqn, key ), ITEM, value, option );
		}
		catch (TimeoutException te) {
			//ignore!
			log.debug("ignoring write lock acquisition failure");
		}
		catch (Exception e) {
			throw new CacheException(e);
		}
	
public java.lang.Objectread(java.lang.Object key)

		try {
			return cache.get( new Fqn( regionFqn, key ), ITEM );
		}
		catch (Exception e) {
			throw new CacheException(e);
		}
	
public voidremove(java.lang.Object key)

		try {
			// tree cache in optimistic mode seems to have as very difficult
			// time with remove calls on non-existent nodes (NPEs)...
			if ( cache.get( new Fqn( regionFqn, key ), ITEM ) != null ) {
				Option option = new Option();
				option.setDataVersion( NonLockingDataVersion.INSTANCE );
				cache.remove( new Fqn( regionFqn, key ), option );
			}
			else {
				log.trace( "skipping remove() call as the underlying node did not seem to exist" );
			}
		}
		catch (Exception e) {
			throw new CacheException(e);
		}
	
public voidsetSource(OptimisticCacheSource source)

		this.source = source;
	
public java.util.MaptoMap()

		try {
			Map result = new HashMap();
			Set childrenNames = cache.getChildrenNames( regionFqn );
			if (childrenNames != null) {
				Iterator iter = childrenNames.iterator();
				while ( iter.hasNext() ) {
					Object key = iter.next();
					result.put(
							key,
					        cache.get( new Fqn( regionFqn, key ), ITEM )
						);
				}
			}
			return result;
		}
		catch (Exception e) {
			throw new CacheException(e);
		}
	
public java.lang.StringtoString()

		return "OptimisticTreeCache(" + regionName + ')";
	
public voidunlock(java.lang.Object key)

		throw new UnsupportedOperationException( "TreeCache is a fully transactional cache: " + regionName );
	
public voidupdate(java.lang.Object key, java.lang.Object value)

		try {
			Option option = new Option();
			option.setDataVersion( NonLockingDataVersion.INSTANCE );
			cache.put( new Fqn( regionFqn, key ), ITEM, value, option );
		}
		catch (Exception e) {
			throw new CacheException(e);
		}
	
public voidwriteInsert(java.lang.Object key, java.lang.Object value, java.lang.Object currentVersion)

		writeUpdate( key, value, currentVersion, null );
	
public voidwriteLoad(java.lang.Object key, java.lang.Object value, java.lang.Object currentVersion)

		try {
			Option option = new Option();
			option.setFailSilently( true );
			option.setDataVersion( NonLockingDataVersion.INSTANCE );
			cache.remove( new Fqn( regionFqn, key ), "ITEM", option );

			option = new Option();
			option.setFailSilently( true );
			DataVersion dv = ( source != null && source.isVersioned() )
			                 ? new DataVersionAdapter( currentVersion, currentVersion, source.getVersionComparator(), source.toString() )
			                 : NonLockingDataVersion.INSTANCE;
			option.setDataVersion( dv );
			cache.put( new Fqn( regionFqn, key ), ITEM, value, option );
		}
		catch (Exception e) {
			throw new CacheException(e);
		}
	
public voidwriteUpdate(java.lang.Object key, java.lang.Object value, java.lang.Object currentVersion, java.lang.Object previousVersion)

		try {
			Option option = new Option();
			DataVersion dv = ( source != null && source.isVersioned() )
			                 ? new DataVersionAdapter( currentVersion, previousVersion, source.getVersionComparator(), source.toString() )
			                 : NonLockingDataVersion.INSTANCE;
			option.setDataVersion( dv );
			cache.put( new Fqn( regionFqn, key ), ITEM, value, option );
		}
		catch ( Exception e ) {
			throw new CacheException( e );
		}