Search in sources :

Example 46 with CompiledXQuery

use of org.exist.xquery.CompiledXQuery in project exist by eXist-db.

the class AuditTrailSessionListener method executeXQuery.

private void executeXQuery(String xqueryResourcePath) {
    if (xqueryResourcePath != null && xqueryResourcePath.length() > 0) {
        xqueryResourcePath = xqueryResourcePath.trim();
        try {
            final BrokerPool pool = BrokerPool.getInstance();
            final Subject sysSubject = pool.getSecurityManager().getSystemSubject();
            try (final DBBroker broker = pool.get(Optional.of(sysSubject))) {
                if (broker == null) {
                    LOG.error("Unable to retrieve DBBroker for {}", sysSubject.getName());
                    return;
                }
                final XmldbURI pathUri = XmldbURI.create(xqueryResourcePath);
                try (final LockedDocument lockedResource = broker.getXMLResource(pathUri, LockMode.READ_LOCK)) {
                    final Source source;
                    if (lockedResource != null) {
                        if (LOG.isTraceEnabled()) {
                            LOG.trace("Resource [{}] exists.", xqueryResourcePath);
                        }
                        source = new DBSource(broker, (BinaryDocument) lockedResource.getDocument(), true);
                    } else {
                        LOG.error("Resource [{}] does not exist.", xqueryResourcePath);
                        return;
                    }
                    final XQuery xquery = pool.getXQueryService();
                    if (xquery == null) {
                        LOG.error("broker unable to retrieve XQueryService");
                        return;
                    }
                    final XQueryPool xqpool = pool.getXQueryPool();
                    CompiledXQuery compiled = xqpool.borrowCompiledXQuery(broker, source);
                    final XQueryContext context;
                    if (compiled == null) {
                        context = new XQueryContext(broker.getBrokerPool());
                    } else {
                        context = compiled.getContext();
                        context.prepareForReuse();
                    }
                    context.setStaticallyKnownDocuments(new XmldbURI[] { pathUri });
                    context.setBaseURI(new AnyURIValue(pathUri.toString()));
                    if (compiled == null) {
                        compiled = xquery.compile(context, source);
                    } else {
                        compiled.getContext().updateContext(context);
                        context.getWatchDog().reset();
                    }
                    final Properties outputProperties = new Properties();
                    try {
                        final long startTime = System.currentTimeMillis();
                        final Sequence result = xquery.execute(broker, compiled, null, outputProperties);
                        final long queryTime = System.currentTimeMillis() - startTime;
                        if (LOG.isTraceEnabled()) {
                            LOG.trace("XQuery execution results: {} in {}ms.", result.toString(), queryTime);
                        }
                    } finally {
                        context.runCleanupTasks();
                        xqpool.returnCompiledXQuery(source, compiled);
                    }
                }
            }
        } catch (final Exception e) {
            LOG.error("Exception while executing [{}] script", xqueryResourcePath, e);
        }
    }
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) Properties(java.util.Properties) Subject(org.exist.security.Subject) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) BinaryDocument(org.exist.dom.persistent.BinaryDocument) XQueryPool(org.exist.storage.XQueryPool) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) DBSource(org.exist.source.DBSource) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Example 47 with CompiledXQuery

use of org.exist.xquery.CompiledXQuery in project exist by eXist-db.

the class RestXqServiceCompiledXQueryCacheImpl method getCompiledQuery.

@Override
public CompiledXQuery getCompiledQuery(final DBBroker broker, final URI xqueryLocation) throws RestXqServiceException {
    final Queue<CompiledXQuery> queue = cache.get(xqueryLocation, key -> new MpmcAtomicArrayQueue<>(DEFAULT_MAX_QUERY_STACK_SIZE));
    CompiledXQuery xquery = queue.poll();
    if (xquery == null) {
        xquery = XQueryCompiler.compile(broker, xqueryLocation);
    } else {
        // prepare the context for re-use
        try {
            xquery.getContext().prepareForReuse();
        } catch (final XPathException e) {
            throw new RestXqServiceException("Unable to prepare compiled XQuery for reuse", e);
        }
    }
    xquery.getContext().prepareForExecution();
    return xquery;
}
Also used : RestXqServiceException(org.exquery.restxq.RestXqServiceException) XPathException(org.exist.xquery.XPathException) CompiledXQuery(org.exist.xquery.CompiledXQuery)

Example 48 with CompiledXQuery

use of org.exist.xquery.CompiledXQuery in project exist by eXist-db.

the class RestXqServiceRegistryPersistence method loadRegistry.

private void loadRegistry(final Path fRegistry) {
    log.info("Loading RESTXQ registry from: {}", fRegistry.toAbsolutePath().toString());
    try (final LineNumberReader reader = new LineNumberReader(Files.newBufferedReader(fRegistry));
        final DBBroker broker = getBrokerPool().getBroker()) {
        // read version line first
        String line = reader.readLine();
        final String versionStr = line.substring(line.indexOf(VERSION_LABEL) + VERSION_LABEL.length() + LABEL_SEP.length());
        if (REGISTRY_FILE_VERSION != Integer.parseInt(versionStr)) {
            log.error("Unable to load RESTXQ registry file: {}. Expected version: " + REGISTRY_FILE_VERSION + " but saw version: {}", fRegistry.toAbsolutePath().toString(), versionStr);
        } else {
            while ((line = reader.readLine()) != null) {
                final String xqueryLocation = line.substring(0, line.indexOf(FIELD_SEP));
                final CompiledXQuery xquery = XQueryCompiler.compile(broker, new URI(xqueryLocation));
                final List<RestXqService> services = XQueryInspector.findServices(xquery);
                getRegistry().register(services);
            }
        }
    } catch (final ExQueryException | IOException | EXistException | URISyntaxException eqe) {
        log.error(eqe.getMessage(), eqe);
    }
    log.info("RESTXQ registry loaded.");
}
Also used : RestXqService(org.exquery.restxq.RestXqService) DBBroker(org.exist.storage.DBBroker) ExQueryException(org.exquery.ExQueryException) CompiledXQuery(org.exist.xquery.CompiledXQuery) IOException(java.io.IOException) EXistException(org.exist.EXistException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) LineNumberReader(java.io.LineNumberReader)

Aggregations

CompiledXQuery (org.exist.xquery.CompiledXQuery)48 XQuery (org.exist.xquery.XQuery)39 XQueryContext (org.exist.xquery.XQueryContext)35 BrokerPool (org.exist.storage.BrokerPool)23 DBBroker (org.exist.storage.DBBroker)23 Sequence (org.exist.xquery.value.Sequence)21 XQueryPool (org.exist.storage.XQueryPool)14 XPathException (org.exist.xquery.XPathException)9 IOException (java.io.IOException)8 Source (org.exist.source.Source)8 URI (java.net.URI)7 EXistException (org.exist.EXistException)7 AnyURIValue (org.exist.xquery.value.AnyURIValue)7 Test (org.junit.Test)7 Properties (java.util.Properties)6 InputStreamReader (java.io.InputStreamReader)5 PermissionDeniedException (org.exist.security.PermissionDeniedException)5 InputSource (org.xml.sax.InputSource)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Source (javax.xml.transform.Source)4