jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1336717 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/api/ oak-core/src/main/java/org/apache/jackrabbit/oak/query/ oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ oak-core/src/test/java/org/apache...
Date Thu, 10 May 2012 14:30:41 GMT
Author: thomasm
Date: Thu May 10 14:30:40 2012
New Revision: 1336717

URL: http://svn.apache.org/viewvc?rev=1336717&view=rev
Log:
OAK-34 Define query API - support workspaces and access rights in queries

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/QueryEngine.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngineImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AstElement.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ChildNodeImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/DescendantNodeImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SameNodeImpl.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryTest.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionDelegate.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryManagerImpl.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/QueryEngine.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/QueryEngine.java?rev=1336717&r1=1336716&r2=1336717&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/QueryEngine.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/QueryEngine.java
Thu May 10 14:30:40 2012
@@ -49,12 +49,13 @@ public interface QueryEngine {
      *
      * @param statement the query statement
      * @param language the language
+     * @param session the content session to use
      * @param bindings the bind variable value bindings
      * @return the result
      * @throws ParseException if the statement could not be parsed
      * @throws IllegalArgumentException if there was an error executing the query
      */
-    Result executeQuery(String statement, String language, Map<String, CoreValue> bindings)
throws ParseException;
+    Result executeQuery(String statement, String language, ContentSession session, Map<String,
CoreValue> bindings) throws ParseException;
 
     // TODO pass namespace mapping
     // TODO pass node type information (select * from [xyz] is supposed to return at least
the mandatory columns for xyz)

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java?rev=1336717&r1=1336716&r2=1336717&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
Thu May 10 14:30:40 2012
@@ -20,6 +20,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.apache.jackrabbit.oak.api.ContentSession;
 import org.apache.jackrabbit.oak.api.CoreValue;
 import org.apache.jackrabbit.oak.api.CoreValueFactory;
 import org.apache.jackrabbit.oak.query.ast.AstVisitorBase;
@@ -69,6 +70,7 @@ public class Query {
     private long offset;
     private boolean prepared;
     private final CoreValueFactory valueFactory;
+    private ContentSession session;
 
     Query(SourceImpl source, ConstraintImpl constraint, OrderingImpl[] orderings,
           ColumnImpl[] columns, CoreValueFactory valueFactory) {
@@ -461,6 +463,10 @@ public class Query {
         return new ArrayList<String>(bindVariableMap.keySet());
     }
 
+    public String getWorkspaceName() {
+        return session.getWorkspaceName();
+    }
+
     public void setQueryEngine(QueryEngineImpl queryEngine) {
         this.queryEngine = queryEngine;
     }
@@ -469,4 +475,8 @@ public class Query {
         return queryEngine.getBestIndex(filter);
     }
 
+    public void setSession(ContentSession session) {
+        this.session = session;
+    }
+
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngineImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngineImpl.java?rev=1336717&r1=1336716&r2=1336717&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngineImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngineImpl.java
Thu May 10 14:30:40 2012
@@ -22,6 +22,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.apache.jackrabbit.oak.api.ContentSession;
 import org.apache.jackrabbit.oak.api.CoreValue;
 import org.apache.jackrabbit.oak.api.CoreValueFactory;
 import org.apache.jackrabbit.oak.api.QueryEngine;
@@ -82,8 +83,9 @@ public class QueryEngineImpl implements 
     }
 
     @Override
-    public ResultImpl executeQuery(String statement, String language, Map<String, CoreValue>
bindings) throws ParseException {
+    public ResultImpl executeQuery(String statement, String language, ContentSession session,
Map<String, CoreValue> bindings) throws ParseException {
         Query q = parseQuery(statement, language);
+        q.setSession(session);
         q.setMicroKernel(mk);
         if (bindings != null) {
             for (Entry<String, CoreValue> e : bindings.entrySet()) {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AstElement.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AstElement.java?rev=1336717&r1=1336716&r2=1336717&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AstElement.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AstElement.java
Thu May 10 14:30:40 2012
@@ -18,6 +18,7 @@
  */
 package org.apache.jackrabbit.oak.query.ast;
 
+import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.query.Query;
 
 abstract class AstElement {
@@ -43,5 +44,35 @@ abstract class AstElement {
         this.query = query;
     }
 
+    /**
+     * Calculate the absolute path (the path including the workspace name).
+     *
+     * @param path the session local path
+     * @return the absolute path
+     */
+    protected String getAbsolutePath(String path) {
+        String workspaceName = query.getWorkspaceName();
+        if (PathUtils.denotesRoot(workspaceName)) {
+            return path;
+        }
+        String p = PathUtils.relativize("/", path);
+        return PathUtils.concat("/", workspaceName, p);
+    }
+
+    /**
+     * Calculate the session local path (the path excluding the workspace name).
+     *
+     * @param path the absolute path
+     * @return the session local path
+     */
+    protected String getLocalPath(String path) {
+        String workspaceName = query.getWorkspaceName();
+        if (PathUtils.denotesRoot(workspaceName)) {
+            return path;
+        }
+        String prefix = PathUtils.concat("/", workspaceName);
+        return PathUtils.concat("/", PathUtils.relativize(prefix, path));
+    }
+
 }
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ChildNodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ChildNodeImpl.java?rev=1336717&r1=1336716&r2=1336717&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ChildNodeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ChildNodeImpl.java
Thu May 10 14:30:40 2012
@@ -61,15 +61,17 @@ public class ChildNodeImpl extends Const
     @Override
     public boolean evaluate() {
         String p = selector.currentPath();
+        String local = getLocalPath(p);
         // the parent of the root is the root,
         // so we need to special case this
-        return !PathUtils.denotesRoot(p) && PathUtils.getParentPath(p).equals(parentPath);
+        return !PathUtils.denotesRoot(local) && PathUtils.getParentPath(local).equals(parentPath);
     }
 
     @Override
     public void apply(FilterImpl f) {
         if (selector == f.getSelector()) {
-            f.restrictPath(parentPath, Filter.PathRestriction.DIRECT_CHILDREN);
+            String path = getAbsolutePath(parentPath);
+            f.restrictPath(path, Filter.PathRestriction.DIRECT_CHILDREN);
         }
     }
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/DescendantNodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/DescendantNodeImpl.java?rev=1336717&r1=1336716&r2=1336717&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/DescendantNodeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/DescendantNodeImpl.java
Thu May 10 14:30:40 2012
@@ -44,7 +44,8 @@ public class DescendantNodeImpl extends 
     @Override
     public boolean evaluate() {
         String p = selector.currentPath();
-        return PathUtils.isAncestor(ancestorPath, p);
+        String path = getAbsolutePath(ancestorPath);
+        return PathUtils.isAncestor(path, p);
     }
 
     @Override
@@ -67,7 +68,8 @@ public class DescendantNodeImpl extends 
     @Override
     public void apply(FilterImpl f) {
         if (f.getSelector() == selector) {
-            f.restrictPath(ancestorPath, Filter.PathRestriction.ALL_CHILDREN);
+            String path = getAbsolutePath(ancestorPath);
+            f.restrictPath(path, Filter.PathRestriction.ALL_CHILDREN);
         }
     }
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SameNodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SameNodeImpl.java?rev=1336717&r1=1336716&r2=1336717&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SameNodeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SameNodeImpl.java
Thu May 10 14:30:40 2012
@@ -42,7 +42,8 @@ public class SameNodeImpl extends Constr
 
     @Override
     public boolean evaluate() {
-        return selector.currentPath().equals(path);
+        String p = getAbsolutePath(path);
+        return selector.currentPath().equals(p);
     }
 
     @Override
@@ -65,7 +66,8 @@ public class SameNodeImpl extends Constr
     @Override
     public void apply(FilterImpl f) {
         if (f.getSelector() == selector) {
-            f.restrictPath(path, Filter.PathRestriction.EXACT);
+            String p = getAbsolutePath(path);
+            f.restrictPath(p, Filter.PathRestriction.EXACT);
         }
     }
 

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryTest.java?rev=1336717&r1=1336716&r2=1336717&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryTest.java
Thu May 10 14:30:40 2012
@@ -16,6 +16,7 @@ package org.apache.jackrabbit.oak.query;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.LineNumberReader;
@@ -27,9 +28,14 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import org.apache.jackrabbit.oak.api.AuthInfo;
+import org.apache.jackrabbit.oak.api.ContentSession;
 import org.apache.jackrabbit.oak.api.CoreValue;
+import org.apache.jackrabbit.oak.api.CoreValueFactory;
+import org.apache.jackrabbit.oak.api.QueryEngine;
 import org.apache.jackrabbit.oak.api.Result;
 import org.apache.jackrabbit.oak.api.ResultRow;
+import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.spi.QueryIndexProvider;
 import org.junit.Test;
 
@@ -40,6 +46,39 @@ public class QueryTest extends AbstractQ
 
     private QueryIndexProvider ip = mk.getIndexer();
     private QueryEngineImpl qe = new QueryEngineImpl(store, mk, ip);
+    private ContentSession session = new ContentSession() {
+
+        @Override
+        public AuthInfo getAuthInfo() {
+            return null;
+        }
+
+        @Override
+        public CoreValueFactory getCoreValueFactory() {
+            return null;
+        }
+
+        @Override
+        public Root getCurrentRoot() {
+            return null;
+        }
+
+        @Override
+        public QueryEngine getQueryEngine() {
+            return null;
+        }
+
+        @Override
+        public String getWorkspaceName() {
+            return "/";
+        }
+
+        @Override
+        public void close() throws IOException {
+            // ignore
+        }
+
+    };
 
     @Test
     public void script() throws Exception {
@@ -58,19 +97,19 @@ public class QueryTest extends AbstractQ
         HashMap<String, CoreValue> sv = new HashMap<String, CoreValue>();
         sv.put("id", vf.createValue("1"));
         Iterator<? extends ResultRow> result;
-        result = qe.executeQuery("select * from [nt:base] where id = $id",
-                QueryEngineImpl.SQL2, sv).getRows().iterator();
+        result = executeQuery("select * from [nt:base] where id = $id",
+                sv).getRows().iterator();
         assertTrue(result.hasNext());
         assertEquals("/test/hello", result.next().getPath());
 
         sv.put("id", vf.createValue("2"));
-        result = qe.executeQuery("select * from [nt:base] where id = $id",
-                QueryEngineImpl.SQL2, sv).getRows().iterator();
+        result = executeQuery("select * from [nt:base] where id = $id",
+                sv).getRows().iterator();
         assertTrue(result.hasNext());
         assertEquals("/test/world", result.next().getPath());
 
-        result = qe.executeQuery("explain select * from [nt:base] where id = 1 order by id",
-                QueryEngineImpl.SQL2, null).getRows().iterator();
+        result = executeQuery("explain select * from [nt:base] where id = 1 order by id",
+                null).getRows().iterator();
         assertTrue(result.hasNext());
         assertEquals("nt:base AS nt:base /* traverse \"//*\" */",
                 result.next().getValue("plan").getString());
@@ -165,7 +204,7 @@ public class QueryTest extends AbstractQ
 
     private List<String> executeQuery(String query) throws ParseException {
         List<String> lines = new ArrayList<String>();
-        Result result = qe.executeQuery(query, QueryEngineImpl.SQL2, null);
+        Result result = executeQuery(query, null);
         for (ResultRow row : result.getRows()) {
             lines.add(readRow(row));
         }
@@ -188,4 +227,8 @@ public class QueryTest extends AbstractQ
         return buff.toString();
     }
 
+    private Result executeQuery(String statement, HashMap<String, CoreValue> sv) throws
ParseException {
+        return qe.executeQuery(statement, QueryEngineImpl.SQL2, session, sv);
+    }
+
 }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionDelegate.java?rev=1336717&r1=1336716&r2=1336717&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionDelegate.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionDelegate.java
Thu May 10 14:30:40 2012
@@ -291,6 +291,10 @@ public class SessionDelegate {
         return workspace.getVersionManager();
     }
 
+    public ContentSession getContentSession() {
+        return contentSession;
+    }
+
     //------------------------------------------------------------< internal >---
 
     Tree getTree(String path) {

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryManagerImpl.java?rev=1336717&r1=1336716&r2=1336717&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryManagerImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryManagerImpl.java
Thu May 10 14:30:40 2012
@@ -32,6 +32,7 @@ import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
 import javax.jcr.query.QueryResult;
 import javax.jcr.query.qom.QueryObjectModelFactory;
+import org.apache.jackrabbit.oak.api.ContentSession;
 import org.apache.jackrabbit.oak.api.CoreValue;
 import org.apache.jackrabbit.oak.api.QueryEngine;
 import org.apache.jackrabbit.oak.api.Result;
@@ -96,7 +97,8 @@ public class QueryManagerImpl implements
             HashMap<String, Value> bindVariableMap, long limit, long offset) throws
RepositoryException {
         try {
             HashMap<String, CoreValue> bindMap = convertMap(bindVariableMap);
-            Result r = queryEngine.executeQuery(statement, language, bindMap);
+            ContentSession s = sessionDelegate.getContentSession();
+            Result r = queryEngine.executeQuery(statement, language, s, bindMap);
             return new QueryResultImpl(sessionDelegate, r);
         } catch (IllegalArgumentException e) {
             throw new InvalidQueryException(e);



Mime
View raw message