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