use of org.exist.storage.XQueryPool in project exist by eXist-db.
the class RESTServer method executeXQuery.
/**
* Directly execute an XQuery stored as a binary document in the database.
*
* @throws PermissionDeniedException
*/
private void executeXQuery(final DBBroker broker, final Txn transaction, final DocumentImpl resource, final HttpServletRequest request, final HttpServletResponse response, final Properties outputProperties, final String servletPath, final String pathInfo) throws XPathException, BadRequestException, PermissionDeniedException {
final Source source = new DBSource(broker, (BinaryDocument) resource, true);
final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
CompiledXQuery compiled = null;
try {
final XQuery xquery = broker.getBrokerPool().getXQueryService();
compiled = pool.borrowCompiledXQuery(broker, source);
XQueryContext context;
if (compiled == null) {
// special header to indicate that the query is not returned from
// cache
response.setHeader("X-XQuery-Cached", "false");
context = new XQueryContext(broker.getBrokerPool());
} else {
response.setHeader("X-XQuery-Cached", "true");
context = compiled.getContext();
context.prepareForReuse();
}
// TODO: don't hardcode this?
context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.append(resource.getCollection().getURI()).toString());
context.setStaticallyKnownDocuments(new XmldbURI[] { resource.getCollection().getURI() });
final HttpRequestWrapper reqw = declareVariables(context, null, request, response);
reqw.setServletPath(servletPath);
reqw.setPathInfo(pathInfo);
final long compilationTime;
if (compiled == null) {
try {
final long compilationStart = System.currentTimeMillis();
compiled = xquery.compile(context, source);
compilationTime = System.currentTimeMillis() - compilationStart;
} catch (final IOException e) {
throw new BadRequestException("Failed to read query from " + resource.getURI(), e);
}
} else {
compilationTime = 0;
}
DebuggeeFactory.checkForDebugRequest(request, context);
boolean wrap = outputProperties.getProperty("_wrap") != null && "yes".equals(outputProperties.getProperty("_wrap"));
try {
final long executeStart = System.currentTimeMillis();
final Sequence result = xquery.execute(broker, compiled, null, outputProperties);
writeResults(response, broker, transaction, result, -1, 1, false, outputProperties, wrap, compilationTime, System.currentTimeMillis() - executeStart);
} finally {
context.runCleanupTasks();
}
} finally {
if (compiled != null) {
pool.returnCompiledXQuery(source, compiled);
}
}
}
use of org.exist.storage.XQueryPool in project exist by eXist-db.
the class Util method withCompiledQuery.
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);
}
}
}
use of org.exist.storage.XQueryPool in project exist by eXist-db.
the class LocalXPathQueryService method execute.
private ResourceSet execute(final LocalXmldbFunction<Source> sourceOp) throws XMLDBException {
return withDb((broker, transaction) -> {
final long start = System.currentTimeMillis();
final Source source = sourceOp.apply(broker, transaction);
final XmldbURI[] docs = new XmldbURI[] { XmldbURI.create(collection.getName(broker, transaction)) };
final XQuery xquery = brokerPool.getXQueryService();
final XQueryPool pool = brokerPool.getXQueryPool();
XQueryContext context;
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
context.setStaticallyKnownDocuments(docs);
if (variableDecls.containsKey(Debuggee.PREFIX + ":session")) {
context.declareVariable(Debuggee.SESSION, variableDecls.get(Debuggee.PREFIX + ":session"));
variableDecls.remove(Debuggee.PREFIX + ":session");
}
setupContext(source, context);
if (compiled == null) {
compiled = xquery.compile(context, source);
}
try {
final Sequence result = xquery.execute(broker, compiled, null, properties);
if (LOG.isDebugEnabled()) {
LOG.debug("query took {} ms.", System.currentTimeMillis() - start);
}
final Properties resourceSetProperties = new Properties(properties);
resourceSetProperties.setProperty(EXistOutputKeys.XDM_SERIALIZATION, "yes");
return result != null ? new LocalResourceSet(user, brokerPool, collection, resourceSetProperties, result, null) : null;
} finally {
compiled.getContext().runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
});
}
use of org.exist.storage.XQueryPool in project exist by eXist-db.
the class Conditional method process.
@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
LOG.debug("Processing xupdate:if ...");
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
final Source source = new StringSource(selectStmt);
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
XQueryContext context;
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
// context.setBackwardsCompatibility(true);
context.setStaticallyKnownDocuments(docs);
declareNamespaces(context);
declareVariables(context);
if (compiled == null)
try {
compiled = xquery.compile(context, source);
} catch (final IOException e) {
throw new EXistException("An exception occurred while compiling the query: " + e.getMessage());
}
Sequence seq = null;
try {
seq = xquery.execute(broker, compiled, null);
} finally {
context.runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
if (seq.effectiveBooleanValue()) {
long mods = 0;
for (final Modification modification : modifications) {
mods += modification.process(transaction);
broker.flush();
}
if (LOG.isDebugEnabled()) {
LOG.debug("{} modifications processed.", mods);
}
return mods;
} else {
return 0;
}
}
use of org.exist.storage.XQueryPool 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);
}
}
}
Aggregations