jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1103785 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk: ./ store/
Date Mon, 16 May 2011 16:21:32 GMT
Author: stefan
Date: Mon May 16 16:21:32 2011
New Revision: 1103785

URL: http://svn.apache.org/viewvc?rev=1103785&view=rev
Log:
MicroKernel prototype (WIP)

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeDelta.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeDiffHandler.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeUtils.java
Removed:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionDiffBuilder.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java?rev=1103785&r1=1103784&r2=1103785&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
Mon May 16 16:21:32 2011
@@ -23,7 +23,8 @@ import org.apache.jackrabbit.mk.json.Dif
 import org.apache.jackrabbit.mk.store.Commit;
 import org.apache.jackrabbit.mk.store.CommitBuilder;
 import org.apache.jackrabbit.mk.store.Node;
-import org.apache.jackrabbit.mk.store.RevisionDiffBuilder;
+import org.apache.jackrabbit.mk.store.NodeDiffHandler;
+import org.apache.jackrabbit.mk.store.NodeUtils;
 import org.apache.jackrabbit.mk.util.PathUtil;
 import org.json.simple.JSONObject;
 import org.json.simple.parser.ContainerFactory;
@@ -36,7 +37,6 @@ import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.ListIterator;
 import java.util.Map;
 
 /**
@@ -163,7 +163,11 @@ public class MicroKernelImpl implements 
 
             // todo escape double quotes in diff
             try {
-                new RevisionDiffBuilder(commit.getParentId(), commit.getId(), rep, new RevisionDiffBuilder.RevisionDiffHandler()
{
+                String path = "/";
+                Node node1 = rep.getNode(commit.getParentId(), path);
+                Node node2 = rep.getNode(commit.getId(), path);
+
+                NodeUtils.diff(path, node1, node2, true, rep.getStore(), new NodeDiffHandler()
{
                     public void propAdded(String nodePath, String propName, String value)
{
                         buf.append("+\"");
                         buf.append(PathUtil.concat(nodePath, propName));
@@ -180,7 +184,7 @@ public class MicroKernelImpl implements 
                         buf.append("\"\n");
                     }
 
-                    public void propDeleted(String nodePath, String propName) {
+                    public void propDeleted(String nodePath, String propName, String value)
{
                         buf.append("-\"");
                         buf.append(PathUtil.concat(nodePath, propName));
                         buf.append("\"\n");
@@ -205,7 +209,11 @@ public class MicroKernelImpl implements 
                         buf.append(PathUtil.concat(nodePath, childName));
                         buf.append("\"\n");
                     }
-                }).build();
+
+                    public void childNodeChanged(String nodePath, String childName, String
oldId, String newId) {
+                        // we're not interested
+                    }
+                });
             } catch (Exception e) {
                 throw new MicroKernelException(e);
             }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java?rev=1103785&r1=1103784&r2=1103785&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java
Mon May 16 16:21:32 2011
@@ -154,10 +154,10 @@ public class CommitBuilder {
     }
 
     public String /* new revId */ doCommit() throws Exception {
-        // todo develop efficient locking strategy providing maximum concurrenncy
-        if (rep.getHeadRevision() != baseRevId) {
+        String currentHead = rep.getHeadRevision();
+        if (!currentHead.equals(baseRevId)) {
             // update base revision to new head
-            this.baseRevId = rep.getHeadRevision();
+            this.baseRevId = currentHead;
             // clear staging area
             staged.clear();
             // replay change log on new base revision
@@ -195,13 +195,27 @@ public class CommitBuilder {
             throw new Exception("internal error: inconsistent staging area content");
         }
 
-        // todo check for concurrent changes (new head), apply lock and merge changes
-        MutableCommit newCommit = new MutableCommit();
-        newCommit.setParentId(baseRevId);
-        newCommit.setCommitTS(System.currentTimeMillis());
-        newCommit.setRootNodeId(rootNodeId);
-        String newRevId = store.putCommit(newCommit);
-        store.setHeadCommit(newRevId);
+        String newRevId;
+        store.lockHead();
+        try {
+            currentHead = rep.getHeadRevision();
+            if (!currentHead.equals(baseRevId)) {
+                // todo recursively merge root of currentHead with new root (rootNodeId)
+
+
+                baseRevId = currentHead;
+            }
+
+            MutableCommit newCommit = new MutableCommit();
+            newCommit.setParentId(baseRevId);
+            newCommit.setCommitTS(System.currentTimeMillis());
+            newCommit.setRootNodeId(rootNodeId);
+            newRevId = store.putCommit(newCommit);
+
+            store.setHeadCommit(newRevId);
+        } finally {
+            store.unlockHead();
+        }
 
         // reset instance in order to be reusable
         staged.clear();

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeDelta.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeDelta.java?rev=1103785&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeDelta.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeDelta.java
Mon May 16 16:21:32 2011
@@ -0,0 +1,88 @@
+/*
+ * 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.store;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ *
+ */
+public class NodeDelta implements NodeDiffHandler {
+
+    Map<String, String> addedProperties = new HashMap<String, String>();
+    Map<String, String> removedProperties = new HashMap<String, String>();
+    Map<String, String> changedProperties = new HashMap<String, String>();
+
+    Map<String, String> addedChildNodes = new HashMap<String, String>();
+    Map<String, String> removedChildNodes = new HashMap<String, String>();
+    Map<String, String> changedChildNodes = new HashMap<String, String>();
+
+    public NodeDelta(Node node1, Node node2) throws Exception {
+        NodeUtils.diff("/", node1, node2, this);
+    }
+
+    public Map<String, String> getAddedProperties() {
+        return addedProperties;
+    }
+
+    public Map<String, String> getRemovedProperties() {
+        return removedProperties;
+    }
+
+    public Map<String, String> getChangedProperties() {
+        return changedProperties;
+    }
+
+    public Map<String, String> getAddedChildNodes() {
+        return addedChildNodes;
+    }
+
+    public Map<String, String> getRemovedChildNodes() {
+        return removedChildNodes;
+    }
+
+    public Map<String, String> getChangedChildNodes() {
+        return changedChildNodes;
+    }
+
+    //------------------------------------------------------< NodeDiffHandler >
+
+    public void propAdded(String nodePath, String propName, String value) {
+        addedProperties.put(propName, value);
+    }
+
+    public void propChanged(String nodePath, String propName, String oldValue, String newValue)
{
+        changedProperties.put(propName, newValue);
+    }
+
+    public void propDeleted(String nodePath, String propName, String value) {
+        removedProperties.put(propName, value);
+    }
+
+    public void childNodeAdded(String nodePath, String childName, String id) {
+        addedChildNodes.put(childName, id);
+    }
+
+    public void childNodeDeleted(String nodePath, String childName, String id) {
+        removedChildNodes.put(childName, id);
+    }
+
+    public void childNodeChanged(String nodePath, String childName, String oldId, String
newId) {
+        changedChildNodes.put(childName, newId);
+    }
+}

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeDiffHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeDiffHandler.java?rev=1103785&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeDiffHandler.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeDiffHandler.java
Mon May 16 16:21:32 2011
@@ -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.store;
+
+/**
+ *
+ */
+public interface NodeDiffHandler {
+
+    void propAdded(String nodePath, String propName, String value);
+
+    void propChanged(String nodePath, String propName, String oldValue, String newValue);
+
+    void propDeleted(String nodePath, String propName, String value);
+
+    void childNodeAdded(String nodePath, String childName, String id);
+
+    void childNodeDeleted(String nodePath, String childName, String id);
+
+    void childNodeChanged(String nodePath, String childName, String oldId, String newId);
+}

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeUtils.java?rev=1103785&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeUtils.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/NodeUtils.java
Mon May 16 16:21:32 2011
@@ -0,0 +1,109 @@
+/*
+ * 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.store;
+
+import org.apache.jackrabbit.mk.util.PathUtil;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ *
+ */
+public class NodeUtils {
+
+    private NodeUtils() {
+    }
+
+    /**
+     * Performs a shallow diff.
+     *
+     * @param path
+     * @param node1
+     * @param node2
+     * @param handler
+     * @throws Exception
+     */
+    public static void diff(String path, Node node1, Node node2,
+                            NodeDiffHandler handler) throws Exception {
+        diff(path, node1, node2, false, null, handler);
+    }
+
+    public static void diff(String path, Node node1, Node node2,
+                            boolean deep, ObjectStore store,
+                            NodeDiffHandler handler) throws Exception {
+
+        // compare properties
+        Map<String, String> oldProps = node1.getProperties();
+        Map<String, String> newProps = node2.getProperties();
+        if (!oldProps.equals(newProps)) {
+            Set<String> set = new HashSet<String>();
+            set.addAll(oldProps.keySet());
+            set.removeAll(newProps.keySet());
+            for (String name : set) {
+                handler.propDeleted(path, name, oldProps.get(name));
+            }
+            set.clear();
+            set.addAll(newProps.keySet());
+            set.removeAll(oldProps.keySet());
+            for (String name : set) {
+                handler.propAdded(path, name, newProps.get(name));
+            }
+            for (Map.Entry<String, String> entry : oldProps.entrySet()) {
+                String val = newProps.get(entry.getKey());
+                if (val != null && !entry.getValue().equals(val)) {
+                    handler.propChanged(path, entry.getKey(), entry.getValue(), val);
+                }
+            }
+        }
+
+        // todo determine copy/move and child-node reorder operations
+
+        // compare child node entries
+        Map<String, String> oldEntries = node1.getChildNodeEntries();
+        Map<String, String> newEntries = node2.getChildNodeEntries();
+        if (!oldEntries.equals(newEntries)) {
+            Set<String> set = new HashSet<String>();
+            set.addAll(oldEntries.keySet());
+            set.removeAll(newEntries.keySet());
+            for (String name : set) {
+                handler.childNodeDeleted(path, name, oldEntries.get(name));
+            }
+            set.clear();
+            set.addAll(newEntries.keySet());
+            set.removeAll(oldEntries.keySet());
+            for (String name : set) {
+                handler.childNodeAdded(path, name, newEntries.get(name));
+            }
+            for (Map.Entry<String, String> entry : oldEntries.entrySet()) {
+                String id = newEntries.get(entry.getKey());
+                if (!entry.getValue().equals(id)) {
+                    handler.childNodeChanged(path, entry.getKey(), entry.getValue(), id);
+                    if (deep && store != null) {
+                        node1 = store.getNode(entry.getValue());
+                        node2 = store.getNode(id);
+                        // do recurse
+                        diff(PathUtil.concat(path, entry.getKey()),
+                                node1, node2, deep, store, handler);
+                    }
+                }
+            }
+        }
+
+    }
+}

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java?rev=1103785&r1=1103784&r2=1103785&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java
Mon May 16 16:21:32 2011
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.mk.store;
 
 import java.io.File;
+import java.util.concurrent.locks.ReentrantLock;
 
 /**
  *
@@ -24,6 +25,7 @@ import java.io.File;
 public class ObjectStore implements Constants {
 
     boolean initialized;
+    ReentrantLock headLock = new ReentrantLock();
     GitStore store;
 
     public ObjectStore() {
@@ -110,6 +112,14 @@ public class ObjectStore implements Cons
         store.setHeadCommitId(commitId);
     }
 
+    public void lockHead() {
+        headLock.lock();
+    }
+
+    public void unlockHead() {
+        headLock.unlock();
+    }
+
     public String computeId(byte[] data) {
         if (!initialized) {
             throw new IllegalStateException("not initialized");



Mime
View raw message