001: package com.jofti.cache.adapter;
002:
003: import java.util.Map;
004: import java.util.Properties;
005:
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008:
009: import com.jofti.api.IndexQuery;
010: import com.jofti.cache.CacheAdapter;
011: import com.jofti.cache.BaseAdaptor;
012: import com.jofti.core.INameSpaceAware;
013: import com.jofti.core.IParsedQuery;
014: import com.jofti.core.InternalIndex;
015: import com.jofti.core.QueryId;
016: import com.jofti.core.QueryType;
017: import com.jofti.exception.JoftiException;
018: import com.jofti.store.Counter;
019: import com.jofti.util.CompositeComparator;
020:
021: /**
022:
023: *
024: * The adapter implementation specific to the Map interface.
025: *
026: * The adapter takes care of the implementation specific details for converting
027: * between the process the index expects and the behaviour of the Map.
028: *
029: * Although it is possible to use this adapter with any Map implementation, it
030: * does not deal with the LinkedHashMap where it has been configured to run as
031: * an LRU cache. As values will dispear from the Map without callback to the index. This also applies to other Map
032: * type structures that silently expire elements.<p>
033: *
034: * Care must be taken when using Maps with similar expiry functionality as it <b> will</b> lead to memory leaks.
035: *
036: *
037: * The start up of the adapter will also try and index any elements already in
038: * the Map.<p>
039: *
040: * @author Steve Woodcock
041: * <p>
042: * @version 1.0
043: * <p>
044: */
045:
046: public class MockAdapter extends BaseAdaptor implements CacheAdapter {
047:
048: private static Log log = LogFactory.getLog(MapAdapter.class);
049:
050: private String name;
051:
052: public MockAdapter() {
053: }
054:
055: public MockAdapter(Object cache) {
056:
057: }
058:
059: public void setCacheImpl(Object cache) {
060:
061: }
062:
063: /* (non-Javadoc)
064: * @see com.jofti.api.IndexCache#get(java.lang.Object)
065: */
066: public Object get(Object key) {
067:
068: Object result = null;
069: if (key != null) {
070:
071: result = "mock";
072:
073: }
074: return result;
075:
076: }
077:
078: /*
079: * (non-Javadoc)
080: *
081: * @see com.jofti.api.IndexCache#put(java.lang.Object, java.lang.Object)
082: */
083: public void put(Object key, Object value) throws JoftiException {
084:
085: Comparable newKey = (Comparable) decorateKey(key);
086:
087: acquireUpdateLock();
088: try {
089: synchronized (getCacheLock(key)) {
090:
091: if (index.contains(newKey)) {
092: Counter.removeCommands++;
093: index.removeByKey(newKey);
094: }
095:
096: // put the value back in
097: index.insert(newKey, value);
098:
099: }
100: } finally {
101: releaseUpdateLock();
102: }
103: }
104:
105: /**
106: * Removes the element which matches the key.
107: * <p>
108: * If no element matches, nothing is removed and no Exception is thrown.
109: * @param key the key of the element to remove
110: * @throws CacheException
111: */
112: public void remove(Object key) throws JoftiException {
113: Comparable newKey = (Comparable) decorateKey(key);
114:
115: acquireUpdateLock();
116: try {
117: synchronized (getCacheLock(key)) {
118:
119: index.removeByKey(newKey);
120:
121: }
122:
123: } catch (Exception e) {
124: throw new JoftiException(e);
125: } finally {
126: releaseUpdateLock();
127: }
128:
129: }
130:
131: /**
132: * Remove all elements in the cache, but leave the cache in a useable state.
133: *
134: * @throws CacheException
135: */
136: public void removeAll() throws JoftiException {
137: acquireUpdateLock();
138: try {
139: if (index != null) {
140: index.removeAll();
141: }
142: } catch (Exception e) {
143: throw new JoftiException(e);
144: } finally {
145: releaseUpdateLock();
146: }
147:
148: }
149:
150: /* (non-Javadoc)
151: * @see com.jofti.cache.LifeCycleAdapter#init(java.util.Properties)
152: */
153: public synchronized void init(Properties properties)
154: throws JoftiException {
155:
156: }
157:
158: /* (non-Javadoc)
159: * @see com.jofti.cache.LifeCycleAdapter#destroy()
160: */
161: public void destroy() throws JoftiException {
162:
163: if (index != null) {
164: index.removeAll();
165: }
166: }
167:
168: /* (non-Javadoc)
169: * @see com.jofti.cache.LifeCycleAdapter#getName()
170: */
171: public String getName() {
172: return name;
173: }
174:
175: /* (non-Javadoc)
176: * @see com.jofti.cache.LifeCycleAdapter#setName(java.lang.String)
177: */
178: public void setName(String name) {
179: this .name = name;
180: }
181:
182: public String toString() {
183: return "MockCache(" + getName() + ')';
184: }
185:
186: /* (non-Javadoc)
187: * @see com.jofti.api.IndexCache#getCacheImpl()
188: */
189: public Object getCacheImpl() {
190:
191: return null;
192: }
193:
194: /* (non-Javadoc)
195: * @see com.jofti.cache.CacheAdapter#setInternalIndex(com.jofti.core.InternalIndex)
196: */
197: public void setInternalIndex(InternalIndex index) {
198: this .index = index;
199:
200: }
201:
202: /* (non-Javadoc)
203: * @see com.jofti.cache.CacheAdapter#start()
204: */
205: public void start() throws JoftiException {
206:
207: }
208:
209: /*
210: * (non-Javadoc)
211: *
212: * @see com.jofti.api.IndexCache#query(com.jofti.api.IndexQuery)
213: */
214:
215: protected Object checkMutable(Object key, Object result) {
216:
217: return result;
218: }
219:
220: /* (non-Javadoc)
221: * @see com.jofti.cache.CacheLocking#getCacheValue(java.lang.Object)
222: */
223: protected Object getCacheValue(Object key) {
224: synchronized (getCacheLock(key)) {
225: return "mock";
226: }
227: }
228:
229: /* (non-Javadoc)
230: * @see com.jofti.cache.CacheLocking#getIndex()
231: */
232: public InternalIndex getIndex() {
233: return index;
234: }
235:
236: public IndexQuery addQuery(String name, IndexQuery query)
237: throws JoftiException {
238:
239: return index.getParserManager().addQuery(name, query);
240: }
241:
242: /* (non-Javadoc)
243: * @see com.jofti.api.Index#getQuery(java.lang.String)
244: */
245: public IndexQuery getQuery(String name) {
246:
247: return index.getParserManager().getQuery(name);
248: }
249:
250: }
|