use of org.exist.xquery.XQuery in project exist by eXist-db.
the class RedirectorServlet method executeQuery.
private Sequence executeQuery(final Source source, final RequestWrapper request, final ResponseWrapper response) throws EXistException, XPathException, PermissionDeniedException, IOException {
final XQuery xquery = getPool().getXQueryService();
final XQueryPool pool = getPool().getXQueryPool();
try (final DBBroker broker = getPool().getBroker()) {
final XQueryContext context;
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
if (compiled == null) {
// special header to indicate that the query is not returned from
// cache
response.setHeader("X-XQuery-Cached", "false");
context = new XQueryContext(getPool());
context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.toString());
compiled = xquery.compile(context, source);
} else {
response.setHeader("X-XQuery-Cached", "true");
context = compiled.getContext();
context.prepareForReuse();
}
try {
return xquery.execute(broker, compiled, null, new Properties());
} finally {
context.runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
}
}
use of org.exist.xquery.XQuery 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.xquery.XQuery in project exist by eXist-db.
the class XQueryCompilationTest method executeQuery.
protected static Either<XPathException, Sequence> executeQuery(final String string) throws EXistException, PermissionDeniedException {
final BrokerPool pool = server.getBrokerPool();
final XQuery xqueryService = pool.getXQueryService();
try (final DBBroker broker = pool.getBroker()) {
try {
return Right(xqueryService.execute(broker, string, null));
} catch (final XPathException e) {
return Left(e);
}
}
}
use of org.exist.xquery.XQuery 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.xquery.XQuery in project exist by eXist-db.
the class ShutdownTest method storeAndShutdown.
public void storeAndShutdown() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, XPathException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
Collection test;
try (final Txn transaction = transact.beginTransaction()) {
test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
assertNotNull(test);
broker.saveCollection(transaction, test);
// store some documents.
for (final String sampleName : SAMPLES.getShakespeareXmlSampleNames()) {
broker.storeDocument(transaction, XmldbURI.create(sampleName), new InputStreamSupplierInputSource(() -> SAMPLES.getShakespeareSample(sampleName)), MimeType.XML_TYPE, test);
}
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
final Sequence result = xquery.execute(broker, "//SPEECH[contains(LINE, 'love')]", null);
assertNotNull(result);
assertEquals(187, result.getItemCount());
transact.commit(transaction);
}
try (final Txn transaction = transact.beginTransaction()) {
broker.removeCollection(transaction, test);
transact.commit(transaction);
}
}
}
Aggregations