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 md...@apache.org on 2012/07/09 20:52:08 UTC

svn commit: r1359334 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core: RootImpl.java TreeImpl.java

Author: mduerig
Date: Mon Jul  9 18:52:07 2012
New Revision: 1359334

URL: http://svn.apache.org/viewvc?rev=1359334&view=rev
Log:
OAK-174: Refactor RootImpl and TreeImpl to take advantage of the child node state builder introduced with OAK-170

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/RootImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/TreeImpl.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/RootImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/RootImpl.java?rev=1359334&r1=1359333&r2=1359334&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/RootImpl.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/RootImpl.java Mon Jul  9 18:52:07 2012
@@ -52,8 +52,7 @@ public class RootImpl implements Root {
     static final Logger log = LoggerFactory.getLogger(RootImpl.class);
 
     /**
-     * Number of {@link #setCurrentRootState} calls for which changes
-     * are kept in memory.
+     * Number of {@link #purge()} calls for which changes are kept in memory.
      */
     private static final int PURGE_LIMIT = 100;
 
@@ -66,11 +65,8 @@ public class RootImpl implements Root {
     /** Actual root element of the {@code Tree} */
     private TreeImpl root;
 
-    /** Current root node state if changed */
-    private NodeState currentRootState;
-
     /**
-     * Number of {@link #setCurrentRootState} occurred so since the lase
+     * Number of {@link #purge()} occurred so since the lase
      * purge.
      */
     private int modCount;
@@ -117,7 +113,6 @@ public class RootImpl implements Root {
 
     @Override
     public boolean move(String sourcePath, String destPath) {
-        purgePendingChanges();
         TreeImpl source = getChild(sourcePath);
         if (source == null) {
             return false;
@@ -128,14 +123,13 @@ public class RootImpl implements Root {
         }
 
         String destName = getName(destPath);
-        if (source.moveTo(destParent, destName)) {
-            currentRootState = null;
-            return branch.move(sourcePath, destPath);
-        }
-        else {
-            currentRootState = null;
+        if (destParent.hasChild(destName)) {
             return false;
         }
+
+        purgePendingChanges();
+        source.moveTo(destParent, destName);
+        return branch.move(sourcePath, destPath);
     }
 
     @Override
@@ -160,7 +154,6 @@ public class RootImpl implements Root {
 
     @Override
     public void refresh() {
-        currentRootState = null;
         // There is a small race here and we risk to get an "earlier" revision for the
         // observation limit than for the branch. This is not a problem though since
         // observation will catch up later on with the next call to ChangeExtractor.getChanges()
@@ -213,10 +206,7 @@ public class RootImpl implements Root {
      */
     @Nonnull
     public NodeState getCurrentRootState() {
-        if (currentRootState == null) {
-            currentRootState = branch.getRoot();
-        }
-        return currentRootState;
+        return root.getNodeState();
     }
 
     /**
@@ -241,42 +231,31 @@ public class RootImpl implements Root {
         return store.getBuilder(nodeState);
     }
 
-    /**
-     * Set the current root node state
-     *
-     * @param nodeState  node state representing the modified root state
-     */
-    public void setCurrentRootState(NodeState nodeState) {
-        currentRootState = nodeState;
-        modCount++;
-        if (needsPurging()) {
+    //------------------------------------------------------------< internal >---
+
+    NodeStateBuilder createRootBuilder() {
+        return store.getBuilder(branch.getRoot());
+    }
+
+    // TODO better way to determine purge limit
+    void purge() {
+        if (++modCount > PURGE_LIMIT) {
+            modCount = 0;
             purgePendingChanges();
         }
     }
 
+    //------------------------------------------------------------< private >---
+
     /**
      * Purge all pending changes to the underlying {@link NodeStoreBranch}.
      * All registered {@link PurgeListener}s are notified.
      */
-    public void purgePendingChanges() {
-        if (currentRootState != null) {
-            branch.setRoot(currentRootState);
-            currentRootState = null;
-            notifyListeners();
-        }
-    }
-
-    //------------------------------------------------------------< private >---
-
-    // TODO better way to determine purge limit
-    private boolean needsPurging() {
-        if (modCount > PURGE_LIMIT) {
-            modCount = 0;
-            return true;
-        }
-        else {
-            return false;
+    private void purgePendingChanges() {
+        if (hasPendingChanges()) {
+            branch.setRoot(getCurrentRootState());
         }
+        notifyListeners();
     }
 
     private void notifyListeners() {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/TreeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/TreeImpl.java?rev=1359334&r1=1359333&r2=1359334&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/TreeImpl.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/TreeImpl.java Mon Jul  9 18:52:07 2012
@@ -54,7 +54,7 @@ public class TreeImpl implements Tree, P
     private String name;
 
     /** Lazily initialised {@code NodeStateBuilder} for the underlying node state */
-    protected NodeStateBuilder nodeStateBuilder;
+    NodeStateBuilder nodeStateBuilder;
 
     private final Children children = new Children();
 
@@ -76,15 +76,12 @@ public class TreeImpl implements Tree, P
             }
 
             @Override
-            protected NodeState getNodeState() {
-                return nodeStateBuilder == null
-                    ? root.getCurrentRootState()
-                    : nodeStateBuilder.getNodeState();
-            }
-
-            @Override
-            protected void updateParentState(NodeState childState) {
-                root.setCurrentRootState(childState);
+            protected synchronized NodeStateBuilder getNodeStateBuilder() {
+                if (nodeStateBuilder == null) {
+                    nodeStateBuilder = root.createRootBuilder();
+                    root.addListener(this);
+                }
+                return nodeStateBuilder;
             }
         };
     }
@@ -255,7 +252,7 @@ public class TreeImpl implements Tree, P
         if (!hasChild(name)) {
             NodeStateBuilder builder = getNodeStateBuilder();
             builder.setNode(name, EMPTY_NODE);
-            updateParentState(builder.getNodeState());
+            root.purge();
         }
 
         TreeImpl child = getChild(name);
@@ -265,14 +262,23 @@ public class TreeImpl implements Tree, P
 
     @Override
     public boolean remove() {
-        return !isRoot() && parent.removeChild(name);
+        if (!isRoot() && parent.hasChild(name)) {
+            NodeStateBuilder builder = parent.getNodeStateBuilder();
+            builder.removeNode(name);
+            parent.children.remove(name);
+            parent.root.purge();
+            return true;
+        }
+        else {
+            return false;
+        }
     }
 
     @Override
     public PropertyState setProperty(String name, CoreValue value) {
         NodeStateBuilder builder = getNodeStateBuilder();
         builder.setProperty(name, value);
-        updateParentState(builder.getNodeState());
+        root.purge();
         PropertyState property = getProperty(name);
         assert property != null;
         return property;
@@ -282,7 +288,7 @@ public class TreeImpl implements Tree, P
     public PropertyState setProperty(String name, List<CoreValue> values) {
         NodeStateBuilder builder = getNodeStateBuilder();
         builder.setProperty(name, values);
-        updateParentState(builder.getNodeState());
+        root.purge();
         PropertyState property = getProperty(name);
         assert property != null;
         return property;
@@ -292,28 +298,7 @@ public class TreeImpl implements Tree, P
     public void removeProperty(String name) {
         NodeStateBuilder builder = getNodeStateBuilder();
         builder.removeProperty(name);
-        updateParentState(builder.getNodeState());
-    }
-
-    /**
-     * Move this tree to the parent at {@code destParent} with the new name
-     * {@code destName}.
-     *
-     * @param destParent  new parent for this tree
-     * @param destName  new name for this tree
-     * @return {@code true} if this tree was moved.
-     */
-    public boolean moveTo(TreeImpl destParent, String destName) {
-        if (destParent.hasChild(destName)) {
-            return false;
-        }
-
-        parent.children.remove(name);
-        destParent.children.put(destName, this);
-
-        name = destName;
-        parent = destParent;
-        return true;
+        root.purge();
     }
 
     //--------------------------------------------------< RootImpl.Listener >---
@@ -334,46 +319,38 @@ public class TreeImpl implements Tree, P
     }
 
     @Nonnull
-    protected NodeState getNodeState() {
+    protected synchronized NodeStateBuilder getNodeStateBuilder() {
         if (nodeStateBuilder == null) {
-            NodeState nodeState = parent.getNodeState().getChildNode(name);
-            assert nodeState != null;
-            return nodeState;
-        }
-        else {
-            return nodeStateBuilder.getNodeState();
+            nodeStateBuilder = parent.getNodeStateBuilder().getChildBuilder(name);
+            root.addListener(this);
         }
+        return nodeStateBuilder;
     }
 
-    protected void updateParentState(NodeState childState) {
-        NodeStateBuilder parentBuilder = parent.getNodeStateBuilder();
-        parentBuilder.setNode(name, childState);
-        parent.updateParentState(parentBuilder.getNodeState());
-    }
+    //------------------------------------------------------------< internal >---
 
-    //------------------------------------------------------------< private >---
+    /**
+     * Move this tree to the parent at {@code destParent} with the new name
+     * {@code destName}.
+     *
+     * @param destParent  new parent for this tree
+     * @param destName  new name for this tree
+     */
+    void moveTo(TreeImpl destParent, String destName) {
+        parent.children.remove(name);
+        destParent.children.put(destName, this);
 
-    private boolean removeChild(String name) {
-        if (hasChild(name)) {
-            NodeStateBuilder builder = getNodeStateBuilder();
-            builder.removeNode(name);
-            children.remove(name);
-            updateParentState(builder.getNodeState());
-            return true;
-        }
-        else {
-            return false;
-        }
+        name = destName;
+        parent = destParent;
     }
 
-    private synchronized NodeStateBuilder getNodeStateBuilder() {
-        if (nodeStateBuilder == null) {
-            nodeStateBuilder = root.getBuilder(getNodeState());
-            root.addListener(this);
-        }
-        return nodeStateBuilder;
+    @Nonnull
+    NodeState getNodeState() {
+        return getNodeStateBuilder().getNodeState();
     }
 
+    //------------------------------------------------------------< private >---
+
     private void buildPath(StringBuilder sb) {
         if (parent != null) {
             parent.buildPath(sb);