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 st...@apache.org on 2012/04/13 18:14:37 UTC

svn commit: r1325827 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/mk/client/ oak-core/src/main/java/org/apache/jackrabbit/mk/server/ oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/ oak-mk/src/main/java/org/apache/jack...

Author: stefan
Date: Fri Apr 13 16:14:36 2012
New Revision: 1325827

URL: http://svn.apache.org/viewvc?rev=1325827&view=rev
Log:
 OAK-48: MicroKernel.getNodes() should return null for not existing nodes instead of throwing an exception

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/client/Client.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/MicroKernelServlet.java
    jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/core/MicroKernelImpl.java
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/core/Repository.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/client/Client.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/client/Client.java?rev=1325827&r1=1325826&r2=1325827&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/client/Client.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/client/Client.java Fri Apr 13 16:14:36 2012
@@ -68,7 +68,7 @@ public class Client implements MicroKern
     /**
      * Create a new instance of this class.
      * 
-     * @param addr socket address
+     * @param url socket address
      */
     public Client(String url) {
         this(getAddress(url));
@@ -237,7 +237,9 @@ public class Client implements MicroKern
             request.addParameter("offset", offset);
             request.addParameter("count", count);
             request.addParameter("filter", filter);
-            return request.getString();
+            // OAK-48: MicroKernel.getNodes() should return null for not existing nodes instead of throwing an exception
+            String result = request.getString();
+            return result.equals("null") ? null : result;
         } catch (IOException e) {
             throw toMicroKernelException(e);
         } finally {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/MicroKernelServlet.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/MicroKernelServlet.java?rev=1325827&r1=1325826&r2=1325827&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/MicroKernelServlet.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/MicroKernelServlet.java Fri Apr 13 16:14:36 2012
@@ -223,6 +223,10 @@ class MicroKernelServlet {
 
             response.setContentType("application/json");
             String json = mk.getNodes(path, revisionId, depth, offset, count, filter);
+            // OAK-48: MicroKernel.getNodes() should return null for not existing nodes instead of throwing an exception
+            if (json == null) {
+                json = "null";
+            }
             if (request.getHeaders().containsKey("User-Agent")) {
                 json = JsopBuilder.prettyPrint(json);
             }

Modified: jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java?rev=1325827&r1=1325826&r2=1325827&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java (original)
+++ jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java Fri Apr 13 16:14:36 2012
@@ -25,6 +25,7 @@ import org.junit.runners.Parameterized;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -72,6 +73,35 @@ public class MicroKernelIT extends Abstr
     }
 
     @Test
+    public void getNodesNonExistingPath() {
+        String head = mk.getHeadRevision();
+
+        String nonExistingPath = "/test/" + System.currentTimeMillis();
+        assertFalse(mk.nodeExists(nonExistingPath, head));
+
+        assertNull(mk.getNodes(nonExistingPath, head, 0, 0, -1, null));
+    }
+
+    @Test
+    public void getNodesNonExistingRevision() {
+        String nonExistingRev = "12345678";
+
+        try {
+            mk.nodeExists("/test", nonExistingRev);
+            Assert.fail("Success with non-existing revision: " + nonExistingRev);
+        } catch (MicroKernelException e) {
+            // expected
+        }
+
+        try {
+            mk.getNodes("/test", nonExistingRev, 0, 0, -1, null);
+            Assert.fail("Success with non-existing revision: " + nonExistingRev);
+        } catch (MicroKernelException e) {
+            // expected
+        }
+    }
+
+    @Test
     public void missingName() {
         String head = mk.getHeadRevision();
 

Modified: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java?rev=1325827&r1=1325826&r2=1325827&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java (original)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java Fri Apr 13 16:14:36 2012
@@ -109,7 +109,7 @@ public interface MicroKernel {
      * </pre>
      *
      * @param fromRevisionId id of first revision to be returned in journal
-     * @param toRevisionId   id of last revision to be returned in journal, if null the current head revision is assumed
+     * @param toRevisionId   id of last revision to be returned in journal, if {@code null} the current head revision is assumed
      * @param filter         (optional) filter criteria
      *                       (e.g. path, property names, etc);
      *                       TODO specify format and semantics
@@ -133,7 +133,7 @@ public interface MicroKernel {
      * </pre>
      *
      * @param fromRevisionId a revision id
-     * @param toRevisionId   another revision id, if null the current head revision is assumed
+     * @param toRevisionId   another revision id, if {@code null} the current head revision is assumed
      * @param filter         (optional) filter criteria
      *                       (e.g. path, property names, etc);
      *                       TODO specify format and semantics
@@ -150,7 +150,7 @@ public interface MicroKernel {
      * Determines whether the specified node exists.
      *
      * @param path       path denoting node
-     * @param revisionId revision id, if null the current head revision is assumed
+     * @param revisionId revision id, if {@code null} the current head revision is assumed
      * @return {@code true} if the specified node exists, otherwise {@code false}
      * @throws MicroKernelException if an error occurs
      */
@@ -165,9 +165,9 @@ public interface MicroKernel {
      *
      *
      * @param path       path denoting node
-     * @param revisionId revision id, if null the current head revision is assumed
+     * @param revisionId revision id, if {@code null} the current head revision is assumed
      * @return the number of child nodes
-     * @throws MicroKernelException if an error occurs
+     * @throws MicroKernelException if the specified node does not exist or if an error occurs
      */
     long getChildNodeCount(String path, String revisionId) throws MicroKernelException;
 
@@ -203,9 +203,9 @@ public interface MicroKernel {
      * {@code getNodes(path, revisionId, 1, 0, -1, null)}
      *
      * @param path       path denoting root of node tree to be retrieved
-     * @param revisionId revision id, if null the current head revision is assumed
-     * @return node tree in JSON format
-     * @throws MicroKernelException if an error occurs
+     * @param revisionId revision id, if {@code null} the current head revision is assumed
+     * @return node tree in JSON format or {@code null} if the specified node does not exist
+     * @throws MicroKernelException if the specified revision does not exist or if another error occurs
      */
     String /* jsonTree */ getNodes(String path, String revisionId) throws MicroKernelException;
 
@@ -234,7 +234,7 @@ public interface MicroKernel {
      * direct child nodes of the root of the returned node tree.
      *
      * @param path       path denoting root of node tree to be retrieved
-     * @param revisionId revision id, if null the current head revision is assumed
+     * @param revisionId revision id, if {@code null} the current head revision is assumed
      * @param depth      maximum depth of returned tree
      * @param offset     start position in the iteration order of child nodes (0 to start at the
      *                   beginning)
@@ -242,8 +242,8 @@ public interface MicroKernel {
      * @param filter     (optional) filter criteria
      *                   (e.g. names of properties to be included, etc);
      *                   TODO specify format and semantics
-     * @return node tree in JSON format
-     * @throws MicroKernelException if an error occurs
+     * @return node tree in JSON format or {@code null} if the specified node does not exist
+     * @throws MicroKernelException if the specified revision does not exist or if another error occurs
      */
     String /* jsonTree */ getNodes(String path, String revisionId, int depth, long offset, int count, String filter) throws MicroKernelException;
 
@@ -262,7 +262,7 @@ public interface MicroKernel {
      *
      * @param path path denoting target node
      * @param jsonDiff changes to be applied in JSON diff format.
-     * @param revisionId id of revision the changes are based on, if null the current head revision is assumed
+     * @param revisionId id of revision the changes are based on, if {@code null} the current head revision is assumed
      * @param message commit message
      * @return id of newly created revision
      * @throws MicroKernelException if an error occurs

Modified: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/core/MicroKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/core/MicroKernelImpl.java?rev=1325827&r1=1325826&r2=1325827&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/core/MicroKernelImpl.java (original)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/core/MicroKernelImpl.java Fri Apr 13 16:14:36 2012
@@ -35,7 +35,6 @@ import org.apache.jackrabbit.mk.model.No
 import org.apache.jackrabbit.mk.model.PropertyState;
 import org.apache.jackrabbit.mk.model.StoredCommit;
 import org.apache.jackrabbit.mk.model.TraversingNodeDiffHandler;
-import org.apache.jackrabbit.mk.store.NotFoundException;
 import org.apache.jackrabbit.mk.store.RevisionProvider;
 import org.apache.jackrabbit.mk.util.CommitGate;
 import org.apache.jackrabbit.mk.util.PathUtils;
@@ -232,17 +231,8 @@ public class MicroKernelImpl implements 
             // to detect 'move' operations
             final HashMap<Id, String> addedNodes = new HashMap<Id, String>();
             final HashMap<Id, String> removedNodes = new HashMap<Id, String>();
-            NodeState node1, node2;
-            try {
-                node1 = rep.getNodeState(fromRevisionId, path);
-            } catch (NotFoundException e) {
-                node1 = null;
-            }
-            try {
-                node2 = rep.getNodeState(toRevisionId, path);
-            } catch (NotFoundException e) {
-                node2 = null;
-            }
+            NodeState node1 = rep.getNodeState(fromRevisionId, path);
+            NodeState node2 = rep.getNodeState(toRevisionId, path);
 
             if (node1 == null) {
                 if (node2 != null) {
@@ -389,52 +379,66 @@ public class MicroKernelImpl implements 
         }
     }
 
-    public boolean nodeExists(String path, String revision) throws MicroKernelException {
+    public boolean nodeExists(String path, String revisionId) throws MicroKernelException {
         if (rep == null) {
             throw new IllegalStateException("this instance has already been disposed");
         }
 
-        Id revisionId = revision == null ? getHeadRevisionId() : Id.fromString(revision);
-        return rep.nodeExists(revisionId, path);
+        Id revId = revisionId == null ? getHeadRevisionId() : Id.fromString(revisionId);
+        try {
+            return rep.nodeExists(revId, path);
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
     }
 
-    public long getChildNodeCount(String path, String revision) throws MicroKernelException {
+    public long getChildNodeCount(String path, String revisionId) throws MicroKernelException {
         if (rep == null) {
             throw new IllegalStateException("this instance has already been disposed");
         }
 
-        Id revisionId = revision == null ? getHeadRevisionId() : Id.fromString(revision);
+        Id revId = revisionId == null ? getHeadRevisionId() : Id.fromString(revisionId);
 
+        NodeState nodeState;
         try {
-            return rep.getNodeState(revisionId, path).getChildNodeCount();
+            nodeState = rep.getNodeState(revId, path);
         } catch (Exception e) {
             throw new MicroKernelException(e);
         }
+        if (nodeState != null) {
+            return nodeState.getChildNodeCount();
+        } else {
+            throw new MicroKernelException("Path " + path + " not found in revision " + revisionId);
+        }
     }
 
-    public String getNodes(String path, String revision) throws MicroKernelException {
-        return getNodes(path, revision, 1, 0, -1, null);
+    public String getNodes(String path, String revisionId) throws MicroKernelException {
+        return getNodes(path, revisionId, 1, 0, -1, null);
     }
 
-    public String getNodes(String path, String revision, int depth, long offset, int count, String filter) throws MicroKernelException {
+    public String getNodes(String path, String revisionId, int depth, long offset, int count, String filter) throws MicroKernelException {
         if (rep == null) {
             throw new IllegalStateException("this instance has already been disposed");
         }
 
-        Id revisionId = revision == null ? getHeadRevisionId() : Id.fromString(revision);
+        Id revId = revisionId == null ? getHeadRevisionId() : Id.fromString(revisionId);
 
         // TODO extract and evaluate filter criteria (such as e.g. ':hash') specified in 'filter' parameter
 
         try {
+            NodeState nodeState = rep.getNodeState(revId, path);
+            if (nodeState == null) {
+                return null;
+            }
             JsopBuilder buf = new JsopBuilder().object();
-            toJson(buf, rep.getNodeState(revisionId, path), depth, (int) offset, count, true);
+            toJson(buf, nodeState, depth, (int) offset, count, true);
             return buf.endObject().toString();
         } catch (Exception e) {
             throw new MicroKernelException(e);
         }
     }
 
-    public String commit(String path, String jsonDiff, String revision, String message) throws MicroKernelException {
+    public String commit(String path, String jsonDiff, String revisionId, String message) throws MicroKernelException {
         if (rep == null) {
             throw new IllegalStateException("this instance has already been disposed");
         }
@@ -442,11 +446,11 @@ public class MicroKernelImpl implements 
             throw new IllegalArgumentException("absolute path expected: " + path);
         }
 
-        Id revisionId = revision == null ? getHeadRevisionId() : Id.fromString(revision);
+        Id revId = revisionId == null ? getHeadRevisionId() : Id.fromString(revisionId);
 
         try {
             JsopTokenizer t = new JsopTokenizer(jsonDiff);
-            CommitBuilder cb = rep.getCommitBuilder(revisionId, message);
+            CommitBuilder cb = rep.getCommitBuilder(revId, message);
             while (true) {
                 int r = t.read();
                 if (r == JsopTokenizer.END) {
@@ -555,7 +559,7 @@ public class MicroKernelImpl implements 
                 }
             }
             Id newHead = cb.doCommit();
-            if (!newHead.equals(revisionId)) {
+            if (!newHead.equals(revId)) {
                 // non-empty commit
                 gate.commit(newHead.toString());
             }

Modified: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/core/Repository.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/core/Repository.java?rev=1325827&r1=1325826&r2=1325827&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/core/Repository.java (original)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/core/Repository.java Fri Apr 13 16:14:36 2012
@@ -90,6 +90,7 @@ public class Repository {
         }
 
         H2Persistence pm = new H2Persistence(homeDir);
+        //org.apache.jackrabbit.mk.persistence.MongoPersistence pm = new org.apache.jackrabbit.mk.persistence.MongoPersistence();
         pm.initialize();
         
         DefaultRevisionStore rs = new DefaultRevisionStore(pm);
@@ -157,8 +158,7 @@ public class Repository {
         return rs.getCommit(id);
     }
 
-    public NodeState getNodeState(Id revId, String path)
-            throws NotFoundException, Exception {
+    public NodeState getNodeState(Id revId, String path) throws Exception {
         if (!initialized) {
             throw new IllegalStateException("not initialized");
         } else if (!PathUtils.isAbsolute(path)) {
@@ -169,34 +169,27 @@ public class Repository {
         for (String name : PathUtils.split(path)) {
             node = node.getChildNode(name);
             if (node == null) {
-                throw new NotFoundException(
-                        "Path " + path + " not found in revision " + revId);
+                break;
             }
         }
         return node;
     }
 
-    public boolean nodeExists(Id revId, String path) {
+    public boolean nodeExists(Id revId, String path) throws Exception {
         if (!initialized) {
             throw new IllegalStateException("not initialized");
         } else if (!PathUtils.isAbsolute(path)) {
             throw new IllegalArgumentException("illegal path");
         }
 
-        try {
-            NodeState node = rs.getNodeState(rs.getRootNode(revId));
-            for (String name : PathUtils.split(path)) {
-                node = node.getChildNode(name);
-                if (node == null) {
-                    return false;
-                }
+        NodeState node = rs.getNodeState(rs.getRootNode(revId));
+        for (String name : PathUtils.split(path)) {
+            node = node.getChildNode(name);
+            if (node == null) {
+                return false;
             }
-            return true;
-        } catch (Exception e) {
-            throw new RuntimeException(
-                    "Failed to check for existence of path "
-                    + path + " in revision " + revId, e);
         }
+        return true;
     }
 
     public CommitBuilder getCommitBuilder(Id revId, String msg) throws Exception {