KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > store > impl > AbstractReadWriteStore


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.store.impl;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Enumeration JavaDoc;
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 /**
36  * This is a base implementation for stores that are synchronized by
37  * using a read/write lock.
38  *
39  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
40  * @version CVS $Id: AbstractReadWriteStore.java,v 1.5 2004/02/28 11:47:31 cziegeler Exp $
41  */

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 JavaDoc m_instrumentableName;
51
52     /** The lock */
53     protected ReadWriteLock lock = new FIFOReadWriteLock();
54     
55     /**
56      * Returns a Object from the store associated with the Key Object
57      *
58      * @param key the Key object
59      * @return the Object associated with Key Object
60      */

61     public Object JavaDoc get(Object JavaDoc key)
62     {
63         Object JavaDoc 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 JavaDoc 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     /**
94      * Store the given object in the indexed data file.
95      *
96      * @param key the key object
97      * @param value the value object
98      * @exception IOException
99      */

100     public void store(Object JavaDoc key, Object JavaDoc value)
101     throws IOException JavaDoc
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 JavaDoc ignore)
119         {
120         }
121         
122     }
123
124     /**
125      * Frees some values of the data file.<br>
126      */

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 JavaDoc ignore)
145         {
146         }
147     }
148
149     /**
150      * Clear the Store of all elements
151      */

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 JavaDoc ignore)
175         {
176         }
177     }
178
179     /**
180      * Removes a value from the data file with the given key.
181      *
182      * @param key the key object
183      */

184     public void remove(Object JavaDoc 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 JavaDoc ignore)
201         {
202         }
203     }
204
205     /**
206      * Test if the the index file contains the given key
207      *
208      * @param key the key object
209      * @return true if Key exists and false if not
210      */

211     public boolean containsKey(Object JavaDoc 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 JavaDoc ignore)
227         {
228             return false;
229         }
230     }
231
232     /**
233      * Returns a Enumeration of all Keys in the indexed file.<br>
234      *
235      * @return Enumeration Object with all existing keys
236      */

237     public Enumeration JavaDoc 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 JavaDoc 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 JavaDoc ignore)
274         {
275             return 0;
276         }
277     }
278
279     public void setInstrumentableName(String JavaDoc name)
280     {
281         m_instrumentableName = name;
282     }
283
284     public String JavaDoc 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     /**
299      * Get the object associated to the given unique key.
300      */

301     protected abstract Object JavaDoc doGet( Object JavaDoc key );
302
303     /**
304      * Store the given object. It is up to the
305      * caller to ensure that the key has a persistent state across
306      * different JVM executions.
307      */

308     protected abstract void doStore( Object JavaDoc key, Object JavaDoc value ) throws IOException JavaDoc;
309
310     /**
311      * Try to free some used memory. The transient store can simply remove
312      * some hold data, the persistent store can free all memory by
313      * writing the data to a persistent store etc.
314      */

315     protected abstract void doFree();
316
317     /**
318      * Remove the object associated to the given key.
319      */

320     protected abstract void doRemove( Object JavaDoc key );
321
322     /**
323      * Clear the Store of all data it holds
324      */

325     protected abstract void doClear();
326
327     /**
328      * Indicates if the given key is associated to a contained object.
329      */

330     protected abstract boolean doContainsKey( Object JavaDoc key );
331
332     /**
333      * Returns the list of used keys as an Enumeration of Objects.
334      */

335     protected abstract Enumeration JavaDoc doGetKeys();
336
337     /**
338      * Returns count of the objects in the store, or -1 if could not be
339      * obtained.
340      */

341     protected abstract int doGetSize();
342 }
343
Popular Tags