org.jboss.cache.loader.jdbm
Class JdbmCacheLoader

java.lang.Object
  extended by org.jboss.cache.loader.jdbm.JdbmCacheLoader
All Implemented Interfaces:
CacheLoader, ExtendedCacheLoader, org.jboss.system.Service

public class JdbmCacheLoader
extends java.lang.Object
implements ExtendedCacheLoader

A persistent CacheLoader based on the JDBM project. See http://jdbm.sourceforge.net/ . Does not support transaction isolation.

The configuration string format is:

environmentDirectoryName[#databaseName]

where databaseName, if omitted, defaults to the ClusterName property of the TreeCache.

Data is sorted out like:

/ = N
/node1 = N
/node1/K/k1 = v1
/node1/K/k2 = v2
/node2 = N
/node2/node3 = N
/node2/node3/K/k1 = v1
/node2/node3/K/k2 = v2
/node2/node4 = N
N represents a node, K represents a key block. k and v represent key/value pairs.

TODO the browse operations lock the entire tree; eventually the JDBM team plans to fix this.

Version:
$Id: JdbmCacheLoader.java,v 1.9 2006/05/24 05:47:27 bstansberry Exp $
Author:
Elias Ross

Constructor Summary
JdbmCacheLoader()
           
 
Method Summary
 void commit(java.lang.Object tx)
          Commits a transaction.
 void create()
           
 void destroy()
           
 void dump()
          Dumps the tree to debug.
 void dump(java.lang.Object key)
          Dumps the tree past the key to debug.
 boolean exists(Fqn name)
          Returns whether the given node exists.
 java.util.Map get(Fqn name)
          Returns a map containing all key-value pairs for the given FQN, or null if the node is not present.
 java.util.Set getChildrenNames(Fqn name)
          Returns an unmodifiable set of relative children names, or returns null if the parent node is not found or if no children are found.
 byte[] loadEntireState()
          Fetches the entire state for this cache from secondary storage (disk, DB) and returns it as a byte buffer.
 byte[] loadState(Fqn subtree)
          Export the contents of the databases as a byte array.
 void prepare(java.lang.Object tx, java.util.List modifications, boolean onePhase)
          Applies and commits the given modifications in one transaction.
 void put(Fqn name, java.util.Map values)
          Stores a map of key-values for a given FQN, but does not delete existing key-value pairs (that is, it does not erase).
 java.lang.Object put(Fqn name, java.lang.Object key, java.lang.Object value)
          Stores a single FQN-key-value record.
 void put(java.util.List modifications)
          Applies the given modifications.
 void remove(Fqn name)
          Deletes the node for a given FQN and all its descendent nodes.
 java.lang.Object remove(Fqn name, java.lang.Object key)
          Deletes a single FQN-key-value record.
 void removeData(Fqn name)
          Clears the map for the given node, but does not remove the node.
 void rollback(java.lang.Object tx)
          Removes transaction in progress.
 void setCache(TreeCache c)
          Sets the TreeCache owner of this cache loader.
 void setConfig(java.util.Properties props)
          Sets the configuration string for this cache loader.
 void setRegionManager(RegionManager manager)
          Sets the RegionManager this object should use to manage marshalling/unmarshalling of different regions using different classloaders.
 void start()
          Opens the environment and the database specified by the configuration string.
 void stop()
          Closes the databases and environment, and nulls references to them.
 void storeEntireState(byte[] state)
          Replace the contents of the databases with the given exported data.
 void storeState(byte[] state, Fqn subtree)
          Replace the contents of the databases with the given exported data.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JdbmCacheLoader

public JdbmCacheLoader()
Method Detail

create

public void create()
            throws java.lang.Exception
Specified by:
create in interface org.jboss.system.Service
Throws:
java.lang.Exception

destroy

public void destroy()
Specified by:
destroy in interface org.jboss.system.Service

start

public void start()
           throws java.lang.Exception
Opens the environment and the database specified by the configuration string. The environment and databases are created if necessary.

Specified by:
start in interface org.jboss.system.Service
Throws:
java.lang.Exception

stop

public void stop()
Closes the databases and environment, and nulls references to them.

Specified by:
stop in interface org.jboss.system.Service

setConfig

public void setConfig(java.util.Properties props)
Sets the configuration string for this cache loader.

Specified by:
setConfig in interface CacheLoader
Parameters:
props - a collection of configuration properties

setCache

public void setCache(TreeCache c)
Sets the TreeCache owner of this cache loader.

Specified by:
setCache in interface CacheLoader
Parameters:
c - The cache on which this loader works

getChildrenNames

public java.util.Set getChildrenNames(Fqn name)
                               throws java.lang.Exception
Returns an unmodifiable set of relative children names, or returns null if the parent node is not found or if no children are found. This is a fairly expensive operation, and is assumed to be performed by browser applications. Calling this method as part of a run-time transaction is not recommended.

Specified by:
getChildrenNames in interface CacheLoader
Parameters:
name - The FQN of the parent
Returns:
Set a set of children. Returns null if no children nodes are present, or the parent is not present
Throws:
java.lang.Exception

get

public java.util.Map get(Fqn name)
                  throws java.lang.Exception
Returns a map containing all key-value pairs for the given FQN, or null if the node is not present. This operation is always non-transactional, even in a transactional environment.

Specified by:
get in interface CacheLoader
Returns:
Map keys and values for the given node. Returns null if the node is not found. If the node is found but has no attributes, this method returns an empty Map.
Throws:
java.lang.Exception

exists

public boolean exists(Fqn name)
               throws java.io.IOException
Returns whether the given node exists.

Specified by:
exists in interface CacheLoader
Returns:
true if node exists, false otherwise
Throws:
java.io.IOException

put

public java.lang.Object put(Fqn name,
                            java.lang.Object key,
                            java.lang.Object value)
                     throws java.lang.Exception
Stores a single FQN-key-value record. Intended to be used in a non-transactional environment, but will use auto-commit in a transactional environment.

Specified by:
put in interface CacheLoader
Throws:
java.lang.Exception

put

public void put(Fqn name,
                java.util.Map values)
         throws java.lang.Exception
Stores a map of key-values for a given FQN, but does not delete existing key-value pairs (that is, it does not erase). Intended to be used in a non-transactional environment, but will use auto-commit in a transactional environment.

Specified by:
put in interface CacheLoader
Parameters:
name - The fully qualified name of the node
values - A Map of attributes. Can be null
Throws:
java.lang.Exception

put

public void put(java.util.List modifications)
         throws java.lang.Exception
Applies the given modifications. Intended to be used in a non-transactional environment, but will use auto-commit in a transactional environment.

Specified by:
put in interface CacheLoader
Parameters:
modifications - A List of modifications
Throws:
java.lang.Exception

remove

public void remove(Fqn name)
            throws java.lang.Exception
Deletes the node for a given FQN and all its descendent nodes. Intended to be used in a non-transactional environment, but will use auto-commit in a transactional environment.

Specified by:
remove in interface CacheLoader
Throws:
java.lang.Exception

remove

public java.lang.Object remove(Fqn name,
                               java.lang.Object key)
                        throws java.lang.Exception
Deletes a single FQN-key-value record. Intended to be used in a non-transactional environment, but will use auto-commit in a transactional environment.

Specified by:
remove in interface CacheLoader
Throws:
java.lang.Exception

removeData

public void removeData(Fqn name)
                throws java.lang.Exception
Clears the map for the given node, but does not remove the node.

Specified by:
removeData in interface CacheLoader
Throws:
java.lang.Exception

prepare

public void prepare(java.lang.Object tx,
                    java.util.List modifications,
                    boolean onePhase)
             throws java.lang.Exception
Applies and commits the given modifications in one transaction.

Specified by:
prepare in interface CacheLoader
Parameters:
tx - The transaction, just used as a hashmap key
modifications - List, a list of all modifications within the given transaction
onePhase - Persist immediately and (for example) commit the local JDBC transaction as well. When true, we won't get a CacheLoader.commit(Object) or CacheLoader.rollback(Object) method call later
Throws:
java.lang.Exception

commit

public void commit(java.lang.Object tx)
            throws java.lang.Exception
Commits a transaction.

Specified by:
commit in interface CacheLoader
Parameters:
tx - transaction to commit
Throws:
java.lang.Exception

rollback

public void rollback(java.lang.Object tx)
Removes transaction in progress.

Specified by:
rollback in interface CacheLoader
Parameters:
tx - transaction to roll back

loadEntireState

public byte[] loadEntireState()
                       throws java.lang.Exception
Description copied from interface: CacheLoader
Fetches the entire state for this cache from secondary storage (disk, DB) and returns it as a byte buffer. This is for initialization of a new cache from a remote cache. The new cache would then call CacheLoader.storeEntireState(byte[]).

Specified by:
loadEntireState in interface CacheLoader
Throws:
java.lang.Exception

loadState

public byte[] loadState(Fqn subtree)
                 throws java.lang.Exception
Export the contents of the databases as a byte array. If the databases are empty a zero-lenth array is returned.

Specified by:
loadState in interface ExtendedCacheLoader
Parameters:
subtree - Fqn naming the root (i.e. highest level parent) node of the subtree for which state is requested.
Throws:
java.lang.Exception
See Also:
TreeCache.activateRegion(String)

storeEntireState

public void storeEntireState(byte[] state)
                      throws java.lang.Exception
Replace the contents of the databases with the given exported data. If state is null or zero-length, the databases will be cleared.

Specified by:
storeEntireState in interface CacheLoader
Throws:
java.lang.Exception

storeState

public void storeState(byte[] state,
                       Fqn subtree)
                throws java.lang.Exception
Replace the contents of the databases with the given exported data. If state is null or zero-length, the databases will be cleared.

Specified by:
storeState in interface ExtendedCacheLoader
Parameters:
state - the state to store
subtree - Fqn naming the root (i.e. highest level parent) node of the subtree included in state. If the Fqns of the data included in state are not already children of subtree, then their Fqns should be altered to make them children of subtree before they are persisted.
Throws:
java.lang.Exception

dump

public void dump()
          throws java.io.IOException
Dumps the tree to debug.

Throws:
java.io.IOException

dump

public void dump(java.lang.Object key)
          throws java.io.IOException
Dumps the tree past the key to debug.

Throws:
java.io.IOException

setRegionManager

public void setRegionManager(RegionManager manager)
Description copied from interface: ExtendedCacheLoader
Sets the RegionManager this object should use to manage marshalling/unmarshalling of different regions using different classloaders.

NOTE: This method is only intended to be used by the TreeCache instance this cache loader is associated with.

Specified by:
setRegionManager in interface ExtendedCacheLoader
Parameters:
manager - the region manager to use, or null.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright 1998-2005 JBoss Inc . All Rights Reserved.