You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by dp...@apache.org on 2007/10/09 15:17:38 UTC

svn commit: r583151 - in /jackrabbit/trunk: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/CachingHierarchyManager.java jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/PathMap.java

Author: dpfister
Date: Tue Oct  9 06:17:35 2007
New Revision: 583151

URL: http://svn.apache.org/viewvc?rev=583151&view=rev
Log:
JCR-1167: Paths not correct after reordering children

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/CachingHierarchyManager.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/PathMap.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/CachingHierarchyManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/CachingHierarchyManager.java?rev=583151&r1=583150&r2=583151&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/CachingHierarchyManager.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/CachingHierarchyManager.java Tue Oct  9 06:17:35 2007
@@ -34,6 +34,7 @@
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
+import java.util.HashMap;
 import java.io.PrintStream;
 
 import javax.jcr.ItemNotFoundException;
@@ -381,8 +382,11 @@
         synchronized (cacheMonitor) {
             LRUEntry entry = (LRUEntry) idCache.get(state.getNodeId());
             if (entry != null) {
-                PathMap.Element element = entry.getElement();
-                Iterator iter = element.getChildren();
+                PathMap.Element parent = entry.getElement();
+                HashMap newChildrenOrder = new HashMap();
+                boolean orderChanged = false;
+
+                Iterator iter = parent.getChildren();
                 while (iter.hasNext()) {
                     PathMap.Element child = (PathMap.Element) iter.next();
                     LRUEntry childEntry = (LRUEntry) child.get();
@@ -405,14 +409,24 @@
                         remove(child);
                         continue;
                     }
-                    if (!cne.getName().equals(child.getName()) ||
-                            cne.getIndex() != child.getNormalizedIndex()) {
-                        /* Child still exists but at a different position */
-                        element.move(child.getPathElement(),
-                                Path.PathElement.create(cne.getName(), cne.getIndex()));
-                        continue;
+
+                    /**
+                     * Put all children into map of new children order - regardless
+                     * whether their position changed or not - as we might need
+                     * to reorder them later on.
+                     */
+                    Path.PathElement newNameIndex = Path.PathElement.create(
+                            cne.getName(), cne.getIndex());
+                    newChildrenOrder.put(newNameIndex, child);
+
+                    if (!newNameIndex.equals(child.getPathElement())) {
+                        orderChanged = true;
                     }
-                    /* At this point the child's position is still valid */
+                }
+
+                if (orderChanged) {
+                    /* If at least one child changed its position, reorder */
+                    parent.setChildren(newChildrenOrder);
                 }
             }
         }

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/PathMap.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/PathMap.java?rev=583151&r1=583150&r2=583151&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/PathMap.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/PathMap.java Tue Oct  9 06:17:35 2007
@@ -353,23 +353,31 @@
         }
 
         /**
-         * Move a child of this element to a different location inside the
-         * same parent.
+         * Sets a new list of children of this element.
          *
-         * @param oldNameIndex old name/index 
-         * @param newNameIndex new name/index
-         * @return <code>true</code> if the element was successfully moved;
-         *         otherwise <code>false</code>
+         * @param children map of children; keys are of type
+         *                 <code>Path.PathElement</code> and values
+         *                 are of type <code>Element</code>
          */
-        public boolean move(Path.PathElement oldNameIndex, 
-                            Path.PathElement newNameIndex) {
-            
-            Element child = remove(oldNameIndex, false, false);
-            if (child != null) {
-                put(newNameIndex, child);
-                return true;
+        public void setChildren(Map children) {
+            // Remove all children without removing the element itself
+            this.children = null;
+            childrenCount = 0;
+
+            // Now add back all items
+            Iterator entries = children.entrySet().iterator();
+            while (entries.hasNext()) {
+                Map.Entry entry = (Map.Entry) entries.next();
+
+                Path.PathElement nameIndex = (Path.PathElement) entry.getKey();
+                Element element = (Element) entry.getValue();
+                put(nameIndex, element);
+            }
+
+            // Special case: if map was empty, handle like removeAll()
+            if (childrenCount == 0 && obj == null && parent != null) {
+                parent.remove(getPathElement(), false, true);
             }
-            return false;
         }
 
         /**