use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class CollectionTest method doc_dynamicallyAvailableCollection_absoluteUri.
@Test
public void doc_dynamicallyAvailableCollection_absoluteUri() throws XPathException, EXistException, PermissionDeniedException {
final BrokerPool pool = BrokerPool.getInstance();
final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
final String collectionUri = "http://from-dynamic-context/collection1";
final String query = "fn:collection('" + collectionUri + "')";
try (final DBBroker broker = pool.getBroker()) {
final XQueryContext context = new XQueryContext(pool);
context.addDynamicallyAvailableCollection(collectionUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
final XQuery xqueryService = pool.getXQueryService();
final CompiledXQuery compiled = xqueryService.compile(context, query);
final Sequence result = xqueryService.execute(broker, compiled, null);
assertFalse(result.isEmpty());
assertEquals(1, result.getItemCount());
assertTrue(result.itemAt(0) instanceof Node);
final Source expectedSource = Input.fromString(doc).build();
final Source actualSource = Input.fromNode((Node) result.itemAt(0)).build();
final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource).checkForIdentical().checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class CollectionTest method doc_dynamicallyAvailableCollection_relativeUri.
@Test
public void doc_dynamicallyAvailableCollection_relativeUri() throws XPathException, EXistException, PermissionDeniedException, URISyntaxException {
final BrokerPool pool = BrokerPool.getInstance();
final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
final String baseUri = "http://from-dynamic-context/";
final String collectionRelativeUri = "collection1";
final String query = "fn:collection('" + collectionRelativeUri + "')";
try (final DBBroker broker = pool.getBroker()) {
final XQueryContext context = new XQueryContext(pool);
context.setBaseURI(new AnyURIValue(new URI(baseUri)));
context.addDynamicallyAvailableCollection(baseUri + collectionRelativeUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
final XQuery xqueryService = pool.getXQueryService();
final CompiledXQuery compiled = xqueryService.compile(context, query);
final Sequence result = xqueryService.execute(broker, compiled, null);
assertFalse(result.isEmpty());
assertEquals(1, result.getItemCount());
assertTrue(result.itemAt(0) instanceof Node);
final Source expectedSource = Input.fromString(doc).build();
final Source actualSource = Input.fromNode((Node) result.itemAt(0)).build();
final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource).checkForIdentical().checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class CustomIndexTest method checkIndex.
// TODO : could be replaced by an XQuery call to index-keys(). See above
private void checkIndex(DBBroker broker, DocumentSet docs, String term, int count) {
NGramIndexWorker index = (NGramIndexWorker) broker.getIndexController().getWorkerByIndexId(NGramIndex.ID);
XQueryContext context = new XQueryContext(broker.getBrokerPool());
Occurrences[] occurrences = index.scanIndex(context, docs, null, null);
int found = 0;
for (int i = 0; i < occurrences.length; i++) {
Occurrences occurrence = occurrences[i];
if (occurrence.getTerm().compareTo(term) == 0)
found++;
}
assertEquals(count, found);
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class LuceneIndexTest method checkIndex.
/**
* It really depends on the Analyzer used with the index,
* but probably you would like to have the 'term' argument all lower cased.
* @see <a href='https://sourceforge.net/p/exist/mailman/message/36436727/'>Help needed with a test case</a>
*/
private Occurrences[] checkIndex(final DocumentSet docs, final DBBroker broker, final QName[] qn, final String term, final int expected) {
final LuceneIndexWorker index = (LuceneIndexWorker) broker.getIndexController().getWorkerByIndexId(LuceneIndex.ID);
final Map<String, Object> hints = new HashMap<>();
if (term != null) {
hints.put(OrderedValuesIndex.START_VALUE, term);
}
if (qn != null && qn.length > 0) {
final List<QName> qnlist = new ArrayList<>(qn.length);
qnlist.addAll(Arrays.asList(qn));
hints.put(QNamedKeysIndex.QNAMES_KEY, qnlist);
}
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
final Occurrences[] occur = index.scanIndex(context, docs, null, hints);
assertEquals(expected, occur.length);
return occur;
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class Modification method select.
/**
* Evaluate the select expression.
*
* @param docs the documents to evaludate the expression over
*
* @return The selected nodes.
*
* @throws PermissionDeniedException if the caller has insufficient priviledges
* @throws EXistException if the database raises an error
* @throws XPathException if the XPath raises an error
*/
protected NodeList select(DocumentSet docs) throws PermissionDeniedException, EXistException, XPathException {
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
final Source source = new StringSource(selectStmt);
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
XQueryContext context;
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
context.setStaticallyKnownDocuments(docs);
declareNamespaces(context);
declareVariables(context);
if (compiled == null)
try {
compiled = xquery.compile(context, source);
} catch (final IOException e) {
throw new EXistException("An exception occurred while compiling the query: " + e.getMessage());
}
Sequence resultSeq = null;
try {
resultSeq = xquery.execute(broker, compiled, null);
} finally {
context.runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
if (!(resultSeq.isEmpty() || Type.subTypeOf(resultSeq.getItemType(), Type.NODE))) {
throw new EXistException("select expression should evaluate to a node-set; got " + Type.getTypeName(resultSeq.getItemType()));
}
if (LOG.isDebugEnabled()) {
LOG.debug("found {} for select: {}", resultSeq.getItemCount(), selectStmt);
}
return resultSeq.toNodeSet();
}
Aggregations