org.jboss.cache.loader
Class AdjListJDBCCacheLoader

java.lang.Object
  extended by org.jboss.cache.loader.AbstractCacheLoader
      extended by org.jboss.cache.loader.AdjListJDBCCacheLoader
All Implemented Interfaces:
Lifecycle, CacheLoader
Direct Known Subclasses:
JDBCCacheLoader, JDBCCacheLoaderOld

@ThreadSafe
public abstract class AdjListJDBCCacheLoader
extends AbstractCacheLoader

Adjacency List Model is the model of persisting trees in which each children holds a reference to its parent. An alternative model is the Nested Set Model (a.k.a. Modified Preorder Model) - this approach adds some additional indexing information to each persisted node. This indexing info is further used for optimizing operations like subtree loading, deleting etc. The indexes are update for each insertion.

Adjacency List Model proved more performance-effective for the following reason: the entire path is persisted rather than only a reference to parent. Looking up nodes heavily relies on that, and the performance is similar as in the case of Modified Preorder Model. Even more there is no costly update indexes operation.

Version:
1.0
Author:
Mircea.Markus@iquestint.com, Galder Zamarreno

Field Summary
protected  ConnectionFactory cf
           
protected  String driverName
           
protected  StripedLock lock
           
protected static Map<Object,Object> NULL_NODE_IN_ROW
           
 
Fields inherited from class org.jboss.cache.loader.AbstractCacheLoader
buddyFqnTransformer, cache, regionManager, transactions
 
Constructor Summary
AdjListJDBCCacheLoader()
           
 
Method Summary
 void commit(Object tx)
          Commits a transaction.
 boolean exists(Fqn name)
          Checks that there is a row for the fqn in the database.
 Map<Object,Object> get(Fqn name)
          Returns a map representing a node.
 Set<String> getChildrenNames(Fqn fqn)
          Fetches child node names (not pathes).
protected  String getDriverName(Connection con)
           
protected abstract  org.apache.commons.logging.Log getLogger()
           
protected  void insertNode(Fqn name, Map dataMap, boolean rowMayExist)
          Inserts a node into the database
protected  Map<Object,Object> loadNode(Fqn name)
          Loads a node from the database.
protected  ByteBuffer marshall(Object obj)
           
protected  void populatePreparedStatementForInsert(Fqn name, Map dataMap, PreparedStatement ps)
          Expects a PreparedStatement binded to AdjListJDBCCacheLoaderConfig.getInsertNodeSql()
 void prepare(Object tx, List<Modification> modifications, boolean one_phase)
          First phase in transaction commit process.
protected  PreparedStatement prepareAndLogStatement(Connection conn, String sql, String... params)
          Creates a prepared statement using the given connection and SQL string, logs the statement that is about to be executed to the logger, and optionally sets String parameters provided on the prepared statement before returning the prepared statement.
protected abstract  AdjListJDBCCacheLoaderConfig processConfig(CacheLoaderConfig.IndividualCacheLoaderConfig base)
           
 Object remove(Fqn name, Object key)
          Removes attribute's value for a key.
 void removeData(Fqn name)
          Nullifies the node.
protected  void reportAndRethrowError(String message, Exception cause)
           
 void rollback(Object tx)
          Rolls back a transaction.
protected  void safeClose(InputStream is)
           
protected  void safeClose(ResultSet rs)
           
protected  void safeClose(Statement st)
           
 void setConfig(CacheLoaderConfig.IndividualCacheLoaderConfig base)
          Sets the configuration.
 void start()
           
 void stop()
           
protected  boolean tableExists(String tableName, Connection con)
           
protected  Object unmarshall(InputStream from)
           
protected  void updateNode(Fqn name, Map<Object,Object> node)
          Updates a node in the database.
 
Methods inherited from class org.jboss.cache.loader.AbstractCacheLoader
create, destroy, doMarshall, doUnmarshall, getMarshaller, getNodeDataList, loadEntireState, loadState, loadStateHelper, move, put, put, regionAwareMarshall, regionAwareUnmarshall, setCache, setRegionManager, storeEntireState, storeState, storeStateHelper
 
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
getConfig, put, put, remove
 

Field Detail

cf

protected ConnectionFactory cf

driverName

protected String driverName

lock

protected StripedLock lock

NULL_NODE_IN_ROW

protected static final Map<Object,Object> NULL_NODE_IN_ROW
Constructor Detail

AdjListJDBCCacheLoader

public AdjListJDBCCacheLoader()
Method Detail

prepareAndLogStatement

protected PreparedStatement prepareAndLogStatement(Connection conn,
                                                   String sql,
                                                   String... params)
                                            throws Exception
Creates a prepared statement using the given connection and SQL string, logs the statement that is about to be executed to the logger, and optionally sets String parameters provided on the prepared statement before returning the prepared statement.

Parameters:
conn - Connection to use to create the prepared statement
sql - SQL to use with the prepared statement
params - optional parameters to add to the statement.
Returns:
a prepared statement
Throws:
Exception - if there are problems

setConfig

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

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

get

public Map<Object,Object> get(Fqn name)
                       throws Exception
Returns a map representing a node.

Parameters:
name - node's fqn
Returns:
node
Throws:
Exception

getChildrenNames

public Set<String> getChildrenNames(Fqn fqn)
                             throws Exception
Fetches child node names (not pathes).

Parameters:
fqn - parent fqn
Returns:
a set of child node names or null if there are not children found for the fqn
Throws:
Exception

removeData

public void removeData(Fqn name)
                throws Exception
Nullifies the node.

Parameters:
name - node's fqn
Throws:
Exception

prepare

public void prepare(Object tx,
                    List<Modification> modifications,
                    boolean one_phase)
             throws Exception
First phase in transaction commit process. The changes are committed if only one phase if requested. All the modifications are committed using the same connection.

Specified by:
prepare in interface CacheLoader
Overrides:
prepare in class AbstractCacheLoader
Parameters:
tx - something representing transaction
modifications - a list of modifications
one_phase - indicates whether it's one or two phase commit transaction
Throws:
Exception

commit

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

Specified by:
commit in interface CacheLoader
Overrides:
commit in class AbstractCacheLoader
Parameters:
tx - the tx to commit
Throws:
Exception

rollback

public void rollback(Object tx)
Rolls back a transaction.

Specified by:
rollback in interface CacheLoader
Overrides:
rollback in class AbstractCacheLoader
Parameters:
tx - the tx to rollback

start

public void start()
           throws Exception
Specified by:
start in interface Lifecycle
Overrides:
start in class AbstractCacheLoader
Throws:
Exception

stop

public void stop()
Specified by:
stop in interface Lifecycle
Overrides:
stop in class AbstractCacheLoader

exists

public boolean exists(Fqn name)
               throws Exception
Checks that there is a row for the fqn in the database.

Parameters:
name - node's fqn
Returns:
true if there is a row in the database for the given fqn even if the node column is null.
Throws:
Exception

remove

public Object remove(Fqn name,
                     Object key)
              throws Exception
Removes attribute's value for a key. If after removal the node contains no attributes, the node is nullified.

Parameters:
name - node's name
key - attribute's key
Returns:
removed value or null if there was no value for the passed in key
Throws:
Exception

loadNode

protected Map<Object,Object> loadNode(Fqn name)
Loads a node from the database.

Parameters:
name - the fqn
Returns:
non-null Map representing the node, null if there is no row with the fqn in the table, NULL_NODE_IN_ROW if there is a row in the table with the fqn but the node column contains null.

insertNode

protected void insertNode(Fqn name,
                          Map dataMap,
                          boolean rowMayExist)
Inserts a node into the database

Parameters:
name - the fqn
dataMap - the node
rowMayExist - if true, then this method will not be strict in testing for 1 row being inserted, since 0 may be inserted if the row already exists.

populatePreparedStatementForInsert

protected void populatePreparedStatementForInsert(Fqn name,
                                                  Map dataMap,
                                                  PreparedStatement ps)
                                           throws Exception
Expects a PreparedStatement binded to AdjListJDBCCacheLoaderConfig.getInsertNodeSql()

Throws:
Exception

updateNode

protected void updateNode(Fqn name,
                          Map<Object,Object> node)
Updates a node in the database.

Parameters:
name - the fqn
node - new node value

getDriverName

protected String getDriverName(Connection con)

tableExists

protected boolean tableExists(String tableName,
                              Connection con)

getLogger

protected abstract org.apache.commons.logging.Log getLogger()

processConfig

protected abstract AdjListJDBCCacheLoaderConfig processConfig(CacheLoaderConfig.IndividualCacheLoaderConfig base)

reportAndRethrowError

protected void reportAndRethrowError(String message,
                                     Exception cause)
                              throws IllegalStateException
Throws:
IllegalStateException

safeClose

protected void safeClose(InputStream is)

safeClose

protected void safeClose(Statement st)

safeClose

protected void safeClose(ResultSet rs)

unmarshall

protected Object unmarshall(InputStream from)
                     throws Exception
Throws:
Exception

marshall

protected ByteBuffer marshall(Object obj)
                       throws Exception
Throws:
Exception


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