jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1149175 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
Date Thu, 21 Jul 2011 13:51:36 GMT
Author: thomasm
Date: Thu Jul 21 13:51:35 2011
New Revision: 1149175

URL: http://svn.apache.org/viewvc?rev=1149175&view=rev
Log:
Node move, rename, reorder.

Added:
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java?rev=1149175&r1=1149174&r2=1149175&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
Thu Jul 21 13:51:35 2011
@@ -109,7 +109,7 @@ public class MemoryKernelImpl implements
                 t.read(':');
                 t.read('{');
                 NodeImpl n = NodeImpl.parse(t, headRevId);
-                headRoot = headRoot.cloneAndAddChildNode(PathUtils.concat(fromRoot, path),
n, headRevId);
+                headRoot = headRoot.cloneAndAddChildNode(PathUtils.concat(fromRoot, path),
false, null, n, headRevId);
                 w.append(n.toString()).append("\n");
                 break;
             case '-':
@@ -139,6 +139,64 @@ public class MemoryKernelImpl implements
                 headRoot = headRoot.cloneAndSetProperty(PathUtils.concat(fromRoot, path),
value, headRevId);
                 w.append("\n");
                 break;
+            case '>':
+                path = t.readString();
+                String from = PathUtils.concat(fromRoot, path);
+                String name = PathUtils.getName(from);
+                t.read(':');
+                String position, target;
+                boolean rename;
+                String to;
+                if (t.matches('{')) {
+                    rename = false;
+                    position = t.readString();
+                    t.read(':');
+                    target = t.readString();
+                    t.read('}');
+                } else {
+                    rename = true;
+                    position = null;
+                    target = t.readString();
+                }
+                boolean before = false;
+                if ("last".equals(position)) {
+                    target = PathUtils.concat(target, name);
+                    position = null;
+                } else if ("first".equals(position)) {
+                    target = PathUtils.concat(target, name);
+                    position = null;
+                    before = true;
+                } else if ("before".equals(position)) {
+                    position = PathUtils.getName(target);
+                    target = PathUtils.getParentPath(target);
+                    target = PathUtils.concat(target, name);
+                    before = true;
+                } else if ("after".equals(position)) {
+                    position = PathUtils.getName(target);
+                    target = PathUtils.getParentPath(target);
+                    target = PathUtils.concat(target, name);
+                } else if (position == null) {
+                    // move
+                } else {
+                    throw new AssertionError("position: " + position);
+                }
+                to = PathUtils.concat(fromRoot, target);
+                boolean inPlaceRename = false;
+                if (rename) {
+                    if (PathUtils.getParentPath(from).equals(PathUtils.getParentPath(to)))
{
+                        inPlaceRename = true;
+                        position = PathUtils.getName(from);
+                    }
+                }
+                NodeImpl node = headRoot.getNode(from);
+                if (!inPlaceRename) {
+                    headRoot = headRoot.cloneAndRemoveChildNode(from, headRevId);
+                }
+                headRoot = headRoot.cloneAndAddChildNode(to, before, position, node, headRevId);
+                if (inPlaceRename) {
+                    headRoot = headRoot.cloneAndRemoveChildNode(from, headRevId);
+                }
+                break;
             default:
                 throw new AssertionError("token type: " + t.getTokenType());
             }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java?rev=1149175&r1=1149174&r2=1149175&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
Thu Jul 21 13:51:35 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.mk.mem;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map.Entry;
@@ -86,11 +87,11 @@ public class NodeImpl {
         return n.getNode(path.substring(index + 1));
     }
 
-    public NodeImpl cloneAndAddChildNode(String path, NodeImpl newNode, long revId) {
+    public NodeImpl cloneAndAddChildNode(String path, boolean before, String position, NodeImpl
newNode, long revId) {
         int index = PathUtils.getNextSlash(path, 0);
         if (index < 0) {
             NodeImpl clone = clone(revId);
-            clone.addChildNode(path, newNode);
+            clone.addChildNode(path, before, position, newNode);
             return clone;
         }
         String child = path.substring(0, index);
@@ -98,7 +99,7 @@ public class NodeImpl {
         if (n == null) {
             throw new RuntimeException("Node not found: " + path);
         }
-        NodeImpl n2 = n.cloneAndAddChildNode(path.substring(index + 1), newNode, revId);
+        NodeImpl n2 = n.cloneAndAddChildNode(path.substring(index + 1), before, position,
newNode, revId);
         NodeImpl c = clone(revId);
         c.childNodes.put(child, n2);
         return c;
@@ -193,7 +194,7 @@ public class NodeImpl {
         json.endObject();
     }
 
-    void addChildNode(String name, NodeImpl node) {
+    void addChildNode(String name, boolean before, String position, NodeImpl node) {
         if (childNodes == null) {
             childNodes = new LinkedHashMap<String, NodeImpl>();
         } else if (childNodes.containsKey(name)) {
@@ -203,6 +204,28 @@ public class NodeImpl {
             node.setProperty(":name", JsopBuilder.encode(name));
         }
         childNodes.put(name, node);
+        if (before || position != null) {
+            boolean moveNext = false;
+            ArrayList<String> move = new ArrayList<String>();
+            for (String entry : childNodes.keySet()) {
+                if (entry.equals(name)) {
+                    // don't move new entry
+                } else if (moveNext) {
+                    move.add(entry);
+                } else if (before && position == null) {
+                    move.add(entry);
+                    moveNext = true;
+                } else if (entry.equals(position)) {
+                    if (before) {
+                        move.add(entry);
+                    }
+                    moveNext = true;
+                }
+            }
+            for (String m : move) {
+                childNodes.put(m, childNodes.remove(m));
+            }
+        }
     }
 
     void removeChildNode(String name) {
@@ -238,7 +261,7 @@ public class NodeImpl {
                 String key = t.readString();
                 t.read(':');
                 if (t.matches('{')) {
-                    node.addChildNode(key, parse(t, revId));
+                    node.addChildNode(key, false, null, parse(t, revId));
                 } else {
                     String value = t.readRawValue().trim();
                     node.setProperty(key, value);
@@ -249,5 +272,4 @@ public class NodeImpl {
         return node;
     }
 
-
 }

Added: 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=1149175&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
(added)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
Thu Jul 21 13:51:35 2011
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mk;
+
+import junit.framework.TestCase;
+import org.apache.jackrabbit.mk.api.MicroKernel;
+
+/**
+ * Test moving nodes.
+ */
+public class MoveNodeTest extends TestCase {
+
+    // private static final String URL = "fs:{homeDir};clean";
+    private static final String URL = "mem:;clean";
+
+    private MicroKernel mk;
+
+    public void setUp() throws Exception {
+        mk = MicroKernelFactory.getInstance(URL);
+    }
+
+    public void tearDown() throws InterruptedException {
+        mk.dispose();
+    }
+
+    public void test() {
+
+        String head = mk.getHeadRevision();
+
+        head = mk.commit("/", "+ \"test\" : {\"a\":{}, \"b\":{}, \"c\":{}}", head, null);
+        head = mk.commit("/", "+ \"test2\" : {}", head, null);
+        head = mk.commit("/", "+ \"test3\" : {}", head, null);
+        String s;
+
+        s = mk.getNodes("/test", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{,a:{},b:{},c:{}}", s);
+        System.out.println(s);
+
+        // rename /test/b to /test/b1 and back
+        head = mk.commit("/", "> \"test/b\" : \"test/b1\"", head, null);
+        s = mk.getNodes("/test", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{,a:{},b1:{},c:{}}", s);
+        head = mk.commit("/", "> \"test/b1\" : \"test/b\"", head, null);
+        s = mk.getNodes("/test", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{,a:{},b:{},c:{}}", s);
+
+        // order c before b
+        head = mk.commit("/", "> \"test/c\" : {\"before\": \"test/b\"}", head, null);
+        s = mk.getNodes("/test", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{,a:{},c:{},b:{}}", s);
+
+        // order a after c
+        head = mk.commit("/", "> \"test/a\" : {\"after\": \"test/c\"}", head, null);
+        s = mk.getNodes("/test", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{,c:{},a:{},b:{}}", s);
+
+        // move /test/a to /test2/a (rename is not supported in this way)
+        head = mk.commit("/", "> \"test/a\" : {\"first\": \"test2\"}", head, null);
+        s = mk.getNodes("/test", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{,c:{},b:{}}", s);
+        s = mk.getNodes("/test2", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{,a:{}}", s);
+
+        // move /test/b to /test2/b, before any other nodes in /test2
+        head = mk.commit("/", "> \"test/b\" : {\"first\": \"test2\"}", head, null);
+        s = mk.getNodes("/test", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{,c:{}}", s);
+        s = mk.getNodes("/test2", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{,b:{},a:{}}", s);
+
+        // move /test/c to /test2, after b
+        head = mk.commit("/", "> \"test/c\" : {\"after\": \"test2/b\"}", head, null);
+        s = mk.getNodes("/test", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{}", s);
+        s = mk.getNodes("/test2", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{,b:{},c:{},a:{}}", s);
+
+        // move /test2/a to /test2, as last
+        head = mk.commit("/", "> \"test2/b\" : {\"last\": \"test2\"}", head, null);
+        s = mk.getNodes("/test2", head);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        assertEquals("{,c:{},a:{},b:{}}", s);
+
+        mk.dispose();
+
+        System.out.println("");
+    }
+
+}
\ No newline at end of file



Mime
View raw message