jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r1433884 - in /jackrabbit/oak/trunk/oak-mongomk/src: main/java/org/apache/jackrabbit/mongomk/impl/command/ main/java/org/apache/jackrabbit/mongomk/impl/json/ test/java/org/apache/jackrabbit/mongomk/ test/java/org/apache/jackrabbit/mongomk/i...
Date Wed, 16 Jan 2013 10:30:38 GMT
Author: mreutegg
Date: Wed Jan 16 10:30:38 2013
New Revision: 1433884

URL: http://svn.apache.org/viewvc?rev=1433884&view=rev
Log:
OAK-535: MergeCommand reads complete tree into memory
- optimize diff normalization

Added:
    jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandler.java
  (with props)
    jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandlerTest.java
  (with props)
Modified:
    jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/command/MergeCommand.java
    jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/MongoAssert.java

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/command/MergeCommand.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/command/MergeCommand.java?rev=1433884&r1=1433883&r2=1433884&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/command/MergeCommand.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/command/MergeCommand.java
Wed Jan 16 10:30:38 2013
@@ -1,10 +1,25 @@
+/*
+ * 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.mongomk.impl.command;
 
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.model.tree.DiffBuilder;
 import org.apache.jackrabbit.mk.model.tree.NodeState;
 import org.apache.jackrabbit.mongomk.api.command.Command;
@@ -13,8 +28,8 @@ import org.apache.jackrabbit.mongomk.api
 import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
 import org.apache.jackrabbit.mongomk.impl.action.FetchCommitAction;
 import org.apache.jackrabbit.mongomk.impl.action.FetchHeadRevisionIdAction;
-import org.apache.jackrabbit.mongomk.impl.json.DefaultJsopHandler;
 import org.apache.jackrabbit.mongomk.impl.json.JsopParser;
+import org.apache.jackrabbit.mongomk.impl.json.NormalizingJsopHandler;
 import org.apache.jackrabbit.mongomk.impl.model.CommitBuilder;
 import org.apache.jackrabbit.mongomk.impl.model.MongoCommit;
 import org.apache.jackrabbit.mongomk.impl.model.NodeImpl;
@@ -81,7 +96,7 @@ public class MergeCommand extends BaseCo
                     // commit does not exist
                 }
             }
-            newCommit = CommitBuilder.build("", diff.toString(),
+            newCommit = CommitBuilder.build("/", diff.toString(),
                     MongoUtil.fromMongoRepresentation(currentHead), message);
 
         } else {
@@ -232,59 +247,4 @@ public class MergeCommand extends BaseCo
         return copy;
     }
 
-    private static class NormalizingJsopHandler extends DefaultJsopHandler {
-
-        private final StringBuilder builder = new StringBuilder();
-
-        @Override
-        public void nodeAdded(String parentPath, String name) {
-            builder.append("+");
-            builder.append(JsopBuilder.encode(concatPath(parentPath, name)));
-            builder.append(":{}");
-        }
-
-        @Override
-        public void nodeCopied(String rootPath,
-                               String oldPath,
-                               String newPath) {
-            builder.append("*");
-            builder.append(JsopBuilder.encode(concatPath(rootPath, oldPath)));
-            builder.append(":");
-            builder.append(JsopBuilder.encode(concatPath(rootPath, newPath)));
-        }
-
-        @Override
-        public void nodeMoved(String rootPath, String oldPath, String newPath) {
-            builder.append(">");
-            builder.append(JsopBuilder.encode(oldPath));
-            builder.append(":");
-            builder.append(JsopBuilder.encode(newPath));
-        }
-
-        @Override
-        public void nodeRemoved(String parentPath, String name) {
-            builder.append("-");
-            builder.append(JsopBuilder.encode(concatPath(parentPath, name)));
-        }
-
-        @Override
-        public void propertySet(String path, String key, Object value, String rawValue) {
-            builder.append("^");
-            builder.append(JsopBuilder.encode(concatPath(path, key)));
-            builder.append(":");
-            builder.append(rawValue);
-        }
-
-        private String concatPath(String parent, String child) {
-            if (parent.length() == 0) {
-                return child;
-            } else {
-                return PathUtils.concat(parent, child);
-            }
-        }
-
-        String getDiff() {
-            return builder.toString();
-        }
-    }
 }
\ No newline at end of file

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandler.java?rev=1433884&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandler.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandler.java
Wed Jan 16 10:30:38 2013
@@ -0,0 +1,145 @@
+/*
+ * 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.mongomk.impl.json;
+
+import java.util.ArrayDeque;
+import java.util.Collections;
+import java.util.Deque;
+
+import org.apache.jackrabbit.mk.json.JsopBuilder;
+import org.apache.jackrabbit.oak.commons.PathUtils;
+
+/**
+* <code>NormalizingJsopHandler</code>...
+*/
+public class NormalizingJsopHandler extends DefaultJsopHandler {
+
+    private final StringBuilder builder = new StringBuilder();
+
+    private Deque<String> commaStack = new ArrayDeque<String>(
+            Collections.singleton(""));
+
+    private Deque<String> pathStack = new ArrayDeque<String>(
+            Collections.singleton("/"));
+
+    public String getDiff() {
+        scopeFor("/");
+        return builder.toString();
+    }
+
+    @Override
+    public void nodeAdded(String parentPath, String name) {
+        String relPath = scopeFor(PathUtils.concat(parentPath, name));
+        if (pathStack.size() == 1) {
+            builder.append("+");
+        } else {
+            maybeAppendComma();
+        }
+        if (relPath.length() > 0) {
+            pathStack.addLast(relPath);
+            commaStack.addLast("");
+        }
+        builder.append(JsopBuilder.encode(relPath));
+        builder.append(":{");
+        resetComma();
+    }
+
+    @Override
+    public void nodeCopied(String rootPath,
+                           String oldPath,
+                           String newPath) {
+        scopeFor("/");
+        builder.append("*");
+        builder.append(JsopBuilder.encode(PathUtils.relativize("/", oldPath)));
+        builder.append(":");
+        builder.append(JsopBuilder.encode(PathUtils.relativize("/", newPath)));
+    }
+
+    @Override
+    public void nodeMoved(String rootPath, String oldPath, String newPath) {
+        scopeFor("/");
+        builder.append(">");
+        builder.append(JsopBuilder.encode(PathUtils.relativize("/", oldPath)));
+        builder.append(":");
+        builder.append(JsopBuilder.encode(PathUtils.relativize("/", newPath)));
+    }
+
+    @Override
+    public void nodeRemoved(String parentPath, String name) {
+        scopeFor("/");
+        builder.append("-");
+        builder.append(JsopBuilder.encode(PathUtils.relativize("/", concatPath(parentPath,
name))));
+    }
+
+    @Override
+    public void propertySet(String path, String key, Object value, String rawValue) {
+        String relPath = scopeFor(path);
+        if (pathStack.size() == 1) {
+            builder.append("^");
+        } else {
+            maybeAppendComma();
+        }
+        builder.append(JsopBuilder.encode(concatPath(relPath, key)));
+        builder.append(":");
+        builder.append(rawValue);
+    }
+
+    /**
+     * Opens a new scope for the given path relative to the current path.
+     * @param path the path of the new scope.
+     * @return the remaining relative path needed for the given scope path.
+     */
+    private String scopeFor(String path) {
+        // close brackets until path stack is the root, the same as path or
+        // an ancestor of path
+        while (pathStack.size() > 1
+                && !path.equals(getCurrentPath())
+                && !PathUtils.isAncestor(getCurrentPath(), path)) {
+            pathStack.removeLast();
+            commaStack.removeLast();
+            builder.append("}");
+        }
+        // remaining path for scope
+        return PathUtils.relativize(getCurrentPath(), path);
+    }
+
+    private String getCurrentPath() {
+        String path = "";
+        for (String element : pathStack) {
+            path = PathUtils.concat(path, element);
+        }
+        return path;
+    }
+
+    private String concatPath(String parent, String child) {
+        if (parent.length() == 0) {
+            return child;
+        } else {
+            return PathUtils.concat(parent, child);
+        }
+    }
+
+    private void resetComma() {
+        commaStack.removeLast();
+        commaStack.addLast("");
+    }
+
+    private void maybeAppendComma() {
+        builder.append(commaStack.removeLast());
+        commaStack.addLast(",");
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandler.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/MongoAssert.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/MongoAssert.java?rev=1433884&r1=1433883&r2=1433884&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/MongoAssert.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/MongoAssert.java
Wed Jan 16 10:30:38 2013
@@ -59,10 +59,11 @@ public class MongoAssert {
         DBCollection commitCollection = nodeStore.getCommitCollection();
         DBObject query = QueryBuilder.start(MongoCommit.KEY_REVISION_ID)
                 .is(commit.getRevisionId()).and(MongoCommit.KEY_MESSAGE)
-                .is(commit.getMessage()).and(MongoCommit.KEY_DIFF).is(commit.getDiff()).and(MongoCommit.KEY_PATH)
+                .is(commit.getMessage()).and(MongoCommit.KEY_PATH)
                 .is(commit.getPath()).and(MongoCommit.KEY_FAILED).notEquals(Boolean.TRUE).get();
         MongoCommit result = (MongoCommit) commitCollection.findOne(query);
         Assert.assertNotNull(result);
+        Assert.assertEquals(commit.getDiff(), result.getDiff());
     }
 
     public static void assertHeadRevision(long revisionId) {

Added: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandlerTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandlerTest.java?rev=1433884&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandlerTest.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandlerTest.java
Wed Jan 16 10:30:38 2013
@@ -0,0 +1,93 @@
+/*
+ * 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.mongomk.impl.json;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * <code>NormalizingJsopHandlerTest</code>...
+ */
+public class NormalizingJsopHandlerTest {
+
+    @Test
+    public void nestedAdd() throws Exception {
+        String jsop = "+\"foo\":{\"bar\":{\"prop\":\"value\"}}";
+        NormalizingJsopHandler handler = new NormalizingJsopHandler();
+        new JsopParser("/", jsop, handler).parse();
+        assertEquals(jsop, handler.getDiff());
+    }
+
+    @Test
+    public void nestedAddRelativePath() throws Exception {
+        String jsop = "+\"foo\":{\"bar/baz\":{\"prop\":\"value\"}}";
+        NormalizingJsopHandler handler = new NormalizingJsopHandler();
+        new JsopParser("/", jsop, handler).parse();
+        assertEquals(jsop, handler.getDiff());
+    }
+
+    @Test
+    public void nestedAddNormalized() throws Exception {
+        String jsop = "+\"foo\":{\"bar\":{\"prop\":\"value\"}}";
+        NormalizingJsopHandler handler = new NormalizingJsopHandler();
+        new JsopParser("/prefix", jsop, handler).parse();
+        String expected = "+\"prefix/foo\":{\"bar\":{\"prop\":\"value\"}}";
+        assertEquals(expected, handler.getDiff());
+    }
+
+    @Test
+    public void nestedAddMultiProperty() throws Exception {
+        String jsop = "+\"foo\":{\"bar\":{\"p1\":\"value\",\"p2\":\"value\"},\"p3\":\"value\"}";
+        NormalizingJsopHandler handler = new NormalizingJsopHandler();
+        new JsopParser("/", jsop, handler).parse();
+        assertEquals(jsop, handler.getDiff());
+    }
+
+    @Test
+    public void nestedAddMultiNode() throws Exception {
+        String jsop = "+\"foo\":{\"bar\":{\"p1\":\"value\"},\"p2\":\"value\",\"baz\":{},\"qux\":{\"p3\":\"value\"}}";
+        NormalizingJsopHandler handler = new NormalizingJsopHandler();
+        new JsopParser("/", jsop, handler).parse();
+        assertEquals(jsop, handler.getDiff());
+    }
+
+    @Test
+    public void nestedAddWithMove() throws Exception {
+        String jsop = "+\"foo\":{\"bar\":{\"prop\":\"value\"}}>\"foo\":\"baz\"";
+        NormalizingJsopHandler handler = new NormalizingJsopHandler();
+        new JsopParser("/", jsop, handler).parse();
+        assertEquals(jsop, handler.getDiff());
+    }
+
+    @Test
+    public void nestedAddWithCopy() throws Exception {
+        String jsop = "+\"foo\":{\"bar\":{\"prop\":\"value\"}}*\"foo\":\"baz\"";
+        NormalizingJsopHandler handler = new NormalizingJsopHandler();
+        new JsopParser("/", jsop, handler).parse();
+        assertEquals(jsop, handler.getDiff());
+    }
+
+    @Test
+    public void nestedAddWithRemove() throws Exception {
+        String jsop = "+\"foo\":{\"bar\":{\"prop\":\"value\"}}-\"foo\"";
+        NormalizingJsopHandler handler = new NormalizingJsopHandler();
+        new JsopParser("/", jsop, handler).parse();
+        assertEquals(jsop, handler.getDiff());
+    }
+
+}

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandlerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/json/NormalizingJsopHandlerTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message