use of org.exist.storage.XQueryPool in project exist by eXist-db.
the class XQueryURLRewrite method runQuery.
private Sequence runQuery(final DBBroker broker, final RequestWrapper request, final HttpServletResponse response, final ModelAndView model, final URLRewrite staticRewrite, final Properties outputProperties) throws ServletException, XPathException, PermissionDeniedException {
// Try to find the XQuery
final SourceInfo sourceInfo = getSourceInfo(broker, request, staticRewrite);
if (sourceInfo == null) {
// no controller found
return Sequence.EMPTY_SEQUENCE;
}
final String basePath = staticRewrite == null ? "." : staticRewrite.getTarget();
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryPool xqyPool = broker.getBrokerPool().getXQueryPool();
CompiledXQuery compiled = null;
if (compiledCache) {
compiled = xqyPool.borrowCompiledXQuery(broker, sourceInfo.source);
}
final XQueryContext queryContext;
if (compiled == null) {
queryContext = new XQueryContext(broker.getBrokerPool());
} else {
queryContext = compiled.getContext();
queryContext.prepareForReuse();
}
// Find correct module load path
queryContext.setModuleLoadPath(sourceInfo.moduleLoadPath);
declareVariables(queryContext, sourceInfo, staticRewrite, basePath, request, response);
if (compiled == null) {
try {
compiled = xquery.compile(queryContext, sourceInfo.source);
} catch (final IOException e) {
throw new ServletException("Failed to read query from " + query, e);
}
}
model.setSourceInfo(sourceInfo);
try {
return xquery.execute(broker, compiled, null, outputProperties);
} finally {
queryContext.runCleanupTasks();
xqyPool.returnCompiledXQuery(sourceInfo.source, compiled);
}
}
use of org.exist.storage.XQueryPool 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();
}
use of org.exist.storage.XQueryPool in project exist by eXist-db.
the class Util method withCompiledQuery.
public 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.storage.XQueryPool 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.storage.XQueryPool 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);
}
}
}
Aggregations