Search in sources :

Example 91 with QueryManager

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

the class QueryPlanTest method traversalVersusPropertyIndex.

@Test
public // OAK-1898
void traversalVersusPropertyIndex() throws Exception {
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Node testRootNode = session.getRootNode().addNode("testroot");
    Node n = testRootNode;
    for (int i = 0; i < 20; i++) {
        n.setProperty("depth", i + 2);
        n = n.addNode("n", "oak:Unstructured");
        n.addMixin("mix:referenceable");
    }
    session.save();
    String xpath = "/jcr:root/testroot/n/n/n/n/n/n/n//*[jcr:uuid]";
    Query q;
    QueryResult result;
    RowIterator it;
    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 not use the index on "jcr:uuid"
    assertEquals("[nt:base] as [a] /* property uuid IS NOT NULL " + "where ([a].[jcr:uuid] is not null) and " + "(isdescendantnode([a], [/testroot/n/n/n/n/n/n/n])) */", 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)

Example 92 with QueryManager

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

the class QueryPlanTest method correctPropertyIndexUsage.

@Test
public // OAK-1898
void correctPropertyIndexUsage() throws Exception {
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Node testRootNode = session.getRootNode().addNode("testroot");
    createPropertyIndex(session, "fiftyPercent");
    createPropertyIndex(session, "tenPercent");
    createPropertyIndex(session, "hundredPercent");
    for (int i = 0; i < 300; i++) {
        Node n = testRootNode.addNode("n" + i, "oak:Unstructured");
        if (i % 10 == 0) {
            n.setProperty("tenPercent", i);
        }
        if (i % 2 == 0) {
            n.setProperty("fiftyPercent", i);
        }
        n.setProperty("hundredPercent", i);
    }
    session.save();
    String xpath = "/jcr:root//*[@tenPercent and @fiftyPercent and @hundredPercent]";
    Query q;
    QueryResult result;
    RowIterator it;
    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 not use the index on "jcr:uuid"
    assertEquals("[nt:base] as [a] /* property tenPercent IS NOT NULL " + "where ([a].[tenPercent] is not null) " + "and ([a].[fiftyPercent] is not null) " + "and ([a].[hundredPercent] 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)

Example 93 with QueryManager

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

the class QueryTest method limit.

@Test
public void limit() throws RepositoryException {
    Session session = getAdminSession();
    Node hello1 = session.getRootNode().addNode("hello1");
    hello1.setProperty("id", "1");
    hello1.setProperty("data", "x");
    session.save();
    Node hello3 = session.getRootNode().addNode("hello3");
    hello3.setProperty("id", "3");
    hello3.setProperty("data", "z");
    session.save();
    Node hello2 = session.getRootNode().addNode("hello2");
    hello2.setProperty("id", "2");
    hello2.setProperty("data", "y");
    session.save();
    ValueFactory vf = session.getValueFactory();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery("select id from [nt:base] where data >= $data order by id", Query.JCR_SQL2);
    q.bindValue("data", vf.createValue("x"));
    for (int limit = 0; limit < 5; limit++) {
        q.setLimit(limit);
        for (int offset = 0; offset < 3; offset++) {
            q.setOffset(offset);
            QueryResult r = q.execute();
            RowIterator it = r.getRows();
            int l = Math.min(Math.max(0, 3 - offset), limit);
            assertEquals(l, r.getRows().getSize());
            assertEquals(l, r.getNodes().getSize());
            Row row;
            for (int x = offset + 1, i = 0; i < limit && x < 4; i++, x++) {
                assertTrue(it.hasNext());
                row = it.nextRow();
                assertEquals("" + x, row.getValue("id").getString());
            }
            assertFalse(it.hasNext());
        }
    }
}
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) ValueFactory(javax.jcr.ValueFactory) Row(javax.jcr.query.Row) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 94 with QueryManager

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

the class QueryTest method simple.

@SuppressWarnings("deprecation")
@Test
public void simple() throws RepositoryException {
    Session session = getAdminSession();
    Node hello = session.getRootNode().addNode("hello");
    hello.setProperty("id", "1");
    hello.setProperty("text", "hello_world");
    session.save();
    Node hello2 = session.getRootNode().addNode("hello2");
    hello2.setProperty("id", "2");
    hello2.setProperty("text", "hello world");
    session.save();
    ValueFactory vf = session.getValueFactory();
    QueryManager qm = session.getWorkspace().getQueryManager();
    // SQL-2
    Query q = qm.createQuery("select text from [nt:base] where id = $id", Query.JCR_SQL2);
    q.bindValue("id", vf.createValue("1"));
    QueryResult r = q.execute();
    RowIterator it = r.getRows();
    assertTrue(it.hasNext());
    Row row = it.nextRow();
    assertEquals("hello_world", row.getValue("text").getString());
    String[] columns = r.getColumnNames();
    assertEquals(1, columns.length);
    assertEquals("text", columns[0]);
    assertFalse(it.hasNext());
    r = q.execute();
    NodeIterator nodeIt = r.getNodes();
    assertTrue(nodeIt.hasNext());
    Node n = nodeIt.nextNode();
    assertEquals("hello_world", n.getProperty("text").getString());
    assertFalse(it.hasNext());
    // SQL
    q = qm.createQuery("select text from [nt:base] where text like 'hello\\_world' escape '\\'", Query.SQL);
    r = q.execute();
    columns = r.getColumnNames();
    assertEquals(3, columns.length);
    assertEquals("text", columns[0]);
    assertEquals("jcr:path", columns[1]);
    assertEquals("jcr:score", columns[2]);
    nodeIt = r.getNodes();
    assertTrue(nodeIt.hasNext());
    n = nodeIt.nextNode();
    assertEquals("hello_world", n.getProperty("text").getString());
    assertFalse(nodeIt.hasNext());
    // XPath
    q = qm.createQuery("//*[@id=1]", Query.XPATH);
    r = q.execute();
    assertEquals(newHashSet("jcr:path", "jcr:score", "jcr:primaryType"), newHashSet(r.getColumnNames()));
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) ValueFactory(javax.jcr.ValueFactory) Row(javax.jcr.query.Row) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 95 with QueryManager

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

the class RemappingTest method testQuery4.

public void testQuery4() throws Exception {
    String statement = "/jcr:root/myRep:security/myRep:authorizables//" + "element(*,myRep:Authorizable)[@my:property='value']";
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery(statement, "xpath");
    q.getBindVariableNames();
    QueryResult qr = q.execute();
    NodeIterator ni = qr.getNodes();
    while (ni.hasNext()) {
        ni.next();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) QueryManager(javax.jcr.query.QueryManager)

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