1 6 package com.jofti.cache.adapter; 7 8 import java.util.ArrayList ; 9 import java.util.Collection ; 10 import java.util.HashMap ; 11 import java.util.HashSet ; 12 import java.util.List ; 13 import java.util.Map ; 14 import java.util.Properties ; 15 import java.util.Set ; 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 37 38 public class ChangeRecorder { 39 40 public final static Object DUMMY_VALUE = new Object (); 41 42 protected InternalIndex index =null; 43 private Set removedKeys = new HashSet (); 44 private Map updatedMap = new HashMap (); 45 46 private ClassIntrospector parser = null; 47 48 private Map removedValues = new HashMap (); 50 51 52 public ChangeRecorder(ClassIntrospector parser){ 53 this.parser = parser; 54 } 55 56 57 62 public void init(Properties props, String indexType) throws JoftiException{ 63 index = GenericIndexFactory.getInstance().createIndex(indexType, parser, props, "localIndex"); 64 65 66 } 67 68 72 public Collection getAllTreeValues() throws JoftiException { 73 74 return ((NameSpacedTreeIndex)index).getAllKeyEntriesFromTree(); 75 76 } 77 78 87 public synchronized void remove(Object key,Object value, ClassIntrospector parser) throws JoftiException{ 88 89 List temp = (List )removedValues.get(key); 90 if (temp == null){ 91 temp = new ArrayList (); 92 93 } 94 temp.add(value); 95 removedValues.put(key, temp); 96 index.remove((Comparable )key,value); 97 98 99 100 101 } 102 103 110 public synchronized void remove(Object key,ClassIntrospector parser )throws JoftiException { 111 112 113 removedKeys.add(key); 114 index.removeByKey((Comparable )key); 115 116 117 118 } 119 120 127 public synchronized void add(Object key, Object value,ClassIntrospector parser) throws JoftiException{ 128 129 130 index.insert((Comparable )key, value); 131 132 133 } 134 135 142 public boolean contains(Object key, ClassIntrospector parser) throws JoftiException{ 143 144 return index.contains((Comparable )key); 145 146 } 147 148 156 public synchronized void update(Object key, Object oldValue,Object value, ClassIntrospector parser) throws JoftiException{ 157 158 index.removeByKey((Comparable )key); 160 161 List temp = (List )updatedMap.get(key); 163 if (temp == null){ 164 temp = new ArrayList (); 165 166 } 167 temp.add(oldValue); 168 updatedMap.put(key, temp); 169 170 index.insert((Comparable )key,value); 172 173 } 174 175 176 Map getRemovedMap(){ 177 return removedValues; 178 } 179 180 181 Map getUpdatedMap(){ 182 return updatedMap; 183 } 184 185 186 189 public Set getRemovedKeys() { 190 return removedKeys; 191 } 192 193 199 public Map query(IndexQuery query) throws JoftiException{ 200 return index.query(query); 201 } 202 } 203 | Popular Tags |