use of org.exist.xquery.XQuery in project exist by eXist-db.
the class AbstractFieldConfig method doBuild.
protected void doBuild(DBBroker broker, DocumentImpl document, NodeId nodeId, Document luceneDoc, CharSequence text) throws PermissionDeniedException, XPathException {
if (!expression.isPresent()) {
processText(text, luceneDoc);
return;
}
compile(broker);
if (!isValid) {
return;
}
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final NodeProxy currentNode = new NodeProxy(document, nodeId);
try {
Sequence result = xquery.execute(broker, compiled, currentNode);
if (!result.isEmpty()) {
processResult(result, luceneDoc);
}
} catch (PermissionDeniedException | XPathException e) {
isValid = false;
throw e;
} finally {
compiled.reset();
compiled.getContext().reset();
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class AbstractFieldConfig method compile.
protected CompiledXQuery compile(DBBroker broker, String code) {
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
try {
return xquery.compile(context, code);
} catch (XPathException | PermissionDeniedException e) {
LOG.error("Failed to compile expression: {}: {}", code, e.getMessage(), e);
isValid = false;
return null;
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class Util method withCompiledQuery.
static <T> T withCompiledQuery(final DBBroker broker, final Source source, final Function2E<CompiledXQuery, T, XPathException, PermissionDeniedException> op) throws XPathException, PermissionDeniedException, IOException {
final BrokerPool pool = broker.getBrokerPool();
final XQuery xqueryService = pool.getXQueryService();
final XQueryPool xqueryPool = pool.getXQueryPool();
final CompiledXQuery compiledQuery = compileQuery(broker, xqueryService, xqueryPool, source);
try {
return op.apply(compiledQuery);
} finally {
if (compiledQuery != null) {
xqueryPool.returnCompiledXQuery(source, compiledQuery);
}
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class LocalXPathQueryService method execute.
private ResourceSet execute(final LocalXmldbFunction<Source> sourceOp) throws XMLDBException {
return withDb((broker, transaction) -> {
final long start = System.currentTimeMillis();
final Source source = sourceOp.apply(broker, transaction);
final XmldbURI[] docs = new XmldbURI[] { XmldbURI.create(collection.getName(broker, transaction)) };
final XQuery xquery = brokerPool.getXQueryService();
final XQueryPool pool = brokerPool.getXQueryPool();
XQueryContext context;
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
context.setStaticallyKnownDocuments(docs);
if (variableDecls.containsKey(Debuggee.PREFIX + ":session")) {
context.declareVariable(Debuggee.SESSION, variableDecls.get(Debuggee.PREFIX + ":session"));
variableDecls.remove(Debuggee.PREFIX + ":session");
}
setupContext(source, context);
if (compiled == null) {
compiled = xquery.compile(context, source);
}
try {
final Sequence result = xquery.execute(broker, compiled, null, properties);
if (LOG.isDebugEnabled()) {
LOG.debug("query took {} ms.", System.currentTimeMillis() - start);
}
final Properties resourceSetProperties = new Properties(properties);
resourceSetProperties.setProperty(EXistOutputKeys.XDM_SERIALIZATION, "yes");
return result != null ? new LocalResourceSet(user, brokerPool, collection, resourceSetProperties, result, null) : null;
} finally {
compiled.getContext().runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
});
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class LocalXPathQueryService method compileAndCheck.
private Either<XPathException, CompiledExpression> compileAndCheck(final DBBroker broker, final Txn transaction, final String query) throws XMLDBException {
final long start = System.currentTimeMillis();
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
try {
setupContext(null, context);
final CompiledExpression expr = xquery.compile(context, query);
if (LOG.isDebugEnabled()) {
LOG.debug("compilation took {}", System.currentTimeMillis() - start);
}
return Either.Right(expr);
} catch (final PermissionDeniedException e) {
throw new XMLDBException(ErrorCodes.PERMISSION_DENIED, e.getMessage(), e);
} catch (final IllegalArgumentException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
} catch (final XPathException e) {
return Either.Left(e);
}
}
Aggregations