1 package com.jofti.cache.adapter; 2 3 4 5 import java.util.Iterator ; 6 import java.util.Map ; 7 import java.util.Properties ; 8 import java.util.Set ; 9 10 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 15 16 17 import com.jofti.api.IndexQuery; 18 import com.jofti.cache.CacheAdapter; 19 import com.jofti.cache.BaseAdaptor; 20 import com.jofti.core.INameSpaceAware; 21 import com.jofti.core.IParsedQuery; 22 import com.jofti.core.InternalIndex; 23 import com.jofti.core.QueryId; 24 import com.jofti.core.QueryType; 25 import com.jofti.exception.JoftiException; 26 import com.jofti.util.CompositeComparator; 27 28 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 29 30 31 55 56 public class MapAdapter extends BaseAdaptor implements CacheAdapter { 57 58 private static Log log = LogFactory.getLog(MapAdapter.class); 59 60 private Map map = new ConcurrentHashMap(); 61 62 private String name; 63 64 65 66 67 68 public MapAdapter() { 69 } 70 71 public MapAdapter(Object cache) { 72 73 this.map = (Map )cache; 74 } 75 76 public void setCacheImpl(Object cache){ 77 this.map = (Map )cache; 78 79 } 80 81 84 public Object get(Object key) { 85 86 Object result = null; 87 if (key != null) { 88 89 90 result = map.get(key); 91 92 } 93 return result; 94 95 } 96 97 98 99 104 public void put(Object key, Object value) throws JoftiException{ 105 106 Comparable newKey = (Comparable )decorateKey(key); 107 108 acquireUpdateLock(); 109 try{ 110 synchronized(getCacheLock(key)) 111 { 112 if (index.contains(newKey)){ 113 index.removeByKey(newKey); 114 } 115 116 map.put(key,value); 118 index.insert(newKey, value); 119 120 121 } 122 } finally { 123 releaseUpdateLock(); 124 } 125 } 126 127 134 public void remove(Object key) throws JoftiException { 135 Comparable newKey = (Comparable )decorateKey(key); 136 137 acquireUpdateLock(); 138 try { 139 synchronized (getCacheLock(key)) { 140 map.remove(key); 141 142 if (index.contains(newKey)) { 143 index.removeByKey(newKey); 144 } 145 146 } 147 148 } catch (Exception e) { 149 throw new JoftiException(e); 150 } finally { 151 releaseUpdateLock(); 152 } 153 154 } 155 156 161 public void removeAll() throws JoftiException{ 162 acquireUpdateLock(); 163 try { 164 if (map != null){ 165 map.clear(); 166 } 167 if (index != null){ 168 index.removeAll(); 169 } 170 } 171 catch (Exception e) { 172 throw new JoftiException(e); 173 } finally{ 174 releaseUpdateLock(); 175 } 176 177 } 178 179 182 public synchronized void init(Properties properties) throws JoftiException { 183 184 185 if (properties != null){ 186 String key = null; 187 for (Iterator it = properties.keySet().iterator();it.hasNext();){ 188 key = (String )it.next(); 189 if (MUTABLE_VALUES.equalsIgnoreCase(key)){ 190 checkMutable = Boolean.valueOf(properties.getProperty(key)).booleanValue(); 191 if (log.isInfoEnabled()){ 192 log.info("Mutability checking is set to " + checkMutable); 193 } 194 } 195 196 } 197 } 198 } 199 200 203 public void destroy() throws JoftiException { 204 205 map = new ConcurrentHashMap(); 206 207 if (index != null){ 208 index.removeAll(); 209 } 210 } 211 212 213 214 217 public String getName() { 218 return name; 219 } 220 221 224 public void setName(String name) { 225 this.name = name; 226 } 227 228 public String toString() { 229 return "HashMapCache(" + getName() + ')'; 230 } 231 232 233 236 public Object getCacheImpl() { 237 238 return map; 239 } 240 241 242 243 246 public void setInternalIndex(InternalIndex index) { 247 this.index = index; 248 249 } 250 251 254 public void start() throws JoftiException { 255 256 loadInitialValues(map); 257 258 259 } 260 261 private void loadInitialValues(Map temp) throws JoftiException { 262 if (map ==null){ 263 log.info("No initial values to index as map is null"); 264 return; 265 } 266 Set entries = temp.entrySet(); 267 int size = entries.size(); 268 Iterator it = entries.iterator(); 269 for (int i=0;i<size;i++) { 270 271 Map.Entry entry = (Map.Entry ) it.next(); 272 273 if (entry.getKey() instanceof Comparable ) { 274 if (entry.getValue() != null) { 275 index.insert((Comparable ) entry.getKey(), entry.getValue()); 277 } 278 } else { 279 log.warn("unable to index value at " + entry.getKey() 280 + "as key is not Comparable"); 281 } 282 } 283 } 284 285 286 287 288 289 292 protected Object getCacheValue(Object key) { 293 synchronized (getCacheLock(key)) { 294 return map.get(key); 295 } 296 } 297 298 301 public InternalIndex getIndex() { 302 return index; 303 } 304 305 public IndexQuery addQuery(String name, IndexQuery query)throws JoftiException { 306 307 return index.getParserManager().addQuery(name, query); 308 } 309 310 313 public IndexQuery getQuery(String name) { 314 315 return index.getParserManager().getQuery(name); 316 } 317 318 } | Popular Tags |