You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by mr...@apache.org on 2016/07/05 15:00:25 UTC

svn commit: r1751494 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/document/ main/java/org/apache/jackrabbit/oak/plugins/document/util/ test/java/org/apache/jackrabbit/oak/plugins/document/

Author: mreutegg
Date: Tue Jul  5 15:00:25 2016
New Revision: 1751494

URL: http://svn.apache.org/viewvc?rev=1751494&view=rev
Log:
OAK-4536: Avoid premature branch

Count changes in InMemory branch state and transition only to Persisted when update.limit is reached. Enable test.

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/CountingDiff.java   (with props)
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BranchStateTest.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java?rev=1751494&r1=1751493&r2=1751494&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java Tue Jul  5 15:00:25 2016
@@ -22,6 +22,7 @@ import static org.apache.jackrabbit.oak.
 import static org.apache.jackrabbit.oak.api.CommitFailedException.OAK;
 import static org.apache.jackrabbit.oak.api.CommitFailedException.STATE;
 import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.COLLISIONS;
+import static org.apache.jackrabbit.oak.plugins.document.util.CountingDiff.countChanges;
 
 import java.util.HashSet;
 import java.util.Random;
@@ -444,14 +445,18 @@ class DocumentNodeStoreBranch implements
      * Transitions to:
      * <ul>
      *     <li>{@link Unmodified} on {@link #setRoot(NodeState)} if the new root is the same
-     *         as the base of this branch or
-     *     <li>{@link Persisted} otherwise.
+     *         as the base of this branch</li>
+     *     <li>{@link Persisted} on {@link #setRoot(NodeState)} if the number of
+     *         changes counted from the base to the new root reaches
+     *         {@link DocumentRootBuilder#UPDATE_LIMIT}.</li>
      *     <li>{@link Merged} on {@link BranchState#merge(CommitHook, CommitInfo, boolean)}</li>
      * </ul>
      */
     private class InMemory extends BranchState {
         /** Root state of the transient head. */
         private NodeState head;
+        /** Number of in-memory updates */
+        private int numUpdates;
 
         @Override
         public String toString() {
@@ -461,6 +466,7 @@ class DocumentNodeStoreBranch implements
         InMemory(DocumentNodeState base, NodeState head) {
             super(base);
             this.head = head;
+            this.numUpdates = countChanges(base, head);
         }
 
         @Override
@@ -474,8 +480,11 @@ class DocumentNodeStoreBranch implements
             if (base.equals(root)) {
                 branchState = new Unmodified(base);
             } else if (!head.equals(root)) {
+                numUpdates += countChanges(head, root);
                 head = root;
-                persist();
+                if (numUpdates > DocumentRootBuilder.UPDATE_LIMIT) {
+                    persist();
+                }
             }
         }
 

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/CountingDiff.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/CountingDiff.java?rev=1751494&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/CountingDiff.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/CountingDiff.java Tue Jul  5 15:00:25 2016
@@ -0,0 +1,85 @@
+/*
+ * 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.oak.plugins.document.util;
+
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.NodeStateDiff;
+
+import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
+import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.MISSING_NODE;
+
+/**
+ * A {@link NodeStateDiff} implementation that counts the differences between
+ * two node states, including their sub tree.
+ */
+public class CountingDiff implements NodeStateDiff {
+
+    private int changes = 0;
+
+    public static int countChanges(NodeState before, NodeState after) {
+        CountingDiff counter = new CountingDiff();
+        after.compareAgainstBaseState(before, counter);
+        return counter.getNumChanges();
+    }
+
+    @Override
+    public boolean propertyAdded(PropertyState after) {
+        inc();
+        return true;
+    }
+
+    @Override
+    public boolean propertyChanged(PropertyState before, PropertyState after) {
+        inc();
+        return true;
+    }
+
+    @Override
+    public boolean propertyDeleted(PropertyState before) {
+        inc();
+        return true;
+    }
+
+    @Override
+    public boolean childNodeAdded(String name, NodeState after) {
+        inc();
+        return after.compareAgainstBaseState(EMPTY_NODE, this);
+    }
+
+    @Override
+    public boolean childNodeChanged(String name,
+                                    NodeState before,
+                                    NodeState after) {
+        inc();
+        return after.compareAgainstBaseState(before, this);
+    }
+
+    @Override
+    public boolean childNodeDeleted(String name, NodeState before) {
+        inc();
+        return MISSING_NODE.compareAgainstBaseState(before, this);
+    }
+
+    public int getNumChanges() {
+        return changes;
+    }
+
+    private void inc() {
+        changes++;
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/CountingDiff.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BranchStateTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BranchStateTest.java?rev=1751494&r1=1751493&r2=1751494&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BranchStateTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BranchStateTest.java Tue Jul  5 15:00:25 2016
@@ -25,7 +25,6 @@ import org.apache.jackrabbit.oak.spi.com
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 
@@ -50,7 +49,6 @@ public class BranchStateTest {
     }
 
     // OAK-4536
-    @Ignore("OAK-4536")
     @Test
     public void commitException() throws Exception {
         NodeBuilder builder = ns.getRoot().builder();

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java?rev=1751494&r1=1751493&r2=1751494&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java Tue Jul  5 15:00:25 2016
@@ -2317,7 +2317,9 @@ public class DocumentNodeStoreTest {
         builder.child("foo");
         b.setRoot(builder.getNodeState());
         // branch state is now InMemory
-        builder.child("bar").setProperty("p", "foo");
+        for (int i = 0; i < DocumentRootBuilder.UPDATE_LIMIT; i++) {
+            builder.child("bar").setProperty("p-" + i, "foo");
+        }
         b.setRoot(builder.getNodeState());
         // branch state is now Persisted