use of javax.jcr.query.Query in project jackrabbit-oak by apache.
the class DescendantSearchTest method runTest.
@Override
public void runTest() throws Exception {
QueryManager manager = session.getWorkspace().getQueryManager();
for (int i = 0; i < NODE_COUNT; i++) {
Query query = createQuery(manager, i);
NodeIterator iterator = query.execute().getNodes();
while (iterator.hasNext()) {
Node node = iterator.nextNode();
if (node.getProperty("testcount").getLong() != i) {
throw new Exception("Invalid test result: " + node.getPath());
}
}
}
}
use of javax.jcr.query.Query in project jackrabbit by apache.
the class XPathQueryEvaluator method eval.
public Iterator<Authorizable> eval() throws RepositoryException {
xPath.append("//element(*,").append(getNtName(builder.getSelector())).append(')');
Value bound = builder.getBound();
long offset = builder.getOffset();
if (bound != null && offset > 0) {
log.warn("Found bound {} and offset {} in limit. Discarding offset.", bound, offset);
offset = 0;
}
Condition condition = builder.getCondition();
String sortCol = builder.getSortProperty();
Direction sortDir = builder.getSortDirection();
if (bound != null) {
if (sortCol == null) {
log.warn("Ignoring bound {} since no sort order is specified");
} else {
Condition boundCondition = builder.property(sortCol, getCollation(sortDir), bound);
condition = condition == null ? boundCondition : builder.and(condition, boundCondition);
}
}
if (condition != null) {
xPath.append('[');
condition.accept(this);
xPath.append(']');
}
if (sortCol != null) {
boolean ignoreCase = builder.getSortIgnoreCase();
xPath.append(" order by ").append(ignoreCase ? "" : "fn:lower-case(").append(sortCol).append(ignoreCase ? " " : ") ").append(sortDir.getDirection());
}
QueryManager queryManager = session.getWorkspace().getQueryManager();
Query query = queryManager.createQuery(xPath.toString(), Query.XPATH);
long maxCount = builder.getMaxCount();
if (maxCount == 0) {
return Iterators.empty();
}
// here (inefficient!) otherwise we can apply the limit in the query
if (builder.getGroupName() == null) {
if (offset > 0) {
query.setOffset(offset);
}
if (maxCount > 0) {
query.setLimit(maxCount);
}
return toAuthorizables(execute(query));
} else {
Iterator<Authorizable> result = toAuthorizables(execute(query));
Iterator<Authorizable> filtered = filter(result, builder.getGroupName(), builder.isDeclaredMembersOnly());
return BoundedIterator.create(offset, maxCount, filtered);
}
}
use of javax.jcr.query.Query in project jackrabbit by apache.
the class ConcurrentQueriesWithUpdatesTest method testQueriesWithUpdates.
public void testQueriesWithUpdates() throws Exception {
final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>());
final AtomicBoolean running = new AtomicBoolean(true);
// track executed queries and do updates at most at the given rate
final BlockingQueue<Object> queryExecuted = new LinkedBlockingQueue<Object>();
Thread queries = new Thread(new Runnable() {
public void run() {
try {
runTask(new Task() {
public void execute(Session session, Node test) throws RepositoryException {
QueryManager qm = session.getWorkspace().getQueryManager();
while (running.get()) {
Query q = qm.createQuery(testPath + "//element(*, nt:unstructured) order by @jcr:score descending", Query.XPATH);
NodeIterator nodes = q.execute().getNodes();
assertEquals("wrong result set size", numNodes, nodes.getSize());
queryExecuted.offer(new Object());
}
}
}, 5, testRootNode.getPath());
} catch (RepositoryException e) {
exceptions.add(e);
}
}
});
queries.start();
Thread update = new Thread(new Runnable() {
public void run() {
try {
runTask(new Task() {
public void execute(Session session, Node test) throws RepositoryException {
Random rand = new Random();
QueryManager qm = session.getWorkspace().getQueryManager();
for (int i = 0; i < NUM_UPDATES; i++) {
try {
// wait at most 10 seconds
queryExecuted.poll(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// ignore
}
Query q = qm.createQuery(testPath + "//node" + rand.nextInt(numNodes) + " order by @jcr:score descending", Query.XPATH);
NodeIterator nodes = q.execute().getNodes();
if (nodes.hasNext()) {
Node n = nodes.nextNode();
n.setProperty("foo", "bar");
session.save();
}
}
}
}, 1, testRootNode.getPath());
} catch (RepositoryException e) {
exceptions.add(e);
}
}
});
update.start();
update.join();
running.set(false);
queries.join();
}
use of javax.jcr.query.Query in project jackrabbit by apache.
the class XPathJcrPathTest method testJcrPath.
/**
* Verify that the jcr:path is present in the query result.
*/
public void testJcrPath() throws RepositoryException, NotExecutableException {
String nodeTypeName = session.getRootNode().getPrimaryNodeType().getName();
String queryStatement = "//element(*, " + nodeTypeName + ")";
// execute the search query
Query query = session.getWorkspace().getQueryManager().createQuery(queryStatement, qsXPATH);
QueryResult result = query.execute();
assertTrue("jcr:path must be present in query result row", Arrays.asList(result.getColumnNames()).contains(jcrPath));
}
use of javax.jcr.query.Query in project jackrabbit by apache.
the class FullTextSearchScoreTest method testOrdering.
public void testOrdering() throws RepositoryException {
QueryObjectModel qom = qf.createQuery(qf.selector(testNodeType, "s"), qf.and(qf.fullTextSearch("s", null, qf.literal(vf.createValue("fox"))), qf.descendantNode("s", testRootNode.getPath())), new Ordering[] { qf.ascending(qf.fullTextSearchScore("s")) }, null);
forQOMandSQL2(qom, new Callable() {
public Object call(Query query) throws RepositoryException {
RowIterator rows = query.execute().getRows();
double previousScore = Double.NaN;
while (rows.hasNext()) {
double score = rows.nextRow().getScore("s");
if (!Double.isNaN(previousScore)) {
assertTrue("wrong order", previousScore <= score);
}
previousScore = score;
}
return null;
}
});
}
Aggregations