Search in sources :

Example 6 with XQueryPool

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

the class SanityReport method ping.

@Override
public long ping(boolean checkQueryEngine) {
    final long start = System.currentTimeMillis();
    lastPingRespTime = -1;
    lastActionInfo = "Ping";
    taskstatus.setStatus(TaskStatus.Status.PING_WAIT);
    // this will block forever.
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getGuestSubject()))) {
        if (checkQueryEngine) {
            final XQuery xquery = pool.getXQueryService();
            final XQueryPool xqPool = pool.getXQueryPool();
            CompiledXQuery compiled = xqPool.borrowCompiledXQuery(broker, TEST_XQUERY);
            if (compiled == null) {
                final XQueryContext context = new XQueryContext(pool);
                compiled = xquery.compile(context, TEST_XQUERY);
            } else {
                compiled.getContext().prepareForReuse();
            }
            try {
                xquery.execute(broker, compiled, null);
            } finally {
                compiled.getContext().runCleanupTasks();
                xqPool.returnCompiledXQuery(TEST_XQUERY, compiled);
            }
        }
    } catch (final Exception e) {
        lastPingRespTime = -2;
        taskstatus.setStatus(TaskStatus.Status.PING_ERROR);
        taskstatus.setStatusChangeTime();
        taskstatus.setReason(e.getMessage());
        changeStatus(taskstatus);
    } finally {
        lastPingRespTime = System.currentTimeMillis() - start;
        taskstatus.setStatus(TaskStatus.Status.PING_OK);
        taskstatus.setStatusChangeTime();
        taskstatus.setReason("ping response time: " + lastPingRespTime);
        changeStatus(taskstatus);
    }
    return lastPingRespTime;
}
Also used : XQueryPool(org.exist.storage.XQueryPool) DBBroker(org.exist.storage.DBBroker) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) EXistException(org.exist.EXistException)

Example 7 with XQueryPool

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

the class AbstractTestRunner method executeQuery.

protected static Sequence executeQuery(final BrokerPool brokerPool, final Source query, final List<Function<XQueryContext, Tuple2<String, Object>>> externalVariableBindings) throws EXistException, PermissionDeniedException, XPathException, IOException, DatabaseConfigurationException {
    final SecurityManager securityManager = requireNonNull(brokerPool.getSecurityManager(), "securityManager is null");
    try (final DBBroker broker = brokerPool.get(Optional.of(securityManager.getSystemSubject()))) {
        final XQueryPool queryPool = brokerPool.getXQueryPool();
        CompiledXQuery compiledQuery = queryPool.borrowCompiledXQuery(broker, query);
        try {
            XQueryContext context;
            if (compiledQuery == null) {
                context = new XQueryContext(broker.getBrokerPool());
            } else {
                context = compiledQuery.getContext();
                context.prepareForReuse();
            }
            // setup misc. context
            context.setBaseURI(new AnyURIValue("/db"));
            if (query instanceof FileSource) {
                final Path queryPath = Paths.get(((FileSource) query).getPath().toAbsolutePath().toString());
                if (Files.isDirectory(queryPath)) {
                    context.setModuleLoadPath(queryPath.toString());
                } else {
                    context.setModuleLoadPath(queryPath.getParent().toString());
                }
            }
            // declare variables for the query
            for (final Function<XQueryContext, Tuple2<String, Object>> externalVariableBinding : externalVariableBindings) {
                final Tuple2<String, Object> nameValue = externalVariableBinding.apply(context);
                context.declareVariable(nameValue._1, nameValue._2);
            }
            final XQuery xqueryService = brokerPool.getXQueryService();
            // compile or update the context
            if (compiledQuery == null) {
                compiledQuery = xqueryService.compile(context, query);
            } else {
                compiledQuery.getContext().updateContext(context);
                context.getWatchDog().reset();
            }
            return xqueryService.execute(broker, compiledQuery, null);
        } finally {
            queryPool.returnCompiledXQuery(query, compiledQuery);
        }
    }
}
Also used : Path(java.nio.file.Path) SecurityManager(org.exist.security.SecurityManager) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) FileSource(org.exist.source.FileSource) XQueryContext(org.exist.xquery.XQueryContext) XQueryPool(org.exist.storage.XQueryPool) DBBroker(org.exist.storage.DBBroker) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2)

Example 8 with XQueryPool

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

the class EmbeddedBinariesTest method executeXQuery.

@Override
protected QueryResultAccessor<Sequence, IOException> executeXQuery(final String query) throws Exception {
    final Source source = new StringSource(query);
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    final XQueryPool pool = brokerPool.getXQueryPool();
    final XQuery xquery = brokerPool.getXQueryService();
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()))) {
        final CompiledXQuery existingCompiled = pool.borrowCompiledXQuery(broker, source);
        final XQueryContext context;
        final CompiledXQuery compiled;
        if (existingCompiled == null) {
            context = new XQueryContext(brokerPool);
            compiled = xquery.compile(context, source);
        } else {
            context = existingCompiled.getContext();
            context.prepareForReuse();
            compiled = existingCompiled;
        }
        final Sequence results = xquery.execute(broker, compiled, null);
        return consumer2E -> {
            try {
                // context.runCleanupTasks();  //TODO(AR) shows the ordering issue with binary values (see comment below)
                consumer2E.accept(results);
            } finally {
                // TODO(AR) performing #runCleanupTasks causes the stream to be closed, so if we do so before we are finished with the results, serialization fails.
                context.runCleanupTasks();
                pool.returnCompiledXQuery(source, compiled);
            }
        };
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock) StringInputSource(org.exist.util.StringInputSource) org.exist.xquery.value(org.exist.xquery.value) IOException(java.io.IOException) ExistEmbeddedServer(org.exist.test.ExistEmbeddedServer) MimeType(org.exist.util.MimeType) Source(org.exist.source.Source) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) DBBroker(org.exist.storage.DBBroker) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) Optional(java.util.Optional) XQueryPool(org.exist.storage.XQueryPool) Lock(org.exist.storage.lock.Lock) ClassRule(org.junit.ClassRule) XQueryContext(org.exist.xquery.XQueryContext) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) DBBroker(org.exist.storage.DBBroker) 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) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) BrokerPool(org.exist.storage.BrokerPool)

Example 9 with XQueryPool

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

the class UserXQueryJob method executeXQuery.

private void executeXQuery(final BrokerPool pool, final DBBroker broker, final Source source, final Properties params) throws PermissionDeniedException, XPathException, JobExecutionException {
    XQueryPool xqPool = null;
    CompiledXQuery compiled = null;
    XQueryContext context = null;
    try {
        // execute the xquery
        final XQuery xquery = pool.getXQueryService();
        xqPool = pool.getXQueryPool();
        // try and get a pre-compiled query from the pool
        compiled = xqPool.borrowCompiledXQuery(broker, source);
        if (compiled == null) {
            context = new XQueryContext(pool);
        } else {
            context = compiled.getContext();
            context.prepareForReuse();
        }
        if (source instanceof DBSource) {
            final XmldbURI collectionUri = ((DBSource) source).getDocumentPath().removeLastSegment();
            context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.append(collectionUri.getCollectionPath()).toString());
            context.setStaticallyKnownDocuments(new XmldbURI[] { collectionUri });
        }
        if (compiled == null) {
            try {
                compiled = xquery.compile(context, source);
            } catch (final IOException e) {
                abort("Failed to read query from " + xqueryResource);
            }
        }
        // declare any parameters as external variables
        if (params != null) {
            String bindingPrefix = params.getProperty("bindingPrefix");
            if (bindingPrefix == null) {
                bindingPrefix = "local";
            }
            for (final Entry param : params.entrySet()) {
                final String key = (String) param.getKey();
                final String value = (String) param.getValue();
                context.declareVariable(bindingPrefix + ":" + key, new StringValue(value));
            }
        }
        xquery.execute(broker, compiled, null);
    } finally {
        if (context != null) {
            context.runCleanupTasks();
        }
        // return the compiled query to the pool
        if (xqPool != null && source != null && compiled != null) {
            xqPool.returnCompiledXQuery(source, compiled);
        }
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) Entry(java.util.Map.Entry) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) DBSource(org.exist.source.DBSource) IOException(java.io.IOException) StringValue(org.exist.xquery.value.StringValue) XmldbURI(org.exist.xmldb.XmldbURI)

Example 10 with XQueryPool

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

the class XQueryContextAttributesTest method withCompiledQuery.

private 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) 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