Search in sources :

Example 86 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.

the class QueryTest method getPlan.

private static String getPlan(Session session, String xpath) throws RepositoryException {
    QueryManager qm = session.getWorkspace().getQueryManager();
    QueryResult qr = qm.createQuery("explain " + xpath, "xpath").execute();
    Row r = qr.getRows().nextRow();
    String plan = r.getValue("plan").getString();
    return plan;
}
Also used : QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager) Row(javax.jcr.query.Row)

Example 87 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.

the class QueryTest method nodeTypeConstraint.

@SuppressWarnings("deprecation")
@Test
public void nodeTypeConstraint() throws Exception {
    Session session = getAdminSession();
    Node root = session.getRootNode();
    Node folder1 = root.addNode("folder1", "nt:folder");
    Node folder2 = root.addNode("folder2", "nt:folder");
    JcrUtils.putFile(folder1, "file", "text/plain", new ByteArrayInputStream("foo bar".getBytes("UTF-8")));
    folder2.addNode("folder3", "nt:folder");
    session.save();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery("//element(*, nt:folder)", Query.XPATH);
    Set<String> paths = new HashSet<String>();
    for (RowIterator it = q.execute().getRows(); it.hasNext(); ) {
        paths.add(it.nextRow().getPath());
    }
    assertEquals(new HashSet<String>(Arrays.asList("/folder1", "/folder2", "/folder2/folder3")), paths);
}
Also used : Query(javax.jcr.query.Query) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) HashSet(java.util.HashSet) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 88 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.

the class QueryTest method doubleQuote.

@Test
public void doubleQuote() throws RepositoryException {
    Session session = getAdminSession();
    Node hello = session.getRootNode().addNode("hello");
    hello.setProperty("x", 1);
    Node world = hello.addNode("world");
    world.setProperty("x", 2);
    session.save();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q;
    q = qm.createQuery("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE(s,[/hello])", Query.JCR_SQL2);
    assertEquals("/hello/world", getPaths(q));
    q = qm.createQuery("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE(s,\"/hello\")", Query.JCR_SQL2);
    assertEquals("/hello/world", getPaths(q));
    try {
        q = qm.createQuery("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE(s,[\"/hello\"])", Query.JCR_SQL2);
        getPaths(q);
        fail();
    } catch (InvalidQueryException e) {
    // expected: absolute path
    }
}
Also used : Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) InvalidQueryException(javax.jcr.query.InvalidQueryException) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 89 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.

the class QueryTest method getCost.

private static double getCost(Session session, String xpath) throws RepositoryException {
    QueryManager qm = session.getWorkspace().getQueryManager();
    QueryResult qr = qm.createQuery("explain measure " + xpath, "xpath").execute();
    Row r = qr.getRows().nextRow();
    String plan = r.getValue("plan").getString();
    String cost = plan.substring(plan.lastIndexOf('{'));
    JsonObject json = parseJson(cost);
    double c = Double.parseDouble(json.getProperties().get("a"));
    return c;
}
Also used : QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager) JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject) Row(javax.jcr.query.Row)

Example 90 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.

the class QueryPlanTest method propertyIndexVersusNodeTypeIndex.

@Test
public // OAK-1902
void propertyIndexVersusNodeTypeIndex() throws Exception {
    Session session = getAdminSession();
    Node nt = session.getRootNode().getNode("oak:index").getNode("nodetype");
    nt.setProperty("entryCount", 200);
    Node uuid = session.getRootNode().getNode("oak:index").getNode("uuid");
    uuid.setProperty("entryCount", 100);
    QueryManager qm = session.getWorkspace().getQueryManager();
    if (session.getRootNode().hasNode("testroot")) {
        session.getRootNode().getNode("testroot").remove();
        session.save();
    }
    Node testRootNode = session.getRootNode().addNode("testroot");
    for (int i = 0; i < 100; i++) {
        Node n = testRootNode.addNode("n" + i, "oak:Unstructured");
        n.addMixin("mix:referenceable");
    }
    session.save();
    Query q;
    QueryResult result;
    RowIterator it;
    String xpath = "/jcr:root//element(*, oak:Unstructured)";
    q = qm.createQuery("explain " + xpath, "xpath");
    result = q.execute();
    it = result.getRows();
    assertTrue(it.hasNext());
    String plan = it.nextRow().getValue("plan").getString();
    // System.out.println("plan: " + plan);
    // should use the node type index
    assertEquals("[oak:Unstructured] as [a] " + "/* nodeType Filter(query=explain select [jcr:path], [jcr:score], * " + "from [oak:Unstructured] as a " + "where isdescendantnode(a, '/') " + "/* xpath: /jcr:root//element(*, oak:Unstructured) */" + ", path=//*) where isdescendantnode([a], [/]) */", plan);
    String xpath2 = "/jcr:root//element(*, oak:Unstructured)[@jcr:uuid]";
    q = qm.createQuery("explain " + xpath2 + "", "xpath");
    result = q.execute();
    it = result.getRows();
    assertTrue(it.hasNext());
    plan = it.nextRow().getValue("plan").getString();
    // should use the index on "jcr:uuid"
    assertEquals("[oak:Unstructured] as [a] " + "/* property uuid IS NOT NULL where ([a].[jcr:uuid] is not null) " + "and (isdescendantnode([a], [/])) */", plan);
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Aggregations

QueryManager (javax.jcr.query.QueryManager)103 Query (javax.jcr.query.Query)69 Node (javax.jcr.Node)61 QueryResult (javax.jcr.query.QueryResult)55 Session (javax.jcr.Session)54 NodeIterator (javax.jcr.NodeIterator)34 RowIterator (javax.jcr.query.RowIterator)27 Test (org.junit.Test)27 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)22 RepositoryException (javax.jcr.RepositoryException)18 Row (javax.jcr.query.Row)14 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)14 FacetResult (org.apache.jackrabbit.oak.query.facet.FacetResult)9 ValueFactory (javax.jcr.ValueFactory)7 NoSuchElementException (java.util.NoSuchElementException)6 InvalidItemStateException (javax.jcr.InvalidItemStateException)3 Value (javax.jcr.Value)3 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2