1 17 package org.apache.excalibur.store.impl; 18 19 import java.io.IOException ; 20 import java.util.Collections ; 21 import java.util.Enumeration ; 22 23 import EDU.oswego.cs.dl.util.concurrent.FIFOReadWriteLock; 24 import EDU.oswego.cs.dl.util.concurrent.ReadWriteLock; 25 import EDU.oswego.cs.dl.util.concurrent.Sync; 26 27 import org.apache.avalon.framework.logger.AbstractLogEnabled; 28 import org.apache.avalon.framework.thread.ThreadSafe; 29 import org.apache.excalibur.instrument.CounterInstrument; 30 import org.apache.excalibur.instrument.Instrument; 31 import org.apache.excalibur.instrument.Instrumentable; 32 import org.apache.excalibur.instrument.ValueInstrument; 33 import org.apache.excalibur.store.Store; 34 35 42 public abstract class AbstractReadWriteStore 43 extends AbstractLogEnabled 44 implements Store, ThreadSafe { 45 46 private ValueInstrument m_sizeInstrument = new ValueInstrument("size"); 47 private CounterInstrument m_hitsInstrument = new CounterInstrument("hits"); 48 private CounterInstrument m_missesInstrument = new CounterInstrument("misses"); 49 50 private String m_instrumentableName; 51 52 53 protected ReadWriteLock lock = new FIFOReadWriteLock(); 54 55 61 public Object get(Object key) 62 { 63 Object value = null; 64 Sync sync = this.lock.writeLock(); 65 try 66 { 67 sync.acquire(); 68 try 69 { 70 value = this.doGet(key); 71 } 72 finally 73 { 74 sync.release(); 75 } 76 } 77 catch (InterruptedException ignore) 78 { 79 } 80 81 if ( null == value ) 82 { 83 m_missesInstrument.increment(); 84 } 85 else 86 { 87 m_hitsInstrument.increment(); 88 } 89 90 return value; 91 } 92 93 100 public void store(Object key, Object value) 101 throws IOException 102 { 103 Sync sync = this.lock.writeLock(); 104 try 105 { 106 sync.acquire(); 107 108 try 109 { 110 this.doStore(key, value); 111 m_sizeInstrument.setValue( doGetSize() ); 112 } 113 finally 114 { 115 sync.release(); 116 } 117 } 118 catch (InterruptedException ignore) 119 { 120 } 121 122 } 123 124 127 public void free() 128 { 129 Sync sync = this.lock.writeLock(); 130 try 131 { 132 sync.acquire(); 133 134 try 135 { 136 this.doFree(); 137 m_sizeInstrument.setValue( doGetSize() ); 138 } 139 finally 140 { 141 sync.release(); 142 } 143 } 144 catch (InterruptedException ignore) 145 { 146 } 147 } 148 149 152 public void clear() 153 { 154 155 if (getLogger().isDebugEnabled()) 156 { 157 getLogger().debug("clear(): Clearing the database "); 158 } 159 160 Sync sync = this.lock.writeLock(); 161 try 162 { 163 sync.acquire(); 164 try 165 { 166 this.doClear(); 167 m_sizeInstrument.setValue( 0 ); 168 } 169 finally 170 { 171 sync.release(); 172 } 173 } 174 catch (InterruptedException ignore) 175 { 176 } 177 } 178 179 184 public void remove(Object key) 185 { 186 Sync sync = this.lock.writeLock(); 187 try 188 { 189 sync.acquire(); 190 try 191 { 192 this.doRemove(key); 193 m_sizeInstrument.setValue( doGetSize() ); 194 } 195 finally 196 { 197 sync.release(); 198 } 199 } 200 catch (InterruptedException ignore) 201 { 202 } 203 } 204 205 211 public boolean containsKey(Object key) 212 { 213 Sync sync = this.lock.readLock(); 214 try 215 { 216 sync.acquire(); 217 try 218 { 219 return this.doContainsKey(key); 220 } 221 finally 222 { 223 sync.release(); 224 } 225 } 226 catch (InterruptedException ignore) 227 { 228 return false; 229 } 230 } 231 232 237 public Enumeration keys() 238 { 239 Sync sync = this.lock.readLock(); 240 try 241 { 242 sync.acquire(); 243 try 244 { 245 return this.doGetKeys(); 246 } 247 finally 248 { 249 sync.release(); 250 } 251 } 252 catch (InterruptedException ignore) 253 { 254 return Collections.enumeration(Collections.EMPTY_LIST); 255 } 256 } 257 258 public int size() 259 { 260 Sync sync = this.lock.readLock(); 261 try 262 { 263 sync.acquire(); 264 try 265 { 266 return this.doGetSize(); 267 } 268 finally 269 { 270 sync.release(); 271 } 272 } 273 catch (InterruptedException ignore) 274 { 275 return 0; 276 } 277 } 278 279 public void setInstrumentableName(String name) 280 { 281 m_instrumentableName = name; 282 } 283 284 public String getInstrumentableName() 285 { 286 return m_instrumentableName; 287 } 288 289 public Instrument[] getInstruments() 290 { 291 return new Instrument[] { m_sizeInstrument, m_hitsInstrument, m_missesInstrument }; 292 } 293 294 public Instrumentable[] getChildInstrumentables() { 295 return Instrumentable.EMPTY_INSTRUMENTABLE_ARRAY; 296 } 297 298 301 protected abstract Object doGet( Object key ); 302 303 308 protected abstract void doStore( Object key, Object value ) throws IOException ; 309 310 315 protected abstract void doFree(); 316 317 320 protected abstract void doRemove( Object key ); 321 322 325 protected abstract void doClear(); 326 327 330 protected abstract boolean doContainsKey( Object key ); 331 332 335 protected abstract Enumeration doGetKeys(); 336 337 341 protected abstract int doGetSize(); 342 } 343 | Popular Tags |