jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1325697 - in /jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk: model/AbstractNode.java model/Node.java model/NodeDiffHandler.java store/DefaultRevisionStore.java store/StoredNodeAsState.java
Date Fri, 13 Apr 2012 10:01:44 GMT
Author: stefan
Date: Fri Apr 13 10:01:43 2012
New Revision: 1325697

URL: http://svn.apache.org/viewvc?rev=1325697&view=rev
Log:
OAK-46: Efficient diffing of large child node lists

Added:
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/NodeDiffHandler.java
Modified:
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/AbstractNode.java
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/Node.java
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/store/DefaultRevisionStore.java
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/store/StoredNodeAsState.java

Modified: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/AbstractNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/AbstractNode.java?rev=1325697&r1=1325696&r2=1325697&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/AbstractNode.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/AbstractNode.java
Fri Apr 13 10:01:43 2012
@@ -20,8 +20,10 @@ import org.apache.jackrabbit.mk.store.Bi
 import org.apache.jackrabbit.mk.store.RevisionProvider;
 
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 
 /**
  *
@@ -80,6 +82,71 @@ public abstract class AbstractNode imple
         return childEntries.getEntries(offset, count);
     }
 
+    public void diff(Node other, NodeDiffHandler handler) {
+        // compare properties
+
+        Map<String, String> oldProps = getProperties();
+        Map<String, String> newProps = other.getProperties();
+
+        for (Map.Entry<String, String> entry : oldProps.entrySet()) {
+            String name = entry.getKey();
+            String val = oldProps.get(name);
+            String newVal = newProps.get(name);
+            if (newVal == null) {
+                handler.propDeleted(name, val);
+            } else {
+                if (!val.equals(newVal)) {
+                    handler.propChanged(name, val, newVal);
+                }
+            }
+        }
+        for (Map.Entry<String, String> entry : newProps.entrySet()) {
+            String name = entry.getKey();
+            if (!oldProps.containsKey(name)) {
+                handler.propAdded(name, entry.getValue());
+            }
+        }
+
+        // compare child node entries
+
+        if (other instanceof AbstractNode) {
+            // OAK-46: Efficient diffing of large child node lists
+
+            // delegate to ChildNodeEntries implementation
+            ChildNodeEntries otherEntries = ((AbstractNode) other).childEntries;
+            for (Iterator<ChildNode> it = childEntries.getAdded(otherEntries); it.hasNext();
) {
+                handler.childNodeAdded(it.next());
+            }
+            for (Iterator<ChildNode> it = childEntries.getRemoved(otherEntries); it.hasNext();
) {
+                handler.childNodeDeleted(it.next());
+            }
+            for (Iterator<ChildNode> it = childEntries.getModified(otherEntries); it.hasNext();
) {
+                ChildNode old = it.next();
+                ChildNode modified = otherEntries.get(old.getName());
+                handler.childNodeChanged(old, modified.getId());
+            }
+            return;
+        }
+
+        for (Iterator<ChildNode> it = getChildNodeEntries(0, -1); it.hasNext(); ) {
+            ChildNode child = it.next();
+            ChildNode newChild = other.getChildNodeEntry(child.getName());
+            if (newChild == null) {
+                handler.childNodeDeleted(child);
+            } else {
+                if (!child.getId().equals(newChild.getId())) {
+                    handler.childNodeChanged(child, newChild.getId());
+                }
+            }
+        }
+        for (Iterator<ChildNode> it = other.getChildNodeEntries(0, -1); it.hasNext();
) {
+            ChildNode child = it.next();
+            if (getChildNodeEntry(child.getName()) == null) {
+                handler.childNodeAdded(child);
+            }
+        }
+    }
+
     public void serialize(Binding binding) throws Exception {
         final Iterator<Map.Entry<String, String>> iter = properties.entrySet().iterator();
         binding.writeMap(":props", properties.size(),

Modified: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/Node.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/Node.java?rev=1325697&r1=1325696&r2=1325697&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/Node.java (original)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/Node.java Fri
Apr 13 10:01:43 2012
@@ -36,5 +36,7 @@ public interface Node {
     
     Iterator<ChildNode> getChildNodeEntries(int offset, int count);
 
+    void diff(Node other, NodeDiffHandler handler);
+
     void serialize(Binding binding) throws Exception;
 }

Added: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/NodeDiffHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/NodeDiffHandler.java?rev=1325697&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/NodeDiffHandler.java
(added)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/NodeDiffHandler.java
Fri Apr 13 10:01:43 2012
@@ -0,0 +1,35 @@
+/*
+ * 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.model;
+
+/**
+ *
+ */
+public interface NodeDiffHandler {
+
+    void propAdded(String propName, String value);
+
+    void propChanged(String propName, String oldValue, String newValue);
+
+    void propDeleted(String propName, String value);
+
+    void childNodeAdded(ChildNode added);
+
+    void childNodeDeleted(ChildNode deleted);
+
+    void childNodeChanged(ChildNode changed, Id newId);
+}

Modified: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/store/DefaultRevisionStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/store/DefaultRevisionStore.java?rev=1325697&r1=1325696&r2=1325697&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/store/DefaultRevisionStore.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/store/DefaultRevisionStore.java
Fri Apr 13 10:01:43 2012
@@ -21,10 +21,15 @@ import java.util.Collections;
 import java.util.Map;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
+import org.apache.jackrabbit.mk.model.ChildNode;
 import org.apache.jackrabbit.mk.model.ChildNodeEntriesMap;
 import org.apache.jackrabbit.mk.model.Id;
 import org.apache.jackrabbit.mk.model.MutableCommit;
 import org.apache.jackrabbit.mk.model.MutableNode;
+import org.apache.jackrabbit.mk.model.Node;
+import org.apache.jackrabbit.mk.model.NodeDiffHandler;
+import org.apache.jackrabbit.mk.model.NodeState;
+import org.apache.jackrabbit.mk.model.NodeStateDiff;
 import org.apache.jackrabbit.mk.model.StoredCommit;
 import org.apache.jackrabbit.mk.model.StoredNode;
 import org.apache.jackrabbit.mk.persistence.Persistence;
@@ -270,4 +275,49 @@ public class DefaultRevisionStore extend
         }
     }
 
+    //------------------------------------------------------------< overrides >
+
+
+    @Override
+    public void compare(final NodeState before, final NodeState after, final NodeStateDiff
diff) {
+        // OAK-46: Efficient diffing of large child node lists
+
+        Node beforeNode = ((StoredNodeAsState) before).unwrap();
+        Node afterNode = ((StoredNodeAsState) after).unwrap();
+
+        beforeNode.diff(afterNode, new NodeDiffHandler() {
+            @Override
+            public void propAdded(String propName, String value) {
+                diff.propertyAdded(after.getProperty(propName));
+            }
+
+            @Override
+            public void propChanged(String propName, String oldValue, String newValue) {
+                diff.propertyChanged(before.getProperty(propName), after.getProperty(propName));
+            }
+
+            @Override
+            public void propDeleted(String propName, String value) {
+                diff.propertyDeleted(before.getProperty(propName));
+            }
+
+            @Override
+            public void childNodeAdded(ChildNode added) {
+                String name = added.getName();
+                diff.childNodeAdded(name, after.getChildNode(name));
+            }
+
+            @Override
+            public void childNodeDeleted(ChildNode deleted) {
+                String name = deleted.getName();
+                diff.childNodeDeleted(name, before.getChildNode(name));
+            }
+
+            @Override
+            public void childNodeChanged(ChildNode changed, Id newId) {
+                String name = changed.getName();
+                diff.childNodeChanged(name, before.getChildNode(name), after.getChildNode(name));
+            }
+        });
+    }
 }

Modified: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/store/StoredNodeAsState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/store/StoredNodeAsState.java?rev=1325697&r1=1325696&r2=1325697&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/store/StoredNodeAsState.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/store/StoredNodeAsState.java
Fri Apr 13 10:01:43 2012
@@ -45,6 +45,10 @@ class StoredNodeAsState extends Abstract
         return node.getId();
     }
 
+    StoredNode unwrap() {
+        return node;
+    }
+
     private static class SimplePropertyState extends AbstractPropertyState {
         private final String name;
         private final String value;



Mime
View raw message