froehlich 2002/10/12 03:36:56
Modified: store/src/java/org/apache/excalibur/store/impl
AbstractFilesystemStore.java
Added: store/src/java/org/apache/excalibur/store/impl
JispStringKey.java AbstractJispFilesystemStore.java
Log:
donated Jisp based store implementation from Cocoon
Revision Changes Path
1.5 +18 -18
jakarta-avalon-excalibur/store/src/java/org/apache/excalibur/store/impl/AbstractFilesystemStore.java
Index: AbstractFilesystemStore.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/store/src/java/org/apache/excalibur/store/impl/AbstractFilesystemStore.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- AbstractFilesystemStore.java 14 Aug 2002 15:33:53 -0000 1.4
+++ AbstractFilesystemStore.java 12 Oct 2002 10:36:55 -0000 1.5
@@ -28,8 +28,8 @@
implements Store, ThreadSafe {
/** The directory repository */
- protected File directoryFile;
- protected volatile String directoryPath;
+ protected File m_directoryFile;
+ protected volatile String m_directoryPath;
/**
* Sets the repository's location
@@ -44,30 +44,30 @@
*/
public void setDirectory(final File directory)
throws IOException {
- this.directoryFile = directory;
+ this.m_directoryFile = directory;
/* Save directory path prefix */
- this.directoryPath = this.getFullFilename(this.directoryFile);
- this.directoryPath += File.separator;
+ this.m_directoryPath = this.getFullFilename(this.m_directoryFile);
+ this.m_directoryPath += File.separator;
/* Does directory exist? */
- if (!this.directoryFile.exists()) {
+ if (!this.m_directoryFile.exists()) {
/* Create it anew */
- if (!this.directoryFile.mkdir()) {
+ if (!this.m_directoryFile.mkdir()) {
throw new IOException(
- "Error creating store directory '" + this.directoryPath + "': ");
+ "Error creating store directory '" + 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())) {
+ if (!(this.m_directoryFile.canRead() && this.m_directoryFile.canWrite())) {
throw new IOException(
- "Directory '" + this.directoryPath + "' is not readable/writable"
+ "Directory '" + this.m_directoryPath + "' is not readable/writable"
);
}
}
@@ -76,7 +76,7 @@
* Returns the repository's full pathname
*/
public String getDirectoryPath() {
- return this.directoryPath;
+ return this.m_directoryPath;
}
/**
@@ -190,7 +190,7 @@
*/
public synchronized Enumeration keys() {
final FSEnumeration enum = new FSEnumeration();
- this.addKeys(enum, this.directoryFile);
+ this.addKeys(enum, this.m_directoryFile);
return enum;
}
@@ -199,11 +199,11 @@
* obtained.
*/
public synchronized int size() {
- return countKeys(this.directoryFile);
+ return countKeys(this.m_directoryFile);
}
protected void addKeys(FSEnumeration enum, File directory) {
- final int subStringBegin = this.directoryFile.getAbsolutePath().length() +
1;
+ final int subStringBegin = this.m_directoryFile.getAbsolutePath().length()
+ 1;
final File[] files = directory.listFiles();
for (int i=0; i<files.length; i++) {
if (files[i].isDirectory()) {
@@ -263,7 +263,7 @@
/* Utility Methods*/
protected File fileFromKey(final Object key) {
- File file = new File(this.directoryFile, this.encode(key.toString()));
+ File file = new File(this.m_directoryFile, this.encode(key.toString()));
File parent = file.getParentFile();
if (parent != null) parent.mkdirs();
return file;
1.1
jakarta-avalon-excalibur/store/src/java/org/apache/excalibur/store/impl/JispStringKey.java
Index: JispStringKey.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.store.impl;
import com.coyotegulch.jisp.KeyObject;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Wrapper class for String Keys to be compatible with the
* Jisp KeyObject.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Gerhard Froehlich</a>
* @version CVS $Id: JispStringKey.java,v 1.1 2002/10/12 10:36:55 froehlich Exp $
*/
final class JispStringKey extends KeyObject {
final static long serialVersionUID = -6894793231339165076L;
private String m_Key;
/**
* Constructor for the JispStringKey object
*/
public JispStringKey() {
m_Key = new String("");
}
/**
* Constructor for the JispStringKey object
*
* @param keyValue the Value of the Key as String
*/
public JispStringKey(String keyValue) {
m_Key = keyValue;
}
/**
* Compares two String Keys
*
* @param key the KeyObject to be compared
* @return 0 if equal, 1 if greater, -1 if less
*/
public int compareTo(KeyObject key) {
if (key instanceof JispStringKey) {
int comp = m_Key.trim().compareTo(((JispStringKey) key).m_Key.trim());
if (comp == 0) {
return KEY_EQUAL;
} else {
if (comp < 0) {
return KEY_LESS;
} else {
return KEY_MORE;
}
}
} else {
return KEY_ERROR;
}
}
/**
* Composes a null Kewy
*
* @return a null Key
*/
public KeyObject makeNullKey() {
return new JispStringKey();
}
/**
* The object implements the writeExternal method to save its contents
* by calling the methods of DataOutput for its primitive values or
* calling the writeObject method of ObjectOutput for objects, strings,
* and arrays.
*
* @param out the stream to write the object to
* @exception IOException
*/
public void writeExternal(ObjectOutput out)
throws IOException {
String outKey;
outKey = new String(m_Key);
out.writeUTF(outKey);
}
/**
* The object implements the readExternal method to restore its contents
* by calling the methods of DataInput for primitive types and readObject
* for objects, strings and arrays. The readExternal method must read the
* values in the same sequence and with the same types as were written by
writeExternal.
*
* @param in the stream to read data from in order to restore the object
* @exception IOException
* @exception ClassNotFoundException
*/
public void readExternal(ObjectInput in)
throws IOException, ClassNotFoundException {
m_Key = in.readUTF();
}
/**
* Overrides the toString() method
*
* @return the Key as String
*/
public String toString() {
return m_Key;
}
}
1.1
jakarta-avalon-excalibur/store/src/java/org/apache/excalibur/store/impl/AbstractJispFilesystemStore.java
Index: AbstractJispFilesystemStore.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.excalibur.store.impl;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.excalibur.store.Store;
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;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Vector;
/**
* This store is based on the Jisp library
* (http://www.coyotegulch.com/jisp/index.html). This store uses B-Tree indexes
* to access variable-length serialized data stored in files.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Gerhard Froehlich</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Vadim Gritsenko</a>
* @version CVS $Id: AbstractJispFilesystemStore.java,v 1.1 2002/10/12 10:36:55
froehlich Exp $
*/
public abstract class AbstractJispFilesystemStore
extends AbstractLogEnabled
implements Store, ThreadSafe, Initializable {
/**
* The directory repository
*/
protected File m_directoryFile;
protected volatile String m_directoryPath;
/**
* The database
*/
protected IndexedObjectDatabase m_Database;
protected BTreeIndex m_Index;
/**
* Sets the repository's location
*/
public void setDirectory(final String directory)
throws IOException {
this.setDirectory(new File(directory));
}
/**
* Sets the repository's location
*/
public void setDirectory(final File directory)
throws IOException {
this.m_directoryFile = directory;
/* Save directory path prefix */
this.m_directoryPath = this.getFullFilename(this.m_directoryFile);
this.m_directoryPath += File.separator;
/* Does directory exist? */
if (!this.m_directoryFile.exists()) {
/* Create it anew */
if (!this.m_directoryFile.mkdir()) {
throw new IOException(
"Error creating store directory '" + this.m_directoryPath + "': ");
}
}
/* Is given file actually a directory? */
if (!this.m_directoryFile.isDirectory()) {
throw new IOException("'" + this.m_directoryPath + "' is not a
directory");
}
/* Is directory readable and writable? */
if (!(this.m_directoryFile.canRead() && this.m_directoryFile.canWrite())) {
throw new IOException(
"Directory '" + this.m_directoryPath + "' is not readable/writable"
);
}
}
/**
* Returns the repository's full pathname
*
* @return the directory as String
*/
public String getDirectoryPath() {
return this.m_directoryPath;
}
/**
* Returns a Object from the store associated with the Key Object
*
* @param key the Key object
* @return the Object associated with Key Object
*/
public synchronized Object get(Object key) {
Object value = null;
try {
value = m_Database.read(this.wrapKeyObject(key), m_Index);
if (getLogger().isDebugEnabled()) {
if (value != null) {
getLogger().debug("Found key: " + key);
} else {
getLogger().debug("NOT Found key: " + key);
}
}
} catch (Exception e) {
getLogger().error("get(..): Exception", e);
}
return value;
}
/**
* Store the given object in the indexed data file.
*
* @param key the key object
* @param value the value object
* @exception IOException
*/
public synchronized void store(Object key, Object value)
throws IOException {
if (getLogger().isDebugEnabled()) {
this.getLogger().debug("store(): Store file with key: "
+ key.toString());
this.getLogger().debug("store(): Store file with value: "
+ value.toString());
}
if (value instanceof Serializable) {
try {
KeyObject[] keyArray = new KeyObject[1];
keyArray[0] = this.wrapKeyObject(key);
m_Database.write(keyArray, (Serializable) value);
} catch (Exception e) {
this.getLogger().error("store(..): Exception", e);
}
} else {
throw new IOException("Object not Serializable");
}
}
/**
* Holds the given object in the indexed data file.
*
* @param key the key object
* @param value the value object
* @exception IOException
*/
public synchronized void hold(Object key, Object value)
throws IOException {
this.store(key, value);
}
/**
* Frees some values of the data file.<br>
* TODO: implementation
*/
public synchronized void free() {
//TODO: implementation
}
/**
* Clear the Store of all elements
*/
public synchronized void clear() {
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);
}
}
/**
* Removes a value from the data file with the given key.
*
* @param key the key object
*/
public synchronized void remove(Object key) {
if (getLogger().isDebugEnabled()) {
this.getLogger().debug("remove(..) Remove item");
}
try {
KeyObject[] keyArray = new KeyObject[1];
keyArray[0] = this.wrapKeyObject(key);
m_Database.remove(keyArray);
} catch (KeyNotFound ignore) {
} catch (Exception e) {
this.getLogger().error("remove(..): Exception", e);
}
}
/**
* Test if the the index file contains the given key
*
* @param key the key object
* @return true if Key exists and false if not
*/
public synchronized boolean containsKey(Object key) {
long res = -1;
try {
res = m_Index.findKey(this.wrapKeyObject(key));
if (getLogger().isDebugEnabled()) {
this.getLogger().debug("containsKey(..): res=" + res);
}
} catch (KeyNotFound ignore) {
} catch (Exception e) {
this.getLogger().error("containsKey(..): Exception", e);
}
if (res > 0) {
return true;
} else {
return false;
}
}
/**
* Returns a Enumeration of all Keys in the indexed file.<br>
*
* @return Enumeration Object with all existing keys
*/
public Enumeration keys() {
BTreeObjectEnumeration enum = new
BTreeObjectEnumeration(m_Database.createIterator(m_Index),this);
return enum;
}
public int size() {
int cnt = 0;
BTreeObjectEnumeration enum = new
BTreeObjectEnumeration(m_Database.createIterator(m_Index),this);
while(enum.hasMoreElements()) {
cnt++;
}
return cnt;
}
/**
* This method wraps around the key Object a Jisp KeyObject.
*
* @param key the key object
* @return the wrapped key object
*/
private KeyObject wrapKeyObject(Object key) {
// TODO: Implementation of Integer and Long keys
String skey = String.valueOf(key);
return new JispStringKey(key.toString());
}
/**
* Get the complete filename corresponding to a (typically relative)
* <code>File</code>.
* This method accounts for the possibility of an error in getting
* the filename's <i>canonical</i> path, returning the io/error-safe
* <i>absolute</i> form instead
*
* @param file The file
* @return The file's absolute filename
*/
public String getFullFilename(File file)
{
try
{
return file.getCanonicalPath();
}
catch (Exception e)
{
return file.getAbsolutePath();
}
}
class BTreeObjectEnumeration implements Enumeration {
private BTreeObjectIterator m_Iterator;
private AbstractJispFilesystemStore m_Store;
public BTreeObjectEnumeration(BTreeObjectIterator iterator,
AbstractJispFilesystemStore 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);
}
// make a string out of it (JispStringKey is not usefull here)
return tmp.toString();
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>