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<>();
}
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;
}
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);
}
};
}
}
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);
}
}
}
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());
}
}
Aggregations