jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r158047 - in incubator/jackrabbit/trunk/contrib/orm-persistence: applications/test/ojb/repository_user.xml create_db_mysql.sql src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBPersistenceManager.java
Date Fri, 18 Mar 2005 10:21:38 GMT
Author: stefan
Date: Fri Mar 18 02:21:34 2005
New Revision: 158047

URL: http://svn.apache.org/viewcvs?view=rev&rev=158047
Log:
JCR-63: /contrib/orm-persistence/ OJBPersistenceManager
committing patch posted by Edgar Poce on behalf of Serge Huber 

Modified:
    incubator/jackrabbit/trunk/contrib/orm-persistence/applications/test/ojb/repository_user.xml
    incubator/jackrabbit/trunk/contrib/orm-persistence/create_db_mysql.sql
    incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBPersistenceManager.java

Modified: incubator/jackrabbit/trunk/contrib/orm-persistence/applications/test/ojb/repository_user.xml
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/orm-persistence/applications/test/ojb/repository_user.xml?view=diff&r1=158046&r2=158047
==============================================================================
--- incubator/jackrabbit/trunk/contrib/orm-persistence/applications/test/ojb/repository_user.xml
(original)
+++ incubator/jackrabbit/trunk/contrib/orm-persistence/applications/test/ojb/repository_user.xml
Fri Mar 18 02:21:34 2005
@@ -274,7 +274,7 @@
        <field-descriptor
          name="size"
          column="BLOB_SIZE"
-         jdbc-type="INTEGER"
+         jdbc-type="BIGINT"
       />
        <field-descriptor
          name="blobValue"

Modified: incubator/jackrabbit/trunk/contrib/orm-persistence/create_db_mysql.sql
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/orm-persistence/create_db_mysql.sql?view=diff&r1=158046&r2=158047
==============================================================================
--- incubator/jackrabbit/trunk/contrib/orm-persistence/create_db_mysql.sql (original)
+++ incubator/jackrabbit/trunk/contrib/orm-persistence/create_db_mysql.sql Fri Mar 18 02:21:34
2005
@@ -1,10 +1,16 @@
+DROP DATABASE IF EXISTS jackrabbit  ;
+
+CREATE DATABASE jackrabbit  ;
+
+USE jackrabbit  ;
+
 CREATE TABLE JCR_NODE(
   UUID VARCHAR(36) NOT NULL,
   PARENT_UUID VARCHAR(36),
   DEFINITION_ID VARCHAR(100),
   NODE_TYPE VARCHAR(100), 
   PRIMARY KEY (UUID)
-);
+) TYPE=InnoDB;
 
 CREATE TABLE JCR_PROPERTY(
   ITEM_ID VARCHAR(200) NOT NULL, 
@@ -15,29 +21,29 @@
   DEFINITION_ID VARCHAR(100), 
   MULTI_VALUED CHAR(1), 
   PRIMARY KEY (ITEM_ID)
-);
+) TYPE=InnoDB;
 
 CREATE TABLE JCR_NODE_MIXIN_TYPE(
   NODE_UUID VARCHAR(36) NOT NULL,
   MIXIN_TYPE VARCHAR(100)
-);
+) TYPE=InnoDB;
 
 CREATE TABLE JCR_NODE_PARENT(
   NODE_UUID VARCHAR(36) NOT NULL,
   PARENT_UUID VARCHAR(36)
-);
+) TYPE=InnoDB;
 
 CREATE TABLE JCR_CHILD_NODE(
   PARENT_UUID VARCHAR(36) NOT NULL,
   UUID VARCHAR(36),
   NAME VARCHAR(100),
   NODE_INDEX INTEGER
-);
+) TYPE=InnoDB;
 
 CREATE TABLE JCR_NODE_PROPERTY(
   PARENT_UUID VARCHAR(36) NOT NULL,
   NAME VARCHAR(100)
-);
+) TYPE=InnoDB;
 
 CREATE TABLE JCR_NODE_REF(
   NREF_ID INTEGER NOT NULL AUTO_INCREMENT, 
@@ -45,17 +51,17 @@
   PROP_UUID VARCHAR(36),
   PROP_NAME VARCHAR(100), 
   PRIMARY KEY (NREF_ID)
-);
+) TYPE=InnoDB;
 
 CREATE TABLE JCR_BLOB(
   BLOB_ID INTEGER NOT NULL AUTO_INCREMENT, 
   PARENT_UUID VARCHAR(36),
   PROP_NAME VARCHAR(100), 
   VALUE_INDEX INTEGER, 
-  BLOB_SIZE INTEGER, 
+  BLOB_SIZE BIGINT, 
   BLOB_VALUE BLOB,
   PRIMARY KEY(BLOB_ID)
-);
+) TYPE=InnoDB;
 
 
 CREATE INDEX JCR_NODE_PROPERTY_INDEX1 ON JCR_NODE_PROPERTY(PARENT_UUID, NAME);
@@ -73,4 +79,4 @@
 CREATE INDEX JCR_NODE_REF_INDEX1 ON JCR_NODE_REF(TARGET_UUID, PROP_UUID, PROP_NAME);
 CREATE INDEX JCR_NODE_REF_INDEX2 ON JCR_NODE_REF(TARGET_UUID);
 
-CREATE INDEX JCR_BLOB_INDEX1 ON JCR_BLOB(PARENT_UUID, PROP_NAME, VALUE_INDEX);
\ No newline at end of file
+CREATE INDEX JCR_BLOB_INDEX1 ON JCR_BLOB(PARENT_UUID, PROP_NAME, VALUE_INDEX);

Modified: incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBPersistenceManager.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBPersistenceManager.java?view=diff&r1=158046&r2=158047
==============================================================================
--- incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBPersistenceManager.java
(original)
+++ incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBPersistenceManager.java
Fri Mar 18 02:21:34 2005
@@ -20,6 +20,7 @@
 import java.io.ByteArrayOutputStream;
 import java.util.ArrayList;
 import java.util.Iterator;
+
 import javax.jcr.PropertyType;
 
 import org.apache.jackrabbit.core.BLOBFileValue;
@@ -27,19 +28,22 @@
 import org.apache.jackrabbit.core.NodeId;
 import org.apache.jackrabbit.core.PropertyId;
 import org.apache.jackrabbit.core.QName;
-import org.apache.jackrabbit.core.state.AbstractPersistenceManager;
+import org.apache.jackrabbit.core.state.ChangeLog;
+import org.apache.jackrabbit.core.state.ItemState;
 import org.apache.jackrabbit.core.state.ItemStateException;
 import org.apache.jackrabbit.core.state.NoSuchItemStateException;
 import org.apache.jackrabbit.core.state.NodeReferences;
 import org.apache.jackrabbit.core.state.NodeReferencesId;
 import org.apache.jackrabbit.core.state.NodeState;
 import org.apache.jackrabbit.core.state.PMContext;
+import org.apache.jackrabbit.core.state.PersistenceManager;
 import org.apache.jackrabbit.core.state.PropertyState;
 import org.apache.jackrabbit.core.state.orm.ORMBlobValue;
 import org.apache.jackrabbit.core.state.orm.ORMNodeReference;
 import org.apache.jackrabbit.core.state.orm.ORMPropertyState;
 import org.apache.log4j.Logger;
 import org.apache.ojb.broker.PersistenceBroker;
+import org.apache.ojb.broker.PersistenceBrokerException;
 import org.apache.ojb.broker.PersistenceBrokerFactory;
 import org.apache.ojb.broker.query.Criteria;
 import org.apache.ojb.broker.query.QueryByCriteria;
@@ -48,227 +52,335 @@
 /**
  * OJB implementation of a Jackrabbit persistence manager.
  */
-public class OJBPersistenceManager extends AbstractPersistenceManager {
+public class OJBPersistenceManager implements PersistenceManager
+{
 
     private static Logger log = Logger.getLogger(OJBPersistenceManager.class);
 
-    private PersistenceBroker broker = null;
     private boolean initialized = false;
 
-    public OJBPersistenceManager() {
+    public OJBPersistenceManager()
+    {
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.PersistenceManager#init
      */
-    public void init(PMContext context) throws Exception {
-        if (initialized) {
+    public void init(PMContext context) throws Exception
+    {
+        // FIXME: A config param to set the broker name would be handy
+        if (initialized)
+        {
             throw new IllegalStateException("already initialized");
         }
-        broker = PersistenceBrokerFactory.defaultPersistenceBroker();
         initialized = true;
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.PersistenceManager#close
      */
-    public void close() throws Exception {
+    public void close() throws Exception
+    {
+        // Nothing to do
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.PersistenceManager#load(NodeId)
      */
     public NodeState load(NodeId nodeId) throws NoSuchItemStateException,
-        ItemStateException {
-        log.debug("Request for " + nodeId.getUUID());
-        OJBNodeState nodeState = new OJBNodeState();
-        nodeState.setUuid(nodeId.getUUID());
-        QueryByIdentity query = new QueryByIdentity(nodeState);
-        OJBNodeState result = (OJBNodeState) broker.getObjectByQuery(query);
-        if (result == null) {
-            throw new NoSuchItemStateException(nodeId.getUUID());
+            ItemStateException
+    {
+        PersistenceBroker broker = PersistenceBrokerFactory
+                .defaultPersistenceBroker();
+        try
+        {
+            log.debug("Request for " + nodeId.getUUID());
+            OJBNodeState nodeState = new OJBNodeState();
+            nodeState.setUuid(nodeId.getUUID());
+            QueryByIdentity query = new QueryByIdentity(nodeState);
+            OJBNodeState result = (OJBNodeState) broker.getObjectByQuery(query);
+            if (result == null)
+            {
+                throw new NoSuchItemStateException(nodeId.getUUID());
+            }
+            NodeState state = createNew(nodeId);
+            result.toPersistentNodeState(state);
+            return state;
+        } catch (PersistenceBrokerException e)
+        {
+            throw new ItemStateException(e);
+        } finally
+        {
+            if (broker != null)
+                broker.close();
         }
-        NodeState state = createNew(nodeId);
-        result.toPersistentNodeState(state);
-        return state;
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.PersistenceManager#load(PropertyId)
      */
-    public PropertyState load(PropertyId propId) throws
-        NoSuchItemStateException, ItemStateException {
-        log.debug("Request for property " + propId);
-        ORMPropertyState propState = new ORMPropertyState(propId);
-        QueryByIdentity query = new QueryByIdentity(propState);
-        PropertyState state = createNew(propId);
-        ORMPropertyState result = (ORMPropertyState) broker.getObjectByQuery(
-            query);
-        if (result == null) {
-            throw new NoSuchItemStateException("Couldn't find property " +
-                                               propId);
-        }
-        result.toPersistentPropertyState(state);
-        if (result.getType().intValue() == PropertyType.BINARY) {
-            // we must now load the binary values.
-            ArrayList internalValueList = new ArrayList();
-            Criteria criteria = new Criteria();
-            criteria.addEqualTo("parentUUID", state.getParentUUID());
-            criteria.addEqualTo("propertyName", state.getName().toString());
-            QueryByCriteria blobQuery = new QueryByCriteria(ORMBlobValue.class,
-                criteria);
-            Iterator resultIter = broker.getCollectionByQuery(blobQuery).
-                iterator();
-            while (resultIter.hasNext()) {
-                ORMBlobValue ormBlobValue = (ORMBlobValue) resultIter.next();
-                ByteArrayInputStream in = new ByteArrayInputStream(ormBlobValue.
-                    getBlobValue());
-                try {
-                    BLOBFileValue blobValue = new BLOBFileValue(in);
-                    internalValueList.add(blobValue);
-                } catch (Throwable t) {
-                    throw new ItemStateException(
-                        "Error while trying to load blob value", t);
+    public PropertyState load(PropertyId propId)
+            throws NoSuchItemStateException, ItemStateException
+    {
+        PersistenceBroker broker = PersistenceBrokerFactory
+                .defaultPersistenceBroker();
+        try
+        {
+            log.debug("Request for property " + propId);
+            ORMPropertyState propState = new ORMPropertyState(propId);
+            QueryByIdentity query = new QueryByIdentity(propState);
+            PropertyState state = createNew(propId);
+            ORMPropertyState result = (ORMPropertyState) broker
+                    .getObjectByQuery(query);
+            if (result == null)
+            {
+                throw new NoSuchItemStateException("Couldn't find property "
+                        + propId);
+            }
+            result.toPersistentPropertyState(state);
+            if (result.getType().intValue() == PropertyType.BINARY)
+            {
+                // we must now load the binary values.
+                ArrayList internalValueList = new ArrayList();
+                Criteria criteria = new Criteria();
+                criteria.addEqualTo("parentUUID", state.getParentUUID());
+                criteria.addEqualTo("propertyName", state.getName().toString());
+                QueryByCriteria blobQuery = new QueryByCriteria(
+                        ORMBlobValue.class, criteria);
+                Iterator resultIter = broker.getCollectionByQuery(blobQuery)
+                        .iterator();
+                while (resultIter.hasNext())
+                {
+                    ORMBlobValue ormBlobValue = (ORMBlobValue) resultIter
+                            .next();
+                    ByteArrayInputStream in = new ByteArrayInputStream(
+                            ormBlobValue.getBlobValue());
+                    try
+                    {
+                        internalValueList.add(InternalValue.create(in));
+                    } catch (Throwable t)
+                    {
+                        throw new ItemStateException(
+                                "Error while trying to load blob value", t);
+                    }
                 }
+                state.setValues((InternalValue[]) internalValueList
+                        .toArray(new InternalValue[internalValueList.size()]));
             }
-            state.setValues( (InternalValue[]) internalValueList.toArray(new
-                InternalValue[internalValueList.size()]));
+            return state;
+        } catch (PersistenceBrokerException e)
+        {
+            throw new ItemStateException(e);
+        } finally
+        {
+            if (broker != null)
+                broker.close();
         }
-        return state;
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.PersistenceManager#load(NodeReferencesId)
      */
-    public NodeReferences load(NodeReferencesId targetId) throws
-        NoSuchItemStateException, ItemStateException {
-        ORMNodeReference nodeRef = new ORMNodeReference();
-        nodeRef.setTargetId(targetId.toString());
-        QueryByCriteria query = new QueryByCriteria(nodeRef);
-        Iterator resultIter = broker.getCollectionByQuery(query).iterator();
-        NodeReferences refs = new NodeReferences(targetId);
-        while (resultIter.hasNext()) {
-            ORMNodeReference curNodeReference = (ORMNodeReference) resultIter.
-                next();
-            refs.addReference(new PropertyId(curNodeReference.
-                                             getPropertyParentUUID(),
-                                             QName.valueOf(curNodeReference.
-                getPropertyName())));
+    public NodeReferences load(NodeReferencesId targetId)
+            throws NoSuchItemStateException, ItemStateException
+    {
+        PersistenceBroker broker = PersistenceBrokerFactory
+                .defaultPersistenceBroker();
+        try
+        {
+            ORMNodeReference nodeRef = new ORMNodeReference();
+            nodeRef.setTargetId(targetId.toString());
+            QueryByCriteria query = new QueryByCriteria(nodeRef);
+            Iterator resultIter = broker.getCollectionByQuery(query).iterator();
+            NodeReferences refs = new NodeReferences(targetId);
+            while (resultIter.hasNext())
+            {
+                ORMNodeReference curNodeReference = (ORMNodeReference) resultIter
+                        .next();
+                refs.addReference(new PropertyId(curNodeReference
+                        .getPropertyParentUUID(), QName
+                        .valueOf(curNodeReference.getPropertyName())));
+            }
+            return refs;
+        } catch (PersistenceBrokerException e)
+        {
+            throw new ItemStateException(e);
+        } finally
+        {
+            if (broker != null)
+                broker.close();
         }
-        return refs;
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.PersistenceManager#exists(NodeId)
      */
-    public boolean exists(NodeId id) throws ItemStateException {
-        OJBNodeState nodeState = new OJBNodeState(id);
-        QueryByIdentity query = new QueryByIdentity(nodeState);
-        OJBNodeState result = (OJBNodeState) broker.getObjectByQuery(query);
-        if (result == null) {
-            return false;
-        } else {
-            return true;
+    public boolean exists(NodeId id) throws ItemStateException
+    {
+        PersistenceBroker broker = PersistenceBrokerFactory
+                .defaultPersistenceBroker();
+        try
+        {
+            OJBNodeState nodeState = new OJBNodeState(id);
+            QueryByIdentity query = new QueryByIdentity(nodeState);
+            OJBNodeState result = (OJBNodeState) broker.getObjectByQuery(query);
+            if (result == null)
+            {
+                return false;
+            } else
+            {
+                return true;
+            }
+        } catch (PersistenceBrokerException e)
+        {
+            throw new ItemStateException(e);
+        } finally
+        {
+            if (broker != null)
+                broker.close();
         }
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.PersistenceManager#exists(PropertyId)
      */
-    public boolean exists(PropertyId id) throws ItemStateException {
-        ORMPropertyState propState = new ORMPropertyState(id);
-        // QueryByCriteria query = new QueryByCriteria(propState);
-        QueryByIdentity query = new QueryByIdentity(propState);
-        ORMPropertyState result = (ORMPropertyState) broker.
-            getObjectByQuery(query);
-        if (result == null) {
-            return false;
-        } else {
-            return true;
+    public boolean exists(PropertyId id) throws ItemStateException
+    {
+        PersistenceBroker broker = PersistenceBrokerFactory
+                .defaultPersistenceBroker();
+        try
+        {
+            ORMPropertyState propState = new ORMPropertyState(id);
+            // QueryByCriteria query = new QueryByCriteria(propState);
+            QueryByIdentity query = new QueryByIdentity(propState);
+            ORMPropertyState result = (ORMPropertyState) broker
+                    .getObjectByQuery(query);
+            if (result == null)
+            {
+                return false;
+            } else
+            {
+                return true;
+            }
+        } catch (PersistenceBrokerException e)
+        {
+            throw new ItemStateException(e);
+        } finally
+        {
+            if (broker != null)
+                broker.close();
         }
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.PersistenceManager#exists(NodeReferencesId)
      */
-    public boolean exists(NodeReferencesId targetId)
-        throws ItemStateException {
-        ORMNodeReference nodeRef = new ORMNodeReference();
-        nodeRef.setTargetId(targetId.toString());
-        QueryByCriteria query = new QueryByCriteria(nodeRef);
-        Iterator resultIter = broker.getCollectionByQuery(query).iterator();
-        NodeReferences refs = new NodeReferences(targetId);
-        if (resultIter.hasNext()) {
-            return true;
+    public boolean exists(NodeReferencesId targetId) throws ItemStateException
+    {
+        PersistenceBroker broker = PersistenceBrokerFactory
+                .defaultPersistenceBroker();
+        try
+        {
+
+            ORMNodeReference nodeRef = new ORMNodeReference();
+            nodeRef.setTargetId(targetId.toString());
+            QueryByCriteria query = new QueryByCriteria(nodeRef);
+            Iterator resultIter = broker.getCollectionByQuery(query).iterator();
+            NodeReferences refs = new NodeReferences(targetId);
+            if (resultIter.hasNext())
+            {
+                return true;
+            }
+            return false;
+        } catch (PersistenceBrokerException e)
+        {
+            throw new ItemStateException(e);
+        } finally
+        {
+            if (broker != null)
+                broker.close();
         }
-        return false;
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#store(NodeState)
      */
-    public void store(NodeState state) throws ItemStateException {
+    private void store(NodeState state, PersistenceBroker broker)
+            throws ItemStateException
+    {
         log.debug("Request to store node " + state.getId());
-        broker.beginTransaction();
         OJBNodeState nodeState = new OJBNodeState(state.getId());
         QueryByIdentity query = new QueryByIdentity(nodeState);
         OJBNodeState result = (OJBNodeState) broker.getObjectByQuery(query);
-        if (result == null) {
+        if (result == null)
+        {
             result = new OJBNodeState();
         }
         result.fromPersistentNodeState(state);
         broker.store(result);
-        broker.commitTransaction();
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#store(PropertyState)
      */
-    public void store(PropertyState state) throws ItemStateException {
+    private void store(PropertyState state, PersistenceBroker broker)
+            throws ItemStateException
+    {
         log.debug("Request to store property " + state.getId());
-        broker.beginTransaction();
         ORMPropertyState propState = new ORMPropertyState(state.getId());
         QueryByIdentity query = new QueryByIdentity(propState);
-        ORMPropertyState result = (ORMPropertyState) broker.getObjectByQuery(
-            query);
-        if (result == null) {
+        ORMPropertyState result = (ORMPropertyState) broker
+                .getObjectByQuery(query);
+        if (result == null)
+        {
             result = new ORMPropertyState();
         }
         result.fromPersistentPropertyState(state);
 
         InternalValue[] values = state.getValues();
-        if (values != null) {
-            for (int i = 0; i < values.length; i++) {
+        if (values != null)
+        {
+            for (int i = 0; i < values.length; i++)
+            {
                 InternalValue val = values[i];
-                if (val != null) {
-                    if (state.getType() == PropertyType.BINARY) {
+                if (val != null)
+                {
+                    if (state.getType() == PropertyType.BINARY)
+                    {
                         Criteria criteria = new Criteria();
-                        criteria.addEqualTo("parentUUID", state.getParentUUID());
-                        criteria.addEqualTo("propertyName",
-                                            state.getName().toString());
+                        criteria
+                                .addEqualTo("parentUUID", state.getParentUUID());
+                        criteria.addEqualTo("propertyName", state.getName()
+                                .toString());
                         criteria.addEqualTo("index", new Integer(i));
                         QueryByCriteria blobQuery = new QueryByCriteria(
-                            ORMBlobValue.class, criteria);
+                                ORMBlobValue.class, criteria);
                         Iterator resultIter = broker.getCollectionByQuery(
-                            blobQuery).iterator();
+                                blobQuery).iterator();
                         ORMBlobValue ormBlobValue = null;
-                        if (resultIter.hasNext()) {
+                        if (resultIter.hasNext())
+                        {
                             ormBlobValue = (ORMBlobValue) resultIter.next();
-                        } else {
+                        } else
+                        {
                             ormBlobValue = new ORMBlobValue();
                             ormBlobValue.setParentUUID(state.getParentUUID());
-                            ormBlobValue.setPropertyName(state.getName().
-                                toString());
+                            ormBlobValue.setPropertyName(state.getName()
+                                    .toString());
                             ormBlobValue.setIndex(new Integer(i));
                         }
-                        BLOBFileValue blobVal = (BLOBFileValue) val.
-                            internalValue();
+                        BLOBFileValue blobVal = (BLOBFileValue) val
+                                .internalValue();
                         result.setValues("");
                         ByteArrayOutputStream out = new ByteArrayOutputStream();
-                        try {
+                        try
+                        {
                             blobVal.spool(out);
-                        } catch (Throwable t) {
-                            broker.abortTransaction();
+                        } catch (Throwable t)
+                        {
+                            // The caller is responsible of aborting the
+                            // transaction
+                            // broker.abortTransaction();
                             throw new ItemStateException(t.getMessage(), t);
                         }
                         ormBlobValue.setSize(new Long(blobVal.getLength()));
@@ -279,76 +391,180 @@
             }
         }
         broker.store(result);
-        broker.commitTransaction();
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#store(NodeReferences)
      */
-    public void store(NodeReferences refs) throws ItemStateException {
+    private void store(NodeReferences refs, PersistenceBroker broker)
+            throws ItemStateException
+    {
+        // destroy all the references before saving
+        destroy(refs, broker);
+        
         Iterator nodeRefPropIdIter = refs.getReferences().iterator();
-        broker.beginTransaction();
-        while (nodeRefPropIdIter.hasNext()) {
+        while (nodeRefPropIdIter.hasNext())
+        {
             PropertyId curPropertyId = (PropertyId) nodeRefPropIdIter.next();
-            ORMNodeReference curNodeReference = new ORMNodeReference(refs.
-                getTargetId().toString(), curPropertyId.getParentUUID(),
-                curPropertyId.getName().toString());
+            ORMNodeReference curNodeReference = new ORMNodeReference(refs
+                    .getTargetId().toString(), curPropertyId.getParentUUID(),
+                    curPropertyId.getName().toString());
             broker.store(curNodeReference);
         }
-        broker.commitTransaction();
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#destroy(NodeState)
      */
-    public void destroy(NodeState state) throws ItemStateException {
+    private void destroy(NodeState state, PersistenceBroker broker)
+            throws ItemStateException
+    {
         log.debug("Deleting node " + state.getId());
-        broker.beginTransaction();
+        
+        // Destroy node
         OJBNodeState nodeState = new OJBNodeState(state.getId());
         QueryByIdentity query = new QueryByIdentity(nodeState);
         OJBNodeState result = (OJBNodeState) broker.getObjectByQuery(query);
-        if (result == null) {
+        if (result == null)
+        {
             result = new OJBNodeState();
         }
         broker.delete(result);
-        broker.commitTransaction();
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#destroy(PropertyState)
      */
-    public void destroy(PropertyState state) throws
-        ItemStateException {
+    private void destroy(PropertyState state, PersistenceBroker broker)
+            throws ItemStateException
+    {
         ORMPropertyState propState = new ORMPropertyState(state);
-        broker.beginTransaction();
         broker.delete(propState);
-        if (state.getType() == PropertyType.BINARY) {
+        if (state.getType() == PropertyType.BINARY)
+        {
             Criteria criteria = new Criteria();
             criteria.addEqualTo("parentUUID", state.getParentUUID());
             criteria.addEqualTo("propertyName", state.getName().toString());
             QueryByCriteria blobQuery = new QueryByCriteria(ORMBlobValue.class,
-                criteria);
+                    criteria);
             broker.deleteByQuery(blobQuery);
         }
-        broker.commitTransaction();
     }
 
     /**
      * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#destroy(NodeReferences)
      */
-    public void destroy(NodeReferences refs) throws ItemStateException {
-        broker.beginTransaction();
+    private void destroy(NodeReferences refs, PersistenceBroker broker)
+            throws ItemStateException
+    {
         ORMNodeReference nodeRef = new ORMNodeReference();
         nodeRef.setTargetId(refs.getTargetId().toString());
         QueryByCriteria query = new QueryByCriteria(nodeRef);
         Iterator resultIter = broker.getCollectionByQuery(query).iterator();
-        while (resultIter.hasNext()) {
-            ORMNodeReference curNodeReference = (ORMNodeReference) resultIter.
-                next();
+        while (resultIter.hasNext())
+        {
+            ORMNodeReference curNodeReference = (ORMNodeReference) resultIter
+                    .next();
             broker.delete(curNodeReference);
         }
-        broker.commitTransaction();
     }
 
+    /**
+     * @see PersistenceManager#createNew
+     */
+    public NodeState createNew(NodeId id)
+    {
+        return new NodeState(id.getUUID(), null, null, NodeState.STATUS_NEW,
+                false);
+    }
+
+    /**
+     * @see PersistenceManager#createNew
+     */
+    public PropertyState createNew(PropertyId id)
+    {
+        return new PropertyState(id.getName(), id.getParentUUID(),
+                PropertyState.STATUS_NEW, false);
+    }
+
+    /**
+     * @see PersistenceManager#store(ChangeLog)
+     * 
+     * This method ensures that changes are either written completely to the
+     * underlying persistence layer, or not at all.
+     */
+    public void store(ChangeLog changeLog) throws ItemStateException
+    {
+        PersistenceBroker broker = PersistenceBrokerFactory
+                .defaultPersistenceBroker();
+        try
+        {
+            broker.beginTransaction() ;
+            Iterator iter = changeLog.deletedStates();
+            while (iter.hasNext())
+            {
+                ItemState state = (ItemState) iter.next();
+                if (state.isNode())
+                {
+                    destroy((NodeState) state, broker);
+                } else
+                {
+                    destroy((PropertyState) state, broker);
+                }
+            }
+            iter = changeLog.addedStates();
+            while (iter.hasNext())
+            {
+                ItemState state = (ItemState) iter.next();
+                if (state.isNode())
+                {
+                    store((NodeState) state, broker);
+                } else
+                {
+                    store((PropertyState) state, broker);
+                }
+            }
+            iter = changeLog.modifiedStates();
+            while (iter.hasNext())
+            {
+                ItemState state = (ItemState) iter.next();
+                if (state.isNode())
+                {
+                    store((NodeState) state, broker);
+                } else
+                {
+                    store((PropertyState) state, broker);
+                }
+            }
+            iter = changeLog.modifiedRefs();
+            while (iter.hasNext())
+            {
+                NodeReferences refs = (NodeReferences) iter.next();
+                if (refs.hasReferences())
+                {
+                    store(refs, broker);
+                } else
+                {
+                    destroy(refs, broker);
+                }
+            }
+            broker.commitTransaction() ;
+        } catch (ItemStateException e)
+        {
+            if (broker != null)
+                broker.abortTransaction();
+            throw e;
+        } catch (PersistenceBrokerException e)
+        {
+            if (broker != null)
+                broker.abortTransaction();
+            throw new ItemStateException("Unable to store", e);
+        } finally
+        {
+            if (broker != null)
+                broker.close();
+        }
+
+    }
 
 }



Mime
View raw message