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