froehlich 2002/09/20 11:16:10 Modified: src/java/org/apache/cocoon/components/store JispFilesystemStore.java Log: added missing clear() and keys() method and a little bit refactoring Revision Changes Path 1.7 +120 -62 xml-cocoon2/src/java/org/apache/cocoon/components/store/JispFilesystemStore.java Index: JispFilesystemStore.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/store/JispFilesystemStore.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- JispFilesystemStore.java 4 Sep 2002 10:20:24 -0000 1.6 +++ JispFilesystemStore.java 20 Sep 2002 18:16:10 -0000 1.7 @@ -64,6 +64,7 @@ import org.apache.cocoon.util.IOUtils; import com.coyotegulch.jisp.BTreeIndex; +import com.coyotegulch.jisp.BTreeObjectIterator; import com.coyotegulch.jisp.IndexedObjectDatabase; import com.coyotegulch.jisp.KeyNotFound; import com.coyotegulch.jisp.KeyObject; @@ -90,24 +91,24 @@ Initializable, Parameterizable { - protected File workDir; - protected File cacheDir; + protected File m_workDir; + protected File m_cacheDir; /** * The directory repository */ - protected File directoryFile; - protected volatile String directoryPath; + protected File m_directoryFile; + protected volatile String m_directoryPath; /** * The database */ - private File databaseFile; - private File indexFile; + private File m_databaseFile; + private File m_indexFile; - private int mOrder; - private IndexedObjectDatabase mDatabase; - private BTreeIndex mIndex; + private int m_Order; + private IndexedObjectDatabase m_Database; + private BTreeIndex m_Index; /** * Sets the repository's location @@ -129,28 +130,28 @@ public void setDirectory(final File directory) throws IOException { - this.directoryFile = directory; + this.m_directoryFile = directory; /* Save directory path prefix */ - this.directoryPath = IOUtils.getFullFilename(this.directoryFile); - this.directoryPath += File.separator; + this.m_directoryPath = IOUtils.getFullFilename(this.m_directoryFile); + this.m_directoryPath += File.separator; - if (!this.directoryFile.exists()) { + if (!this.m_directoryFile.exists()) { /* Create it new */ - if (!this.directoryFile.mkdir()) { + if (!this.m_directoryFile.mkdir()) { throw new IOException("Error creating store directory '" + - this.directoryPath + "'"); + this.m_directoryPath + "'"); } } /* Is given file actually a directory? */ - if (!this.directoryFile.isDirectory()) { - throw new IOException("'" + this.directoryPath + "' is not a directory"); + if (!this.m_directoryFile.isDirectory()) { + throw new IOException("'" + this.m_directoryPath + "' is not a directory"); } /* Is directory readable and writable? */ - if (!(this.directoryFile.canRead() && this.directoryFile.canWrite())) { - throw new IOException("Directory '" + this.directoryPath + + if (!(this.m_directoryFile.canRead() && this.m_directoryFile.canWrite())) { + throw new IOException("Directory '" + this.m_directoryPath + "' is not readable/writable"); } } @@ -162,8 +163,8 @@ * @exception ContextException */ public void contextualize(final Context context) throws ContextException { - this.workDir = (File)context.get(Constants.CONTEXT_WORK_DIR); - this.cacheDir = (File)context.get(Constants.CONTEXT_CACHE_DIR); + this.m_workDir = (File)context.get(Constants.CONTEXT_WORK_DIR); + this.m_cacheDir = (File)context.get(Constants.CONTEXT_CACHE_DIR); } /** @@ -172,7 +173,7 @@ * <UL> * <LI> datafile = the name of the data file (Default: cocoon.dat) * </LI> - * <LI> indexfile = the name of the index file (Default: cocoon.idx) + * <LI> m_indexFile = the name of the index file (Default: cocoon.idx) * </LI> * <LI> order = The page size of the B-Tree</LI> * </UL> @@ -185,22 +186,22 @@ try { if (params.getParameterAsBoolean("use-cache-directory", false)) { if (this.getLogger().isDebugEnabled()) - getLogger().debug("Using cache directory: " + cacheDir); - setDirectory(cacheDir); + getLogger().debug("Using cache directory: " + m_cacheDir); + setDirectory(m_cacheDir); } else if (params.getParameterAsBoolean("use-work-directory", false)) { if (this.getLogger().isDebugEnabled()) - getLogger().debug("Using work directory: " + workDir); - setDirectory(workDir); + getLogger().debug("Using work directory: " + m_workDir); + setDirectory(m_workDir); } else if (params.getParameter("directory", null) != null) { String dir = params.getParameter("directory"); - dir = IOUtils.getContextFilePath(workDir.getPath(), dir); + dir = IOUtils.getContextFilePath(m_workDir.getPath(), dir); if (this.getLogger().isDebugEnabled()) getLogger().debug("Using directory: " + dir); setDirectory(new File(dir)); } else { try { // Default - setDirectory(workDir); + setDirectory(m_workDir); } catch (IOException e) { // Ignored } @@ -210,16 +211,16 @@ } String databaseName = params.getParameter("datafile", "cocoon.dat"); - String indexName = params.getParameter("indexfile", "cocoon.idx"); - mOrder = params.getParameterAsInteger("order", 301); + String indexName = params.getParameter("m_indexFile", "cocoon.idx"); + m_Order = params.getParameterAsInteger("order", 301); if (getLogger().isDebugEnabled()) { this.getLogger().debug("Database file name = " + databaseName); this.getLogger().debug("Index file name = " + indexName); - this.getLogger().debug("Order=" + mOrder); + this.getLogger().debug("Order=" + m_Order); } - databaseFile = new File(directoryFile, databaseName); - indexFile = new File(directoryFile, indexName); + m_databaseFile = new File(m_directoryFile, databaseName); + m_indexFile = new File(m_directoryFile, indexName); } /** @@ -231,21 +232,21 @@ } try { - if (databaseFile.exists()) { + if (m_databaseFile.exists()) { if (getLogger().isDebugEnabled()) { this.getLogger().debug("initialize(): Datafile exists"); } - mDatabase = new IndexedObjectDatabase(databaseFile.toString(), false); - mIndex = new BTreeIndex(indexFile.toString()); - mDatabase.attachIndex(mIndex); + m_Database = new IndexedObjectDatabase(m_databaseFile.toString(), false); + m_Index = new BTreeIndex(m_indexFile.toString()); + m_Database.attachIndex(m_Index); } else { if (getLogger().isDebugEnabled()) { this.getLogger().debug("initialize(): Datafile does not exist"); } - mDatabase = new IndexedObjectDatabase(databaseFile.toString(), false); - mIndex = new BTreeIndex(indexFile.toString(), - mOrder, new JispStringKey(), false); - mDatabase.attachIndex(mIndex); + m_Database = new IndexedObjectDatabase(m_databaseFile.toString(), false); + m_Index = new BTreeIndex(m_indexFile.toString(), + m_Order, new JispStringKey(), false); + m_Database.attachIndex(m_Index); } } catch (KeyNotFound ignore) { } catch (Exception e) { @@ -259,7 +260,7 @@ * @return the directory as String */ public String getDirectoryPath() { - return this.directoryPath; + return this.m_directoryPath; } /** @@ -268,10 +269,10 @@ * @param key the Key object * @return the Object associated with Key Object */ - public Object get(Object key) { + public synchronized Object get(Object key) { Object value = null; try { - value = mDatabase.read(this.wrapKeyObject(key), mIndex); + value = m_Database.read(this.wrapKeyObject(key), m_Index); if (getLogger().isDebugEnabled()) { if (value != null) { getLogger().debug("Found key: " + key); @@ -292,7 +293,7 @@ * @param value the value object * @exception IOException */ - public void store(Object key, Object value) + public synchronized void store(Object key, Object value) throws IOException { if (getLogger().isDebugEnabled()) { @@ -306,7 +307,7 @@ try { KeyObject[] keyArray = new KeyObject[1]; keyArray[0] = this.wrapKeyObject(key); - mDatabase.write(keyArray, (Serializable) value); + m_Database.write(keyArray, (Serializable) value); } catch (Exception e) { this.getLogger().error("store(..): Exception", e); } @@ -322,7 +323,7 @@ * @param value the value object * @exception IOException */ - public void hold(Object key, Object value) + public synchronized void hold(Object key, Object value) throws IOException { this.store(key, value); } @@ -331,7 +332,7 @@ * Frees some values of the data file.<br> * TODO: implementation */ - public void free() { + public synchronized void free() { //TODO: implementation } @@ -339,12 +340,18 @@ * Clear the Store of all elements */ public synchronized void clear() { - Enumeration enum = this.keys(); - while (enum.hasMoreElements()) { - Object key = enum.nextElement(); - if (null != key) { - this.remove(key); + BTreeObjectEnumeration enum = new BTreeObjectEnumeration(m_Database.createIterator(m_Index),this); + + if (getLogger().isDebugEnabled()) { + this.getLogger().debug("clear(): Clearing the database "); + } + + while(enum.hasMoreElements()) { + Object tmp = enum.nextElement(); + if (getLogger().isDebugEnabled()) { + this.getLogger().debug("clear(): Removing key: " + tmp.toString()); } + this.remove(tmp); } } @@ -353,7 +360,7 @@ * * @param key the key object */ - public void remove(Object key) { + public synchronized void remove(Object key) { if (getLogger().isDebugEnabled()) { this.getLogger().debug("remove(..) Remove item"); } @@ -361,7 +368,7 @@ try { KeyObject[] keyArray = new KeyObject[1]; keyArray[0] = this.wrapKeyObject(key); - mDatabase.remove(keyArray); + m_Database.remove(keyArray); } catch (KeyNotFound ignore) { } catch (Exception e) { this.getLogger().error("remove(..): Exception", e); @@ -374,11 +381,11 @@ * @param key the key object * @return true if Key exists and false if not */ - public boolean containsKey(Object key) { + public synchronized boolean containsKey(Object key) { long res = -1; try { - res = mIndex.findKey(this.wrapKeyObject(key)); + res = m_Index.findKey(this.wrapKeyObject(key)); if (getLogger().isDebugEnabled()) { this.getLogger().debug("containsKey(..): res=" + res); } @@ -400,13 +407,19 @@ * @return Enumeration Object with all existing keys */ public Enumeration keys() { - // TODO: Implementation - throw new RuntimeException("JispFilesystemStore does not implement method keys()."); + BTreeObjectEnumeration enum = new BTreeObjectEnumeration(m_Database.createIterator(m_Index),this); + return enum; } public int size() { - // TODO: Unsupported - return 0; + int cnt = 0; + + BTreeObjectEnumeration enum = new BTreeObjectEnumeration(m_Database.createIterator(m_Index),this); + + while(enum.hasMoreElements()) { + cnt++; + } + return cnt; } /** @@ -419,5 +432,50 @@ // TODO: Implementation of Integer and Long keys String skey = String.valueOf(key); return new JispStringKey(key.toString()); + } + + class BTreeObjectEnumeration implements Enumeration { + private BTreeObjectIterator m_Iterator; + private JispFilesystemStore m_Store; + + public BTreeObjectEnumeration(BTreeObjectIterator iterator, JispFilesystemStore store) { + m_Iterator = iterator; + m_Store = store; + } + + public boolean hasMoreElements() { + boolean hasMore = false; + Object tmp = null; + + try { + tmp = m_Iterator.getKey(); + + if(m_Iterator.moveNext()) { + hasMore = true; + } + + /* resets iterator to the old state **/ + m_Iterator.moveTo((KeyObject)tmp); + } catch (IOException ioe) { + m_Store.getLogger().error("store(..): Exception", ioe); + } catch (ClassNotFoundException cnfe) { + m_Store.getLogger().error("store(..): Exception", cnfe); + } + return hasMore; + } + + public Object nextElement() { + Object tmp = null; + + try { + tmp = m_Iterator.getKey(); + m_Iterator.moveNext(); + } catch (IOException ioe) { + m_Store.getLogger().error("store(..): Exception", ioe); + } catch (ClassNotFoundException cnfe) { + m_Store.getLogger().error("store(..): Exception", cnfe); + } + return tmp; + } } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]