1 package com.jofti.cache.adapter; 2 3 4 5 6 import java.util.Map ; 7 import java.util.Properties ; 8 9 10 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 15 16 import com.jofti.api.IndexQuery; 17 import com.jofti.cache.CacheAdapter; 18 import com.jofti.cache.BaseAdaptor; 19 import com.jofti.core.INameSpaceAware; 20 import com.jofti.core.IParsedQuery; 21 import com.jofti.core.InternalIndex; 22 import com.jofti.core.QueryId; 23 import com.jofti.core.QueryType; 24 import com.jofti.exception.JoftiException; 25 import com.jofti.store.Counter; 26 import com.jofti.util.CompositeComparator; 27 28 29 53 54 public class MockAdapter extends BaseAdaptor implements CacheAdapter { 55 56 private static Log log = LogFactory.getLog(MapAdapter.class); 57 58 59 private String name; 60 61 62 63 64 public MockAdapter() { 65 } 66 67 public MockAdapter(Object cache) { 68 69 } 70 71 public void setCacheImpl(Object cache){ 72 73 } 74 75 78 public Object get(Object key) { 79 80 Object result = null; 81 if (key != null) { 82 83 84 result = "mock"; 85 86 } 87 return result; 88 89 } 90 91 92 93 98 public void put(Object key, Object value) throws JoftiException{ 99 100 Comparable newKey = (Comparable )decorateKey(key); 101 102 acquireUpdateLock(); 103 try{ 104 synchronized(getCacheLock(key)) 105 { 106 107 if (index.contains(newKey)){ 108 Counter.removeCommands++; 109 index.removeByKey(newKey); 110 } 111 112 index.insert(newKey, value); 114 115 116 } 117 } finally { 118 releaseUpdateLock(); 119 } 120 } 121 122 129 public void remove(Object key) throws JoftiException { 130 Comparable newKey = (Comparable )decorateKey(key); 131 132 acquireUpdateLock(); 133 try { 134 synchronized (getCacheLock(key)) { 135 136 137 index.removeByKey(newKey); 138 139 } 140 141 } catch (Exception e) { 142 throw new JoftiException(e); 143 } finally { 144 releaseUpdateLock(); 145 } 146 147 } 148 149 154 public void removeAll() throws JoftiException{ 155 acquireUpdateLock(); 156 try { 157 if (index != null){ 158 index.removeAll(); 159 } 160 } 161 catch (Exception e) { 162 throw new JoftiException(e); 163 } finally{ 164 releaseUpdateLock(); 165 } 166 167 } 168 169 172 public synchronized void init(Properties properties) throws JoftiException { 173 174 175 176 } 177 178 181 public void destroy() throws JoftiException { 182 183 184 if (index != null){ 185 index.removeAll(); 186 } 187 } 188 189 190 191 194 public String getName() { 195 return name; 196 } 197 198 201 public void setName(String name) { 202 this.name = name; 203 } 204 205 public String toString() { 206 return "MockCache(" + getName() + ')'; 207 } 208 209 210 213 public Object getCacheImpl() { 214 215 return null; 216 } 217 218 219 220 223 public void setInternalIndex(InternalIndex index) { 224 this.index = index; 225 226 } 227 228 231 public void start() throws JoftiException { 232 233 234 235 } 236 237 238 239 244 245 protected Object checkMutable(Object key, Object result) { 246 247 248 return result; 249 } 250 251 252 255 protected Object getCacheValue(Object key) { 256 synchronized (getCacheLock(key)) { 257 return "mock"; 258 } 259 } 260 261 264 public InternalIndex getIndex() { 265 return index; 266 } 267 268 public IndexQuery addQuery(String name, IndexQuery query)throws JoftiException { 269 270 return index.getParserManager().addQuery(name, query); 271 } 272 273 276 public IndexQuery getQuery(String name) { 277 278 return index.getParserManager().getQuery(name); 279 } 280 281 } | Popular Tags |