org.jboss.cache.loader
Class AbstractCacheLoader

java.lang.Object
  extended by org.jboss.cache.loader.AbstractCacheLoader
All Implemented Interfaces:
CacheLoader
Direct Known Subclasses:
AbstractDelegatingCacheLoader, AdjListJDBCCacheLoader, BdbjeCacheLoader, ClusteredCacheLoader, FileCacheLoader, ChainingCacheLoader, JdbmCacheLoader, LocalDelegatingCacheLoader, TcpDelegatingCacheLoader

public abstract class AbstractCacheLoader
extends Object
implements CacheLoader

A convenience abstract implementation of a CacheLoader. Specific methods to note are methods like storeState(org.jboss.cache.Fqn,java.io.ObjectInputStream), loadState(org.jboss.cache.Fqn,java.io.ObjectOutputStream), storeEntireState(java.io.ObjectInputStream) and loadEntireState(java.io.ObjectOutputStream) which have convenience implementations here.

Also useful to note is the implementation of put(java.util.List), used during the prepare phase of a transaction.

Since:
2.0.0
Author:
Manik Surtani (manik@jboss.org), Galder Zamarreno

Field Summary
protected  CacheSPI cache
           
protected  RegionManager regionManager
           
protected  Map<Object,List<Modification>> transactions
          HashMap>.
 
Constructor Summary
AbstractCacheLoader()
           
 
Method Summary
 void commit(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.
protected  void doMarshall(Fqn fqn, Object toMarshall)
           
protected  Object doUnmarshall(Fqn fqn, Object toUnmarshall)
           
protected  Marshaller getMarshaller()
           
protected  void getNodeDataList(Fqn fqn, List<NodeData> list)
           
 void loadEntireState(ObjectOutputStream os)
          Fetches the entire state for this cache from secondary storage (disk, database) and writes it to a provided ObjectOutputStream.
 void loadState(Fqn subtree, ObjectOutputStream os)
          Fetches a portion of the state for this cache from secondary storage (disk, database) and writes it to a provided ObjectOutputStream.
protected  void loadStateHelper(Fqn fqn, ObjectOutputStream out)
          Do a preorder traversal: visit the node first, then the node's children
 void prepare(Object tx, List<Modification> modifications, boolean one_phase)
          Prepares a list of modifications.
 void put(Fqn fqn, Map<Object,Object> attributes, boolean erase)
           
 void put(List<Modification> modifications)
          Applies all modifications to the backend store.
protected  void regionAwareMarshall(Fqn fqn, Object toMarshall)
           
protected  Object regionAwareUnmarshall(Fqn fqn, Object toUnmarshall)
           
 void rollback(Object tx)
          Rolls the transaction back.
 void setCache(CacheSPI c)
          Sets the CacheSPI that is maintaining this CacheLoader.
 void setRegionManager(RegionManager regionManager)
          Sets the RegionManager this object should use to manage marshalling/unmarshalling of different regions using different classloaders.
 void start()
          Lifecycle method, called when the cache loader is started.
 void stop()
          Lifecycle method, called when the cache loader is stopped.
 void storeEntireState(ObjectInputStream is)
          Stores the entire state for this cache by reading it from a provided ObjectInputStream.
 void storeState(Fqn subtree, ObjectInputStream in)
          Stores 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
 
Methods inherited from interface org.jboss.cache.loader.CacheLoader
exists, get, getConfig, getChildrenNames, put, put, remove, remove, removeData, setConfig
 

Field Detail

cache

protected CacheSPI cache

regionManager

protected RegionManager regionManager

transactions

protected Map<Object,List<Modification>> transactions
HashMap>. List of open transactions. Note that this is purely transient, as we don't use a log, recovery is not available

Constructor Detail

AbstractCacheLoader

public AbstractCacheLoader()
Method Detail

put

public void put(Fqn fqn,
                Map<Object,Object> attributes,
                boolean erase)
         throws Exception
Throws:
Exception

storeEntireState

public void storeEntireState(ObjectInputStream is)
                      throws Exception
Description copied from interface: CacheLoader
Stores the entire state for this cache by reading it from a provided ObjectInputStream. The state was provided to this cache by calling CacheLoader.loadEntireState(ObjectOutputStream)} on some other cache instance. State currently in storage gets overwritten.

Implementations of this method should not catch any exception or close the given ObjectInputStream parameter. In order to ensure cacheloader interoperability contents of the cache are read from the ObjectInputStream as a sequence of NodeData objects.

Default implementation is provided by AbstractCacheLoader and ensures cacheloader interoperability. Implementors are encouraged to consider extending AbstractCacheLoader prior to implementing completely custom cacheloader.

Specified by:
storeEntireState in interface CacheLoader
Parameters:
is - ObjectInputStream to read state
Throws:
Exception
See Also:
storeEntireState(ObjectInputStream), NodeData

storeState

public void storeState(Fqn subtree,
                       ObjectInputStream in)
                throws Exception
Description copied from interface: CacheLoader
Stores 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"

Implementations of this method should not catch any exception or close the given ObjectInputStream parameter. In order to ensure cacheloader interoperability contents of the cache are read from the ObjectInputStream as a sequence of NodeData objects.

Default implementation is provided by AbstractCacheLoader and ensures cacheloader interoperability. Implementors are encouraged to consider extending AbstractCacheLoader prior to implementing completely custom cacheloader.

Specified by:
storeState in interface CacheLoader
Parameters:
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.
in - ObjectInputStream to read state
Throws:
Exception
See Also:
storeState(Fqn,ObjectInputStream), NodeData

loadEntireState

public void loadEntireState(ObjectOutputStream os)
                     throws Exception
Description copied from interface: CacheLoader
Fetches the entire state for this cache from secondary storage (disk, database) and writes it to a provided ObjectOutputStream. State written to the provided ObjectOutputStream parameter is used for initialization of a new CacheImpl instance. When the state gets transferred to the new cache instance its cacheloader calls CacheLoader.storeEntireState(ObjectInputStream)

Implementations of this method should not catch any exception or close the given ObjectOutputStream parameter. In order to ensure cacheloader interoperability contents of the cache are written to the ObjectOutputStream as a sequence of NodeData objects.

Default implementation is provided by AbstractCacheLoader and ensures cacheloader interoperability. Implementors are encouraged to consider extending AbstractCacheLoader prior to implementing completely custom cacheloader.

Specified by:
loadEntireState in interface CacheLoader
Parameters:
os - ObjectOutputStream to write state
Throws:
Exception
See Also:
loadEntireState(ObjectOutputStream), NodeData

loadState

public void loadState(Fqn subtree,
                      ObjectOutputStream os)
               throws Exception
Description copied from interface: CacheLoader
Fetches a portion of the state for this cache from secondary storage (disk, database) and writes it to a provided ObjectOutputStream. State written to the provided ObjectOutputStream parameter is used for activation of a portion of a new CacheImpl instance. When the state gets transferred to the new cache instance its cacheloader calls CacheLoader.storeState(Fqn,ObjectInputStream).

Implementations of this method should not catch any exception or close the given ObjectOutputStream parameter. In order to ensure cacheloader interoperability contents of the cache are written to the ObjectOutputStream as a sequence of NodeData objects.

Default implementation is provided by AbstractCacheLoader and ensures cacheloader interoperability. Implementors are encouraged to consider extending AbstractCacheLoader prior to implementing completely custom cacheloader.

Specified by:
loadState in interface CacheLoader
Parameters:
subtree - Fqn naming the root (i.e. highest level parent) node of the subtree for which state is requested.
os - ObjectOutputStream to write state
Throws:
Exception
See Also:
loadState(Fqn,ObjectOutputStream), Region.activate(), NodeData

setCache

public void setCache(CacheSPI c)
Description copied from interface: CacheLoader
Sets the CacheSPI that is maintaining this CacheLoader. This method allows this CacheLoader to set a reference to the CacheSPI. 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

setRegionManager

public void setRegionManager(RegionManager regionManager)
Description copied from interface: CacheLoader
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 CacheSPI instance this cache loader is associated with.

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

regionAwareMarshall

protected void regionAwareMarshall(Fqn fqn,
                                   Object toMarshall)
                            throws Exception
Throws:
Exception

regionAwareUnmarshall

protected Object regionAwareUnmarshall(Fqn fqn,
                                       Object toUnmarshall)
                                throws Exception
Throws:
Exception

doMarshall

protected void doMarshall(Fqn fqn,
                          Object toMarshall)
                   throws Exception
Throws:
Exception

doUnmarshall

protected Object doUnmarshall(Fqn fqn,
                              Object toUnmarshall)
                       throws Exception
Throws:
Exception

loadStateHelper

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

Parameters:
fqn - Start node
out -
Throws:
Exception

getNodeDataList

protected void getNodeDataList(Fqn fqn,
                               List<NodeData> list)
                        throws Exception
Throws:
Exception

put

public void put(List<Modification> modifications)
         throws 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 - A List of modifications
Throws:
Exception

getMarshaller

protected Marshaller getMarshaller()

create

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

Specified by:
create in interface CacheLoader
Throws:
Exception

start

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

Specified by:
start in interface CacheLoader
Throws:
Exception

stop

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

Specified by:
stop in interface CacheLoader

destroy

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

Specified by:
destroy in interface CacheLoader

prepare

public void prepare(Object tx,
                    List<Modification> modifications,
                    boolean one_phase)
             throws 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.

Specified by:
prepare in interface CacheLoader
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:
Exception

commit

public void commit(Object tx)
            throws 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:
Exception

rollback

public void rollback(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


Copyright © 2008 JBoss, a division of Red Hat. All Rights Reserved.