Search in sources :

Example 71 with RowIterator

use of javax.jcr.query.RowIterator 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)

Example 72 with RowIterator

use of javax.jcr.query.RowIterator 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 73 with RowIterator

use of javax.jcr.query.RowIterator 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 74 with RowIterator

use of javax.jcr.query.RowIterator 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 75 with RowIterator

use of javax.jcr.query.RowIterator 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)

Aggregations

RowIterator (javax.jcr.query.RowIterator)86 Node (javax.jcr.Node)48 QueryResult (javax.jcr.query.QueryResult)45 QueryManager (javax.jcr.query.QueryManager)27 Row (javax.jcr.query.Row)27 Query (javax.jcr.query.Query)25 Test (org.junit.Test)20 Session (javax.jcr.Session)17 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)15 RepositoryException (javax.jcr.RepositoryException)12 Value (javax.jcr.Value)11 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)8 ValueFactory (javax.jcr.ValueFactory)7 ArrayList (java.util.ArrayList)5 NodeIterator (javax.jcr.NodeIterator)5 NoSuchElementException (java.util.NoSuchElementException)4 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)4 RowIteratorAdapter (org.apache.jackrabbit.commons.iterator.RowIteratorAdapter)4 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3