use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.
the class QueryTest method getPlan.
private static String getPlan(Session session, String xpath) throws RepositoryException {
QueryManager qm = session.getWorkspace().getQueryManager();
QueryResult qr = qm.createQuery("explain " + xpath, "xpath").execute();
Row r = qr.getRows().nextRow();
String plan = r.getValue("plan").getString();
return plan;
}
use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.
the class QueryTest method nodeTypeConstraint.
@SuppressWarnings("deprecation")
@Test
public void nodeTypeConstraint() throws Exception {
Session session = getAdminSession();
Node root = session.getRootNode();
Node folder1 = root.addNode("folder1", "nt:folder");
Node folder2 = root.addNode("folder2", "nt:folder");
JcrUtils.putFile(folder1, "file", "text/plain", new ByteArrayInputStream("foo bar".getBytes("UTF-8")));
folder2.addNode("folder3", "nt:folder");
session.save();
QueryManager qm = session.getWorkspace().getQueryManager();
Query q = qm.createQuery("//element(*, nt:folder)", Query.XPATH);
Set<String> paths = new HashSet<String>();
for (RowIterator it = q.execute().getRows(); it.hasNext(); ) {
paths.add(it.nextRow().getPath());
}
assertEquals(new HashSet<String>(Arrays.asList("/folder1", "/folder2", "/folder2/folder3")), paths);
}
use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.
the class QueryTest method doubleQuote.
@Test
public void doubleQuote() throws RepositoryException {
Session session = getAdminSession();
Node hello = session.getRootNode().addNode("hello");
hello.setProperty("x", 1);
Node world = hello.addNode("world");
world.setProperty("x", 2);
session.save();
QueryManager qm = session.getWorkspace().getQueryManager();
Query q;
q = qm.createQuery("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE(s,[/hello])", Query.JCR_SQL2);
assertEquals("/hello/world", getPaths(q));
q = qm.createQuery("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE(s,\"/hello\")", Query.JCR_SQL2);
assertEquals("/hello/world", getPaths(q));
try {
q = qm.createQuery("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE(s,[\"/hello\"])", Query.JCR_SQL2);
getPaths(q);
fail();
} catch (InvalidQueryException e) {
// expected: absolute path
}
}
use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.
the class QueryTest method getCost.
private static double getCost(Session session, String xpath) throws RepositoryException {
QueryManager qm = session.getWorkspace().getQueryManager();
QueryResult qr = qm.createQuery("explain measure " + xpath, "xpath").execute();
Row r = qr.getRows().nextRow();
String plan = r.getValue("plan").getString();
String cost = plan.substring(plan.lastIndexOf('{'));
JsonObject json = parseJson(cost);
double c = Double.parseDouble(json.getProperties().get("a"));
return c;
}
use of javax.jcr.query.QueryManager 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);
}
Aggregations