jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1170245 - in /jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src: main/java/org/apache/jackrabbit/spi2microkernel/QueryProcessor.java test/java/org/apache/jackrabbit/spi2microkernel/QueryTest.java test/resources/queryPlans.txt
Date Tue, 13 Sep 2011 17:09:10 GMT
Author: thomasm
Date: Tue Sep 13 17:09:10 2011
New Revision: 1170245

URL: http://svn.apache.org/viewvc?rev=1170245&view=rev
Log:
Simple query mechanism.

Modified:
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/QueryProcessor.java
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/QueryTest.java
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/resources/queryPlans.txt

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/QueryProcessor.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/QueryProcessor.java?rev=1170245&r1=1170244&r2=1170245&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/QueryProcessor.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/QueryProcessor.java
Tue Sep 13 17:09:10 2011
@@ -29,12 +29,12 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.mk.index.Indexer;
 import org.apache.jackrabbit.mk.index.PrefixIndex;
 import org.apache.jackrabbit.mk.index.PropertyIndex;
-import org.apache.jackrabbit.mk.util.PathUtils;
 import org.apache.jackrabbit.spi.IdFactory;
 import org.apache.jackrabbit.spi.ItemInfo;
 import org.apache.jackrabbit.spi.Name;
@@ -122,11 +122,16 @@ public class QueryProcessor {
                     QueryPlan plan = new QueryPlan();
                     plan.queryPattern = r.readLine().trim();
                     String indexName = r.readLine().trim();
+                    if (indexName.equals("-")) {
+                        plan.fixedResult = true;
+                    }
                     plan.index = indexes.get(indexName);
                     plan.propertyValue = r.readLine().trim();
-                    plan.columnNames = r.readLine().trim().split(",");
+                    String cn = "jcr:path,jcr:score," + r.readLine().trim();
+                    plan.columnNames = cn.split(",");
                     plan.selectorNames = new Name[plan.columnNames.length];
-                    String[] sel = r.readLine().trim().split(",");
+                    cn = "jcr:path,jcr:score," + r.readLine().trim();
+                    String[] sel = cn.trim().split(",");
                     NameFactory f = NameFactoryImpl.getInstance();
                     for (int i=0; i<sel.length; i++) {
                         String[] s = sel[i].split(":");
@@ -182,49 +187,71 @@ public class QueryProcessor {
         String propertyValue;
         String[] columnNames;
         Name[] selectorNames;
+        boolean fixedResult;
 
         boolean matches(String language, String query) {
             return query.trim().matches(queryPattern.trim());
         }
 
         public String toString() {
-            return queryPattern + " index:" + index.name + " columnNames:" + Arrays.toString(columnNames)
+ " propertyNames:" + Arrays.toString(selectorNames) + " propertyValue:" + propertyValue;
+            return queryPattern + " index:" + (index == null ? "-" : index.name) +
+                    " columnNames:" + Arrays.toString(columnNames) + " propertyNames:" +
Arrays.toString(selectorNames) + " propertyValue:" + propertyValue;
         }
 
         public QueryInfo execute(SessionInfo sessionInfo) throws RepositoryException {
             QueryInfoImpl result = new QueryInfoImpl(columnNames, selectorNames);
-            if (index.propertyIndex != null) {
+            if (index != null && index.propertyIndex != null) {
                 String v = propertyValue.length() == 0 ? null : propertyValue;
                 Iterator<String> paths = index.propertyIndex.getPaths(v, microKernel
                         .getHeadRevision());
                 while (paths.hasNext()) {
                     String path = paths.next();
-                    path = path.substring(PathUtils.getNextSlash(path, 1));
-                    NodeId node = idFactory.createNodeId((String) null, Paths.stringToPath(path));
-                    log.info("  row: " + path + " " + node);
-                    Iterator<? extends ItemInfo> it = rep.getItemInfos(sessionInfo,
node);
-                    HashMap<String, QValue> values = new HashMap<String, QValue>();
-                    while (it.hasNext()) {
-                        ItemInfo info = it.next();
-                        if (!info.denotesNode()) {
-                            PropertyInfo p = (PropertyInfo) rep.getItemInfos(sessionInfo,
-                                    info.getId()).next();
-                            String propertyName = p.getPath().getLastElement().getName().toString();
-                            values.put(propertyName, p.getValues()[0]);
-                        }
-                    }
-                    QValue[] valueList = new QValue[selectorNames.length];
-                    for (int i = 0; i < selectorNames.length; i++) {
-                        Name selector = selectorNames[i];
-                        valueList[i] = values.get(selector.toString());
-                    }
-                    QueryInfoImpl.QueryResultRowImpl row = new QueryInfoImpl.QueryResultRowImpl(node,
valueList);
-                    result.addRow(row);
+                    path = path.substring(path.indexOf('/', 1));
+                    log.info("  row: " + path);
+                    result.addRow(getRow(sessionInfo, path));
                 }
+            } else if (fixedResult) {
+                String path = propertyValue;
+                log.info("  fixed row: " + path);
+                result.addRow(getRow(sessionInfo, path));
             }
             return result;
         }
 
+        private QueryInfoImpl.QueryResultRowImpl getRow(SessionInfo sessionInfo, String path)
throws RepositoryException {
+            NodeId nodeId = idFactory.createNodeId((String) null, Paths.stringToPath(path));
+            Iterator<? extends ItemInfo> it = rep.getItemInfos(sessionInfo, nodeId);
+            HashMap<String, QValue> values = new HashMap<String, QValue>();
+            while (it.hasNext()) {
+                ItemInfo info = it.next();
+                if (!info.denotesNode()) {
+                    PropertyInfo p = (PropertyInfo) rep.getItemInfos(sessionInfo,
+                            info.getId()).next();
+                    String propertyName = p.getPath().getLastElement().getName().toString();
+                    values.put(propertyName, p.getValues()[0]);
+                }
+            }
+            QValue[] valueList = new QValue[selectorNames.length];
+            for (int i = 0; i < selectorNames.length; i++) {
+                Name selector = selectorNames[i];
+                QValue v;
+                if (columnNames[i].equals("jcr:path")) {
+                    v = vf.create(path, PropertyType.STRING);
+                } else if (columnNames[i].equals("jcr:score")) {
+                    v = vf.create(0.5);
+                } else {
+                    v = values.get(selector.toString());
+                }
+                if (v == null) {
+                    v = vf.create("", PropertyType.STRING);
+                }
+                valueList[i] = v;
+            }
+            QueryInfoImpl.QueryResultRowImpl row = new QueryInfoImpl.QueryResultRowImpl(nodeId,
+                    valueList);
+            return row;
+        }
+
     }
 
 }

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/QueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/QueryTest.java?rev=1170245&r1=1170244&r2=1170245&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/QueryTest.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/QueryTest.java
Tue Sep 13 17:09:10 2011
@@ -14,7 +14,9 @@
 package org.apache.jackrabbit.spi2microkernel;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import java.util.Collections;
 import java.util.Iterator;
 import javax.imageio.spi.ServiceRegistry;
@@ -24,8 +26,11 @@ import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.RepositoryFactory;
 import javax.jcr.Session;
+import javax.jcr.Value;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryResult;
+import javax.jcr.query.Row;
+import javax.jcr.query.RowIterator;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -84,6 +89,40 @@ public class QueryTest {
     }
 
     @Test
+    public void dummyQuery() throws RepositoryException {
+        Session s = getSession();
+        Query q = s.getWorkspace().getQueryManager().createQuery(
+                "/*", Query.JCR_SQL2);
+        QueryResult res = q.execute();
+        NodeIterator it = res.getNodes();
+        Node n = it.nextNode();
+        assertEquals("/", n.getPath());
+        assertFalse(it.hasNext());
+    }
+
+    @Test
+    public void dummyQueryRows() throws RepositoryException {
+        Session s = getSession();
+        Query q = s.getWorkspace().getQueryManager().createQuery(
+                "/*", Query.JCR_SQL2);
+        QueryResult res = q.execute();
+        String[] colNames = res.getColumnNames();
+        assertEquals(3, colNames.length);
+        assertEquals("jcr:path", colNames[0]);
+        assertEquals("jcr:score", colNames[1]);
+        assertEquals("jcr:test", colNames[2]);
+        RowIterator it = res.getRows();
+        assertTrue(it.hasNext());
+        Row r = it.nextRow();
+        assertEquals("/", r.getPath());
+        Value[] values = r.getValues();
+        System.out.println(values);
+        assertEquals("/", r.getValue("jcr:path").getString());
+        assertEquals("0.5", r.getValue("jcr:score").getString());
+        assertFalse(it.hasNext());
+    }
+
+    @Test
     public void createRepository() throws RepositoryException {
         Repository repository = getRepository();
         assertNotNull(repository);

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/resources/queryPlans.txt
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/resources/queryPlans.txt?rev=1170245&r1=1170244&r2=1170245&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/resources/queryPlans.txt
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/resources/queryPlans.txt
Tue Sep 13 17:09:10 2011
@@ -11,3 +11,9 @@ SELECT test:vanityPath, test:redirect FR
 testPath,redirect
 test:vanityPath,test:redirect
 
+plan
+/\*
+-
+/
+jcr:test
+jcr:test
\ No newline at end of file



Mime
View raw message