You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by as...@apache.org on 2004/06/29 03:25:27 UTC
cvs commit: jakarta-turbine-jcs/src/experimental/org/apache/jcs/auxiliary/disk/bdbje BDBJECacheManager.java BDBJECacheFactory.java BDBJECacheAttributes.java BDBJECache.java BDBJE.java
asmuts 2004/06/28 18:25:27
Added: src/experimental/org/apache/jcs/auxiliary/disk/bdbje
BDBJECacheManager.java BDBJECacheFactory.java
BDBJECacheAttributes.java BDBJECache.java
BDBJE.java
Log:
intial check in of an experimental Berkely db plugin. This is not finished.
Revision Changes Path
1.1 jakarta-turbine-jcs/src/experimental/org/apache/jcs/auxiliary/disk/bdbje/BDBJECacheManager.java
Index: BDBJECacheManager.java
===================================================================
package org.apache.jcs.auxiliary.disk.bdbje;
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.Hashtable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jcs.auxiliary.AuxiliaryCache;
import org.apache.jcs.auxiliary.AuxiliaryCacheManager;
/**
* This uses one berkely db for all regions.
*
*/
public class BDBJECacheManager
implements AuxiliaryCacheManager
{
private final static Log log = LogFactory.getLog( BDBJECacheManager.class );
private static int clients;
private static BDBJECacheManager instance;
private Hashtable caches = new Hashtable();
private BDBJECacheAttributes defaultCacheAttributes;
/* right now use one berkely db for all regions */
private BDBJE je;
/**
* Constructor for the IndexedDiskCacheManager object
*
* @param defaultCacheAttributes Default attributes for caches managed by
* the instance.
*/
private BDBJECacheManager( BDBJECacheAttributes defaultCacheAttributes )
{
this.defaultCacheAttributes = defaultCacheAttributes;
je = new BDBJE( defaultCacheAttributes );
if ( log.isDebugEnabled() )
{
log.debug( "Created JE" );
}
}
/**
* Gets the singleton instance of the manager
*
* @param defaultCacheAttributes If the instance has not yet been created,
* it will be initialized with this set of
* default attributes.
* @return The instance value
*/
public static BDBJECacheManager getInstance( BDBJECacheAttributes
defaultCacheAttributes )
{
if ( instance == null )
{
synchronized ( BDBJECacheManager.class )
{
if ( instance == null )
{
instance = new BDBJECacheManager( defaultCacheAttributes );
}
}
}
clients++;
return instance;
}
/**
* Gets an IndexedDiskCache for the supplied name using the default
* attributes.
*
* @see #getCache( IndexedDiskCacheAttributes }
*
* @param cacheName Name that will be used when creating attributes.
* @return A cache.
*/
public AuxiliaryCache getCache( String cacheName )
{
BDBJECacheAttributes cacheAttributes =
( BDBJECacheAttributes ) defaultCacheAttributes.copy();
cacheAttributes.setCacheName( cacheName );
return getCache( cacheAttributes );
}
/**
* Get an IndexedDiskCache for the supplied attributes. Will provide an
* existing cache for the name attribute if one has been created, or will
* create a new cache.
*
* @param cacheAttributes Attributes the cache should have.
* @return A cache, either from the existing set or newly created.
*
*/
public AuxiliaryCache getCache( BDBJECacheAttributes cacheAttributes )
{
AuxiliaryCache cache = null;
String cacheName = cacheAttributes.getCacheName();
log.debug( "Getting cache named: " + cacheName );
synchronized ( caches )
{
// Try to load the cache from the set that have already been
// created. This only looks at the name attribute.
cache = ( AuxiliaryCache ) caches.get( cacheName );
// If it was not found, create a new one using the supplied
// attributes
if ( cache == null )
{
cache = new BDBJECache( cacheAttributes, je );
caches.put( cacheName, cache );
}
}
if ( log.isDebugEnabled() )
{
log.debug( "returning cache = '" + cache + "'" );
}
return cache;
}
/**
* Gets the cacheType attribute of the DiskCacheManager object
*
* @return The cacheType value
*/
public int getCacheType()
{
return DISK_CACHE;
}
}
1.1 jakarta-turbine-jcs/src/experimental/org/apache/jcs/auxiliary/disk/bdbje/BDBJECacheFactory.java
Index: BDBJECacheFactory.java
===================================================================
package org.apache.jcs.auxiliary.disk.bdbje;
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jcs.auxiliary.AuxiliaryCache;
import org.apache.jcs.auxiliary.AuxiliaryCacheAttributes;
import org.apache.jcs.auxiliary.AuxiliaryCacheFactory;
import org.apache.jcs.engine.control.CompositeCache;
/**
* This factory creates Berkeley DB JE disk cache auxiliaries.
*/
public class BDBJECacheFactory
implements AuxiliaryCacheFactory
{
private final static Log log =
LogFactory.getLog( BDBJECacheFactory.class );
private String name;
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.AuxiliaryCacheFactory#createCache(org.apache.jcs.auxiliary.AuxiliaryCacheAttributes, org.apache.jcs.engine.control.CompositeCache)
*/
public AuxiliaryCache createCache(
AuxiliaryCacheAttributes attr,
CompositeCache cache )
{
BDBJECacheAttributes jeattr = ( BDBJECacheAttributes ) attr;
BDBJECacheManager jecm = BDBJECacheManager.getInstance( jeattr );
return jecm.getCache( jeattr );
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.AuxiliaryCacheFactory#setName(java.lang.String)
*/
public void setName( String s )
{
this.name = s;
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.AuxiliaryCacheFactory#getName()
*/
public String getName()
{
return this.name;
}
}
1.1 jakarta-turbine-jcs/src/experimental/org/apache/jcs/auxiliary/disk/bdbje/BDBJECacheAttributes.java
Index: BDBJECacheAttributes.java
===================================================================
package org.apache.jcs.auxiliary.disk.bdbje;
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.jcs.auxiliary.AuxiliaryCacheAttributes;
/**
* Attributes for Berkeley DB JE disk cache auxiliary.
*/
public class BDBJECacheAttributes
implements AuxiliaryCacheAttributes
{
private String cacheName;
private String name;
private String diskPath;
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.AuxiliaryCacheAttributes#setCacheName(java.lang.String)
*/
public void setCacheName( String s )
{
cacheName = s;
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.AuxiliaryCacheAttributes#getCacheName()
*/
public String getCacheName()
{
return cacheName;
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.AuxiliaryCacheAttributes#setName(java.lang.String)
*/
public void setName( String s )
{
name = s;
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.AuxiliaryCacheAttributes#getName()
*/
public String getName()
{
return name;
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.AuxiliaryCacheAttributes#copy()
*/
public AuxiliaryCacheAttributes copy()
{
try
{
return ( AuxiliaryCacheAttributes )this.clone();
}
catch ( Exception e )
{
}
return this;
}
/**
* Sets the diskPath attribute of CacheAttributes object
*
* @param path The new diskPath value
*/
public void setDiskPath( String path )
{
this.diskPath = path.trim();
}
/**
* Gets the diskPath attribute of the CacheAttributes object
*
* @return The diskPath value
*/
public String getDiskPath()
{
return this.diskPath;
}
}
1.1 jakarta-turbine-jcs/src/experimental/org/apache/jcs/auxiliary/disk/bdbje/BDBJECache.java
Index: BDBJECache.java
===================================================================
package org.apache.jcs.auxiliary.disk.bdbje;
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.Serializable;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jcs.auxiliary.disk.AbstractDiskCache;
import org.apache.jcs.engine.behavior.ICacheElement;
/**
* One BDBJECache per regions. For now they share one underlying Berekeley DB.
*/
public class BDBJECache
extends AbstractDiskCache
{
private final static Log log =
LogFactory.getLog( BDBJECache.class );
/* right now we are using one berkely db for all regions */
private BDBJE je;
public BDBJECache( BDBJECacheAttributes attr, BDBJE je )
{
super( attr.getCacheName() );
this.je = je;
if ( log.isDebugEnabled() )
{
log.debug( "constructed BDBJECache" );
}
// Initialization finished successfully, so set alive to true.
alive = true;
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.AuxiliaryCache#getGroupKeys(java.lang.String)
*/
public Set getGroupKeys( String groupName )
{
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.apache.jcs.engine.behavior.ICache#getSize()
*/
public int getSize()
{
// TODO Auto-generated method stub
return 0;
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.disk.AbstractDiskCache#doGet(java.io.Serializable)
*/
protected ICacheElement doGet( Serializable key )
{
if ( log.isDebugEnabled() )
{
log.debug( "doGet, key '" + key + "'" );
}
ICacheElement retVal = null;
try
{
retVal = je.get( key );
}
catch ( Exception e )
{
log.error( e );
}
return retVal;
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.disk.AbstractDiskCache#doUpdate(org.apache.jcs.engine.behavior.ICacheElement)
*/
protected void doUpdate( ICacheElement element )
{
if ( log.isDebugEnabled() )
{
log.debug( "doUpdate, key '" + element.getKey() + "'" );
}
try
{
je.update( element );
}
catch ( Exception e )
{
log.error( e );
}
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.disk.AbstractDiskCache#doRemove(java.io.Serializable)
*/
protected boolean doRemove( Serializable key )
{
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.disk.AbstractDiskCache#doRemoveAll()
*/
protected void doRemoveAll()
{
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.apache.jcs.auxiliary.disk.AbstractDiskCache#doDispose()
*/
protected void doDispose()
{
// TODO Auto-generated method stub
}
}
1.1 jakarta-turbine-jcs/src/experimental/org/apache/jcs/auxiliary/disk/bdbje/BDBJE.java
Index: BDBJE.java
===================================================================
package org.apache.jcs.auxiliary.disk.bdbje;
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.io.Serializable;
import java.io.IOException;
import org.apache.jcs.engine.behavior.ICacheElement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;
/**
* For now this is shared by all regions.
*/
public class BDBJE
{
private final static Log log = LogFactory.getLog( BDBJE.class );
private File envDir;
private Environment coreEnv;
private Database coreDb;
private Database catalogDb;
private StoredClassCatalog catalog;
private BDBJECacheAttributes attributes;
/**
*
*/
public BDBJE( BDBJECacheAttributes attr )
{
attributes = attr;
envDir = new File( attributes.getDiskPath() );
init();
}
private void init()
{
try
{
/* Create a new, transactional database environment */
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setTransactional( true );
envConfig.setAllowCreate( true );
coreEnv = new Environment( envDir, envConfig );
/* Make a database within that environment */
Transaction txn = coreEnv.beginTransaction( null, null );
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional( true );
dbConfig.setAllowCreate( true );
coreDb = coreEnv.openDatabase( txn, "bindingsDb", dbConfig );
/*
* A class catalog database is needed for storing class descriptions
* for the serial binding used below. This avoids storing class
* descriptions redundantly in each record.
*/
DatabaseConfig catalogConfig = new DatabaseConfig();
catalogConfig.setTransactional( true );
catalogConfig.setAllowCreate( true );
catalogDb = coreEnv.openDatabase( txn, "catalogDb", catalogConfig );
catalog = new StoredClassCatalog( catalogDb );
txn.commit();
}
catch ( Exception e )
{
log.error( "Problem init", e );
}
if ( log.isDebugEnabled() )
{
log.debug( "Intitialized BDBJE" );
}
} // end intit
/** Getts an item from the cache. */
public ICacheElement get( Serializable key ) throws IOException
{
if ( log.isDebugEnabled() )
{
log.debug( "get key= '" + key + "'" );
}
ICacheElement ice = null;
try
{
/* DatabaseEntry represents the key and data of each record */
DatabaseEntry searchKey = new DatabaseEntry();
EntryBinding keyBinding =
new SerialBinding( catalog, key.getClass() );
// store for data
// DatabaseEntry dataEntry = new DatabaseEntry();
/*
* Create a serial binding for MyData data objects. Serial bindings
* can be used to store any Serializable object.
*/
EntryBinding dataBinding =
new SerialBinding( catalog, ICacheElement.class );
keyBinding.objectToEntry( key, searchKey );
// retrieve the data
Cursor cursor = coreDb.openCursor( null, null );
//if (cursor.getNext(keyEntry, dataEntry, LockMode.DEFAULT)
// == OperationStatus.SUCCESS) {
//
// searchKey is the key that we want to find in the secondary db.
//DatabaseEntry searchKey = new DatabaseEntry(key..getBytes());
// foundKey and foundData are populated from the primary entry that
// is associated with the secondary db key.
DatabaseEntry foundKey = new DatabaseEntry();
DatabaseEntry foundData = new DatabaseEntry();
OperationStatus retVal =
cursor.getSearchKey( searchKey,
foundData, LockMode.DEFAULT );
if ( retVal == OperationStatus.SUCCESS )
{
//Object keyN = bind.entryToObject(keyEntry);
ice =
( ICacheElement ) dataBinding.entryToObject( foundData );
if ( log.isDebugEnabled() )
{
log.debug( "key=" + key + " ice=" + ice );
}
}
cursor.close();
}
catch ( Exception e )
{
log.error( "Problem updating", e );
}
return ice;
}
/** Puts a cache item to the cache. */
public void update( ICacheElement item ) throws IOException
{
try
{
Transaction txn = coreEnv.beginTransaction( null, null );
/*
* Create a serial binding for MyData data objects. Serial bindings
* can be used to store any Serializable object.
*/
EntryBinding dataBinding =
new SerialBinding( catalog, ICacheElement.class );
EntryBinding keyBinding =
new SerialBinding( catalog, item.getKey().getClass() );
/* DatabaseEntry represents the key and data of each record */
DatabaseEntry dataEntry = new DatabaseEntry();
DatabaseEntry keyEntry = new DatabaseEntry();
dataBinding.objectToEntry( item, dataEntry );
keyBinding.objectToEntry( item.getKey(), keyEntry );
OperationStatus status = coreDb.put( txn, keyEntry, dataEntry );
if ( log.isInfoEnabled() )
{
log.info( "Put key '" + item.getKey() + "' on disk \n status = '" +
status + "'" );
}
/*
* Note that put will throw a DatabaseException when
* error conditions are found such as deadlock.
* However, the status return conveys a variety of
* information. For example, the put might succeed,
* or it might not succeed if the record exists
* and duplicates were not
*/
if ( status != OperationStatus.SUCCESS )
{
throw new DatabaseException(
"Data insertion got status " + status );
}
txn.commit();
}
catch ( Exception e )
{
log.error( e );
}
}
/** Removes the given key from the specified cache. */
public void remove( String cacheName, Serializable key ) throws IOException
{
}
/** Remove all keys from the sepcified cache. */
public void removeAll( String cacheName, long requesterId ) throws
IOException
{
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org