jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1326735 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak: api/TransientNodeState.java kernel/KernelBranch.java kernel/TransientKernelNodeState.java
Date Mon, 16 Apr 2012 18:42:23 GMT
Author: mduerig
Date: Mon Apr 16 18:42:23 2012
New Revision: 1326735

URL: http://svn.apache.org/viewvc?rev=1326735&view=rev
Log:
OAK-18: Define Oak API 
decouple TransientKernelNodeState from KernelBranch and Branch

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TransientNodeState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelBranch.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TransientKernelNodeState.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TransientNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TransientNodeState.java?rev=1326735&r1=1326734&r2=1326735&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TransientNodeState.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TransientNodeState.java
Mon Apr 16 18:42:23 2012
@@ -48,11 +48,6 @@ public interface TransientNodeState {
     TransientNodeState getParent();
 
     /**
-     * @return  the branch this state belongs to
-     */
-    Branch getBranch();
-
-    /**
      * Get a property state
      * @param name name of the property state
      * @return  the property state with the given {@code name} or {@code null}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelBranch.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelBranch.java?rev=1326735&r1=1326734&r2=1326735&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelBranch.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelBranch.java
Mon Apr 16 18:42:23 2012
@@ -23,6 +23,7 @@ import org.apache.jackrabbit.mk.json.Jso
 import org.apache.jackrabbit.oak.api.Branch;
 import org.apache.jackrabbit.oak.api.Scalar;
 import org.apache.jackrabbit.oak.api.TransientNodeState;
+import org.apache.jackrabbit.oak.kernel.TransientKernelNodeState.Listener;
 
 import java.util.List;
 
@@ -45,23 +46,22 @@ import static org.apache.jackrabbit.mk.u
  */
 public class KernelBranch implements Branch {
 
+    /** Log of changes to this branch */
+    private final ChangeLog changeLog = new ChangeLog();
+
     /** Base node state of this private branch */
     private final NodeState base;
 
     /** Root state of this branch */
     private final TransientKernelNodeState root;
 
-    /** Json diff of this private branch */
-    private final StringBuilder jsop;
-
     /**
      * Create a new branch for the given base node state
      * @param base  base node state of the private branch
      */
     KernelBranch(NodeState base) {
         this.base = base;
-        root = new TransientKernelNodeState(this, base);
-        jsop = new StringBuilder();
+        root = new TransientKernelNodeState(base, changeLog);
     }
 
     @Override
@@ -78,7 +78,6 @@ public class KernelBranch implements Bra
         }
 
         sourceNode.move(destParent, destName);
-        jsop.append(">\"").append(sourcePath).append("\":\"").append(destPath).append('"');
     }
 
     @Override
@@ -95,7 +94,6 @@ public class KernelBranch implements Bra
         }
 
         sourceNode.copy(destParent, destName);
-        jsop.append("*\"").append(sourcePath).append("\":\"").append(destPath).append('"');
     }
 
     @Override
@@ -124,30 +122,10 @@ public class KernelBranch implements Bra
     KernelNodeState mergeInto(MicroKernel microkernel, KernelNodeState target) {
         String targetPath = target.getPath();
         String targetRevision = target.getRevision();
-        String rev = microkernel.commit(targetPath, jsop.toString(), targetRevision, null);
+        String rev = microkernel.commit(targetPath, changeLog.toJsop(), targetRevision, null);
         return new KernelNodeState(microkernel, targetPath, rev);
     }
 
-    void addNode(TransientKernelNodeState state, String name) {
-        jsop.append("+\"").append(path(state, name)).append("\":{}");
-    }
-
-    void removeNode(TransientKernelNodeState state, String name) {
-        jsop.append("-\"").append(path(state, name)).append('"');
-    }
-
-    void setProperty(TransientKernelNodeState state, String name, Scalar value) {
-        jsop.append("^\"").append(path(state, name)).append("\":").append(encode(value));
-    }
-
-    void setProperty(TransientKernelNodeState state, String name, List<Scalar> values)
{
-        jsop.append("^\"").append(path(state, name)).append("\":").append(encode(values));
-    }
-
-    void removeProperty(TransientKernelNodeState state, String name) {
-        jsop.append("^\"").append(path(state, name)).append("\":null");
-    }
-
     /**
      * Get a transient node state for the node identified by
      * {@code path}
@@ -173,7 +151,7 @@ public class KernelBranch implements Bra
      * @param name The item name.
      * @return relative path of the item {@code name}
      */
-    private static String path(TransientKernelNodeState state, String name) {
+    private static String path(TransientNodeState state, String name) {
         String path = state.getPath();
         return path.isEmpty() ? name : path + '/' + name;
     }
@@ -204,4 +182,52 @@ public class KernelBranch implements Bra
         return sb.toString();
     }
 
+    /**
+     * This {@code Listener} implementation records all changes to
+     * a associated branch as JSOP.
+     */
+    private static class ChangeLog implements Listener {
+        private final StringBuilder jsop = new StringBuilder();
+
+        @Override
+        public void addNode(TransientKernelNodeState state, String name) {
+            jsop.append("+\"").append(path(state, name)).append("\":{}");
+        }
+
+        @Override
+        public void removeNode(TransientKernelNodeState state, String name) {
+            jsop.append("-\"").append(path(state, name)).append('"');
+        }
+
+        @Override
+        public void setProperty(TransientKernelNodeState state, String name, Scalar value)
{
+            jsop.append("^\"").append(path(state, name)).append("\":").append(encode(value));
+        }
+
+        @Override
+        public void setProperty(TransientKernelNodeState state, String name, List<Scalar>
values) {
+            jsop.append("^\"").append(path(state, name)).append("\":").append(encode(values));
+        }
+
+        @Override
+        public void removeProperty(TransientKernelNodeState state, String name) {
+            jsop.append("^\"").append(path(state, name)).append("\":null");
+        }
+
+        @Override
+        public void move(TransientKernelNodeState state, String name, TransientKernelNodeState
moved) {
+            jsop.append(">\"").append(path(state, name)).append("\":\"")
+                    .append(moved.getPath()).append('"');
+        }
+
+        @Override
+        public void copy(TransientKernelNodeState state, String name, TransientKernelNodeState
copied) {
+            jsop.append("*\"").append(path(state, name)).append("\":\"")
+                    .append(copied.getPath()).append('"');
+        }
+
+        public String toJsop() {
+            return jsop.toString();
+        }
+    }
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TransientKernelNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TransientKernelNodeState.java?rev=1326735&r1=1326734&r2=1326735&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TransientKernelNodeState.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TransientKernelNodeState.java
Mon Apr 16 18:42:23 2012
@@ -41,8 +41,6 @@ import static org.apache.jackrabbit.oak.
 import static org.apache.jackrabbit.oak.util.Iterators.map;
 
 public class TransientKernelNodeState implements TransientNodeState {
-    /** Branch this state belongs to */
-    private final KernelBranch branch;
 
     /**
      * Underlying persistent state or {@code null} if this instance represents an
@@ -50,6 +48,15 @@ public class TransientKernelNodeState im
      */
     private final NodeState persistentState;
 
+    /** Parent of this state */
+    private TransientKernelNodeState parent;
+
+    /** Name of this state */
+    private String name;
+
+    /** Listener for changes on this node state */
+    private final Listener listener;
+
     /** Resolved persistent child states */
     private final Map<String, TransientKernelNodeState> existingChildNodes =
             new HashMap<String, TransientKernelNodeState>();
@@ -68,45 +75,98 @@ public class TransientKernelNodeState im
     /** Transiently removed property states */
     private final Set<String> removedProperties = new HashSet<String>();
 
-    /** Name of this state */
-    private String name;
+    /**
+     * Listener for changes on {@code TransientKernelNodeState}s
+     */
+    interface Listener {
 
-    /** Parent of this state */
-    private TransientKernelNodeState parent;
+        /**
+         * The node of the given {@code name} has been added to {@code state}.
+         * @param state  parent state to which a node was added
+         * @param name  name of the added node
+         */
+        void addNode(TransientKernelNodeState state, String name);
+
+        /**
+         * The node of the given {@code name} has been removed from {@code state}
+         * @param state  parent state from which a node was removed
+         * @param name  name of the removed node
+         */
+        void removeNode(TransientKernelNodeState state, String name);
+
+        /**
+         * The property of the given {@code name} and {@code value} has been set.
+         * @param state  state on which the property was set.
+         * @param name  name of the property
+         * @param value  value of the property
+         */
+        void setProperty(TransientKernelNodeState state, String name, Scalar value);
+
+        /**
+         * The property of the given {@code name} and {@code values} has been set.
+         * @param state  state on which the property was set.
+         * @param name  name of the property
+         * @param values  values of the property
+         */
+        void setProperty(TransientKernelNodeState state, String name, List<Scalar>
values);
+
+        /**
+         * The property of the given {@code name} has been removed.
+         * @param state  state on which the property was removed.
+         * @param name  name of the property
+         */
+        void removeProperty(TransientKernelNodeState state, String name);
+
+        /**
+         * The node of the given {@code name} has been moved.
+         * @param state  parent state from which the property was moved
+         * @param name  name of the moved property
+         * @param moved  moved property
+         */
+        void move(TransientKernelNodeState state, String name, TransientKernelNodeState moved);
+
+        /**
+         * The node of the given {@code name} been copies.
+         * @param state  parent state from which the property way copies
+         * @param name  name of the copied property
+         * @param copied  copied property
+         */
+        void copy(TransientKernelNodeState state, String name, TransientKernelNodeState copied);
+    }
 
     /**
      * Create a new instance representing the root of a branch.
-     * @param branch  the branch this state belongs to
      * @param persistentState  underlying persistent state
+     * @param listener  change listener
      */
-    TransientKernelNodeState(KernelBranch branch, NodeState persistentState) {
-        this(branch, persistentState, null, "");
+    TransientKernelNodeState(NodeState persistentState, Listener listener) {
+        this(persistentState, null, "", listener);
     }
 
     /**
      * Create a new instance representing a added node state
-     * @param branch  the branch the state belongs to
      * @param parent  the parent state of the state
      * @param name  name of the state
+     * @param listener  change listener
      */
-    private TransientKernelNodeState(KernelBranch branch, TransientKernelNodeState parent,
String name) {
-        this(branch, null, parent, name);
+    private TransientKernelNodeState(TransientKernelNodeState parent, String name, Listener
listener) {
+        this(null, parent, name, listener);
     }
 
     /**
      * Create a new instance with an underlying persistent state
-     * @param branch  the branch the state belongs to
      * @param persistedState  underlying persistent state
      * @param parent  the parent state of the state
      * @param name  name of the state
+     * @param listener  change listener
      */
-    private TransientKernelNodeState(KernelBranch branch, NodeState persistedState,
-            TransientKernelNodeState parent, String name) {
+    private TransientKernelNodeState(NodeState persistedState, TransientKernelNodeState parent,
+            String name, Listener listener) {
 
-        this.branch = branch;
         this.persistentState = persistedState;
         this.parent = parent;
         this.name = name;
+        this.listener = listener;
     }
 
     /**
@@ -119,7 +179,7 @@ public class TransientKernelNodeState im
     private TransientKernelNodeState(TransientKernelNodeState state, TransientKernelNodeState
parent,
             String name) {
 
-        branch = state.branch;
+        listener = state.listener;
         persistentState = state.persistentState;
         this.parent = parent;
         this.name = name;
@@ -167,11 +227,6 @@ public class TransientKernelNodeState im
     }
 
     @Override
-    public KernelBranch getBranch() {
-        return branch;
-    }
-
-    @Override
     public PropertyState getProperty(String name) {
         PropertyState state = addedProperties.get(name);
         if (state != null) {
@@ -316,8 +371,10 @@ public class TransientKernelNodeState im
     @Override
     public TransientNodeState addNode(String name) {
         if (!hasNode(name)) {
-            addedNodes.put(name, new TransientKernelNodeState(branch, this, name));
-            branch.addNode(this, name);
+            addedNodes.put(name, new TransientKernelNodeState(this, name, listener));
+            if (listener != null) {
+                listener.addNode(this, name);
+            }
         }
 
         return getChildNode(name);
@@ -327,7 +384,9 @@ public class TransientKernelNodeState im
     public void removeNode(String name) {
         if (hasNode(name)) {
             markNodeRemoved(name);
-            branch.removeNode(this, name);
+            if (listener != null) {
+                listener.removeNode(this, name);
+            }
         }
     }
 
@@ -335,14 +394,18 @@ public class TransientKernelNodeState im
     public void setProperty(String name, Scalar value) {
         PropertyState propertyState = new KernelPropertyState(name, value);
         setProperty(propertyState);
-        branch.setProperty(this, name, value);
+        if (listener != null) {
+            listener.setProperty(this, name, value);
+        }
     }
 
     @Override
     public void setProperty(String name, List<Scalar> values) {
         PropertyState propertyState = new KernelPropertyState(name, values);
         setProperty(propertyState);
-        branch.setProperty(this, name, values);
+        if (listener != null) {
+            listener.setProperty(this, name, values);
+        }
     }
 
     @Override
@@ -352,11 +415,11 @@ public class TransientKernelNodeState im
             // Mark as removed if removing existing
             removedProperties.add(name);
         }
-        branch.removeProperty(this, name);
+        if (listener != null) {
+            listener.removeProperty(this, name);
+        }
     }
 
-    //------------------------------------------------------------< internal >---
-
     /**
      * Move this node state to the parent node state at {@code destParent}
      * with the new name {@code destName}.
@@ -364,12 +427,18 @@ public class TransientKernelNodeState im
      * @param destParent  new parent for this node state
      * @param destName  new name for this node state
      */
-    void move(TransientKernelNodeState destParent, String destName) {
+    public void move(TransientKernelNodeState destParent, String destName) {
         parent.markNodeRemoved(name);
 
+        TransientKernelNodeState oldParent = parent;
+        String oldName = name;
+
         name = destName;
         parent = destParent;
         destParent.addedNodes.put(destName, this);
+        if (listener != null) {
+            listener.move(oldParent, oldName, this);
+        }
     }
 
     /**
@@ -379,11 +448,16 @@ public class TransientKernelNodeState im
      * @param destParent  parent for the copied node state
      * @param destName  name for the copied node state
      */
-    void copy(TransientKernelNodeState destParent, String destName) {
-        destParent.addedNodes.put(destName,
-                new TransientKernelNodeState(this, destParent, destName));
+    public void copy(TransientKernelNodeState destParent, String destName) {
+        TransientKernelNodeState copy = new TransientKernelNodeState(this, destParent, destName);
+        destParent.addedNodes.put(destName, copy);
+        if (listener != null) {
+            listener.copy(parent, name, copy);
+        }
     }
 
+    //------------------------------------------------------------< internal >---
+
     private void markNodeRemoved(String name) {
         addedNodes.remove(name);
         if (hasExistingNode(name)) {
@@ -420,7 +494,7 @@ public class TransientKernelNodeState im
             if (state == null) {
                 return null;
             }
-            transientState = new TransientKernelNodeState(branch, state, this, name);
+            transientState = new TransientKernelNodeState(state, this, name, listener);
             existingChildNodes.put(name, transientState);
         }
         return transientState;



Mime
View raw message