KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > cache > adapter > ChangeRecorder


1 /*
2  * Created on 19-Feb-2005
3  *
4
5  */

6 package com.jofti.cache.adapter;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Properties JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import com.jofti.api.IndexQuery;
18 import com.jofti.btree.BTree;
19 import com.jofti.core.GenericIndexFactory;
20 import com.jofti.core.InternalIndex;
21 import com.jofti.exception.JoftiException;
22 import com.jofti.introspect.ClassIntrospector;
23 import com.jofti.tree.NameSpacedTreeIndex;
24 import com.jofti.tree.TreeIndex;
25
26
27
28 /**
29  *
30  *
31  * Used by the JBossCacheAdapter to provide Transaction isolation for the
32  * changes to the index. In effect each transaction acquires its own mini-index which stores the
33  * changes to the larger main index. These changes are then applied on a commit (after the cache changes have committed).
34  *
35  * @author Steve Woodcock (steve@jofti.com)<p>
36  */

37
38 public class ChangeRecorder {
39
40     public final static Object JavaDoc DUMMY_VALUE = new Object JavaDoc();
41     
42     protected InternalIndex index =null;
43     private Set JavaDoc removedKeys = new HashSet JavaDoc();
44     private Map JavaDoc updatedMap = new HashMap JavaDoc();
45     
46     private ClassIntrospector parser = null;
47     
48     //stores values that were originally in the map but have been removed in the transaction
49
private Map JavaDoc removedValues = new HashMap JavaDoc();
50
51
52     public ChangeRecorder(ClassIntrospector parser){
53         this.parser = parser;
54     }
55     
56     
57     /**
58      * Initialises a new NameSpacedtreeIndex so local changes can be stored.
59      * @param props - properties to strat index with.
60      * @throws JoftiException
61      */

62     public void init(Properties JavaDoc props, String JavaDoc indexType) throws JoftiException{
63         index = GenericIndexFactory.getInstance().createIndex(indexType, parser, props, "localIndex");
64         
65         
66     }
67     
68     /**
69      * @return A collection of all the local tree values in the recorder
70      * @throws JoftiException
71      */

72     public Collection JavaDoc getAllTreeValues() throws JoftiException {
73         
74         return ((NameSpacedTreeIndex)index).getAllKeyEntriesFromTree();
75         
76     }
77     
78     /**
79      * Removes an object from the local index. This method also adds the removed key and
80      * values to the removedValues map so when committed a full list of removed objects can be
81      * identified.
82      * @param key
83      * @param value
84      * @param parser
85      * @throws JoftiException
86      */

87     public synchronized void remove(Object JavaDoc key,Object JavaDoc value, ClassIntrospector parser) throws JoftiException{
88         
89         List JavaDoc temp = (List JavaDoc)removedValues.get(key);
90         if (temp == null){
91             temp = new ArrayList JavaDoc();
92             
93         }
94         temp.add(value);
95         removedValues.put(key, temp);
96         index.remove((Comparable JavaDoc)key,value);
97         
98             
99
100
101     }
102     
103     /**
104      * Removes an object mapping from the change recorder.
105      * <p>
106      * @param key
107      * @param parser
108      * @throws JoftiException
109      */

110     public synchronized void remove(Object JavaDoc key,ClassIntrospector parser )throws JoftiException {
111         
112
113         removedKeys.add(key);
114         index.removeByKey((Comparable JavaDoc)key);
115     
116
117
118     }
119     
120     /**
121      * Adds a value to the ChangeRecorder.<p>
122      * @param key
123      * @param value
124      * @param parser
125      * @throws JoftiException
126      */

127     public synchronized void add(Object JavaDoc key, Object JavaDoc value,ClassIntrospector parser) throws JoftiException{
128         
129     
130         index.insert((Comparable JavaDoc)key, value);
131         
132         
133     }
134
135     /**
136      * Checks the local index to see if it contains the key.<p>
137      * @param key
138      * @param parser
139      * @return a boolean indicating if the change recorder contains the key
140      * @throws JoftiException
141      */

142     public boolean contains(Object JavaDoc key, ClassIntrospector parser) throws JoftiException{
143         
144         return index.contains((Comparable JavaDoc)key);
145         
146     }
147     
148     /**
149      * Notifies the Change Recorder that an attribute has been updated in the local index.<p>
150      * @param key
151      * @param oldValue
152      * @param value
153      * @param parser
154      * @throws JoftiException
155      */

156     public synchronized void update(Object JavaDoc key, Object JavaDoc oldValue,Object JavaDoc value, ClassIntrospector parser) throws JoftiException{
157                 
158         // remove if we already have set it
159
index.removeByKey((Comparable JavaDoc)key);
160
161         // add to the updated list
162
List JavaDoc temp = (List JavaDoc)updatedMap.get(key);
163         if (temp == null){
164             temp = new ArrayList JavaDoc();
165             
166         }
167         temp.add(oldValue);
168         updatedMap.put(key, temp);
169         
170         // insert the new value
171
index.insert((Comparable JavaDoc)key,value);
172     
173     }
174         
175
176     Map JavaDoc getRemovedMap(){
177         return removedValues;
178     }
179         
180     
181     Map JavaDoc getUpdatedMap(){
182         return updatedMap;
183     }
184     
185
186     /**
187      * @return Returns the removedKeys.
188      */

189     public Set JavaDoc getRemovedKeys() {
190         return removedKeys;
191     }
192     
193     /**
194       * Executes a query against the local index.
195      * @param query
196      * @return A Map containing the results of the query
197      * @throws JoftiException
198      */

199     public Map JavaDoc query(IndexQuery query) throws JoftiException{
200         return index.query(query);
201      }
202 }
203
Popular Tags