jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r432582 - in /jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state: ItemState.java NodeState.java PropertyState.java SessionItemStateManager.java
Date Fri, 18 Aug 2006 13:43:42 GMT
Author: mreutegg
Date: Fri Aug 18 06:43:42 2006
New Revision: 432582

URL: http://svn.apache.org/viewvc?rev=432582&view=rev
Log:
- Implement ItemState.revert() and use in SessionItemStateManager.undo()
- Removed some JR diffs

Modified:
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/ItemState.java
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/NodeState.java
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/PropertyState.java
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/SessionItemStateManager.java

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/ItemState.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/ItemState.java?rev=432582&r1=432581&r2=432582&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/ItemState.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/ItemState.java
Fri Aug 18 06:43:42 2006
@@ -23,6 +23,7 @@
 import org.slf4j.LoggerFactory;
 
 import java.util.Collection;
+import java.util.Set;
 
 /**
  * <code>ItemState</code> represents the state of an <code>Item</code>.
@@ -475,6 +476,15 @@
     public abstract void remove() throws ItemStateException;
 
     /**
+     * Reverts this item state to its initial status and adds itself to the Set
+     * of <code>affectedItemStates</code> if it reverted itself.
+     *
+     * @param affectedItemStates the set of affected item states that reverted
+     *                           themselfes.
+     */
+    public abstract void revert(Set affectedItemStates);
+
+    /**
      * Add an <code>ItemStateListener</code>
      *
      * @param listener the new listener to be informed on modifications
@@ -503,8 +513,8 @@
      */
     public void stateCreated(ItemState created) {
         // underlying state has been permanently created
-        setStatus(STATUS_EXISTING); // TODO: shouldn't the status change happen after pull?
         pull();
+        setStatus(STATUS_EXISTING);
     }
 
     /**

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/NodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/NodeState.java?rev=432582&r1=432581&r2=432582&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/NodeState.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/NodeState.java
Fri Aug 18 06:43:42 2006
@@ -590,6 +590,101 @@
     }
 
     /**
+     * Reverts all property and child node states that belong to this
+     * <code>NodeState</code> and finally reverts this <code>NodeState</code>.
+     *
+     * @inheritDoc
+     * @see ItemState#revert(Set)
+     */
+    public void revert(Set affectedItemStates) {
+        if (overlayedState == null) {
+            throw new IllegalStateException("revert cannot be called on workspace state");
+        }
+        // copy to new list, when a property is reverted it may call this node
+        // state to remove itself from properties.
+        List props = new ArrayList(properties.values());
+        for (Iterator it = props.iterator(); it.hasNext(); ) {
+            PropertyReference ref = (PropertyReference) it.next();
+            if (ref.isResolved()) {
+                try {
+                    PropertyState propState = ref.getPropertyState();
+                    propState.revert(affectedItemStates);
+                } catch (ItemStateException e) {
+                    // should not happen because PropertyReference is resolved
+                    log.warn("Unable to get PropertyState from resolved PropertyReference");
+                }
+            } else {
+                // not touched or accessed before
+            }
+        }
+
+        // revert property states in attic
+        props.clear();
+        props.addAll(propertiesInAttic.values());
+        for (Iterator it = props.iterator(); it.hasNext(); ) {
+            PropertyReference ref = (PropertyReference) it.next();
+            try {
+                PropertyState propState = ref.getPropertyState();
+                propState.revert(affectedItemStates);
+            } catch (ItemStateException e) {
+                // probably stale destroyed property
+                // cleaned up when propertiesInAttic is cleared
+            }
+        }
+        propertiesInAttic.clear();
+
+        // now revert child node states
+        List children = new ArrayList(childNodeEntries);
+        for (Iterator it = children.iterator(); it.hasNext(); ) {
+            ChildNodeReference ref = (ChildNodeReference) it.next();
+            if (ref.isResolved()) {
+                try {
+                    NodeState nodeState = ref.getNodeState();
+                    nodeState.revert(affectedItemStates);
+                } catch (ItemStateException e) {
+                    // should not happen because ChildNodeReference is resolved
+                    log.warn("Unable to get NodeState from resolved ChildNodeReference");
+                }
+            } else {
+                // not touched or accessed before
+            }
+        }
+
+        // now revert this node state
+        switch (status) {
+            case STATUS_EXISTING:
+                // nothing to do
+                break;
+            case STATUS_EXISTING_MODIFIED:
+            case STATUS_EXISTING_REMOVED:
+            case STATUS_STALE_MODIFIED:
+                // revert state from overlayed
+                pull();
+                setStatus(STATUS_EXISTING);
+                affectedItemStates.add(this);
+                break;
+            case STATUS_NEW:
+                // set removed
+                setStatus(STATUS_REMOVED);
+                // remove from parent
+                parent.childNodeStateRemoved(this);
+                affectedItemStates.add(this);
+                break;
+            case STATUS_REMOVED:
+                // shouldn't happen actually, because a 'removed' state is not
+                // accessible anymore
+                log.warn("trying to revert an already removed node state");
+                parent.childNodeStateRemoved(this);
+                break;
+            case STATUS_STALE_DESTROYED:
+                // overlayed state does not exist anymore
+                parent.childNodeStateRemoved(this);
+                affectedItemStates.add(this);
+                break;
+        }
+    }
+
+    /**
      * Determines if there is a property entry with the specified
      * <code>QName</code>.
      *

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/PropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/PropertyState.java?rev=432582&r1=432581&r2=432582&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/PropertyState.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/PropertyState.java
Fri Aug 18 06:43:42 2006
@@ -32,6 +32,8 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.Set;
+
 /**
  * <code>PropertyState</code> represents the state of a <code>Property</code>.
  */
@@ -105,6 +107,47 @@
             setStatus(STATUS_EXISTING_REMOVED);
         }
         parent.propertyStateRemoved(this);
+    }
+
+    /**
+     * @inheritDoc
+     * @see ItemState#revert(Set)
+     */
+    public void revert(Set affectedItemStates) {
+        if (overlayedState == null) {
+            throw new IllegalStateException("revert cannot be called on workspace state");
+        }
+        switch (status) {
+            case STATUS_EXISTING:
+                // nothing to do
+                break;
+            case STATUS_EXISTING_MODIFIED:
+            case STATUS_EXISTING_REMOVED:
+            case STATUS_STALE_MODIFIED:
+                // revert state from overlayed
+                pull();
+                setStatus(STATUS_EXISTING);
+                affectedItemStates.add(this);
+                break;
+            case STATUS_NEW:
+                // set removed
+                setStatus(STATUS_REMOVED);
+                // and remove from parent
+                parent.propertyStateRemoved(this);
+                affectedItemStates.add(this);
+                break;
+            case STATUS_REMOVED:
+                // shouldn't happen actually, because a 'removed' state is not
+                // accessible anymore
+                log.warn("trying to revert an already removed property state");
+                parent.propertyStateRemoved(this);
+                break;
+            case STATUS_STALE_DESTROYED:
+                // overlayed does not exist anymore
+                parent.propertyStateRemoved(this);
+                affectedItemStates.add(this);
+                break;
+        }
     }
 
     /**

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/SessionItemStateManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/SessionItemStateManager.java?rev=432582&r1=432581&r2=432582&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/SessionItemStateManager.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/state/SessionItemStateManager.java
Fri Aug 18 06:43:42 2006
@@ -101,7 +101,6 @@
     /**
      * State manager for the transient items
      */
-    // DIFF JACKRABBIT: private final TransientItemStateManager transientStateMgr;
     private final TransientItemStateManager transientStateMgr;
 
     /**
@@ -122,7 +121,6 @@
                                    ItemStateValidator validator,
                                    NamespaceResolver nsResolver) {
         this.workspaceItemStateMgr = workspaceItemStateMgr;
-        // DIFF JACKRABBIT: this.transientStateMgr = new TransientItemStateManager();
         this.transientStateMgr = new TransientItemStateManager(idFactory, workspaceItemStateMgr);
         // DIFF JR: validator added
         this.validator = validator;
@@ -303,99 +301,16 @@
      *                            to be canceled as well in another sub-tree.
      */
     public void undo(ItemState itemState) throws ItemStateException {
-        if (itemState.getParent() == null) {
-            // optimization for root
-            transientStateMgr.disposeAllItemStates();
-            return;
-        }
-
-        // list of transient items that should be discarded
-        ChangeLog changeLog = new ChangeLog();
-
-        // check status of current item's state
-        if (itemState.isTransient()) {
-            switch (itemState.getStatus()) {
-                // safety... should have been checked befoe
-                case ItemState.STATUS_NEW:
-                    throw new ItemStateException("Unexpected state: cannot start undo from
a new item state.");
-
-                case ItemState.STATUS_STALE_MODIFIED:
-                case ItemState.STATUS_STALE_DESTROYED:
-                case ItemState.STATUS_EXISTING_MODIFIED:
-                    // add this item's state to the list
-                    changeLog.modified(itemState);
-                    break;
-                default:
-                    log.debug("unexpected state status (" + itemState.getStatus() + ")");
-                    // ignore
-                    break;
-            }
-        }
-
-        if (itemState.isNode()) {
-            NodeState nodeState = (NodeState)itemState;
-            // build list of 'new', 'modified' or 'stale' descendants
-            Iterator iter = getDescendantTransientItemStates(nodeState);
-            while (iter.hasNext()) {
-                ItemState childState = (ItemState) iter.next();
-                switch (childState.getStatus()) {
-                    case ItemState.STATUS_STALE_MODIFIED:
-                    case ItemState.STATUS_STALE_DESTROYED:
-                    case ItemState.STATUS_NEW:
-                    case ItemState.STATUS_EXISTING_MODIFIED:
-                        // add new or modified state to the list
-                        changeLog.modified(childState);
-                        break;
-
-                    default:
-                        log.debug("unexpected state status (" + childState.getStatus() +
")");
-                        // ignore
-                        break;
-                }
-            }
-
-            // build list of deleted states
-            Iterator atticIter = getDescendantTransientItemStatesInAttic(nodeState);
-            while (atticIter.hasNext()) {
-                ItemState transientState = (ItemState) atticIter.next();
-                changeLog.deleted(transientState);
-            }
-        }
+        // TODO: check if self contained
 
-        /**
-         * build set of item id's which are within the scope of
-         * (i.e. affected by) this cancel operation
-         */
-        Set affectedStates = new HashSet();
-        Iterator it = new IteratorChain(changeLog.modifiedStates(), changeLog.deletedStates());
-        while (it.hasNext()) {
-            affectedStates.add(it.next());
-        }
-        collectOperations(affectedStates, changeLog);
+        Set affectedItemStates = new HashSet();
+        itemState.revert(affectedItemStates);
 
-        // process list of 'new', 'modified' or 'stale' transient states
-        Iterator transIter = changeLog.modifiedStates();
-        while (transIter.hasNext()) {
-            // dispose the transient state, it is no longer used;
-            // this will indirectly (through stateDiscarded listener method)
-            // either restore or permanently invalidate the wrapping Item instances
-            ItemState transientState = (ItemState) transIter.next();
-            transientStateMgr.disposeItemState(transientState);
-        }
-        // process list of deleted states
-        Iterator remIter = changeLog.deletedStates();
-        while (remIter.hasNext()) {
-            ItemState rmState = (ItemState) remIter.next();
-            // dispose the transient state; this will indirectly (through
-            // stateDiscarded listener method) resurrect the wrapping Item instances
-            transientStateMgr.disposeItemStateInAttic(rmState);
-        }
+        ChangeLog changeLog = new ChangeLog();
+        collectOperations(affectedItemStates, changeLog);
 
         // remove all canceled operations
-        Iterator opIter = changeLog.getOperations();
-        while (opIter.hasNext()) {
-            transientStateMgr.removeOperation((Operation) opIter.next());
-        }
+        transientStateMgr.disposeOperations(changeLog.getOperations());
     }
 
     /**
@@ -534,7 +449,6 @@
      * @return an iterator over descendant transient item state instances in the attic
      */
     private Iterator getDescendantTransientItemStatesInAttic(NodeState parent) {
-        // DIFF JACKRABBIT: if (!transientStateMgr.hasAnyItemStatesInAttic()) {
         if (!transientStateMgr.hasDeletedItemStates()) {
             return Collections.EMPTY_LIST.iterator();
         }



Mime
View raw message