Search in sources :

Example 91 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class CustomIndexTest method xupdateRename.

@Test
public void xupdateRename() throws EXistException, LockException, XPathException, PermissionDeniedException, SAXException, IOException, ParserConfigurationException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        checkIndex(broker, docs, "cha", 1);
        checkIndex(broker, docs, "le8", 1);
        XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
        assertNotNull(proc);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        String xupdate = XUPDATE_START + "   <xu:rename select=\"//item[@id='2']\">renamed</xu:rename>" + XUPDATE_END;
        Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(broker, docs, "tab", 0);
        transact.commit(transaction);
    }
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) Modification(org.exist.xupdate.Modification) DBBroker(org.exist.storage.DBBroker) InputSource(org.xml.sax.InputSource) TransactionManager(org.exist.storage.txn.TransactionManager) XQuery(org.exist.xquery.XQuery) StringReader(java.io.StringReader) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 92 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class CustomIndexTest method indexKeys.

@Test
public void indexKeys() throws SAXException, PermissionDeniedException, XPathException, EXistException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "util:index-key-occurrences(/test/item, 'cha', 'ngram-index')", null);
        // Sequence seq = xquery.execute("util:index-key-occurrences(/test/item, 'cha', 'org.exist.indexing.impl.NGramIndex')", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "util:index-key-occurrences(/test/item, 'le8', 'ngram-index')", null);
        // seq = xquery.execute("util:index-key-occurrences(/test/item, 'le8', 'org.exist.indexing.impl.NGramIndex')", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "util:index-key-documents(/test/item, 'le8', 'ngram-index')", null);
        // seq = xquery.execute("util:index-key-documents(/test/item, 'le8', 'org.exist.indexing.impl.NGramIndex')", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "util:index-key-documents(/test/item, 'le8', 'ngram-index')", null);
        // seq = xquery.execute("util:index-key-doucments(/test/item, 'le8', 'org.exist.indexing.impl.NGramIndex')", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        String queryBody = "declare function local:callback($key as item(), $data as xs:int+)\n" + "as element()+ {\n" + "    <item>\n" + "        <key>{$key}</key>\n" + "        <frequency>{$data[1]}</frequency>\n" + "    </item>\n" + "};\n" + "\n";
        String query = queryBody + "util:index-keys(/test/item, \'\', util:function(xs:QName(\'local:callback\'), 2), 1000, 'ngram-index')";
        // String query = queryBody + "util:index-keys(/test/item, \'\', util:function(xs:QName(\'local:callback\'), 2), 1000, 'org.exist.indexing.impl.NGramIndex')";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        // TODO : check cardinality
        StringWriter out = new StringWriter();
        Properties props = new Properties();
        props.setProperty(OutputKeys.INDENT, "yes");
        SAXSerializer serializer = new SAXSerializer(out, props);
        serializer.startDocument();
        for (SequenceIterator i = seq.iterate(); i.hasNext(); ) {
            Item next = i.nextItem();
            next.toSAX(broker, serializer, props);
        }
        serializer.endDocument();
    // TODO : check content
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) StringWriter(java.io.StringWriter) SequenceIterator(org.exist.xquery.value.SequenceIterator) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) Properties(java.util.Properties) SAXSerializer(org.exist.util.serializer.SAXSerializer) BrokerPool(org.exist.storage.BrokerPool)

Example 93 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class CustomIndexTest method dropIndex.

@Test
public void dropIndex() throws EXistException, PermissionDeniedException, XPathException, LockException, TriggerException, IOException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        checkIndex(broker, docs, "cha", 1);
        checkIndex(broker, docs, "le8", 1);
        try (final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
            assertNotNull(root);
            root.removeXMLResource(transaction, broker, XmldbURI.create("test_string.xml"));
        }
        checkIndex(broker, docs, "cha", 0);
        seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        transact.commit(transaction);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) XQuery(org.exist.xquery.XQuery) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 94 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class LuceneMatchListenerTest method indexByQName.

/**
 * Test match highlighting for index configured by QName, e.g.
 * &lt;create qname="a"/&gt;.
 */
@Test
public void indexByQName() throws EXistException, PermissionDeniedException, XPathException, SAXException, CollectionConfigurationException, LockException, IOException {
    configureAndStore(CONF2, XML);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//para[ft:query(., 'mixed')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        String result = queryResult2String(broker, seq);
        XMLAssert.assertEquals("<para>some paragraph with <hi>" + MATCH_START + "mixed" + MATCH_END + "</hi> content.</para>", result);
        seq = xquery.execute(broker, "//para[ft:query(., '+nested +inner +elements')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq);
        XMLAssert.assertEquals("<para>another paragraph with <note><hi>" + MATCH_START + "nested" + MATCH_END + "</hi> " + MATCH_START + "inner" + MATCH_END + "</note> " + MATCH_START + "elements" + MATCH_END + ".</para>", result);
        seq = xquery.execute(broker, "//para[ft:query(term, 'term')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq);
        XMLAssert.assertEquals("<para>a third paragraph with <term>" + MATCH_START + "term" + MATCH_END + "</term>.</para>", result);
        seq = xquery.execute(broker, "//para[ft:query(., '+double +match')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq);
        XMLAssert.assertEquals("<para>" + MATCH_START + "double" + MATCH_END + " " + MATCH_START + "match" + MATCH_END + " " + MATCH_START + "double" + MATCH_END + " " + MATCH_START + "match" + MATCH_END + "</para>", result);
        seq = xquery.execute(broker, "for $para in //para[ft:query(., '+double +match')] return\n" + "   <hit>{$para}</hit>", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq);
        XMLAssert.assertEquals("<hit><para>" + MATCH_START + "double" + MATCH_END + " " + MATCH_START + "match" + MATCH_END + " " + MATCH_START + "double" + MATCH_END + " " + MATCH_START + "match" + MATCH_END + "</para></hit>", result);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 95 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class Modification method select.

/**
 * Evaluate the select expression.
 *
 * @param docs the documents to evaludate the expression over
 *
 * @return The selected nodes.
 *
 * @throws PermissionDeniedException if the caller has insufficient priviledges
 * @throws EXistException if the database raises an error
 * @throws XPathException if the XPath raises an error
 */
protected NodeList select(DocumentSet docs) throws PermissionDeniedException, EXistException, XPathException {
    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.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 resultSeq = null;
    try {
        resultSeq = xquery.execute(broker, compiled, null);
    } finally {
        context.runCleanupTasks();
        pool.returnCompiledXQuery(source, compiled);
    }
    if (!(resultSeq.isEmpty() || Type.subTypeOf(resultSeq.getItemType(), Type.NODE))) {
        throw new EXistException("select expression should evaluate to a node-set; got " + Type.getTypeName(resultSeq.getItemType()));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("found {} for select: {}", resultSeq.getItemCount(), selectStmt);
    }
    return resultSeq.toNodeSet();
}
Also used : XQueryPool(org.exist.storage.XQueryPool) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) StringSource(org.exist.source.StringSource) IOException(java.io.IOException) EXistException(org.exist.EXistException) Sequence(org.exist.xquery.value.Sequence) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source)

Aggregations

XQuery (org.exist.xquery.XQuery)135 Sequence (org.exist.xquery.value.Sequence)108 DBBroker (org.exist.storage.DBBroker)107 BrokerPool (org.exist.storage.BrokerPool)105 CompiledXQuery (org.exist.xquery.CompiledXQuery)59 Test (org.junit.Test)36 XQueryContext (org.exist.xquery.XQueryContext)33 Txn (org.exist.storage.txn.Txn)32 XPathException (org.exist.xquery.XPathException)21 TransactionManager (org.exist.storage.txn.TransactionManager)17 Item (org.exist.xquery.value.Item)16 InputSource (org.xml.sax.InputSource)16 XQueryPool (org.exist.storage.XQueryPool)15 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)12 DocumentSet (org.exist.dom.persistent.DocumentSet)12 StringReader (java.io.StringReader)11 Properties (java.util.Properties)11 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)11 Modification (org.exist.xupdate.Modification)11 XUpdateProcessor (org.exist.xupdate.XUpdateProcessor)11