Search in sources :

Example 1 with XQueryPool

use of org.exist.storage.XQueryPool in project exist by eXist-db.

the class RESTServer method executeXQuery.

/**
 * Directly execute an XQuery stored as a binary document in the database.
 *
 * @throws PermissionDeniedException
 */
private void executeXQuery(final DBBroker broker, final Txn transaction, final DocumentImpl resource, final HttpServletRequest request, final HttpServletResponse response, final Properties outputProperties, final String servletPath, final String pathInfo) throws XPathException, BadRequestException, PermissionDeniedException {
    final Source source = new DBSource(broker, (BinaryDocument) resource, true);
    final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
    CompiledXQuery compiled = null;
    try {
        final XQuery xquery = broker.getBrokerPool().getXQueryService();
        compiled = pool.borrowCompiledXQuery(broker, source);
        XQueryContext context;
        if (compiled == null) {
            // special header to indicate that the query is not returned from
            // cache
            response.setHeader("X-XQuery-Cached", "false");
            context = new XQueryContext(broker.getBrokerPool());
        } else {
            response.setHeader("X-XQuery-Cached", "true");
            context = compiled.getContext();
            context.prepareForReuse();
        }
        // TODO: don't hardcode this?
        context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.append(resource.getCollection().getURI()).toString());
        context.setStaticallyKnownDocuments(new XmldbURI[] { resource.getCollection().getURI() });
        final HttpRequestWrapper reqw = declareVariables(context, null, request, response);
        reqw.setServletPath(servletPath);
        reqw.setPathInfo(pathInfo);
        final long compilationTime;
        if (compiled == null) {
            try {
                final long compilationStart = System.currentTimeMillis();
                compiled = xquery.compile(context, source);
                compilationTime = System.currentTimeMillis() - compilationStart;
            } catch (final IOException e) {
                throw new BadRequestException("Failed to read query from " + resource.getURI(), e);
            }
        } else {
            compilationTime = 0;
        }
        DebuggeeFactory.checkForDebugRequest(request, context);
        boolean wrap = outputProperties.getProperty("_wrap") != null && "yes".equals(outputProperties.getProperty("_wrap"));
        try {
            final long executeStart = System.currentTimeMillis();
            final Sequence result = xquery.execute(broker, compiled, null, outputProperties);
            writeResults(response, broker, transaction, result, -1, 1, false, outputProperties, wrap, compilationTime, System.currentTimeMillis() - executeStart);
        } finally {
            context.runCleanupTasks();
        }
    } finally {
        if (compiled != null) {
            pool.returnCompiledXQuery(source, compiled);
        }
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) HttpRequestWrapper(org.exist.http.servlets.HttpRequestWrapper) DBSource(org.exist.source.DBSource) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) InputSource(org.xml.sax.InputSource) URLSource(org.exist.source.URLSource)

Example 2 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 3 with XQueryPool

use of org.exist.storage.XQueryPool 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);
        }
    });
}
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) Sequence(org.exist.xquery.value.Sequence) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) FileSource(org.exist.source.FileSource)

Example 4 with XQueryPool

use of org.exist.storage.XQueryPool in project exist by eXist-db.

the class Conditional method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    LOG.debug("Processing xupdate:if ...");
    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.setBackwardsCompatibility(true);
    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 seq = null;
    try {
        seq = xquery.execute(broker, compiled, null);
    } finally {
        context.runCleanupTasks();
        pool.returnCompiledXQuery(source, compiled);
    }
    if (seq.effectiveBooleanValue()) {
        long mods = 0;
        for (final Modification modification : modifications) {
            mods += modification.process(transaction);
            broker.flush();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} modifications processed.", mods);
        }
        return mods;
    } else {
        return 0;
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) 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 5 with XQueryPool

use of org.exist.storage.XQueryPool in project exist by eXist-db.

the class RedirectorServlet method executeQuery.

private Sequence executeQuery(final Source source, final RequestWrapper request, final ResponseWrapper response) throws EXistException, XPathException, PermissionDeniedException, IOException {
    final XQuery xquery = getPool().getXQueryService();
    final XQueryPool pool = getPool().getXQueryPool();
    try (final DBBroker broker = getPool().getBroker()) {
        final XQueryContext context;
        CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
        if (compiled == null) {
            // special header to indicate that the query is not returned from
            // cache
            response.setHeader("X-XQuery-Cached", "false");
            context = new XQueryContext(getPool());
            context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.toString());
            compiled = xquery.compile(context, source);
        } else {
            response.setHeader("X-XQuery-Cached", "true");
            context = compiled.getContext();
            context.prepareForReuse();
        }
        try {
            return xquery.execute(broker, compiled, null, new Properties());
        } finally {
            context.runCleanupTasks();
            pool.returnCompiledXQuery(source, compiled);
        }
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext)

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