[exo-jcr-commits] exo-jcr SVN: r713 - jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache.
do-not-reply at jboss.org
do-not-reply at jboss.org
Tue Nov 17 06:57:53 EST 2009
Author: pnedonosko
Date: 2009-11-17 06:57:53 -0500 (Tue, 17 Nov 2009)
New Revision: 713
Modified:
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCCacheLoader.java
Log:
EXOJCR-203: loader put(List) rework, JDBC connection replaced on WorkspaceStroageConnection
Modified: jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCCacheLoader.java
===================================================================
--- jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCCacheLoader.java 2009-11-17 11:53:08 UTC (rev 712)
+++ jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCCacheLoader.java 2009-11-17 11:57:53 UTC (rev 713)
@@ -32,7 +32,6 @@
import org.exoplatform.services.jcr.datamodel.NodeData;
import org.exoplatform.services.jcr.datamodel.PropertyData;
import org.exoplatform.services.jcr.datamodel.QPathEntry;
-import org.exoplatform.services.jcr.impl.storage.jdbc.JDBCStorageConnection;
import org.exoplatform.services.jcr.storage.WorkspaceDataContainer;
import org.exoplatform.services.jcr.storage.WorkspaceStorageConnection;
import org.exoplatform.services.log.ExoLogger;
@@ -56,14 +55,15 @@
public class JDBCCacheLoader extends AbstractCacheLoader
{
protected static final Log LOG = ExoLogger.getLogger("jcr.JDBCCacheLoader");
-
+
private IndividualCacheLoaderConfig config;
-
+
private WorkspaceDataContainer dataContainer;
- public JDBCCacheLoader() {
+ public JDBCCacheLoader()
+ {
}
-
+
/**
* Init the loader DataContainer with given WorkspaceDataContainer instance.
*
@@ -79,156 +79,187 @@
this.dataContainer = dataContainer;
}
-
+
/**
+ * Put all Modifications into JDBC database, but don't commite them.
+ *
+ * @param modifications List if Modification
+ * @param conn WorkspaceStorageConnection
+ * @throws RepositoryException if error
+ */
+ protected void apply(WorkspaceStorageConnection conn, List<Modification> modifications) throws RepositoryException
+ {
+ if (LOG.isDebugEnabled())
+ {
+ LOG.debug("Modifications list size = " + modifications.size());
+ }
+
+ // Prepare modifications list.
+ // Will be added oldValueData to modification for UPDATE.
+ //prepareModifications(modifications);
+
+ for (Modification m : modifications)
+ {
+ switch (m.getType())
+ {
+ case PUT_DATA :
+ LOG.info("PUT_DATA modification");
+ break;
+ case PUT_DATA_ERASE :
+ LOG.info("PUT_DATA_ERASE modification");
+ break;
+ case PUT_KEY_VALUE :
+
+ if (m.getOldValue() != null)
+ doUpdate(m, conn);
+ else
+ doAdd(m, conn);
+
+ break;
+ case REMOVE_DATA :
+ LOG.info("REMOVE_DATA modification");
+ break;
+ case REMOVE_KEY_VALUE :
+ LOG.info("REMOVE_KEY_VALUE modification");
+ break;
+ case REMOVE_NODE :
+
+ doRemove(m, conn);
+
+ break;
+ case MOVE :
+ LOG.info("MOVE modification");
+ break;
+ default :
+ throw new CacheException("Unknown modification " + m.getType());
+ }
+ }
+ }
+
+ /**
* {@inheritDoc}
*/
public void put(List<Modification> modifications) throws Exception
{
- LOG.info("Modifications list size = " + modifications.size());
- // Prepare modifications list.
- // Will be added oldValueData to modification for UPDATE.
- prepareModifications(modifications);
-
- JDBCStorageConnection jdbcConnection = (JDBCStorageConnection)dataContainer.openConnection();
-
- try
- {
- for (Modification m : modifications)
- {
- switch (m.getType())
- {
- case PUT_DATA:
- break;
- case PUT_DATA_ERASE:
- break;
- case PUT_KEY_VALUE:
-
- if (m.getOldValue() != null)
- doUpdate(m, jdbcConnection);
- else
- doAdd(m, jdbcConnection);
-
- break;
- case REMOVE_DATA:
- break;
- case REMOVE_KEY_VALUE:
- break;
- case REMOVE_NODE:
-
- doRemove(m, jdbcConnection);
-
- break;
- case MOVE:
- break;
- default:
- throw new CacheException("Unknown modification " + m.getType());
- }
- }
-
- if (jdbcConnection != null) {
- jdbcConnection.commit();
- jdbcConnection = null;
- }
- }
- catch (Exception e)
- {
- LOG.error("Can not put data to DB", e);
- throw e;
- }
- finally
- {
- if (jdbcConnection != null && jdbcConnection.isOpened()) {
- jdbcConnection.rollback();
- }
-
- }
+ if (LOG.isDebugEnabled())
+ {
+ LOG.debug("Modifications list size = " + modifications.size());
+ }
+
+ // Prepare modifications list.
+ // Will be added oldValueData to modification for UPDATE.
+ //prepareModifications(modifications);
+
+ WorkspaceStorageConnection conn = dataContainer.openConnection();
+ try
+ {
+ apply(conn, modifications);
+ conn.commit();
+ }
+ catch (RepositoryException e)
+ {
+ LOG.error("RepositoryException: ", e); // TODO cleanup
+ throw new RepositoryException(e);
+ }
+ catch (IllegalStateException e)
+ {
+ LOG.error("IllegalStateException: ", e); // TODO cleanup
+ throw new IllegalStateException(e);
+ }
+ finally
+ {
+ if (conn != null && conn.isOpened())
+ {
+ conn.rollback();
+ }
+ }
}
/**
* Remove NodeData or PropertyData.
*
* @param modification
- * @param jdbcConnection
+ * @param conn
* @param identifier
* @throws IllegalStateException
* @throws RepositoryException
*/
- private void doRemove(Modification modification, JDBCStorageConnection jdbcConnection)
- throws IllegalStateException, RepositoryException
+ private void doRemove(Modification modification, WorkspaceStorageConnection conn) throws IllegalStateException,
+ RepositoryException
{
- if ( modification.getFqn().size() == 2 && (modification.getFqn().get(0).equals(JBossCacheStorage.NODES) ||
- modification.getFqn().get(0).equals(JBossCacheStorage.PROPS)))
+ if (modification.getFqn().size() == 2
+ && (modification.getFqn().get(0).equals(JBossCacheStorage.NODES) || modification.getFqn().get(0).equals(
+ JBossCacheStorage.PROPS)))
{
// TODO The removed ItemData was setting to value in prepareModifications();
// That is need because we do not get NodeData from DB if we delete property with primaytype.
-
-// String identifier = (String) modification.getFqn().get(1);
-// ItemData itemData = jdbcConnection.getItemData(identifier);
-
- ItemData itemData = (ItemData) modification.getValue();
-
+
+ // String identifier = (String) modification.getFqn().get(1);
+ // ItemData itemData = jdbcConnection.getItemData(identifier);
+
+ ItemData itemData = (ItemData)modification.getValue();
+
if (itemData instanceof NodeData)
- jdbcConnection.delete((NodeData) itemData);
+ conn.delete((NodeData)itemData);
if (itemData instanceof PropertyData)
- jdbcConnection.delete((PropertyData) itemData);
+ conn.delete((PropertyData)itemData);
}
}
-
+
/**
* Performs ADD to NodeData and PropertyData.
* @param modification
- * @param jdbcConnection
+ * @param conn
* @throws IllegalStateException
* @throws RepositoryException
*/
- private void doAdd(Modification modification, JDBCStorageConnection jdbcConnection)
- throws IllegalStateException, RepositoryException
+ private void doAdd(Modification modification, WorkspaceStorageConnection conn) throws IllegalStateException,
+ RepositoryException
{
if (modification.getValue() instanceof NodeData)
{
//add node data
NodeData nodeData = (NodeData)modification.getValue();
- jdbcConnection.add(nodeData);
+ conn.add(nodeData);
}
else if (modification.getValue() instanceof PropertyData)
{
//add property data
PropertyData propertyData = (PropertyData)modification.getValue();
- jdbcConnection.add(propertyData);
+ conn.add(propertyData);
}
}
-
+
/**
* Performs UPDATE to NodeData and PropertyData.
* @param modification
- * @param jdbcConnection
+ * @param conn
* @throws IllegalStateException
* @throws RepositoryException
*/
- private void doUpdate(Modification modification, JDBCStorageConnection jdbcConnection)
- throws IllegalStateException, RepositoryException
+ private void doUpdate(Modification modification, WorkspaceStorageConnection conn) throws IllegalStateException,
+ RepositoryException
{
if (modification.getValue() instanceof NodeData)
{
//update node data
NodeData nodeData = (NodeData)modification.getValue();
- jdbcConnection.update(nodeData);
+ conn.update(nodeData);
}
else if (modification.getValue() instanceof PropertyData)
{
//update property data
PropertyData propertyData = (PropertyData)modification.getValue();
- jdbcConnection.update(propertyData);
+ conn.update(propertyData);
}
}
-
+
/**
* Prepare list of modifications.
*
@@ -237,47 +268,49 @@
* @param modifications
* @throws RepositoryException
*/
+ @Deprecated
private void prepareModifications(List<Modification> modifications) throws RepositoryException
{
- JDBCStorageConnection jdbcConnection = (JDBCStorageConnection) dataContainer.openConnection();
+ WorkspaceStorageConnection conn = dataContainer.openConnection();
- try {
+ try
+ {
for (int i = 0; i < modifications.size(); i++)
{
Modification m = modifications.get(i);
if (m.getType() == ModificationType.PUT_KEY_VALUE)
{
ItemData itemData = null;
-
+
//Check add or update node data.
if (m.getValue() instanceof NodeData)
{
- NodeData nodeData = (NodeData) m.getValue();
- itemData = jdbcConnection.getItemData(nodeData.getIdentifier());
-
+ NodeData nodeData = (NodeData)m.getValue();
+ itemData = conn.getItemData(nodeData.getIdentifier());
+
// Set oldValueData for update node.
- if (itemData != null)
+ if (itemData != null)
modifications.get(i).setOldValue(itemData);
}
else if (m.getValue() instanceof PropertyData)
{
- PropertyData propertyData = (PropertyData) m.getValue();
- itemData = jdbcConnection.getItemData(propertyData.getIdentifier());
-
+ PropertyData propertyData = (PropertyData)m.getValue();
+ itemData = conn.getItemData(propertyData.getIdentifier());
+
// Set oldValueData for update property.
- if (itemData != null)
+ if (itemData != null)
modifications.get(i).setOldValue(itemData);
- }
+ }
}
else if (m.getType() == ModificationType.REMOVE_NODE)
{
if (m.getFqn().size() == 2
- && (m.getFqn().get(0).equals(JBossCacheStorage.NODES)
- || m.getFqn().get(0).equals(JBossCacheStorage.PROPS)))
+ && (m.getFqn().get(0).equals(JBossCacheStorage.NODES) || m.getFqn().get(0).equals(
+ JBossCacheStorage.PROPS)))
{
- String id = (String) m.getFqn().get(1);
- ItemData removedItemData = jdbcConnection.getItemData(id);
-
+ String id = (String)m.getFqn().get(1);
+ ItemData removedItemData = conn.getItemData(id);
+
// Set valueData for update property or node.
if (removedItemData != null)
modifications.get(i).setValue(removedItemData);
@@ -287,7 +320,7 @@
}
finally
{
- jdbcConnection.close();
+ conn.close();
}
}
@@ -311,7 +344,7 @@
{
Map<Object, Object> attrs = new LinkedHashMap<Object, Object>();
-
+
if (name.size() > 1)
{
if (name.get(0).equals(JBossCacheStorage.NODES))
@@ -324,8 +357,8 @@
try
{
- NodeData nodeData = (NodeData) conn.getItemData(name.getLastElementAsString());
-
+ NodeData nodeData = (NodeData)conn.getItemData(name.getLastElementAsString());
+
if (nodeData != null)
{
attrs.put(JBossCacheStorage.ITEM_DATA, nodeData);
@@ -339,28 +372,28 @@
}
finally
{
- conn.close();
+ conn.close();
}
}
// /$NODES/<NODE_ID>/<SUB_NODE_NAME>
else if (name.size() == 3)
{
QPathEntry nodeName = QPathEntry.parse(name.getLastElementAsString());
-
+
// return [ITEM_Id : nodeData_ID]
WorkspaceStorageConnection conn = dataContainer.openConnection();
try
{
- NodeData parentNodeData = (NodeData) conn.getItemData((String)name.get(1));
-
+ NodeData parentNodeData = (NodeData)conn.getItemData((String)name.get(1));
+
//TODO We do not throw exception if parentNodeData not exists in DB, because we use that method (get(Fqn name)) in exists(Fqn name).
/*if (parentNodeData == null)
throw new JDBCCacheLoaderException("The parent node with ID = " + (String)name.get(1) + " not exis, FQN = '" + name + "'.");*/
-
+
if (parentNodeData != null)
{
- NodeData nodeData = (NodeData) conn.getItemData(parentNodeData, nodeName);
+ NodeData nodeData = (NodeData)conn.getItemData(parentNodeData, nodeName);
if (nodeData != null)
attrs.put(JBossCacheStorage.ITEM_ID, nodeData.getIdentifier());
}
@@ -381,19 +414,19 @@
try
{
- PropertyData propertyData = (PropertyData) conn.getItemData(name.getLastElementAsString());
-
+ PropertyData propertyData = (PropertyData)conn.getItemData(name.getLastElementAsString());
+
if (propertyData != null)
- attrs.put(JBossCacheStorage.ITEM_DATA, propertyData);
+ attrs.put(JBossCacheStorage.ITEM_DATA, propertyData);
}
finally
{
conn.close();
}
- }
+ }
}
}
-
+
return (attrs.size() == 0 ? null : attrs);
}
@@ -412,19 +445,20 @@
public Set<?> getChildrenNames(Fqn name) throws Exception
{
// return child nodes names
-
+
Set<String> childs = new LinkedHashSet<String>();
-
+
// /$NODES/<NODE_ID>
- if (name.size() == 2)
+ if (name.size() == 2)
{
WorkspaceStorageConnection conn = dataContainer.openConnection();
try
{
- NodeData parentNodeData = (NodeData)conn.getItemData((String) name.get(1));
-
+ NodeData parentNodeData = (NodeData)conn.getItemData((String)name.get(1));
+
if (parentNodeData == null)
- throw new JDBCCacheLoaderException("The parent node with ID = " + (String)name.get(1) + " not exis, FQN = '" + name + "'.");
+ throw new JDBCCacheLoaderException("The parent node with ID = " + (String)name.get(1)
+ + " not exis, FQN = '" + name + "'.");
// get child nodes by parent Id
for (NodeData node : conn.getChildNodesData(parentNodeData))
@@ -437,7 +471,7 @@
conn.close();
}
}
-
+
return (childs.size() == 0 ? null : childs);
}
@@ -452,8 +486,66 @@
/**
* {@inheritDoc}
*/
+ public void setConfig(IndividualCacheLoaderConfig config)
+ {
+ this.config = config;
+
+ }
+
+ // TRANSACTION support
+
+ /**
+ * {@inheritDoc}
+ */
+ public void prepare(Object tx, List<Modification> modifications, boolean one_phase) throws Exception
+ {
+
+ put(modifications);
+
+ if (one_phase)
+ {
+ //put(modifications);
+ //commitJDBC();
+ }
+ else
+ {
+ //transactions.put(tx, modifications);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void commit(Object tx) throws Exception
+ {
+ // List<Modification> modifications = transactions.remove(tx);
+ // if (modifications == null)
+ // {
+ // throw new Exception("transaction " + tx + " not found in transaction table");
+ // }
+ // put(modifications);
+
+ //commitJDBC();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void rollback(Object tx)
+ {
+ //transactions.remove(tx);
+
+ //rollbackJDBC();
+ }
+
+ // SHOULD NOT BE USED methods
+
+ /**
+ * {@inheritDoc}
+ */
public Object put(Fqn name, Object key, Object value) throws Exception
{
+ LOG.error("The method 'put(Fqn name, Object key, Object value))' should not be called.");
throw new JDBCCacheLoaderException("The method 'put(Fqn name, Object key, Object value))' should not be called.");
}
@@ -462,7 +554,9 @@
*/
public void put(Fqn name, Map<Object, Object> attributes) throws Exception
{
- throw new JDBCCacheLoaderException("The method 'put(Fqn name, Map<Object, Object> attributes)' should not be called.");
+ LOG.error("The method 'put(Fqn name, Map<Object, Object> attributes)' should not be called.");
+ throw new JDBCCacheLoaderException(
+ "The method 'put(Fqn name, Map<Object, Object> attributes)' should not be called.");
}
/**
@@ -470,6 +564,7 @@
*/
public Object remove(Fqn fqn, Object key) throws Exception
{
+ LOG.error("The method 'remove(Fqn fqn, Object key)' should not be called.");
throw new JDBCCacheLoaderException("The method 'remove(Fqn fqn, Object key)' should not be called.");
}
@@ -478,6 +573,7 @@
*/
public void remove(Fqn fqn) throws Exception
{
+ LOG.error("The method 'remove(Fqn fqn)' should not be called.");
throw new JDBCCacheLoaderException("The method 'remove(Fqn fqn)' should not be called.");
}
@@ -486,16 +582,10 @@
*/
public void removeData(Fqn fqn) throws Exception
{
+ LOG.error("The method 'removeData(Fqn fqn)' should not be called.");
throw new JDBCCacheLoaderException("The method 'removeData(Fqn fqn)' should not be called.");
}
- /**
- * {@inheritDoc}
- */
- public void setConfig(IndividualCacheLoaderConfig config)
- {
- this.config = config;
+ // etc.
- }
-
}
More information about the exo-jcr-commits
mailing list