Search in sources :

Example 11 with XQueryPool

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);
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool)

Example 12 with XQueryPool

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();
}
Also used : XQueryPool(org.exist.storage.XQueryPool) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) StringSource(org.exist.source.StringSource) IOException(java.io.IOException) EXistException(org.exist.EXistException) Sequence(org.exist.xquery.value.Sequence) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source)

Example 13 with XQueryPool

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);
        }
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) BrokerPool(org.exist.storage.BrokerPool)

Example 14 with XQueryPool

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);
        }
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) BrokerPool(org.exist.storage.BrokerPool)

Example 15 with XQueryPool

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);
        }
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) BrokerPool(org.exist.storage.BrokerPool)

Aggregations

XQueryPool (org.exist.storage.XQueryPool)24 XQuery (org.exist.xquery.XQuery)15 CompiledXQuery (org.exist.xquery.CompiledXQuery)14 BrokerPool (org.exist.storage.BrokerPool)10 XQueryContext (org.exist.xquery.XQueryContext)10 Source (org.exist.source.Source)9 DBBroker (org.exist.storage.DBBroker)8 IOException (java.io.IOException)7 StringSource (org.exist.source.StringSource)7 DBSource (org.exist.source.DBSource)6 XmldbURI (org.exist.xmldb.XmldbURI)6 Sequence (org.exist.xquery.value.Sequence)6 EXistException (org.exist.EXistException)3 URLSource (org.exist.source.URLSource)3 InputSource (org.xml.sax.InputSource)3 Path (java.nio.file.Path)2 Optional (java.util.Optional)2 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)2 Collection (org.exist.collections.Collection)2 BinaryDocument (org.exist.dom.persistent.BinaryDocument)2