Search in sources :

Example 6 with RowIterator

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

the class QueryTest method skip.

@Test
public void skip() throws RepositoryException {
    Session session = getAdminSession();
    Node hello1 = session.getRootNode().addNode("hello1");
    hello1.setProperty("id", "1");
    hello1.setProperty("data", "x");
    session.save();
    Node hello3 = hello1.addNode("hello3");
    hello3.setProperty("id", "3");
    hello3.setProperty("data", "z");
    session.save();
    Node hello2 = hello3.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 i = -1; i < 5; i++) {
        QueryResult r = q.execute();
        RowIterator it = r.getRows();
        assertEquals(3, r.getRows().getSize());
        assertEquals(3, r.getNodes().getSize());
        Row row;
        try {
            it.skip(i);
            assertTrue(i >= 0 && i <= 3);
        } catch (IllegalArgumentException e) {
            assertEquals(-1, i);
        } catch (NoSuchElementException e) {
            assertTrue(i >= 2);
        }
        if (i <= 0) {
            assertTrue(it.hasNext());
            row = it.nextRow();
            assertEquals("1", row.getValue("id").getString());
        }
        if (i <= 1) {
            assertTrue(it.hasNext());
            row = it.nextRow();
            assertEquals("2", row.getValue("id").getString());
        }
        if (i <= 2) {
            assertTrue(it.hasNext());
            row = it.nextRow();
            assertEquals("3", 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) NoSuchElementException(java.util.NoSuchElementException) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 7 with RowIterator

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

the class QueryFulltextTest method excerpt.

@Test
public void excerpt() throws Exception {
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Node testRootNode = session.getRootNode().addNode("testroot");
    Node n1 = testRootNode.addNode("node1");
    n1.setProperty("text", "hello world");
    n1.setProperty("desc", "description");
    Node n2 = testRootNode.addNode("node2");
    n2.setProperty("text", "Hello World");
    n2.setProperty("desc", "Description");
    session.save();
    Query q;
    RowIterator it;
    Row row;
    String s;
    String xpath = "//*[jcr:contains(., 'hello')]/rep:excerpt(.) order by @jcr:path";
    q = qm.createQuery(xpath, "xpath");
    it = q.execute().getRows();
    row = it.nextRow();
    String path = row.getPath();
    s = row.getValue("rep:excerpt(.)").getString();
    assertTrue(path + ":" + s + " (1)", s.indexOf("<strong>hello</strong> world") >= 0);
    assertTrue(path + ":" + s + " (2)", s.indexOf("description") >= 0);
    row = it.nextRow();
    path = row.getPath();
    s = row.getValue("rep:excerpt(.)").getString();
    // TODO is this expected?
    assertTrue(path + ":" + s + " (3)", s.indexOf("Hello World") >= 0);
    assertTrue(path + ":" + s + " (4)", s.indexOf("Description") >= 0);
    xpath = "//*[jcr:contains(., 'hello')]/rep:excerpt(.) order by @jcr:path";
    q = qm.createQuery(xpath, "xpath");
    it = q.execute().getRows();
    row = it.nextRow();
    path = row.getPath();
    s = row.getValue("rep:excerpt(text)").getString();
    assertTrue(path + ":" + s + " (5)", s.indexOf("<strong>hello</strong> world") >= 0);
    assertTrue(path + ":" + s + " (6)", s.indexOf("description") < 0);
    row = it.nextRow();
    path = row.getPath();
    s = row.getValue("rep:excerpt(text)").getString();
    // TODO is this expected?
    assertTrue(path + ":" + s + " (7)", s.indexOf("Hello World") >= 0);
    assertTrue(path + ":" + s + " (8)", s.indexOf("Description") < 0);
}
Also used : Query(javax.jcr.query.Query) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Row(javax.jcr.query.Row) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 8 with RowIterator

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

the class QueryTest method propertyIndexWithDeclaringNodeTypeAndRelativQuery.

@Test
public void propertyIndexWithDeclaringNodeTypeAndRelativQuery() throws RepositoryException {
    Session session = getAdminSession();
    RowIterator rit;
    QueryResult r;
    String query;
    query = "//element(*, rep:Authorizable)[@rep:principalName = 'admin']";
    r = session.getWorkspace().getQueryManager().createQuery("explain " + query, "xpath").execute();
    rit = r.getRows();
    assertEquals("[rep:Authorizable] as [a] /* property principalName = admin " + "where [a].[rep:principalName] = 'admin' */", rit.nextRow().getValue("plan").getString());
    query = "//element(*, rep:Authorizable)[admin/@rep:principalName = 'admin']";
    r = session.getWorkspace().getQueryManager().createQuery("explain " + query, "xpath").execute();
    rit = r.getRows();
    assertEquals("[rep:Authorizable] as [a] /* nodeType " + "Filter(query=explain select [jcr:path], [jcr:score], * " + "from [rep:Authorizable] as a " + "where [admin/rep:principalName] = 'admin' " + "/* xpath: //element(*, rep:Authorizable)[" + "admin/@rep:principalName = 'admin'] */, path=*, " + "property=[admin/rep:principalName=[admin]]) " + "where [a].[admin/rep:principalName] = 'admin' */", rit.nextRow().getValue("plan").getString());
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 9 with RowIterator

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

the class QueryPlanTest method uuidIndex.

@Test
public void uuidIndex() throws Exception {
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Node testRootNode = session.getRootNode().addNode("testroot");
    Node n = testRootNode.addNode("node");
    n.addMixin("mix:referenceable");
    session.save();
    // this matches just one node (exact path),
    // so it should use the TraversintIndex
    String xpath = "/jcr:root/testroot/node[@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();
    assertEquals("[nt:base] as [a] /* traverse \"/testroot/node\" where " + "([a].[jcr:uuid] is not null) " + "and (issamenode([a], [/testroot/node])) */", plan);
    // verify the result
    q = qm.createQuery(xpath, "xpath");
    result = q.execute();
    it = result.getRows();
    assertTrue(it.hasNext());
    String path = it.nextRow().getPath();
    assertEquals("/testroot/node", path);
    assertFalse(it.hasNext());
    // this potentially matches many nodes,
    // so it should use the index on the UUID property
    xpath = "/jcr:root/testroot/*[@jcr:uuid]";
    q = qm.createQuery("explain " + xpath, "xpath");
    result = q.execute();
    it = result.getRows();
    assertTrue(it.hasNext());
    plan = it.nextRow().getValue("plan").getString();
    assertEquals("[nt:base] as [a] /* property uuid IS NOT NULL " + "where ([a].[jcr:uuid] is not null) " + "and (ischildnode([a], [/testroot])) */", 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 10 with RowIterator

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

the class QueryPlanTest method nodeType.

@Test
public void nodeType() throws Exception {
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Node nodetype = session.getRootNode().getNode("oak:index").getNode("nodetype");
    nodetype.setProperty("entryCount", 10000000);
    Node testRootNode = session.getRootNode().addNode("testroot");
    Node n1 = testRootNode.addNode("node1");
    Node n2 = n1.addNode("node2");
    n2.addNode("node3");
    session.save();
    String sql2 = "select [jcr:path] as [path] from [nt:base] " + "where [node2/node3/jcr:primaryType] is not null " + "and isdescendantnode('/testroot')";
    Query q;
    QueryResult result;
    RowIterator it;
    q = qm.createQuery("explain " + sql2, Query.JCR_SQL2);
    result = q.execute();
    it = result.getRows();
    assertTrue(it.hasNext());
    String plan = it.nextRow().getValue("plan").getString();
    // should not use the index on "jcr:primaryType"
    assertEquals("[nt:base] as [nt:base] /* traverse \"/testroot//*\" " + "where ([nt:base].[node2/node3/jcr:primaryType] is not null) " + "and (isdescendantnode([nt:base], [/testroot])) " + "*/", plan);
    // verify the result
    q = qm.createQuery(sql2, Query.JCR_SQL2);
    result = q.execute();
    it = result.getRows();
    assertTrue(it.hasNext());
    String path = it.nextRow().getValue("path").getString();
    assertEquals("/testroot/node1", path);
}
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

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