Search in sources :

Example 16 with CompiledXQuery

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

the class ExistXqueryRegistry method findServices.

public List<RestXqService> findServices(final DBBroker broker, final DocumentImpl document) throws ExQueryException {
    try {
        final CompiledXQuery compiled = XQueryCompiler.compile(broker, document);
        /*
             * examine the compiled query, record all modules and modules of modules.
             * Keep a dependencies list so that we can act on it if a module is deleted.
             */
        final Map<String, Set<String>> queryDependenciesTree = XQueryInspector.getDependencies(compiled);
        recordQueryDependenciesTree(queryDependenciesTree);
        /*
             * A compiled query may be a missing dependency for another query
             * so reexamine queries with missing dependencies
             */
        reexamineModulesWithResolvedDependencies(broker, document.getURI().toString());
        /*
             * remove any potentially re-compiled query from the
             * invalid queries list
             */
        removeInvalidQuery(document.getURI());
        return XQueryInspector.findServices(compiled);
    } catch (final RestXqServiceCompilationException e) {
        // if there was a missing dependency then record it
        final MissingModuleHint missingModuleHint = extractMissingModuleHint(e);
        if (missingModuleHint != null) {
            if (missingModuleHint.dependantModule == null) {
                recordMissingDependency(missingModuleHint.moduleHint, document.getURI());
            } else {
                // avoids wrong missing dependency dependant being recorded for a complex module tree
                try {
                    recordMissingDependency(missingModuleHint.dependantModule, document.getURI());
                    recordMissingDependency(missingModuleHint.moduleHint, XmldbURI.xmldbUriFor(missingModuleHint.dependantModule));
                } catch (final URISyntaxException use) {
                    recordInvalidQuery(document.getURI());
                    LOG.error("XQuery '{}' could not be compiled! {}", document.getURI(), e.getMessage());
                }
            }
        } else {
            recordInvalidQuery(document.getURI());
            LOG.error("XQuery '{}' could not be compiled! {}", document.getURI(), e.getMessage());
        }
    /*
             * This may be the recompilation of a query
             * so we should unregister any of its missing
             * services. Luckily this is taken care of in
             * the before{EVENT} trigger functions
             */
    }
    return new ArrayList<>();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) CompiledXQuery(org.exist.xquery.CompiledXQuery) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException)

Example 17 with CompiledXQuery

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

the class Util method compileQuery.

static CompiledXQuery compileQuery(final DBBroker broker, final XQuery xqueryService, final XQueryPool xqueryPool, final Source query) throws PermissionDeniedException, XPathException, IOException {
    CompiledXQuery compiled = xqueryPool.borrowCompiledXQuery(broker, query);
    XQueryContext context;
    if (compiled == null) {
        context = new XQueryContext(broker.getBrokerPool());
    } else {
        context = compiled.getContext();
        context.prepareForReuse();
    }
    if (compiled == null) {
        compiled = xqueryService.compile(context, query);
    } else {
        compiled.getContext().updateContext(context);
        context.getWatchDog().reset();
    }
    return compiled;
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext)

Example 18 with CompiledXQuery

use of org.exist.xquery.CompiledXQuery 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 19 with CompiledXQuery

use of org.exist.xquery.CompiledXQuery 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 20 with CompiledXQuery

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

the class DocTest method docAvailable_dynamicallyAvailableDocument_relativeUri.

@Test
public void docAvailable_dynamicallyAvailableDocument_relativeUri() throws XPathException, EXistException, PermissionDeniedException, URISyntaxException {
    final BrokerPool pool = BrokerPool.getInstance();
    final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
    final String baseUri = "http://from-dynamic-context/";
    final String docRelativeUri = "doc1";
    final String query = "fn:doc-available('" + docRelativeUri + "')";
    try (final DBBroker broker = pool.getBroker()) {
        final XQueryContext context = new XQueryContext(pool);
        context.setBaseURI(new AnyURIValue(new URI(baseUri)));
        context.addDynamicallyAvailableDocument(baseUri + docRelativeUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
        final XQuery xqueryService = pool.getXQueryService();
        final CompiledXQuery compiled = xqueryService.compile(context, query);
        final Sequence result = xqueryService.execute(broker, compiled, null);
        assertFalse(result.isEmpty());
        assertEquals(1, result.getItemCount());
        assertTrue(result.itemAt(0).toJavaObject(Boolean.class).booleanValue());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) URI(java.net.URI) BrokerPool(org.exist.storage.BrokerPool)

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