Search in sources :

Example 56 with StringSource

use of org.exist.source.StringSource in project exist by eXist-db.

the class QueryPoolTest method differentQueries.

@Test
public void differentQueries() throws XMLDBException {
    EXistXQueryService service = (EXistXQueryService) testCollection.getService("XQueryService", "1.0");
    for (int i = 0; i < 1000; i++) {
        String query = "update insert <node id='id" + Integer.toHexString(i) + "'>" + "Some longer text <b>content</b> in this node. Some longer text <b>content</b> in this node. " + "Some longer text <b>content</b> in this node. Some longer text <b>content</b> in this node." + "</node> " + "into //test[@id = 't1']";
        service.execute(new StringSource(query));
    }
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) StringSource(org.exist.source.StringSource) Test(org.junit.Test)

Example 57 with StringSource

use of org.exist.source.StringSource in project exist by eXist-db.

the class InternalModuleTest method moduleVariables.

@Test
public void moduleVariables() throws XMLDBException {
    final Source querySource = new StringSource(getModuleVariableQuery("org.exist.xquery.InternalModuleTest$TestModuleWithVariables"));
    final EXistXQueryService queryService = (EXistXQueryService) existServer.getRoot().getService("XQueryService", "1.0");
    moduleVariablesQuery(queryService, querySource, COUNTER.get());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) StringSource(org.exist.source.StringSource) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) Test(org.junit.Test)

Example 58 with StringSource

use of org.exist.source.StringSource in project exist by eXist-db.

the class LowLevelTextTest method borrowCompiledXQueryNewStringSource.

/**
 * test with a new StringSource object having same content
 */
@Test
public void borrowCompiledXQueryNewStringSource() throws PermissionDeniedException {
    xqueryPool.returnCompiledXQuery(stringSource, preCompiledXQuery);
    StringSource localStringSource = new StringSource(TEST_XQUERY_SOURCE);
    callAndTestBorrowCompiledXQuery(localStringSource);
}
Also used : StringSource(org.exist.source.StringSource)

Example 59 with StringSource

use of org.exist.source.StringSource in project exist by eXist-db.

the class RESTServer method search.

/**
 * TODO: pass request and response objects to XQuery.
 *
 * @param broker the database broker
 * @param transaction the database transaction
 * @param query the XQuery
 * @param path the path of the request
 * @param namespaces any XQuery namespace bindings
 * @param variables any XQuery variable bindings
 * @param howmany the number of items in the results to return
 * @param start the start position in the results to return
 * @param typed whether the result nodes should be typed
 * @param outputProperties the serialization properties
 * @param wrap true to wrap the result of the XQuery in an exist:result
 * @param cache whether to cache the results
 * @param request the request
 * @param response the response
 *
 * @throws BadRequestException if a bad request is made
 * @throws PermissionDeniedException if the request has insufficient permissions
 * @throws XPathException if the XQuery raises an error
 */
protected void search(final DBBroker broker, final Txn transaction, final String query, final String path, final List<Namespace> namespaces, final ElementImpl variables, final int howmany, final int start, final boolean typed, final Properties outputProperties, final boolean wrap, final boolean cache, final HttpServletRequest request, final HttpServletResponse response) throws BadRequestException, PermissionDeniedException, XPathException {
    if (xquerySubmission == EXistServlet.FeatureEnabled.FALSE) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return;
    } else if (xquerySubmission == EXistServlet.FeatureEnabled.AUTHENTICATED_USERS_ONLY) {
        final Subject currentSubject = broker.getCurrentSubject();
        if (!currentSubject.isAuthenticated() || currentSubject.getId() == RealmImpl.GUEST_GROUP_ID) {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
    }
    final String sessionIdParam = outputProperties.getProperty(Serializer.PROPERTY_SESSION_ID);
    if (sessionIdParam != null) {
        try {
            final int sessionId = Integer.parseInt(sessionIdParam);
            if (sessionId > -1) {
                final Sequence cached = sessionManager.get(query, sessionId);
                if (cached != null) {
                    LOG.debug("Returning cached query result");
                    writeResults(response, broker, transaction, cached, howmany, start, typed, outputProperties, wrap, 0, 0);
                } else {
                    LOG.debug("Cached query result not found. Probably timed out. Repeating query.");
                }
            }
        } catch (final NumberFormatException e) {
            throw new BadRequestException("Invalid session id passed in query request: " + sessionIdParam);
        }
    }
    final XmldbURI pathUri = XmldbURI.createInternal(path);
    final Source source = new StringSource(query);
    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) {
            context = new XQueryContext(broker.getBrokerPool());
        } else {
            context = compiled.getContext();
            context.prepareForReuse();
        }
        context.setStaticallyKnownDocuments(new XmldbURI[] { pathUri });
        context.setBaseURI(new AnyURIValue(pathUri.toString()));
        declareNamespaces(context, namespaces);
        declareVariables(context, variables, request, response);
        final long compilationTime;
        if (compiled == null) {
            final long compilationStart = System.currentTimeMillis();
            compiled = xquery.compile(context, source);
            compilationTime = System.currentTimeMillis() - compilationStart;
        } else {
            compiled.getContext().updateContext(context);
            context.getWatchDog().reset();
            compilationTime = 0;
        }
        try {
            final long executeStart = System.currentTimeMillis();
            final Sequence resultSequence = xquery.execute(broker, compiled, null, outputProperties);
            final long executionTime = System.currentTimeMillis() - executeStart;
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found {} in {}ms.", resultSequence.getItemCount(), executionTime);
            }
            if (cache) {
                final int sessionId = sessionManager.add(query, resultSequence);
                outputProperties.setProperty(Serializer.PROPERTY_SESSION_ID, Integer.toString(sessionId));
                if (!response.isCommitted()) {
                    response.setIntHeader("X-Session-Id", sessionId);
                }
            }
            writeResults(response, broker, transaction, resultSequence, howmany, start, typed, outputProperties, wrap, compilationTime, executionTime);
        } finally {
            context.runCleanupTasks();
        }
    } catch (final IOException e) {
        throw new BadRequestException(e.getMessage(), e);
    } finally {
        if (compiled != null) {
            pool.returnCompiledXQuery(source, compiled);
        }
    }
}
Also used : Subject(org.exist.security.Subject) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) InputSource(org.xml.sax.InputSource) URLSource(org.exist.source.URLSource) XQueryPool(org.exist.storage.XQueryPool) StringSource(org.exist.source.StringSource) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

StringSource (org.exist.source.StringSource)59 Source (org.exist.source.Source)53 Txn (org.exist.storage.txn.Txn)41 BrokerPool (org.exist.storage.BrokerPool)40 DBBroker (org.exist.storage.DBBroker)40 Sequence (org.exist.xquery.value.Sequence)40 Test (org.junit.Test)37 StringInputSource (org.exist.util.StringInputSource)32 Collection (org.exist.collections.Collection)8 DBSource (org.exist.source.DBSource)8 IOException (java.io.IOException)7 Map (java.util.Map)7 EXistException (org.exist.EXistException)7 XmldbURI (org.exist.xmldb.XmldbURI)7 Element (org.w3c.dom.Element)7 InputSource (org.xml.sax.InputSource)7 Diff (org.xmlunit.diff.Diff)7 XQueryPool (org.exist.storage.XQueryPool)6 XQueryContext (org.exist.xquery.XQueryContext)6 IntegerValue (org.exist.xquery.value.IntegerValue)6