org.jboss.cache.loader
Class FileCacheLoader

java.lang.Object
  extended by org.jboss.cache.loader.FileCacheLoader
All Implemented Interfaces:
CacheLoader, ExtendedCacheLoader, org.jboss.system.Service
Direct Known Subclasses:
FileExtendedCacheLoader

public class FileCacheLoader
extends java.lang.Object
implements ExtendedCacheLoader

Simple file-based CacheLoader implementation. Nodes are directories, attributes of a node is a file in the directory

Version:
$Id: FileCacheLoader.java,v 1.15.2.2 2007/06/29 13:33:38 gzamarreno Exp $
Author:
Bela Ban, Galder Zamarreno

Field Summary
static java.lang.String DATA
          TreeCache data file.
static java.lang.String DIR_SUFFIX
          TreeCache directory suffix.
protected  StripedLock lock
           
 
Constructor Summary
FileCacheLoader()
           
 
Method Summary
 void commit(java.lang.Object tx)
          Commits the transaction.
 void create()
           
 void destroy()
           
 boolean exists(Fqn fqn)
          Returns true if the CacheLoader has a node with a Fqn.
 java.util.Map get(Fqn fqn)
          Returns all keys and values from the persistent store, given a fully qualified name.
 java.util.Set getChildrenNames(Fqn fqn)
          Returns a set of children node names as Strings.
protected  java.util.Map loadAttributes(Fqn fqn)
           
 byte[] loadEntireState()
          Loads the entire state from the filesystem and returns it as a byte buffer.
 byte[] loadState(Fqn subtree)
          Fetch a portion of the state for this cache from secondary storage (disk, DB) and return it as a byte buffer.
protected  void loadStateFromFilessystem(Fqn fqn, java.io.ObjectOutputStream out)
          Do a preorder traversal: visit the node first, then the node's children
 void prepare(java.lang.Object tx, java.util.List modifications, boolean one_phase)
          Prepares a list of modifications.
 void put(Fqn fqn, java.util.Map attributes)
          Puts all entries of the map into the existing map of the given node, overwriting existing keys, but not clearing the existing map before insertion.
 void put(Fqn fqn, java.util.Map attributes, boolean erase)
           
 java.lang.Object put(Fqn fqn, java.lang.Object key, java.lang.Object value)
          Puts a key and value into the attribute map of a given node.
 void put(java.util.List modifications)
          Applies all modifications to the backend store.
 void remove(Fqn fqn)
          Removes the given node and all its subnodes.
 java.lang.Object remove(Fqn fqn, java.lang.Object key)
          Removes the given key and value from the attributes of the given node.
 void removeData(Fqn fqn)
          Removes all attributes from a given node, but doesn't delete the node itself or any subnodes.
 void rollback(java.lang.Object tx)
          Rolls the transaction back.
 void setCache(TreeCache c)
          Sets the TreeCache that is maintaining this CacheLoader.
 void setConfig(java.util.Properties props)
          Sets the configuration.
 void setRegionManager(RegionManager manager)
          Sets the RegionManager this object should use to manage marshalling/unmarshalling of different regions using different classloaders.
 void start()
           
 void stop()
           
protected  void storeAttributes(Fqn fqn, java.util.Map attrs)
           
 void storeEntireState(byte[] state)
          Stores the state given as a byte buffer to the filesystem.
 void storeState(byte[] state, Fqn subtree)
          Store the given portion of the cache tree's state in secondary storage.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lock

protected final StripedLock lock

DATA

public static final java.lang.String DATA
TreeCache data file.

See Also:
Constant Field Values

DIR_SUFFIX

public static final java.lang.String DIR_SUFFIX
TreeCache directory suffix.

See Also:
Constant Field Values
Constructor Detail

FileCacheLoader

public FileCacheLoader()
Method Detail

setConfig

public void setConfig(java.util.Properties props)
Description copied from interface: CacheLoader
Sets the configuration. This is called before Service.create() and Service.start().

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

setCache

public void setCache(TreeCache c)
Description copied from interface: CacheLoader
Sets the TreeCache that is maintaining this CacheLoader. This method allows this CacheLoader to invoke methods on TreeCache, including fetching additional configuration information. This method is called be called after the CacheLoader instance has been constructed.

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

create

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

start

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

stop

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

destroy

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

getChildrenNames

public java.util.Set getChildrenNames(Fqn fqn)
                               throws java.lang.Exception
Description copied from interface: CacheLoader
Returns a set of children node names as Strings. All names are relative to this parent Fqn. Returns null if the named node is not found or there are no children. The returned set must not be modifiable. Implementors can use Collections.unmodifiableSet(Set) to make the set unmodifiable.

Specified by:
getChildrenNames in interface CacheLoader
Parameters:
fqn - 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 fqn)
                  throws java.lang.Exception
Description copied from interface: CacheLoader
Returns all keys and values from the persistent store, given a fully qualified name. NOTE that the expected return value of this method has changed from JBossCache 1.2.x and before! This will affect cache loaders written prior to JBossCache 1.3.0 and such implementations should be checked for compliance with the behaviour expected.

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 fqn)
               throws java.lang.Exception
Description copied from interface: CacheLoader
Returns true if the CacheLoader has a node with a Fqn.

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

put

public java.lang.Object put(Fqn fqn,
                            java.lang.Object key,
                            java.lang.Object value)
                     throws java.lang.Exception
Description copied from interface: CacheLoader
Puts a key and value into the attribute map of a given node. If the node does not exist, all parent nodes from the root down are created automatically. Returns the old value.

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

put

public void put(Fqn fqn,
                java.util.Map attributes)
         throws java.lang.Exception
Description copied from interface: CacheLoader
Puts all entries of the map into the existing map of the given node, overwriting existing keys, but not clearing the existing map before insertion. This is the same behavior as Map.putAll(java.util.Map). If the node does not exist, all parent nodes from the root down are created automatically

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

put

public void put(Fqn fqn,
                java.util.Map attributes,
                boolean erase)
         throws java.lang.Exception
Throws:
java.lang.Exception

put

public void put(java.util.List modifications)
         throws java.lang.Exception
Description copied from interface: CacheLoader
Applies all modifications to the backend store. Changes may be applied in a single operation.

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

remove

public java.lang.Object remove(Fqn fqn,
                               java.lang.Object key)
                        throws java.lang.Exception
Description copied from interface: CacheLoader
Removes the given key and value from the attributes of the given node. Does nothing if the node doesn't exist Returns the removed value.

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

remove

public void remove(Fqn fqn)
            throws java.lang.Exception
Description copied from interface: CacheLoader
Removes the given node and all its subnodes.

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

removeData

public void removeData(Fqn fqn)
                throws java.lang.Exception
Description copied from interface: CacheLoader
Removes all attributes from a given node, but doesn't delete the node itself or any subnodes.

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

prepare

public void prepare(java.lang.Object tx,
                    java.util.List modifications,
                    boolean one_phase)
             throws java.lang.Exception
Description copied from interface: CacheLoader
Prepares a list of modifications. For example, for a DB-based CacheLoader:
  1. Create a local (JDBC) transaction
  2. Associate the local transaction with tx (tx is the key)
  3. Execute the coresponding SQL statements against the DB (statements derived from modifications)
For non-transactional CacheLoader (e.g. file-based), this could be a null operation.

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
one_phase - 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
Description copied from interface: CacheLoader
Commits the transaction. A DB-based CacheLoader would look up the local JDBC transaction asociated with tx and commit that transaction. Non-transactional CacheLoaders could simply write the data that was previously saved transiently under the given tx key, to (for example) a file system (note this only holds if the previous prepare() did not define one_phase=true

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

rollback

public void rollback(java.lang.Object tx)
Description copied from interface: CacheLoader
Rolls the transaction back. A DB-based CacheLoader would look up the local JDBC transaction asociated with tx and roll back that transaction.

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

loadEntireState

public byte[] loadEntireState()
                       throws java.lang.Exception
Loads the entire state from the filesystem and returns it as a byte buffer. The format of the byte buffer must be a list of NodeData elements

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

loadState

public byte[] loadState(Fqn subtree)
                 throws java.lang.Exception
Description copied from interface: ExtendedCacheLoader
Fetch a portion of the state for this cache from secondary storage (disk, DB) and return it as a byte buffer. This is for activation of a portion of new cache from a remote cache. The new cache would then call ExtendedCacheLoader.storeState(byte[], Fqn).

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
Stores the state given as a byte buffer to the filesystem. The byte buffer contains a list of zero or more NodeData elements

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

storeState

public void storeState(byte[] state,
                       Fqn subtree)
                throws java.lang.Exception
Description copied from interface: ExtendedCacheLoader
Store the given portion of the cache tree's state in secondary storage. Overwrite whatever is currently in secondary storage. If the transferred state has Fqns equal to or children of parameter subtree, then no special behavior is required. Otherwise, ensure that the state is integrated under the given subtree. Typically in the latter case subtree would be the Fqn of the buddy backup region for a buddy group; e.g.

If the the transferred state had Fqns starting with "/a" and subtree was "/_BUDDY_BACKUP_/192.168.1.2:5555" then the state should be stored in the local persistent store under "/_BUDDY_BACKUP_/192.168.1.2:5555/a"

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

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.

loadStateFromFilessystem

protected void loadStateFromFilessystem(Fqn fqn,
                                        java.io.ObjectOutputStream out)
                                 throws java.lang.Exception
Do a preorder traversal: visit the node first, then the node's children

Parameters:
fqn - Start node
out -
Throws:
java.lang.Exception

loadAttributes

protected java.util.Map loadAttributes(Fqn fqn)
                                throws java.lang.Exception
Throws:
java.lang.Exception

storeAttributes

protected void storeAttributes(Fqn fqn,
                               java.util.Map attrs)
                        throws java.lang.Exception
Throws:
java.lang.Exception


Copyright © 1998-2005 JBoss Inc . All Rights Reserved.