Search in sources :

Example 76 with QueryManager

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

the class SearchScalabilityBenchmark method execute.

@Override
public void execute(Repository repository, Credentials credentials, ExecutionContext context) throws Exception {
    Session session = repository.login(credentials);
    QueryManager qm;
    try {
        qm = session.getWorkspace().getQueryManager();
        search(qm, context);
    } catch (RepositoryException e) {
        e.printStackTrace();
    }
}
Also used : QueryManager(javax.jcr.query.QueryManager) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Example 77 with QueryManager

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

the class CompatibilityIssuesTest method testSearchDescendentUsingXPath.

@Test
public void testSearchDescendentUsingXPath() throws Exception {
    Session adminSession = getAdminSession();
    String testNodePath = "/home/users/geometrixx-outdoors/emily.andrews@mailinator.com/social/relationships/following/aaron.mcdonald@mailinator.com";
    Node testNode = JcrUtils.getOrCreateByPath(testNodePath, null, adminSession);
    testNode.setProperty("id", "aaron.mcdonald@mailinator.com");
    AccessControlManager acMgr = adminSession.getAccessControlManager();
    JackrabbitAccessControlList tmpl = AccessControlUtils.getAccessControlList(acMgr, "/home/users/geometrixx-outdoors");
    ValueFactory vf = adminSession.getValueFactory();
    Map<String, Value> restrictions = new HashMap<String, Value>();
    restrictions.put("rep:glob", vf.createValue("*/social/relationships/following/*"));
    tmpl.addEntry(EveryonePrincipal.getInstance(), new Privilege[] { acMgr.privilegeFromName(Privilege.JCR_READ) }, true, restrictions);
    acMgr.setPolicy(tmpl.getPath(), tmpl);
    adminSession.save();
    Session anonymousSession = getRepository().login(new GuestCredentials());
    QueryManager qm = anonymousSession.getWorkspace().getQueryManager();
    Query q = qm.createQuery("/jcr:root/home//social/relationships/following//*[@id='aaron.mcdonald@mailinator.com']", Query.XPATH);
    QueryResult r = q.execute();
    RowIterator it = r.getRows();
    Assert.assertTrue(it.hasNext());
    anonymousSession.logout();
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) Query(javax.jcr.query.Query) HashMap(java.util.HashMap) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) JackrabbitAccessControlList(org.apache.jackrabbit.api.security.JackrabbitAccessControlList) QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) Value(javax.jcr.Value) QueryManager(javax.jcr.query.QueryManager) GuestCredentials(javax.jcr.GuestCredentials) Session(javax.jcr.Session) Test(org.junit.Test)

Example 78 with QueryManager

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

the class OperationLoggerTest method testQueryLogger.

public void testQueryLogger() throws Exception {
    Node node1 = testRootNode.addNode(nodeName1);
    //Log batch size is 100
    for (int i = 0; i < 200; i++) {
        node1.addNode("foo" + i, "oak:Unstructured").setProperty("foo", "bar");
    }
    superuser.save();
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    start();
    String stmt = "select * from [nt:base] where foo = 'bar'";
    Query q = qm.createQuery(stmt, Query.JCR_SQL2);
    QueryResult r = q.execute();
    Iterators.size(r.getRows());
    stop();
    boolean queryStmtLog = false;
    boolean queryIterationLog = false;
    for (ILoggingEvent e : logs.list) {
        if (OPS_QUERY.equals(e.getLoggerName())) {
            if (e.getMessage().contains("Executed query")) {
                assertEquals(stmt, e.getArgumentArray()[0]);
                assertTrue(e.getArgumentArray()[1] instanceof Number);
                queryStmtLog = true;
            }
            if (e.getMessage().contains("Iterated over")) {
                queryIterationLog = true;
            }
        }
    }
    assertTrue("Did not find query log", queryStmtLog);
    assertTrue("Did not find query iteration log", queryIterationLog);
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent)

Example 79 with QueryManager

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

the class QueryPlanTest method pathAndPropertyRestrictions.

@Test
@Ignore("OAK-1372")
public void pathAndPropertyRestrictions() throws Exception {
    // TODO work in progress
    ;
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Node testRootNode = session.getRootNode().addNode("testroot");
    Node b = testRootNode.addNode("b");
    Node c = b.addNode("c");
    Node d = c.addNode("d");
    Node e1 = d.addNode("e1");
    e1.setProperty("type", "1");
    Node e2 = d.addNode("e2");
    e2.setProperty("type", "2");
    Node e3 = d.addNode("e3");
    e3.setProperty("type", "3");
    session.save();
    String xpath = "/jcr:root/testroot//b/c/d/*[@jcr:uuid='1' or @jcr:uuid='2'] ";
    String sql2 = "select d.[jcr:path] as [jcr:path], d.[jcr:score] as [jcr:score], d.* " + "from [nt:base] as a inner join [nt:base] as b on ischildnode(b, a) " + "inner join [nt:base] as c on ischildnode(c, b) " + "inner join [nt:base] as d on ischildnode(d, c) " + "where name(a) = 'b' " + "and isdescendantnode(a, '/testroot') " + "and name(b) = 'c' " + "and name(c) = 'd' " + "and (d.[jcr:uuid] = '1' or d.[jcr:uuid] = '2')";
    sql2 = "select d.[jcr:path] as [jcr:path], d.[jcr:score] as [jcr:score], d.* " + "from [nt:base] as d " + "where (d.[jcr:uuid] = '1' or d.[jcr:uuid] = '2')";
    sql2 = "select d.[jcr:path] as [jcr:path], d.[jcr:score] as [jcr:score], d.* " + "from [nt:base] as d " + "inner join [nt:base] as c on ischildnode(d, c) " + "inner join [nt:base] as b on ischildnode(c, b) " + "inner join [nt:base] as a on ischildnode(b, a) " + "where name(a) = 'b' " + "and isdescendantnode(a, '/testroot') " + "and name(b) = 'c' " + "and name(c) = 'd' " + "and (d.[jcr:uuid] = '1' or d.[jcr:uuid] = '2')";
    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();
    assertEquals("", plan);
    // [nt:base] as [a] /* traverse "/testroot//*" 
    // where (name([a]) = cast('b' as string)) 
    // and (isdescendantnode([a], [/testroot])) */ 
    // inner join [nt:base] as [b] /* traverse 
    // "/path/from/the/join/selector/*" where name([b]) = cast('c' as string) */ 
    // on ischildnode([b], [a]) inner join [nt:base] as [c] 
    // /* traverse "/path/from/the/join/selector/*"
    // where name([c]) = cast('d' as string) */ on ischildnode([c], [b]) 
    // inner join [nt:base] as [d] /* traverse "/path/from/the/join/selector/*" 
    // where ([d].[type] is not null) and ([d].[type] in(cast('1' as string), cast('2' as string))) */ 
    // on ischildnode([d], [c])
    //        assertEquals("[nt:base] as [nt:base] /* traverse \"*\" " + 
    //                "where [nt:base].[node2/node3/jcr:primaryType] is not null */", 
    //                plan);
    // verify the result
    q = qm.createQuery(xpath, "xpath");
    result = q.execute();
    it = result.getRows();
    assertTrue(it.hasNext());
    String path = it.nextRow().getValue("path").getString();
    assertEquals("/testroot/b/c/d/e1", path);
    path = it.nextRow().getValue("path").getString();
    assertEquals("/testroot/b/c/d/e2", path);
    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) Session(javax.jcr.Session) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 80 with QueryManager

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

the class QueryPlanTest method propertyEqualsVersusPropertyNotNull.

@Test
public // OAK-1903
void propertyEqualsVersusPropertyNotNull() throws Exception {
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Node testRootNode = session.getRootNode().addNode("testroot");
    createPropertyIndex(session, "notNull");
    createPropertyIndex(session, "equals");
    for (int i = 0; i < 100; i++) {
        Node n = testRootNode.addNode("n" + i, "oak:Unstructured");
        if (i % 2 == 0) {
            n.setProperty("notNull", i);
        }
        n.setProperty("equals", 1);
    }
    session.save();
    String xpath = "/jcr:root//*[@notNull and @equals=1]";
    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 notNull IS NOT NULL " + "where ([a].[notNull] is not null) " + "and ([a].[equals] = 1) " + "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