org.jboss.cache.loader
Class FileCacheLoader

java.lang.Object
  extended by org.jboss.cache.loader.AbstractCacheLoader
      extended by org.jboss.cache.loader.FileCacheLoader
All Implemented Interfaces:
CacheLoader

@ThreadSafe
public class FileCacheLoader
extends AbstractCacheLoader

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

The FileCacheLoader has some severe limitations which restrict it's use in a production environment, or if used in such an environment, it should be used with due care and sufficient understanding of these limitations.

As a rule of thumb, it is recommended that the FileCacheLoader not be used in a highly concurrent, transactional or stressful environment, and it's use is restricted to testing.

In terms of concurrency, file systems are notoriously inconsistent in their implementations of concurrent locks. To get around this and to meet the thread safety contracts set out in CacheLoader, this implementation uses a StripedLock

Version:
$Id: FileCacheLoader.java,v 1.35 2007/06/19 18:49:33 msurtani Exp $
Author:
Bela Ban, Galder Zamarreno, Manik Surtani

Field Summary
static java.lang.String DATA
          CacheImpl data file.
static java.lang.String DIR_SUFFIX
          CacheImpl directory suffix.
static java.util.regex.Pattern FQN_PATTERN
          For fqn, check '*' '<' '>' '|' '"' '?' and also '\' '/' and ':'
protected  StripedLock lock
           
static java.util.regex.Pattern PATH_PATTERN
          For full path, check '*' '<' '>' '|' '"' '?' Regex: [\*<>|"?]
 
Fields inherited from class org.jboss.cache.loader.AbstractCacheLoader
cache, regionManager
 
Constructor Summary
FileCacheLoader()
           
 
Method Summary
 void commit(java.lang.Object tx)
          Commits the transaction.
 void create()
          Lifecycle method, called when the cache loader is created.
 void destroy()
          Lifecycle method, called when the cache loader is destroyed.
 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 Fqn
 java.util.Set<java.lang.String> getChildrenNames(Fqn fqn)
          Returns a set of children node names.
 CacheLoaderConfig.IndividualCacheLoaderConfig getConfig()
          Gets the configuration.
protected  boolean isCharacterPortableLocation(java.lang.String fileAbsolutePath)
           
protected  boolean isCharacterPortableTree(Fqn fqn)
           
protected  boolean isLengthPortablePath(java.lang.String absoluteFqnPath)
           
protected  java.util.Map loadAttributes(Fqn fqn)
           
protected  void marshall(java.lang.Object obj, java.io.File to)
           
 void prepare(java.lang.Object tx, java.util.List<Modification> 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 remove(Fqn fqn)
          Removes the given node and all its subnodes, does nothing if the node does not exist.
 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 setConfig(CacheLoaderConfig.IndividualCacheLoaderConfig base)
          Sets the configuration.
 void start()
          Lifecycle method, called when the cache loader is started.
 void stop()
          Lifecycle method, called when the cache loader is stopped.
protected  void storeAttributes(Fqn fqn, java.util.Map attrs)
           
protected  java.lang.Object unmarshall(java.io.File from)
           
 
Methods inherited from class org.jboss.cache.loader.AbstractCacheLoader
getMarshaller, getNodeDataList, loadEntireState, loadState, loadStateHelper, put, setCache, setRegionManager, storeEntireState, storeState
 
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
CacheImpl data file.

See Also:
Constant Field Values

DIR_SUFFIX

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

See Also:
Constant Field Values

PATH_PATTERN

public static final java.util.regex.Pattern PATH_PATTERN
For full path, check '*' '<' '>' '|' '"' '?' Regex: [\*<>|"?]


FQN_PATTERN

public static final java.util.regex.Pattern FQN_PATTERN
For fqn, check '*' '<' '>' '|' '"' '?' and also '\' '/' and ':'

Constructor Detail

FileCacheLoader

public FileCacheLoader()
Method Detail

setConfig

public void setConfig(CacheLoaderConfig.IndividualCacheLoaderConfig base)
Description copied from interface: CacheLoader
Sets the configuration. This is called before CacheLoader.create() and CacheLoader.start().

Parameters:
base - May be an instance of the CacheLoaderConfig.IndividualCacheLoaderConfig base class, in which case the cache loader should use the CacheLoaderConfig.IndividualCacheLoaderConfig.getProperties() method to find configuration information. Alternatively, may be a type-specific subclass of CacheLoaderConfig.IndividualCacheLoaderConfig, if there is one.

getConfig

public CacheLoaderConfig.IndividualCacheLoaderConfig getConfig()
Description copied from interface: CacheLoader
Gets the configuration.

Returns:
the configuration, represented by a CacheLoaderConfig.IndividualCacheLoaderConfig object.

create

public void create()
            throws java.lang.Exception
Description copied from interface: CacheLoader
Lifecycle method, called when the cache loader is created.

Throws:
java.lang.Exception

start

public void start()
           throws java.lang.Exception
Description copied from interface: CacheLoader
Lifecycle method, called when the cache loader is started.

Throws:
java.lang.Exception

stop

public void stop()
Description copied from interface: CacheLoader
Lifecycle method, called when the cache loader is stopped.


destroy

public void destroy()
Description copied from interface: CacheLoader
Lifecycle method, called when the cache loader is destroyed.


getChildrenNames

public java.util.Set<java.lang.String> getChildrenNames(Fqn fqn)
                                                 throws java.lang.Exception
Description copied from interface: CacheLoader
Returns a set of children node names. 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(java.util.Set) to make the set unmodifiable.

Implementors may impose restrictions on the contents of an Fqn (such as Strings-only) and as such, indirectly impose the same restriction on the contents of a Set returned by getChildrenNames().

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 Fqn

Parameters:
fqn - the Fqn to search for.
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.

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.

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

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
Overrides:
put in class AbstractCacheLoader
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.

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, does nothing if the node does not exist.

Parameters:
fqn - the Fqn of the node
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.

Parameters:
fqn - the Fqn of the node
Throws:
java.lang.Exception

prepare

public void prepare(java.lang.Object tx,
                    java.util.List<Modification> 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 corresponding SQL statements against the DB (statements derived from modifications)
For non-transactional CacheLoader (e.g. file-based), the implementation could attempt to implement it's own transactional logic, attempting to write data to a temp location (or memory) and writing it to the proper location upon commit.

Parameters:
tx - The transaction, indended to be used by implementations as an identifier of the transaction (and not necessarily a JTA Transaction object)
modifications - A List containing Modifications, for 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

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.

Parameters:
tx - transaction to roll back

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

isCharacterPortableLocation

protected boolean isCharacterPortableLocation(java.lang.String fileAbsolutePath)

isCharacterPortableTree

protected boolean isCharacterPortableTree(Fqn fqn)

isLengthPortablePath

protected boolean isLengthPortablePath(java.lang.String absoluteFqnPath)

unmarshall

protected java.lang.Object unmarshall(java.io.File from)
                               throws java.lang.Exception
Throws:
java.lang.Exception

marshall

protected void marshall(java.lang.Object obj,
                        java.io.File to)
                 throws java.lang.Exception
Throws:
java.lang.Exception