jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1330314 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel: KernelNodeStateBuilder.java KernelNodeStore.java NodeStateBuilder.java NodeStore.java
Date Wed, 25 Apr 2012 14:39:08 GMT
Author: mduerig
Date: Wed Apr 25 14:39:07 2012
New Revision: 1330314

URL: http://svn.apache.org/viewvc?rev=1330314&view=rev
Log:
OAK-9: Internal tree builder
- add NodeStateBuilder
- add method to acquire and apply NodeStateBuilder to NodeStore

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStateBuilder.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/NodeStateBuilder.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/NodeStore.java

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStateBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStateBuilder.java?rev=1330314&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStateBuilder.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStateBuilder.java
Wed Apr 25 14:39:07 2012
@@ -0,0 +1,196 @@
+/*
+ * 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.kernel;
+
+import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.apache.jackrabbit.mk.api.MicroKernelException;
+import org.apache.jackrabbit.mk.util.PathUtils;
+import org.apache.jackrabbit.oak.api.CoreValueFactory;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.util.CoreValueUtil;
+
+/**
+ * FIXME document
+ */
+public class KernelNodeStateBuilder implements NodeStateBuilder {
+    private final MicroKernel kernel;
+    private final CoreValueFactory valueFactory;
+    private final String path;
+
+    private String revision;
+
+    public KernelNodeStateBuilder(MicroKernel kernel, CoreValueFactory valueFactory, String
path,
+            String revision) {
+
+        this.kernel = kernel;
+        this.valueFactory = valueFactory;
+        this.path = path;
+
+        this.revision = revision;
+    }
+
+    @Override
+    public NodeState getNodeState() {
+        assertNotStale();
+
+        return new KernelNodeState(kernel, valueFactory, path, revision);
+    }
+
+    @Override
+    public NodeStateBuilder getChildBuilder(String name) {
+        String targetPath = PathUtils.concat(path, name);
+        return kernel.nodeExists(targetPath, revision)
+            ? new KernelNodeStateBuilder(kernel, valueFactory, targetPath, revision)
+            : null;
+    }
+
+    @Override
+    public NodeStateBuilder addNode(String name, NodeState nodeState) {
+        String targetPath = PathUtils.concat(path, name);
+        StringBuilder jsop = new StringBuilder();
+        buildJsop(targetPath, nodeState, jsop);
+        revision = kernel.commit("", jsop.toString(), revision, null);
+        return new KernelNodeStateBuilder(kernel, valueFactory, targetPath, revision);
+    }
+
+    @Override
+    public NodeStateBuilder addNode(String name) {
+        String targetPath = PathUtils.concat(path, name);
+        if (kernel.nodeExists(targetPath, revision)) {
+            return null;
+        }
+        else {
+            revision = kernel.commit("", "+\""  + targetPath + "\":{}", revision, null);
+            return new KernelNodeStateBuilder(kernel, valueFactory, targetPath, revision);
+        }
+    }
+
+    @Override
+    public boolean removeNode(String name) {
+        String targetPath = PathUtils.concat(path, name);
+        if (kernel.nodeExists(targetPath, revision)) {
+            revision = kernel.commit("", "-\""  + targetPath + '"', revision, null);
+            return true;
+        }
+        else {
+            return false;
+        }
+    }
+
+    @Override
+    public void setProperty(PropertyState property) {
+        String targetPath = PathUtils.concat(path, property.getName());
+        String value = property.isArray()
+            ? CoreValueUtil.toJsonArray(property.getValues())
+            : CoreValueUtil.toJsonValue(property.getValue());
+
+        revision = kernel.commit("", "^\"" + targetPath + "\":" + value, revision, null);
+    }
+
+    @Override
+    public void removeProperty(String name) {
+        String targetPath = PathUtils.concat(path, name);
+        revision = kernel.commit("", "^\"" + targetPath + "\":null", revision, null);
+    }
+
+    @Override
+    public boolean moveTo(NodeStateBuilder destParent, String destName) {
+        if (!(destParent instanceof KernelNodeStateBuilder)) {
+            throw new IllegalArgumentException("Alien builder for destParent");
+        }
+
+        if (destParent.getChildBuilder(destName) != null) {
+            return false;
+        }
+
+        KernelNodeStateBuilder destParentBuilder = (KernelNodeStateBuilder) destParent;
+
+        String destParentPath = destParentBuilder.getPath();
+        String targetPath = PathUtils.concat(destParentPath, destName);
+
+        revision = kernel.commit("", ">\"" + path + "\":\"" + targetPath + '"',
+                revision, null);
+        return true;
+    }
+
+    @Override
+    public boolean copyTo(NodeStateBuilder destParent, String destName) {
+        if (!(destParent instanceof KernelNodeStateBuilder)) {
+            throw new IllegalArgumentException("Alien builder for destParent");
+        }
+
+        if (destParent.getChildBuilder(destName) != null) {
+            return false;
+        }
+
+        KernelNodeStateBuilder destParentBuilder = (KernelNodeStateBuilder) destParent;
+
+        String destParentPath = destParentBuilder.getPath();
+        String targetPath = PathUtils.concat(destParentPath, destName);
+
+        revision = kernel.commit("", "*\"" + path + "\":\"" + targetPath + '"',
+                revision, null);
+        return true;
+    }
+
+    //------------------------------------------------------------< internal >---
+
+    String getPath() {
+        return path;
+    }
+
+    boolean apply() {
+        assertNotStale();
+
+        try {
+            kernel.merge(revision, null);
+            revision = null;
+            return true;
+        }
+        catch (MicroKernelException e) {
+            // TODO log
+            return false;
+        }
+    }
+
+    //------------------------------------------------------------< private >---
+
+    private void assertNotStale() {
+        if (revision == null) {
+            throw new IllegalStateException("This branch has been merged already");
+        }
+    }
+
+    private static void buildJsop(String path, NodeState nodeState, StringBuilder jsop) {
+        jsop.append("+\"").append(path).append("\":{}");
+
+        for (PropertyState property : nodeState.getProperties()) {
+            String targetPath = PathUtils.concat(path, property.getName());
+            String value = property.isArray()
+                ? CoreValueUtil.toJsonArray(property.getValues())
+                : CoreValueUtil.toJsonValue(property.getValue());
+
+            jsop.append("^\"").append(targetPath).append("\":").append(value);
+        }
+
+        for (ChildNodeEntry child : nodeState.getChildNodeEntries(0, -1)) {
+            String targetPath = PathUtils.concat(path, child.getName());
+            buildJsop(targetPath, child.getNodeState(), jsop);
+        }
+    }
+
+}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStore.java?rev=1330314&r1=1330313&r2=1330314&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStore.java
Wed Apr 25 14:39:07 2012
@@ -43,6 +43,28 @@ public class KernelNodeStore implements 
     }
 
     @Override
+    public NodeStateBuilder getBuilder(NodeState nodeState) {
+        if (!(nodeState instanceof KernelNodeState)) {
+            throw new IllegalArgumentException("Alien node state");
+        }
+
+        KernelNodeState kernelNodeState = (KernelNodeState) nodeState;
+        String branchRevision = kernel.branch(kernelNodeState.getRevision());
+        String path = kernelNodeState.getPath();
+        return new KernelNodeStateBuilder(kernel, valueFactory, path, branchRevision);
+    }
+
+    @Override
+    public boolean apply(NodeStateBuilder builder) {
+        if (!(builder instanceof  KernelNodeStateBuilder)) {
+            throw new IllegalArgumentException("Alien builder");
+        }
+        
+        KernelNodeStateBuilder kernelNodeStateBuilder = (KernelNodeStateBuilder) builder;
+        return kernelNodeStateBuilder.apply();
+    }
+
+    @Override
     public void compare(NodeState before, NodeState after, NodeStateDiff diff) {
         compareProperties(before, after, diff);
         compareChildNodes(before, after, diff);

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/NodeStateBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/NodeStateBuilder.java?rev=1330314&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/NodeStateBuilder.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/NodeStateBuilder.java
Wed Apr 25 14:39:07 2012
@@ -0,0 +1,34 @@
+/*
+ * 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.kernel;
+
+import org.apache.jackrabbit.oak.api.PropertyState;
+
+/**
+ * FIXME document
+ */
+public interface NodeStateBuilder {
+    NodeState getNodeState();
+    NodeStateBuilder getChildBuilder(String name);
+    NodeStateBuilder addNode(String name, NodeState nodeState);
+    NodeStateBuilder addNode(String name);
+    boolean removeNode(String name);
+    void setProperty(PropertyState property);
+    void removeProperty(String name);
+    boolean moveTo(NodeStateBuilder destParent, String destName);
+    boolean copyTo(NodeStateBuilder destParent, String destName);
+}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/NodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/NodeStore.java?rev=1330314&r1=1330313&r2=1330314&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/NodeStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/NodeStore.java
Wed Apr 25 14:39:07 2012
@@ -37,6 +37,20 @@ public interface NodeStore {
     NodeState getRoot();
 
     /**
+     * FIXME document
+     * @param nodeState
+     * @return
+     */
+    NodeStateBuilder getBuilder(NodeState nodeState);
+
+    /**
+     * FIXME document
+     * @param builder
+     * @return
+     */
+    boolean apply(NodeStateBuilder builder);
+
+    /**
      * Compares the given two node states. Any found differences are
      * reported by calling the relevant added, changed or deleted methods
      * of the given handler.



Mime
View raw message