Search in sources :

Example 16 with Session

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

the class QueryTest method approxCount.

@Test
public void approxCount() throws Exception {
    Session session = createAdminSession();
    session.getNode("/oak:index/counter").setProperty("resolution", 100);
    session.save();
    // but the counter index is not always up to date, so we need a loop
    for (int i = 0; i < 100; i++) {
        double c = getCost(session, "//*[@x=1]");
        if (c > 0 && c < 100000) {
            break;
        }
        // create a few nodes, in case there are not enough nodes
        // for the node counter index to be available
        Node testNode = session.getRootNode().addNode("test" + i);
        for (int j = 0; j < 100; j++) {
            testNode.addNode("n" + j);
        }
        session.save();
        // wait for async indexing (the node counter index is async)
        Thread.sleep(100);
    }
    double c = getCost(session, "//*[@x=1]");
    assertTrue("cost: " + c, c > 0 && c < 100000);
    // *without* the counter index, the estimated cost to traverse is high
    session.getNode("/oak:index/counter").remove();
    session.save();
    double c2 = getCost(session, "//*[@x=1]");
    assertTrue("cost: " + c2, c2 > 1000000);
    session.logout();
}
Also used : Node(javax.jcr.Node) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 17 with Session

use of javax.jcr.Session 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 18 with Session

use of javax.jcr.Session 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 19 with Session

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

the class SlowObservationIT method setup.

@Before
public void setup() throws RepositoryException {
    if (!isDocumentNodeStore()) {
        return;
    }
    Session session = getAdminSession();
    Node nodetypeIndex = session.getRootNode().getNode("oak:index").getNode("nodetype");
    nodetypeIndex.remove();
    Node testNode;
    testNode = session.getRootNode().addNode(TEST_NODE, "oak:Unstructured");
    testNode.setProperty("test", 0);
    testNode = session.getRootNode().addNode(TEST2_NODE, "oak:Unstructured");
    testNode.setProperty("test", 0);
    session.save();
}
Also used : Node(javax.jcr.Node) Session(javax.jcr.Session) Before(org.junit.Before)

Example 20 with Session

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

the class QueryTest method xpathEscapeTest.

@SuppressWarnings("deprecation")
@Test
public void xpathEscapeTest() throws RepositoryException {
    Session writer = createAdminSession();
    Session reader = createAdminSession();
    UserManager uMgr = ((JackrabbitSession) writer).getUserManager();
    String uid = "testUser";
    try {
        User user = uMgr.createUser("testUser", "pw");
        writer.getNode(user.getPath()).addNode(".tokens", "rep:Unstructured");
        writer.save();
        QueryManager qm = reader.getWorkspace().getQueryManager();
        Query q = qm.createQuery("/jcr:root//*[_x002e_tokens/@jcr:primaryType]", Query.XPATH);
        NodeIterator res = q.execute().getNodes();
        assertEquals(1, res.getSize());
    } finally {
        Authorizable a = uMgr.getAuthorizable(uid);
        if (a != null) {
            a.remove();
            writer.save();
        }
        if (reader != null) {
            reader.logout();
        }
        if (writer != null) {
            writer.logout();
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) User(org.apache.jackrabbit.api.security.user.User) Query(javax.jcr.query.Query) UserManager(org.apache.jackrabbit.api.security.user.UserManager) QueryManager(javax.jcr.query.QueryManager) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Aggregations

Session (javax.jcr.Session)1393 Node (javax.jcr.Node)761 Test (org.junit.Test)387 RepositoryException (javax.jcr.RepositoryException)304 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)162 Property (javax.jcr.Property)90 SimpleCredentials (javax.jcr.SimpleCredentials)88 Privilege (javax.jcr.security.Privilege)84 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)77 Value (javax.jcr.Value)71 Query (javax.jcr.query.Query)69 NodeIterator (javax.jcr.NodeIterator)68 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)65 QueryManager (javax.jcr.query.QueryManager)63 HashMap (java.util.HashMap)57 IOException (java.io.IOException)56 ArrayList (java.util.ArrayList)55 AccessControlManager (javax.jcr.security.AccessControlManager)55 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)52 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)49