You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by th...@apache.org on 2011/06/15 15:45:47 UTC
svn commit: r1136039 - in /jackrabbit/sandbox/microkernel/src:
main/java/org/apache/jackrabbit/mk/ main/java/org/apache/jackrabbit/mk/api/
main/java/org/apache/jackrabbit/mk/mem/
main/java/org/apache/jackrabbit/mk/store/ test/java/org/apache/jackrabbit...
Author: thomasm
Date: Wed Jun 15 13:45:47 2011
New Revision: 1136039
URL: http://svn.apache.org/viewvc?rev=1136039&view=rev
Log:
Simplify the API a bit (remove getChildNodes)
Modified:
jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java
jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Constants.java
jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java
jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/ConcurrentWriteTest.java
jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/HelloWorld.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=1136039&r1=1136038&r2=1136039&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 Wed Jun 15 13:45:47 2011
@@ -175,7 +175,7 @@ public class MicroKernelImpl implements
buff.append("+ ").
key(PathUtil.concat(nodePath, childName));
try {
- toJson(rep.getStore().getNode(id), childName, Integer.MAX_VALUE, false, buff);
+ toJson(buff, rep.getStore().getNode(id), childName, Integer.MAX_VALUE, 0, -1);
} catch (Exception e) {
buff.value("ERROR: failed to retrieve node " + id);
}
@@ -213,46 +213,20 @@ public class MicroKernelImpl implements
}
}
- public String getNodes(String path, int depth, String revisionId) throws MicroKernelException {
- if (rep == null) {
- throw new IllegalStateException("this instance has already been disposed");
- }
+ public String getNodes(String path, String revisionId) throws MicroKernelException {
+ return getNodes(path, revisionId, 1, 0, -1);
+ }
+ public String getNodes(String path, String revisionId, int depth, long offset, int count) throws MicroKernelException {
try {
JsopBuilder buf = new JsopBuilder().object();
- toJson(rep.getNode(revisionId, path), PathUtil.getName(path), depth, true, buf);
+ toJson(buf, rep.getNode(revisionId, path), PathUtil.getName(path), depth, offset, count);
return buf.endObject().toString();
} catch (Exception e) {
throw new MicroKernelException(e);
}
}
- public String getChildNodes(String path, long offset, long count, int depth, String revisionId) throws MicroKernelException {
- if (rep == null) {
- throw new IllegalStateException("this instance has already been disposed");
- }
-
- try {
- Node node = rep.getNode(revisionId, path);
- JsopBuilder buf = new JsopBuilder().array();
- long pos = 0;
- for (Map.Entry<String, String> entry : node.getChildNodeEntries().entrySet()) {
- if (pos < offset) {
- pos++;
- continue;
- }
- if (pos - offset >= count) {
- break;
- }
- buf.value(entry.getKey());
- pos++;
- }
- return buf.endArray().toString();
- } catch (Exception e) {
- throw new MicroKernelException(e);
- }
- }
-
public long getChildNodeCount(String path, String revisionId) throws MicroKernelException {
if (rep == null) {
throw new IllegalStateException("this instance has already been disposed");
@@ -365,27 +339,26 @@ public class MicroKernelImpl implements
//-------------------------------------------------------< implementation >
- void toJson(Node node, String name, int depth, boolean metaProps, JsopBuilder builder) throws Exception {
- if (metaProps) {
- builder.key(":name").value(name);
- }
+ void toJson(JsopBuilder builder, Node node, String name, int depth, long offset, int count) throws Exception {
for (Map.Entry<String, String> prop : node.getProperties().entrySet()) {
builder.key(prop.getKey()).encodedValue(prop.getValue());
}
-
long childCount = node.getChildNodeCount();
- if (metaProps) {
- builder.key(":childNodeCount").value(childCount);
- }
+ builder.key(":childNodeCount").value(childCount);
if (childCount > 0 && depth >= 0) {
for (Map.Entry<String, String> child : node.getChildNodeEntries().entrySet()) {
- String childName = child.getKey();
- builder.key(childName).object();
- if (depth > 0) {
- String childId = child.getValue();
- toJson(rep.getStore().getNode(childId), childName, depth - 1, metaProps, builder);
+ if (offset-- > 0) {
+ continue;
+ }
+ if (count < 0 || count-- > 0) {
+ String childName = child.getKey();
+ builder.key(childName).object();
+ if (depth > 0) {
+ String childId = child.getValue();
+ toJson(builder, rep.getStore().getNode(childId), childName, depth - 1, 0, -1);
+ }
+ builder.endObject();
}
- builder.endObject();
}
}
}
Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java?rev=1136039&r1=1136038&r2=1136039&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java Wed Jun 15 13:45:47 2011
@@ -153,122 +153,79 @@ public interface MicroKernel {
boolean nodeExists(String path, String revisionId) throws MicroKernelException;
/**
- * Returns the node tree rooted at the specified parent node. The depth of
- * the returned tree is governed by the <code>depth</code> parameter:
- * <table>
- * <tr><td>depth = 0</td><td>just properties, including <code>:childNodeCount</code></td></tr>
- * <tr><td>depth = 1</td><td>properties, child nodes and their properties (including <code>:childNodeCount</code>)</td></tr>
- * <tr><td>depth = 2</td><td>[and so on...]</td></tr>
- * </table>
- * <p/>
- * Format:
+ * Returns the node tree rooted at the specified parent node with depth 1.
+ * Depth 1 means all properties of the node are returned, including the list
+ * of child nodes and their properties (including
+ * <code>:childNodeCount</code>). Example:
* <pre>
* {
- * ":name" : "parent",
- * "someprop" : "someval",
- * ":childNodeCount" : 2,
- * ":childNodes" : [
- * { ":name" : "child1", "prop1" : "foo", ":childNodeCount" : 1, ":childNodes" : [
- * { ":name" : "grandchild", "propA" : "blahblah", ":childNodeCount" : 0 }
- * ]},
- * { ":name" : "child2", "prop1" : "bar", ":childNodeCount" : 0}
- * ]
+ * "someprop": "someval",
+ * ":childNodeCount": 2,
+ * "child1" : {
+ * "prop1": "foo",
+ * ":childNodeCount": 2
+ * },
+ * "child2": {
+ * "prop1": "bar"
+ * ":childNodeCount": 0
+ * }
* }
* </pre>
+ * The collection of name/value pairs denoting child nodes is assumed to be
+ * ordered.
*
- * Some remarks:
+ * Remarks:
* <ul>
- * <li><code>:childNodeCount > 0</code> indicates that the node does have child nodes,
- * although they might not be included in the tree.</li>
- * <li><code>:childNodeCount > lengthOf(:childNodes)</code> indicates that the
- * node does have more child nodes than those included in the tree. Large number
- * of child nodes can be retrieved in chunks using the
- * {@link #getChildNodes(String, long, long, int, String)} method</li>
+ * <li>If the property <code>:childNodeCount</code> is 0, then the
+ * node does not have child nodes.
+ * <li>If the value of <code>:childNodeCount</code> is larger than the list
+ * of returned child nodes, then node does have more child nodes than those
+ * included in the tree. Large number of child nodes can be retrieved in
+ * chunks using {@link #getNodes(String, String, int, long, int)}</li>
* </ul>
- *
- * TBD: Simpler and more intuitive format, albeit not strictly JSON compliant
- * (the collection of name/value pairs denoting child nodes is assumed to be ordered):
- * <pre>
- * {
- * ":name" : "parent",
- * "someprop" : "someval",
- * ":childNodeCount" : 2,
- * "child1" : {
- * "prop1" : "foo",
- * ":childNodeCount" : 1,
- * "grandchild" : {
- * "propA" : "blahblah",
- * ":childNodeCount" : 0
- * }
- * }
- * "child2" : {
- * "prop1" : "bar",
- * ":childNodeCount" : 0
- * }
- * }
- * </pre>
+ * This method is a convenience method for
+ * <code>getNodes(path, revisionId, 1, 0, -1)</code>
*
* @param path path denoting root of node tree to be retrieved
- * @param depth maximum depth of returned tree
* @param revisionId revision
* @return node tree in JSON format
* @throws MicroKernelException if an error occurs
*/
- // TODO depth: need to discuss if this makes sense
- String /* jsonTree */ getNodes(String path, int depth, String revisionId) throws MicroKernelException;
-
- // specialized methods for reading flat hierarchies
+ String /* jsonTree */ getNodes(String path, String revisionId) throws MicroKernelException;
/**
- * Similar to {@link #getNodes(String, int, String)}, except that this method
- * returns an array of child node trees and allows for reading large lists
- * of child nodes in chunks (by specifying appropriate values for
- * <code>offset</code> and <code>count</code>).
- * <p/>
- * The depth of the returned trees is governed by the <code>depth</code> parameter:
+ * Returns the node tree rooted at the specified parent node with the
+ * specified depth, maximum child node count and offset. The depth of the
+ * returned tree is governed by the <code>depth</code> parameter:
* <table>
- * <tr><td>depth = 0</td><td>child nodes with just properties, including <code>:childNodeCount</code></td></tr>
- * <tr><td>depth = 1</td><td>child nodes with properties, grandchildren with their properties (including <code>:childNodeCount</code>)</td></tr>
- * <tr><td>depth = 2</td><td>[and so on...]</td></tr>
+ * <tr>
+ * <td>depth = 0</td>
+ * <td>properties, including <code>:childNodeCount</code> and the list
+ * of child node names (as empty objects)</td>
+ * </tr>
+ * <tr>
+ * <td>depth = 1</td>
+ * <td>properties, child nodes and their properties (including
+ * <code>:childNodeCount</code>)</td>
+ * </tr>
+ * <tr>
+ * <td>depth = 2</td>
+ * <td>[and so on...]</td>
+ * </tr>
* </table>
- * <p/>
- * Format:
- * <pre>
- * [
- * { ":name" : "child1", "prop1" : "foo", ":childNodeCount" : 101},
- * { ":name" : "child2", "prop1" : "bar", ":childNodeCount" : 0},
- * { ":name" : "child3", "prop1" : "blah", ":childNodeCount" : 2}
- * ]
- * </pre>
- *
- * @param path path denoting parent node
- * @param offset start position in child node list
- * @param count maximum number of child nodes to retrieve
- * @param depth maximum depth of returned trees
- * @param revisionId revision
- * @return array of node tree in JSON format
- * @throws MicroKernelException if an error occurs
- */
- // TODO depth: does it even make sense in this context?
- // if depth is needed, why not use getNodes?
- String /* array of jsonTrees */ getChildNodes(String path, long offset, long count, int depth, String revisionId) throws MicroKernelException;
-
- /**
- * Returns the number of child nodes of the specified parent node.
- *
- * @todo if we represent the number of child nodes as a special property on the parent (":childNodeCount") there's probably no need for a specific api method (TBD)
+ * Offset and count only affect the returned child node list of this node.
*
- * @param path path denoting parent node
+ * @param path path denoting root of node tree to be retrieved
* @param revisionId revision
- * @return number of child nodes
+ * @param depth maximum depth of returned tree
+ * @param offset start position in child node list (0 to start at the
+ * beginning)
+ * @param count maximum number of child nodes to retrieve (-1 for as many as
+ * possible)
+ * @return node tree in JSON format
* @throws MicroKernelException if an error occurs
*/
- // TODO is it required? might as well use getNodes(path) with depth 0
- // except that parsing isn't required here
- // (but parsing is required when calling getChildNodes anyway)
- long /* count */ getChildNodeCount(String path, String revisionId)
- throws MicroKernelException;
-
+ String /* jsonTree */ getNodes(String path, String revisionId, int depth, long offset, int count) throws MicroKernelException;
//------------------------------------------------------------< WRITE ops >
Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Constants.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Constants.java?rev=1136039&r1=1136038&r2=1136039&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Constants.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Constants.java Wed Jun 15 13:45:47 2011
@@ -21,8 +21,7 @@ package org.apache.jackrabbit.mk.mem;
*/
public class Constants {
public static final boolean TYPE_AS_COMMENT = false;
- public static final boolean NODE_NAME_AS_PROPERTY = true;
- public static final boolean CHILD_NODE_COUNT_0 = true;
+ public static final boolean NODE_NAME_AS_PROPERTY = false;
public static final boolean JSON_NEWLINES = false;
public static final int VERSION_MAJOR = 3;
Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java?rev=1136039&r1=1136038&r2=1136039&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java Wed Jun 15 13:45:47 2011
@@ -125,30 +125,6 @@ public class MemoryKernelImpl implements
}
}
- public long getChildNodeCount(String path, String revisionId) {
- return getNode(path, revisionId).getChildNodeCount();
- }
-
- public String getChildNodes(String path, long offset, long count, int depth, String revisionId) {
- JsopBuilder json = new JsopBuilder().array();
- getNode(path, revisionId).appendChildNodes(json, (int) offset, (int) count);
- return json.endArray().toString();
- }
-
- private NodeImpl getNode(String path, String revisionId) {
- if (!path.startsWith("/")) {
- throw new IllegalArgumentException("not a root path: " + path);
- }
- path = path.substring(1);
- long revId = parseRevisionId(revisionId);
- NodeImpl root = roots.get(revId);
- NodeImpl n = root.getNode(path);
- if (n == null) {
- throw new RuntimeException("path not found: " + path);
- }
- return n;
- }
-
public String getHeadRevision() {
return getRevisionId(headRevId);
}
@@ -159,7 +135,11 @@ public class MemoryKernelImpl implements
return "";
}
- public String getNodes(String path, int depth, String revisionId) {
+ public String getNodes(String path, String revisionId) {
+ return getNodes(path, revisionId, 1, 0, -1);
+ }
+
+ public String getNodes(String path, String revisionId, int depth, long offset, int count) {
if (!path.startsWith("/")) {
throw new IllegalArgumentException("not a root path: " + path);
}
@@ -171,7 +151,7 @@ public class MemoryKernelImpl implements
throw new RuntimeException("path not found: " + path);
}
JsopBuilder json = new JsopBuilder();
- n.append(json, true, depth);
+ n.append(json, depth, offset, count);
return json.toString();
}
@@ -181,6 +161,7 @@ public class MemoryKernelImpl implements
}
public boolean nodeExists(String path, String revisionId) {
+ // TODO use a bloom filter
if (!path.startsWith("/")) {
throw new IllegalArgumentException("not a root path: " + path);
}
Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java?rev=1136039&r1=1136038&r2=1136039&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java Wed Jun 15 13:45:47 2011
@@ -141,7 +141,7 @@ public class NodeImpl {
public String toString() {
JsopBuilder json = new JsopBuilder();
- append(json, true, 1);
+ append(json, 0, 0, -1);
return json.toString();
}
@@ -153,31 +153,35 @@ public class NodeImpl {
}
}
- public void append(JsopBuilder json, boolean includeProperties, int depth) {
- if (includeProperties) {
- json.object();
- if (properties != null) {
- for (Entry<String, String> e : properties.entrySet()) {
- json.key(e.getKey()).encodedValue(e.getValue());
- }
+ public void append(JsopBuilder json, int depth, long offset, int count) {
+ json.object();
+ if (properties != null) {
+ for (Entry<String, String> e : properties.entrySet()) {
+ json.key(e.getKey()).encodedValue(e.getValue());
}
}
- if (childNodes != null) {
+ if (childNodes == null) {
+ json.key(":childNodeCount").value(0);
+ } else {
json.key(":childNodeCount").value(childNodes.size());
- if (depth >= 0) {
+ if (count != 0) {
for (Entry<String, NodeImpl> e : childNodes.entrySet()) {
- json.key(e.getKey());
- e.getValue().append(json, true, depth - 1);
+ if (offset > 0) {
+ offset--;
+ continue;
+ }
+ if (count < 0 || count-- > 0) {
+ json.key(e.getKey());
+ if (depth == 0) {
+ json.object().endObject();
+ } else {
+ e.getValue().append(json, depth - 1, 0, -1);
+ }
+ }
}
}
- } else {
- if (Constants.CHILD_NODE_COUNT_0) {
- json.key(":childNodeCount").value(0);
- }
- }
- if (includeProperties) {
- json.endObject();
}
+ json.endObject();
}
void addChildNode(String name, NodeImpl node) {
Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java?rev=1136039&r1=1136038&r2=1136039&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java Wed Jun 15 13:45:47 2011
@@ -261,6 +261,7 @@ public class H2RevisionStore extends Sim
if (!initialized) {
throw new IllegalStateException("not initialized");
}
+ // TODO encode length in the blob id
Connection con = cp.getConnection();
try {
final PreparedStatement stmt = con.prepareStatement(
Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/ConcurrentWriteTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/ConcurrentWriteTest.java?rev=1136039&r1=1136038&r2=1136039&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/ConcurrentWriteTest.java (original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/ConcurrentWriteTest.java Wed Jun 15 13:45:47 2011
@@ -22,11 +22,11 @@ import org.apache.jackrabbit.mk.api.Micr
public class ConcurrentWriteTest extends TestCase {
- private static final String URL = "fs:{homeDir};clean";
- // private static final String URL = "mem:";
+ // private static final String URL = "fs:{homeDir};clean";
+ private static final String URL = "mem:";
private static final int NUM_THREADS = 10;
- private static final int NUM_CHILDNODES = 100;
+ private static final int NUM_CHILDNODES = 1000;
private MicroKernel mk;
@@ -77,7 +77,7 @@ public class ConcurrentWriteTest extends
System.out.println("duration: " + (t1 - t0) + "ms");
head = mk.getHeadRevision();
- String json = mk.getNodes("/", Integer.MAX_VALUE, head);
+ String json = mk.getNodes("/", head, Integer.MAX_VALUE, 0, -1);
System.out.println("new HEAD: " + head);
//System.out.println(json);
System.out.println();
Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/HelloWorld.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/HelloWorld.java?rev=1136039&r1=1136038&r2=1136039&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/HelloWorld.java (original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/HelloWorld.java Wed Jun 15 13:45:47 2011
@@ -42,8 +42,8 @@ public class HelloWorld {
head = mk.commit("/", "-\"test\"", head);
}
- head = mk.commit("/", "+\"foo\" : {\"bar\":{\"x\":\"\\/NaN\\/\"}}", head);
- String s = mk.getNodes("/foo/bar", 0, head);
+ head = mk.commit("/", "+\"foo\" : {\"bar\":{\"x\":\"100\"}}", head);
+ String s = mk.getNodes("/foo/bar", head);
System.out.println(s);
// head = mk.commit(null, "+\"/test\" : {\"name\":\"hello\"}", head);
@@ -54,16 +54,18 @@ public class HelloWorld {
head = mk.commit("/", "^\"test/x\" : null", head);
// head = mk.getHeadRevision();
- s = mk.getNodes("/test", 0, head);
+ s = mk.getNodes("/test", head);
// {":name":"test","name":"hello",":childNodeCount":0}
System.out.println(s);
head = mk.commit("/", "-\"test\"", head);
- s = mk.getNodes("/", 0, head);
+ s = mk.getNodes("/", head);
System.out.println(s);
mk.dispose();
+
+ System.out.println("");
}
}