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 2012/02/28 11:10:02 UTC

svn commit: r1294544 - in /jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader: ./ Cursor.java Filter.java Index.java IndexReader.java NodeReader.java TraversingCursor.java TraversingReader.java

Author: thomasm
Date: Tue Feb 28 10:10:02 2012
New Revision: 1294544

URL: http://svn.apache.org/viewvc?rev=1294544&view=rev
Log:
Query implementation (WIP)

Added:
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/
      - copied from r1292828, jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/index/
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/Cursor.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/Filter.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/IndexReader.java
      - copied, changed from r1292828, jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/index/Index.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/NodeReader.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/TraversingCursor.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/TraversingReader.java
Removed:
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/Index.java

Added: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/Cursor.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/Cursor.java?rev=1294544&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/Cursor.java (added)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/Cursor.java Tue Feb 28 10:10:02 2012
@@ -0,0 +1,49 @@
+/*
+ * 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.query.reader;
+
+import org.apache.jackrabbit.mk.simple.NodeImpl;
+
+/**
+ * A cursor to read a number of nodes sequentially.
+ */
+public interface Cursor {
+
+    /**
+     * Skip to the next node if one is available.
+     *
+     * @return true if another row is available
+     */
+    boolean next();
+
+    /**
+     * The path of the current node.
+     *
+     * @return the path
+     */
+    String currentPath();
+
+    /**
+     * The current node.
+     *
+     * @return the node
+     */
+    NodeImpl currentNode();
+
+}

Added: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/Filter.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/Filter.java?rev=1294544&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/Filter.java (added)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/Filter.java Tue Feb 28 10:10:02 2012
@@ -0,0 +1,124 @@
+/*
+ * 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.query.reader;
+
+/**
+ * An index filter / lookup condition.
+ */
+public class Filter {
+
+    /**
+     *  The path, or "/" (the root node, meaning no filter) if not set.
+     */
+    private String path = "/";
+
+    /**
+     *  The node type, or null if not set.
+     */
+    private String nodeType;
+
+    /**
+     *  The value prefix, or null if not set.
+     */
+    private String valuePrefix;
+
+    /**
+     * The first value to read, or null to read from the beginning.
+     */
+    private String first;
+
+    private boolean firstExcluding;
+
+    /**
+     * The last value to read, or null to read until the end.
+     */
+    private String last;
+
+    private boolean lastExcluding;
+
+    /**
+     * Only return distinct values.
+     */
+    private boolean distinct;
+
+    public String getPath() {
+        return path;
+    }
+
+    public void setPath(String path) {
+        this.path = path;
+    }
+
+    public String getNodeType() {
+        return nodeType;
+    }
+
+    public void setNodeType(String nodeType) {
+        this.nodeType = nodeType;
+    }
+
+    public String getValuePrefix() {
+        return valuePrefix;
+    }
+
+    public void setValuePrefix(String valuePrefix) {
+        this.valuePrefix = valuePrefix;
+    }
+
+    public String getFirst() {
+        return first;
+    }
+
+    public void setFirst(String first) {
+        this.first = first;
+    }
+
+    public boolean isFirstExcluding() {
+        return firstExcluding;
+    }
+
+    public void setFirstExcluding(boolean firstExcluding) {
+        this.firstExcluding = firstExcluding;
+    }
+
+    public String getLast() {
+        return last;
+    }
+
+    public void setLast(String last) {
+        this.last = last;
+    }
+
+    public boolean isLastExcluding() {
+        return lastExcluding;
+    }
+
+    public void setLastExcluding(boolean lastExcluding) {
+        this.lastExcluding = lastExcluding;
+    }
+
+    public boolean isDistinct() {
+        return distinct;
+    }
+
+    public void setDistinct(boolean distinct) {
+        this.distinct = distinct;
+    }
+
+}

Copied: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/IndexReader.java (from r1292828, jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/index/Index.java)
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/IndexReader.java?p2=jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/IndexReader.java&p1=jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/index/Index.java&r1=1292828&r2=1294544&rev=1294544&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/index/Index.java (original)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/IndexReader.java Tue Feb 28 10:10:02 2012
@@ -16,9 +16,9 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.jackrabbit.query.index;
+package org.apache.jackrabbit.query.reader;
 
-public class Index {
+public class IndexReader {
 
     /**
      *  The path of what the index contains, or null if not set.
@@ -90,4 +90,9 @@ public class Index {
         return buff.toString();
     }
 
+    public Cursor execute() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
 }

Added: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/NodeReader.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/NodeReader.java?rev=1294544&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/NodeReader.java (added)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/NodeReader.java Tue Feb 28 10:10:02 2012
@@ -0,0 +1,45 @@
+/*
+ * 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.query.reader;
+
+/**
+ * A node reader. The reader should use the data in the filter if possible to speed up reading.
+ */
+public interface NodeReader {
+
+    /**
+     * Estimate the cost to use this reader with the given filter. The returned
+     * cost is a value between 1 (very fast; lookup of a unique node) and the
+     * estimated number of nodes to traverse.
+     *
+     * @param filter the filter
+     * @return the estimated cost in number of read nodes
+     */
+    double getCost(Filter filter);
+
+    /**
+     * Start reading nodes.
+     *
+     * @param filter the filter
+     * @param revisionId the revision
+     * @return a cursor to iterate over the result
+     */
+    Cursor query(Filter filter, String revisionId);
+
+}

Added: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/TraversingCursor.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/TraversingCursor.java?rev=1294544&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/TraversingCursor.java (added)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/TraversingCursor.java Tue Feb 28 10:10:02 2012
@@ -0,0 +1,96 @@
+/*
+ * 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.query.reader;
+
+import java.util.ArrayList;
+import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.apache.jackrabbit.mk.simple.NodeImpl;
+import org.apache.jackrabbit.mk.util.PathUtils;
+
+/**
+ * A cursor that reads all nodes in a given subtree.
+ */
+public class TraversingCursor implements Cursor {
+
+    private static final int CHILD_COUNT = 2000;
+
+    private MicroKernel mk;
+    private String revisionId;
+    private ArrayList<NodeCursor> nodes = new ArrayList<NodeCursor>();
+    private NodeImpl currentNode;
+    private String currentNodeName;
+
+    public TraversingCursor(MicroKernel mk, String revisionId, String path) {
+        this.mk = mk;
+        this.revisionId = revisionId;
+        loadDepth(path, 0);
+    }
+
+    private void loadDepth(String path, long offset) {
+        while (true) {
+            String s = mk.getNodes(path, revisionId, 0, offset, CHILD_COUNT);
+            NodeCursor c = new NodeCursor();
+            c.node = NodeImpl.parse(s);
+            c.node.setPath(path);
+            nodes.add(c);
+            String child = c.node.getChildNodeName(0);
+            if (child != null) {
+                path = PathUtils.concat(path, child);
+            } else {
+                break;
+            }
+        }
+    }
+
+    @Override
+    public NodeImpl currentNode() {
+        if (currentNode == null) {
+            currentNode = NodeImpl.parse(mk.getNodes(currentPath(), revisionId));
+        }
+        return currentNode;
+    }
+
+    @Override
+    public String currentPath() {
+        NodeCursor c = nodes.get(nodes.size() - 1);
+        return PathUtils.concat(c.node.getPath(), currentNodeName);
+    }
+
+    @Override
+    public boolean next() {
+        currentNode = null;
+        while (true) {
+            if (nodes.size() == 0) {
+                return false;
+            }
+            NodeCursor c = nodes.get(nodes.size() - 1);
+            long pos = c.pos++ % CHILD_COUNT;
+            currentNodeName = c.node.getChildNodeName(pos);
+            if (currentNodeName != null) {
+                return true;
+            }
+            nodes.remove(nodes.size() - 1);
+            if (c.pos <= c.node.getTotalChildNodeCount()) {
+                loadDepth(c.node.getPath(), c.pos);
+            }
+        }
+    }
+
+    static class NodeCursor {
+        NodeImpl node;
+        long offset;
+        long pos;
+    }
+
+}

Added: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/TraversingReader.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/TraversingReader.java?rev=1294544&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/TraversingReader.java (added)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/reader/TraversingReader.java Tue Feb 28 10:10:02 2012
@@ -0,0 +1,51 @@
+/*
+ * 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.query.reader;
+
+import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.apache.jackrabbit.mk.util.PathUtils;
+
+public class TraversingReader implements NodeReader {
+
+    private final MicroKernel mk;
+
+    public TraversingReader(MicroKernel mk) {
+        this.mk = mk;
+    }
+
+    @Override
+    public Cursor query(Filter filter, String revisionId) {
+        return new TraversingCursor(mk, revisionId, filter.getPath());
+    }
+
+    @Override
+    public double getCost(Filter filter) {
+        String path = filter.getPath();
+        // TODO estimate or read the node count
+        double nodeCount = 10000000;
+        if (!PathUtils.denotesRoot(path)) {
+            for (int depth = PathUtils.getDepth(path); depth > 0; depth--) {
+                // estimate 10 child nodes per node
+                nodeCount /= 10;
+            }
+        }
+        return nodeCount;
+    }
+
+}