1
//$Id: TreeCache.java 9965 2006-05-30 18:00:28Z steve.ebersole@jboss.com $
2
package org.hibernate.cache;
4
import java.util.HashMap;
5
import java.util.Iterator;
9
import javax.transaction.SystemException;
10
import javax.transaction.Transaction;
11
import javax.transaction.TransactionManager;
13
import org.apache.commons.logging.Log;
14
import org.apache.commons.logging.LogFactory;
15
import org.jboss.cache.Fqn;
16
import org.jboss.cache.lock.TimeoutException;
19
* Represents a particular region within the given JBossCache TreeCache.
23
public class TreeCache implements Cache {
25
private static final Log log = LogFactory.getLog(TreeCache.class);
27
private static final String ITEM = "item";
29
private org.jboss.cache.TreeCache cache;
30
private final String regionName;
31
private final Fqn regionFqn;
32
private final TransactionManager transactionManager;
34
public TreeCache(org.jboss.cache.TreeCache cache, String regionName, TransactionManager transactionManager)
35
throws CacheException {
37
this.regionName = regionName;
38
this.regionFqn = Fqn.fromString( regionName.replace( '.', '/' ) );
39
this.transactionManager = transactionManager;
42
public Object get(Object key) throws CacheException {
43
Transaction tx = suspend();
52
public Object read(Object key) throws CacheException {
54
return cache.get( new Fqn( regionFqn, key ), ITEM );
57
throw new CacheException(e);
61
public void update(Object key, Object value) throws CacheException {
63
cache.put( new Fqn( regionFqn, key ), ITEM, value );
66
throw new CacheException(e);
70
public void put(Object key, Object value) throws CacheException {
71
Transaction tx = suspend();
73
//do the failfast put outside the scope of the JTA txn
74
cache.putFailFast( new Fqn( regionFqn, key ), ITEM, value, 0 );
76
catch (TimeoutException te) {
78
log.debug("ignoring write lock acquisition failure");
81
throw new CacheException(e);
88
private void resume(Transaction tx) {
90
if (tx!=null) transactionManager.resume(tx);
93
throw new CacheException("Could not resume transaction", e);
97
private Transaction suspend() {
98
Transaction tx = null;
100
if ( transactionManager!=null ) {
101
tx = transactionManager.suspend();
104
catch (SystemException se) {
105
throw new CacheException("Could not suspend transaction", se);
110
public void remove(Object key) throws CacheException {
112
cache.remove( new Fqn( regionFqn, key ) );
114
catch (Exception e) {
115
throw new CacheException(e);
119
public void clear() throws CacheException {
121
cache.remove( regionFqn );
123
catch (Exception e) {
124
throw new CacheException(e);
128
public void destroy() throws CacheException {
130
// NOTE : evict() operates locally only (i.e., does not propogate
131
// to any other nodes in the potential cluster). This is
132
// exactly what is needed when we destroy() here; destroy() is used
133
// as part of the process of shutting down a SessionFactory; thus
134
// these removals should not be propogated
135
cache.evict( regionFqn );
137
catch( Exception e ) {
138
throw new CacheException( e );
142
public void lock(Object key) throws CacheException {
143
throw new UnsupportedOperationException( "TreeCache is a fully transactional cache" + regionName );
146
public void unlock(Object key) throws CacheException {
147
throw new UnsupportedOperationException( "TreeCache is a fully transactional cache: " + regionName );
150
public long nextTimestamp() {
151
return System.currentTimeMillis() / 100;
154
public int getTimeout() {
155
return 600; //60 seconds
158
public String getRegionName() {
162
public long getSizeInMemory() {
166
public long getElementCountInMemory() {
168
Set children = cache.getChildrenNames( regionFqn );
169
return children == null ? 0 : children.size();
171
catch (Exception e) {
172
throw new CacheException(e);
176
public long getElementCountOnDisk() {
182
Map result = new HashMap();
183
Set childrenNames = cache.getChildrenNames( regionFqn );
184
if (childrenNames != null) {
185
Iterator iter = childrenNames.iterator();
186
while ( iter.hasNext() ) {
187
Object key = iter.next();
190
cache.get( new Fqn( regionFqn, key ), ITEM )
196
catch (Exception e) {
197
throw new CacheException(e);
201
public String toString() {
202
return "TreeCache(" + regionName + ')';