jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1227230 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/ main/java/org/apache/jackrabbit/mk/model/ main/java/org/apache/jackrabbit/mk/util/ test/java/org/apache/jackrabbit/mk/ test/java/org/apache/jackrabbit/mk...
Date Wed, 04 Jan 2012 17:27:50 GMT
Author: stefan
Date: Wed Jan  4 17:27:50 2012
New Revision: 1227230

URL: http://svn.apache.org/viewvc?rev=1227230&view=rev
Log:
- fixed PathUtils.isDescendantOf
- fixed OOM caused by '> /parent : /parent/child'

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/CommitBuilder.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/MutableNode.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java?rev=1227230&r1=1227229&r2=1227230&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
Wed Jan  4 17:27:50 2012
@@ -406,18 +406,33 @@ public class MicroKernelImpl implements 
                         pos = t.getLastPos();
                         String subPath = t.readString();
                         t.read(':');
-                        t.read('{');
-                        String nodePath = PathUtils.concat(path, subPath);
-                        if (!PathUtils.isAbsolute(nodePath)) {
-                            throw new Exception("absolute path expected: " + nodePath + ",
pos: " + pos);
-                        }
-                        String parentPath = PathUtils.getParentPath(nodePath);
-                        String nodeName = PathUtils.getName(nodePath);
-                        // build the list of added nodes recursively
-                        LinkedList<AddNodeOperation> list = new LinkedList<AddNodeOperation>();
-                        addNode(list, parentPath, nodeName, t);
-                        for (AddNodeOperation op : list) {
-                            cb.addNode(op.path, op.name, op.props);
+                        if (t.matches('{')) {
+                            String nodePath = PathUtils.concat(path, subPath);
+                            if (!PathUtils.isAbsolute(nodePath)) {
+                                throw new Exception("absolute path expected: " + nodePath
+ ", pos: " + pos);
+                            }
+                            String parentPath = PathUtils.getParentPath(nodePath);
+                            String nodeName = PathUtils.getName(nodePath);
+                            // build the list of added nodes recursively
+                            LinkedList<AddNodeOperation> list = new LinkedList<AddNodeOperation>();
+                            addNode(list, parentPath, nodeName, t);
+                            for (AddNodeOperation op : list) {
+                                cb.addNode(op.path, op.name, op.props);
+                            }
+                        } else {
+                            String value;
+                            if (t.matches(JsopTokenizer.NULL)) {
+                                value = null;
+                            } else {
+                                value = t.readRawValue().trim();
+                            }
+                            String targetPath = PathUtils.concat(path, subPath);
+                            if (!PathUtils.isAbsolute(targetPath)) {
+                                throw new Exception("absolute path expected: " + targetPath
+ ", pos: " + pos);
+                            }
+                            String parentPath = PathUtils.getParentPath(targetPath);
+                            String propName = PathUtils.getName(targetPath);
+                            cb.setProperty(parentPath, propName, value);
                         }
                         break;
                     }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/CommitBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/CommitBuilder.java?rev=1227230&r1=1227229&r2=1227230&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/CommitBuilder.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/CommitBuilder.java
Wed Jan  4 17:27:50 2012
@@ -27,7 +27,6 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
 
 /**
  *
@@ -65,7 +64,7 @@ public class CommitBuilder {
         newChild.getProperties().putAll(properties);
 
         // id will be computed on commit
-        modParent.put(new ChildNodeEntry(nodeName, ""));
+        modParent.add(new ChildNodeEntry(nodeName, ""));
         String newPath = PathUtils.concat(parentNodePath, nodeName);
         staged.put(newPath, newChild);
         // update change log
@@ -89,6 +88,10 @@ public class CommitBuilder {
     }
 
     public void moveNode(String srcPath, String destPath) throws NotFoundException, Exception
{
+        if (PathUtils.isAncestor(srcPath, destPath)) {
+            throw new Exception("target path cannot be descendant of source path: " + destPath);
+        }
+
         String srcParentPath = PathUtils.getParentPath(srcPath);
         String srcNodeName = PathUtils.getName(srcPath);
 
@@ -96,6 +99,15 @@ public class CommitBuilder {
         String destNodeName = PathUtils.getName(destPath);
 
         MutableNode srcParent = getOrCreateStagedNode(srcParentPath);
+        if (srcParentPath.equals(destParentPath)) {
+            if (srcParent.getChildNodeEntry(destNodeName) != null) {
+                throw new Exception("node already exists at move destination path: " + destPath);
+            }
+            if (srcParent.rename(srcNodeName, destNodeName) == null) {
+                throw new NotFoundException(srcPath);
+            }
+            return;
+        }
         ChildNodeEntry srcCNE = srcParent.remove(srcNodeName);
         if (srcCNE == null) {
             throw new NotFoundException(srcPath);
@@ -105,7 +117,7 @@ public class CommitBuilder {
         if (destParent.getChildNodeEntry(destNodeName) != null) {
             throw new Exception("node already exists at move destination path: " + destPath);
         }
-        destParent.put(new ChildNodeEntry(destNodeName, srcCNE.getId()));
+        destParent.add(new ChildNodeEntry(destNodeName, srcCNE.getId()));
 
         // update staging area
         moveStagedNodes(srcPath, destPath);
@@ -128,7 +140,7 @@ public class CommitBuilder {
         }
 
         MutableNode destParent = getOrCreateStagedNode(destParentPath);
-        destParent.put(new ChildNodeEntry(destNodeName, srcCNE.getId()));
+        destParent.add(new ChildNodeEntry(destNodeName, srcCNE.getId()));
 
         // update change log
         changeLog.add(new CopyNode(srcPath, destPath));
@@ -304,7 +316,7 @@ public class CommitBuilder {
             if (PathUtils.denotesRoot(path)) {
                 rootNodeId = id;
             } else {
-                staged.get(PathUtils.getParentPath(path)).put(new ChildNodeEntry(PathUtils.getName(path),
id));
+                staged.get(PathUtils.getParentPath(path)).add(new ChildNodeEntry(PathUtils.getName(path),
id));
             }
         }
         if (rootNodeId == null) {
@@ -350,10 +362,10 @@ public class CommitBuilder {
 
         // todo fixme
         for (Map.Entry<String, String> entry : ourChanges.getAddedChildNodes ().entrySet())
{
-            mergedNode.put(new ChildNodeEntry(entry.getKey(), entry.getValue()));
+            mergedNode.add(new ChildNodeEntry(entry.getKey(), entry.getValue()));
         }
         for (Map.Entry<String, String> entry : ourChanges.getChangedChildNodes ().entrySet())
{
-            mergedNode.put(new ChildNodeEntry(entry.getKey(), entry.getValue()));
+            mergedNode.add(new ChildNodeEntry(entry.getKey(), entry.getValue()));
         }
         for (String name : ourChanges.getRemovedChildNodes().keySet()) {
             mergedNode.remove(name);

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/MutableNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/MutableNode.java?rev=1227230&r1=1227229&r2=1227230&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/MutableNode.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/MutableNode.java
Wed Jan  4 17:27:50 2012
@@ -16,6 +16,9 @@
  */
 package org.apache.jackrabbit.mk.model;
 
+import java.util.LinkedHashMap;
+import java.util.Map;
+
 /**
  *
  */
@@ -28,11 +31,30 @@ public class MutableNode extends Abstrac
         super(other);
     }
 
-    public ChildNodeEntry put(ChildNodeEntry newEntry) {
+    public ChildNodeEntry add(ChildNodeEntry newEntry) {
         return childEntries.put(newEntry.getName(), newEntry);
     }
     
     public ChildNodeEntry remove(String name) {
         return childEntries.remove(name);
     }
+
+    public ChildNodeEntry rename(String oldName, String newName) {
+        if (oldName.equals(newName)) {
+            return childEntries.get(oldName);
+        }
+        LinkedHashMap<String, ChildNodeEntry> clone = 
+                (LinkedHashMap<String, ChildNodeEntry>) childEntries.clone();
+        childEntries.clear();
+        ChildNodeEntry oldCNE = null;
+        for (Map.Entry<String, ChildNodeEntry> entry : clone.entrySet()) {
+            if (entry.getKey().equals(oldName)) {
+                oldCNE = entry.getValue();
+                childEntries.put(newName, new ChildNodeEntry(newName, oldCNE.getId()));
+            } else {
+                childEntries.put(entry.getKey(), entry.getValue());
+            }
+        }
+        return oldCNE;
+    }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java?rev=1227230&r1=1227229&r2=1227230&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
Wed Jan  4 17:27:50 2012
@@ -324,7 +324,7 @@ public class PathUtils {
     public static boolean isAncestor(String ancestor, String path) {
         assertValid(ancestor);
         assertValid(path);
-        return path.startsWith(ancestor) && path.length() > ancestor.length();
+        return path.startsWith(ancestor) && getDepth(path) > getDepth(ancestor);
     }
 
     /**

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java?rev=1227230&r1=1227229&r2=1227230&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
Wed Jan  4 17:27:50 2012
@@ -49,22 +49,11 @@ public class MoveNodeTest extends MultiM
 
     @Test
     public void addProperty() {
-        if (!isMemoryKernel(mk)) {
-            return;
-        }
-
         // add a property /test/c
         commit("/", "+ \"test/c\": 123");
         Assert.assertEquals("{c:123,a,b,c}", getNode("/test"));
         assertJournal("+\"/test/c\":123");
 
-        // duplicate add property should fail
-        try {
-            commit("/", "+ \"test/c\": 123");
-            Assert.fail();
-        } catch (MicroKernelException e) {
-            // expected
-        }
         Assert.assertEquals("{c:123,a,b,c}", getNode("/test"));
     }
 
@@ -75,10 +64,6 @@ public class MoveNodeTest extends MultiM
 
     @Test
     public void rename() {
-        if (!isMemoryKernel(mk)) {
-            return;
-        }
-
         // rename /test/b
         commit("/", "> \"test/b\": \"test/b1\"");
         Assert.assertEquals("{a,b1,c}", getNode("/test"));
@@ -281,6 +266,19 @@ public class MoveNodeTest extends MultiM
         }
     }
 
+    @Test
+    public void moveTryBecomeDescendantOfSelf() {
+        // move /test to /test/a/test
+
+        try {
+            // try to move /test to /test/a/test
+            commit("/", "> \"test\": \"/test/a/test\"");
+            fail();
+        } catch (Exception e) {
+            // expected
+        }
+    }
+
     private void commit(String root, String diff) {
         head = mk.commit(root, diff, head, null);
     }

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java?rev=1227230&r1=1227229&r2=1227230&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java
Wed Jan  4 17:27:50 2012
@@ -148,6 +148,7 @@ public class PathTest extends TestCase {
         assertFalse(PathUtils.isAncestor("/", parent + "/" + child));
         assertTrue(PathUtils.isAncestor("/" + parent, "/" + parent + "/" + child));
         assertFalse(PathUtils.isAncestor(parent, child));
+        assertFalse(PathUtils.isAncestor("/" + parent, "/" + parent + "123"));
 
         // relativize
         assertEquals("", PathUtils.relativize("/", "/"));



Mime
View raw message