~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/cache/TreeCache.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: TreeCache.java 9965 2006-05-30 18:00:28Z steve.ebersole@jboss.com $
 
2
package org.hibernate.cache;
 
3
 
 
4
import java.util.HashMap;
 
5
import java.util.Iterator;
 
6
import java.util.Map;
 
7
import java.util.Set;
 
8
 
 
9
import javax.transaction.SystemException;
 
10
import javax.transaction.Transaction;
 
11
import javax.transaction.TransactionManager;
 
12
 
 
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;
 
17
 
 
18
/**
 
19
 * Represents a particular region within the given JBossCache TreeCache.
 
20
 *
 
21
 * @author Gavin King
 
22
 */
 
23
public class TreeCache implements Cache {
 
24
        
 
25
        private static final Log log = LogFactory.getLog(TreeCache.class);
 
26
 
 
27
        private static final String ITEM = "item";
 
28
 
 
29
        private org.jboss.cache.TreeCache cache;
 
30
        private final String regionName;
 
31
        private final Fqn regionFqn;
 
32
        private final TransactionManager transactionManager;
 
33
 
 
34
        public TreeCache(org.jboss.cache.TreeCache cache, String regionName, TransactionManager transactionManager) 
 
35
        throws CacheException {
 
36
                this.cache = cache;
 
37
                this.regionName = regionName;
 
38
                this.regionFqn = Fqn.fromString( regionName.replace( '.', '/' ) );
 
39
                this.transactionManager = transactionManager;
 
40
        }
 
41
 
 
42
        public Object get(Object key) throws CacheException {
 
43
                Transaction tx = suspend();
 
44
                try {
 
45
                        return read(key);
 
46
                }
 
47
                finally {
 
48
                        resume( tx );
 
49
                }
 
50
        }
 
51
        
 
52
        public Object read(Object key) throws CacheException {
 
53
                try {
 
54
                        return cache.get( new Fqn( regionFqn, key ), ITEM );
 
55
                }
 
56
                catch (Exception e) {
 
57
                        throw new CacheException(e);
 
58
                }
 
59
        }
 
60
 
 
61
        public void update(Object key, Object value) throws CacheException {
 
62
                try {
 
63
                        cache.put( new Fqn( regionFqn, key ), ITEM, value );
 
64
                }
 
65
                catch (Exception e) {
 
66
                        throw new CacheException(e);
 
67
                }
 
68
        }
 
69
 
 
70
        public void put(Object key, Object value) throws CacheException {
 
71
                Transaction tx = suspend();
 
72
                try {
 
73
                        //do the failfast put outside the scope of the JTA txn
 
74
                        cache.putFailFast( new Fqn( regionFqn, key ), ITEM, value, 0 );
 
75
                }
 
76
                catch (TimeoutException te) {
 
77
                        //ignore!
 
78
                        log.debug("ignoring write lock acquisition failure");
 
79
                }
 
80
                catch (Exception e) {
 
81
                        throw new CacheException(e);
 
82
                }
 
83
                finally {
 
84
                        resume( tx );
 
85
                }
 
86
        }
 
87
 
 
88
        private void resume(Transaction tx) {
 
89
                try {
 
90
                        if (tx!=null) transactionManager.resume(tx);
 
91
                }
 
92
                catch (Exception e) {
 
93
                        throw new CacheException("Could not resume transaction", e);
 
94
                }
 
95
        }
 
96
 
 
97
        private Transaction suspend() {
 
98
                Transaction tx = null;
 
99
                try {
 
100
                        if ( transactionManager!=null ) {
 
101
                                tx = transactionManager.suspend();
 
102
                        }
 
103
                }
 
104
                catch (SystemException se) {
 
105
                        throw new CacheException("Could not suspend transaction", se);
 
106
                }
 
107
                return tx;
 
108
        }
 
109
 
 
110
        public void remove(Object key) throws CacheException {
 
111
                try {
 
112
                        cache.remove( new Fqn( regionFqn, key ) );
 
113
                }
 
114
                catch (Exception e) {
 
115
                        throw new CacheException(e);
 
116
                }
 
117
        }
 
118
 
 
119
        public void clear() throws CacheException {
 
120
                try {
 
121
                        cache.remove( regionFqn );
 
122
                }
 
123
                catch (Exception e) {
 
124
                        throw new CacheException(e);
 
125
                }
 
126
        }
 
127
 
 
128
        public void destroy() throws CacheException {
 
129
                try {
 
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 );
 
136
                }
 
137
                catch( Exception e ) {
 
138
                        throw new CacheException( e );
 
139
                }
 
140
        }
 
141
 
 
142
        public void lock(Object key) throws CacheException {
 
143
                throw new UnsupportedOperationException( "TreeCache is a fully transactional cache" + regionName );
 
144
        }
 
145
 
 
146
        public void unlock(Object key) throws CacheException {
 
147
                throw new UnsupportedOperationException( "TreeCache is a fully transactional cache: " + regionName );
 
148
        }
 
149
 
 
150
        public long nextTimestamp() {
 
151
                return System.currentTimeMillis() / 100;
 
152
        }
 
153
 
 
154
        public int getTimeout() {
 
155
                return 600; //60 seconds
 
156
        }
 
157
 
 
158
        public String getRegionName() {
 
159
                return regionName;
 
160
        }
 
161
 
 
162
        public long getSizeInMemory() {
 
163
                return -1;
 
164
        }
 
165
 
 
166
        public long getElementCountInMemory() {
 
167
                try {
 
168
                        Set children = cache.getChildrenNames( regionFqn );
 
169
                        return children == null ? 0 : children.size();
 
170
                }
 
171
                catch (Exception e) {
 
172
                        throw new CacheException(e);
 
173
                }
 
174
        }
 
175
 
 
176
        public long getElementCountOnDisk() {
 
177
                return 0;
 
178
        }
 
179
        
 
180
        public Map toMap() {
 
181
                try {
 
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();
 
188
                                        result.put( 
 
189
                                                        key, 
 
190
                                                        cache.get( new Fqn( regionFqn, key ), ITEM )
 
191
                                                );
 
192
                                }
 
193
                        }
 
194
                        return result;
 
195
                }
 
196
                catch (Exception e) {
 
197
                        throw new CacheException(e);
 
198
                }
 
199
        }
 
200
        
 
201
        public String toString() {
 
202
                return "TreeCache(" + regionName + ')';
 
203
        }
 
204
        
 
205
}