KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25
26 import com.coyotegulch.jisp.BTreeIndex;
27 import com.coyotegulch.jisp.BTreeIterator;
28 import com.coyotegulch.jisp.IndexedObjectDatabase;
29 import com.coyotegulch.jisp.KeyNotFound;
30 import com.coyotegulch.jisp.KeyObject;
31
32 import org.apache.avalon.framework.thread.ThreadSafe;
33 import org.apache.excalibur.store.Store;
34
35 /**
36  * This store is based on the Jisp library
37  * (http://www.coyotegulch.com/jisp/index.html). This store uses B-Tree indexes
38  * to access variable-length serialized data stored in files.
39  *
40  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
41  * @version CVS $Id: AbstractJispFilesystemStore.java,v 1.7 2004/02/28 11:47:31 cziegeler Exp $
42  */

43 public abstract class AbstractJispFilesystemStore
44     extends AbstractReadWriteStore
45     implements Store, ThreadSafe {
46     
47     /** The directory repository */
48     protected File JavaDoc m_directoryFile;
49     
50     /** The database */
51     protected IndexedObjectDatabase m_Database;
52     
53     /** And the index */
54     protected BTreeIndex m_Index;
55     
56     /**
57      * Sets the repository's location
58      */

59     public void setDirectory(final File JavaDoc directory)
60         throws IOException JavaDoc
61     {
62         this.m_directoryFile = directory;
63         
64         /* Does directory exist? */
65         if (!this.m_directoryFile.exists())
66         {
67             /* Create it anew */
68             if (!this.m_directoryFile.mkdirs())
69             {
70                 throw new IOException JavaDoc(
71                     "Error creating store directory '" + this.m_directoryFile.getAbsolutePath() + "'. ");
72             }
73         }
74         
75         /* Is given file actually a directory? */
76         if (!this.m_directoryFile.isDirectory())
77         {
78             throw new IOException JavaDoc("'" + this.m_directoryFile.getAbsolutePath() + "' is not a directory");
79         }
80         
81         /* Is directory readable and writable? */
82         if (!(this.m_directoryFile.canRead() && this.m_directoryFile.canWrite()))
83         {
84             throw new IOException JavaDoc(
85                 "Directory '" + this.m_directoryFile.getAbsolutePath() + "' is not readable/writable"
86                 );
87         }
88     }
89     
90     /**
91      * Returns a Object from the store associated with the Key Object
92      *
93      * @param key the Key object
94      * @return the Object associated with Key Object
95      */

96     protected Object JavaDoc doGet(Object JavaDoc key)
97     {
98         Object JavaDoc value = null;
99         
100         try
101         {
102             value = m_Database.read(this.wrapKeyObject(key), m_Index);
103             if (getLogger().isDebugEnabled())
104             {
105                 if (value != null)
106                 {
107                     getLogger().debug("Found key: " + key);
108                 }
109                 else
110                 {
111                     getLogger().debug("NOT Found key: " + key);
112                 }
113             }
114         }
115         catch (Exception JavaDoc e)
116         {
117             getLogger().error("get(..): Exception", e);
118         }
119         
120         return value;
121     }
122     
123     /**
124      * Store the given object in the indexed data file.
125      *
126      * @param key the key object
127      * @param value the value object
128      * @exception IOException
129      */

130     protected void doStore(Object JavaDoc key, Object JavaDoc value)
131         throws IOException JavaDoc
132     {
133         
134         if (getLogger().isDebugEnabled())
135         {
136             getLogger().debug("store(): Store file with key: "
137                 + key.toString());
138             getLogger().debug("store(): Store file with value: "
139                 + value.toString());
140         }
141         
142         if (value instanceof Serializable JavaDoc)
143         {
144             try
145             {
146                 KeyObject[] keyArray = new KeyObject[1];
147                 keyArray[0] = this.wrapKeyObject(key);
148                 m_Database.write(keyArray, (Serializable JavaDoc) value);
149             }
150             catch (Exception JavaDoc e)
151             {
152                 getLogger().error("store(..): Exception", e);
153             }
154         }
155         else
156         {
157             throw new IOException JavaDoc("Object not Serializable");
158         }
159     }
160     
161     /**
162      * Frees some values of the data file.<br>
163      * TODO: implementation
164      */

165     public void free()
166     {
167         // if we ever implement this, we should implement doFree()
168
}
169     
170     /* (non-Javadoc)
171      * @see org.apache.excalibur.store.impl.AbstractReadWriteStore#doFree()
172      */

173     protected void doFree() {
174     }
175     
176     /**
177      * Clear the Store of all elements
178      */

179     protected void doClear()
180     {
181         
182         if (getLogger().isDebugEnabled())
183         {
184             getLogger().debug("clear(): Clearing the database ");
185         }
186         
187         try
188         {
189             
190             //empty the cache before clearing
191
m_Index.emptyPageCache();
192             final BTreeIterator iter = new BTreeIterator(m_Index);
193             Object JavaDoc key;
194             KeyObject[] keyArray = new KeyObject[1];
195             while(iter.isValid()) {
196                 key = iter.getKey();
197                 if (key != null) {
198                     //This key should not be wrapped in a JipsKey because it comes from the iterator.
199
keyArray[0] = (KeyObject) key;
200                     m_Database.remove( keyArray );
201                 }
202                 if (!iter.moveNext()){
203                     break;
204                 }
205             }
206         }
207         catch (Exception JavaDoc ignore)
208         {
209             getLogger().error("store(..): Exception", ignore);
210         }
211     }
212     
213     /**
214      * Removes a value from the data file with the given key.
215      *
216      * @param key the key object
217      */

218     protected void doRemove(Object JavaDoc key)
219     {
220         if (getLogger().isDebugEnabled())
221         {
222             getLogger().debug("remove(..) Remove item");
223         }
224         
225         try
226         {
227             KeyObject[] keyArray = new KeyObject[1];
228             keyArray[0] = wrapKeyObject(key);
229             m_Database.remove(keyArray);
230         }
231         catch (KeyNotFound ignore)
232         {
233         }
234         catch (Exception JavaDoc e)
235         {
236             getLogger().error("remove(..): Exception", e);
237         }
238     }
239     
240     /**
241      * Test if the the index file contains the given key
242      *
243      * @param key the key object
244      * @return true if Key exists and false if not
245      */

246     protected boolean doContainsKey(Object JavaDoc key)
247     {
248         long res = -1;
249         
250         try
251         {
252             res = m_Index.findKey(this.wrapKeyObject(key));
253             if (getLogger().isDebugEnabled())
254             {
255                 getLogger().debug("containsKey(..): res=" + res);
256             }
257         }
258         catch (KeyNotFound ignore)
259         {
260         }
261         catch (Exception JavaDoc e)
262         {
263             getLogger().error("containsKey(..): Exception", e);
264         }
265         
266         if (res > 0)
267         {
268             return true;
269         }
270         else
271         {
272             return false;
273         }
274     }
275     
276     /**
277      * Returns a Enumeration of all Keys in the indexed file.<br>
278      *
279      * @return Enumeration Object with all existing keys
280      */

281     protected Enumeration JavaDoc doGetKeys()
282     {
283         try
284         {
285             return new BTreeObjectEnumeration(new BTreeIterator(m_Index), this);
286         }
287         catch (Exception JavaDoc ignore)
288         {
289             return Collections.enumeration(Collections.EMPTY_LIST);
290         }
291     }
292     
293     protected int doGetSize()
294     {
295         return m_Index.count();
296     }
297     
298     /**
299      * This method wraps around the key Object a Jisp KeyObject.
300      *
301      * @param key the key object
302      * @return the wrapped key object
303      */

304     protected KeyObject wrapKeyObject(Object JavaDoc key)
305     {
306         return new JispKey( key );
307     }
308     
309     /**
310      * Return the Null JispKey
311      */

312     protected KeyObject getNullKey()
313     {
314         return new JispKey().makeNullKey();
315     }
316     
317     class BTreeObjectEnumeration implements Enumeration JavaDoc
318     {
319         private Object JavaDoc m_Next;
320         private BTreeIterator m_Iterator;
321         private AbstractJispFilesystemStore m_Store;
322         
323         public BTreeObjectEnumeration(BTreeIterator iterator, AbstractJispFilesystemStore store)
324         {
325             m_Iterator = iterator;
326             m_Store = store;
327             
328             // Obtain first element. If any.
329
try
330             {
331                 m_Next = m_Iterator.getKey();
332             }
333             catch (IOException JavaDoc ioe)
334             {
335                 m_Store.getLogger().error("store(..): Exception", ioe);
336                 m_Next = null;
337             }
338         }
339         
340         public boolean hasMoreElements()
341         {
342             return (m_Next != null);
343         }
344         
345         public Object JavaDoc nextElement() throws NoSuchElementException JavaDoc
346         {
347             if (m_Next == null)
348             {
349                 throw new NoSuchElementException JavaDoc();
350             }
351             
352             // Save current element
353
Object JavaDoc tmp = m_Next;
354             
355             // Advance to the next element
356
try
357             {
358                 if (m_Iterator.moveNext())
359                 {
360                     m_Next = m_Iterator.getKey();
361                 }
362                 else
363                 {
364                     // Can't move to the next element - no more elements.
365
m_Next = null;
366                 }
367             }
368             catch (IOException JavaDoc ioe)
369             {
370                 m_Store.getLogger().error("store(..): Exception", ioe);
371                 m_Next = null;
372             }
373             catch (ClassNotFoundException JavaDoc cnfe)
374             {
375                 m_Store.getLogger().error("store(..): Exception", cnfe);
376                 m_Next = null;
377             }
378             
379             // Return the real key
380
return ((JispKey) tmp).getKey();
381         }
382     }
383     
384 }
385
Popular Tags