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();
}
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());
}
}
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);
}
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();
}
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();
}
}
}
Aggregations