Search in sources :

Example 11 with Modification

use of org.exist.xupdate.Modification in project exist by eXist-db.

the class CustomIndexTest method xupdateInsert.

@Test
public void xupdateInsert() 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:append select=\"/test\">" + "       <item id='4'><description>Armchair</description><price>340</price></item>" + "   </xu:append>" + XUPDATE_END;
        Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(broker, docs, "arm", 1);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-before select=\"//item[@id = '1']\">" + "           <item id='0'><description>Wheelchair</description><price>1230</price></item>" + "       </xu:insert-before>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(broker, docs, "hee", 1);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-after select=\"//item[@id = '1']\">" + "           <item id='1.1'><description>refrigerator</description><price>777</price></item>" + "       </xu:insert-after>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(broker, docs, "ref", 1);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-after select=\"//item[@id = '1']/description\">" + "           <price>999</price>" + "       </xu:insert-after>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(broker, docs, "999", 1);
        checkIndex(broker, docs, "cha", 1);
        checkIndex(broker, docs, "ir9", 1);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-before select=\"//item[@id = '1']/description\">" + "           <price>888</price>" + "       </xu:insert-before>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(broker, docs, "999", 1);
        checkIndex(broker, docs, "888", 1);
        checkIndex(broker, docs, "88c", 1);
        checkIndex(broker, docs, "att", 1);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:append select=\"//item[@id = '1']\">" + "           <xu:attribute name=\"attr\">abc</xu:attribute>" + "       </xu:append>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(broker, docs, "att", 0);
        checkIndex(broker, docs, "abc", 1);
        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 12 with Modification

use of org.exist.xupdate.Modification in project exist by eXist-db.

the class CustomIndexTest method xupdateReplace.

@Test
public void xupdateReplace() throws LockException, XPathException, PermissionDeniedException, SAXException, EXistException, 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:replace select=\"//item[@id = '1']\">" + "       <item id='4'><description>Wheelchair</description><price>809.50</price></item>" + "   </xu:replace>" + XUPDATE_END;
        Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(broker, docs, "whe", 1);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "   <xu:replace select=\"//item[@id = '4']/description\">" + "       <description>Armchair</description>" + "   </xu:replace>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(broker, docs, "whe", 0);
        checkIndex(broker, docs, "arm", 1);
        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 13 with Modification

use of org.exist.xupdate.Modification in project exist by eXist-db.

the class LuceneIndexTest method xupdateRemove.

/**
 * Remove nodes from different levels of the tree and check if the index is
 * correctly updated.
 */
@Test
public void xupdateRemove() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, LockException, IOException, XPathException, ParserConfigurationException, QName.IllegalQNameException {
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG2, XML2, "xupdate.xml");
    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(docs, broker, new QName[] { new QName("description") }, "chair", 1);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 5);
        checkIndex(docs, broker, new QName[] { new QName("condition") }, null, 2);
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//item[ft:query(description, 'chair')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
        assertNotNull(proc);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        String xupdate = XUPDATE_START + "   <xu:remove select=\"//item[@id='2']/condition\"/>" + XUPDATE_END;
        Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(docs, broker, new QName[] { new QName("condition") }, null, 1);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 4);
        checkIndex(docs, broker, new QName[] { new QName("condition") }, "good", 0);
        checkIndex(docs, broker, new QName[] { new QName("item") }, "good", 0);
        Occurrences[] o = checkIndex(docs, broker, new QName[] { new QName("description") }, "table", 1);
        assertEquals("table", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("description") }, "cabinet", 1);
        assertEquals("cabinet", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "table", 1);
        assertEquals("table", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "cabinet", 1);
        assertEquals("cabinet", o[0].getTerm());
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "   <xu:remove select=\"//item[@id='3']/description/text()\"/>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "   <xu:remove select=\"//item[@id='1']\"/>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        o = checkIndex(docs, broker, new QName[] { new QName("description") }, null, 1);
        assertEquals("table", o[0].getTerm());
        checkIndex(docs, broker, new QName[] { new QName("description") }, "chair", 0);
        checkIndex(docs, broker, new QName[] { new QName("item") }, "chair", 0);
        transact.commit(transaction);
    }
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) Modification(org.exist.xupdate.Modification) InputSource(org.xml.sax.InputSource) QName(org.exist.dom.QName) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) StringReader(java.io.StringReader) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) BrokerPool(org.exist.storage.BrokerPool)

Example 14 with Modification

use of org.exist.xupdate.Modification in project exist by eXist-db.

the class LuceneIndexTest method xupdateInsert.

/**
 * Remove nodes from different levels of the tree and check if the index is
 * correctly updated.
 */
@Test
public void xupdateInsert() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, LockException, IOException, XPathException, ParserConfigurationException, QName.IllegalQNameException {
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG2, XML2, "xupdate.xml");
    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()) {
        final Occurrences[] occur = checkIndex(docs, broker, new QName[] { new QName("description") }, "chair", 1);
        assertEquals("chair", occur[0].getTerm());
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 5);
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//item[ft:query(description, 'chair')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        // Append to root node
        final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
        assertNotNull(proc);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        String xupdate = XUPDATE_START + "   <xu:append select=\"/test\">" + "       <item id='4'><description>Armchair</description> <condition>bad</condition></item>" + "   </xu:append>" + XUPDATE_END;
        Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        Occurrences[] o = checkIndex(docs, broker, new QName[] { new QName("condition") }, null, 2);
        checkIndex(docs, broker, new QName[] { new QName("description") }, null, 4);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 6);
        o = checkIndex(docs, broker, new QName[] { new QName("condition") }, "bad", 1);
        assertEquals("bad", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("description") }, "armchair", 1);
        assertEquals("armchair", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "bad", 1);
        assertEquals("bad", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "armchair", 1);
        assertEquals("armchair", o[0].getTerm());
        // Insert before top element
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-before select=\"//item[@id = '1']\">" + "           <item id='0'><description>Wheelchair</description> <condition>poor</condition></item>" + "       </xu:insert-before>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(docs, broker, new QName[] { new QName("condition") }, null, 3);
        checkIndex(docs, broker, new QName[] { new QName("description") }, null, 5);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 8);
        o = checkIndex(docs, broker, new QName[] { new QName("condition") }, "poor", 1);
        assertEquals("poor", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("description") }, "wheelchair", 1);
        assertEquals("wheelchair", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "poor", 1);
        assertEquals("poor", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "wheelchair", 1);
        assertEquals("wheelchair", o[0].getTerm());
        // Insert after element
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-after select=\"//item[@id = '1']\">" + "           <item id='1.1'><description>refrigerator</description> <condition>perfect</condition></item>" + "       </xu:insert-after>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(docs, broker, new QName[] { new QName("condition") }, null, 4);
        checkIndex(docs, broker, new QName[] { new QName("description") }, null, 6);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 10);
        o = checkIndex(docs, broker, new QName[] { new QName("condition") }, "perfect", 1);
        assertEquals("perfect", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("description") }, "refrigerator", 1);
        assertEquals("refrigerator", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "perfect", 1);
        assertEquals("perfect", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "refrigerator", 1);
        assertEquals("refrigerator", o[0].getTerm());
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-after select=\"//item[@id = '1']/description\">" + "           <condition>average</condition>" + "       </xu:insert-after>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(docs, broker, new QName[] { new QName("condition") }, null, 5);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 11);
        o = checkIndex(docs, broker, new QName[] { new QName("condition") }, "average", 1);
        assertEquals("average", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "average", 1);
        assertEquals("average", o[0].getTerm());
        // Insert before nested element
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-before select=\"//item[@id = '1']/description\">" + "           <condition>awesome</condition>" + "       </xu:insert-before>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(docs, broker, new QName[] { new QName("condition") }, null, 6);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 12);
        o = checkIndex(docs, broker, new QName[] { new QName("condition") }, "awesome", 1);
        assertEquals("awesome", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "awesome", 1);
        assertEquals("awesome", o[0].getTerm());
        // Overwrite attribute
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:append select=\"//item[@id = '1']\">" + "           <xu:attribute name=\"attr\">abc</xu:attribute>" + "       </xu:append>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        QName[] qnattr = { new QName("attr", XMLConstants.NULL_NS_URI, XMLConstants.DEFAULT_NS_PREFIX, ElementValue.ATTRIBUTE) };
        o = checkIndex(docs, broker, qnattr, null, 1);
        assertEquals("abc", o[0].getTerm());
        checkIndex(docs, broker, qnattr, "attribute", 0);
        transact.commit(transaction);
    }
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) Modification(org.exist.xupdate.Modification) InputSource(org.xml.sax.InputSource) QName(org.exist.dom.QName) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) StringReader(java.io.StringReader) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) BrokerPool(org.exist.storage.BrokerPool)

Example 15 with Modification

use of org.exist.xupdate.Modification 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)

Aggregations

Modification (org.exist.xupdate.Modification)23 XUpdateProcessor (org.exist.xupdate.XUpdateProcessor)23 InputSource (org.xml.sax.InputSource)23 StringReader (java.io.StringReader)20 Txn (org.exist.storage.txn.Txn)18 TransactionManager (org.exist.storage.txn.TransactionManager)12 XQuery (org.exist.xquery.XQuery)11 Sequence (org.exist.xquery.value.Sequence)11 BrokerPool (org.exist.storage.BrokerPool)10 DBBroker (org.exist.storage.DBBroker)10 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)8 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)8 DocumentSet (org.exist.dom.persistent.DocumentSet)5 CompiledXQuery (org.exist.xquery.CompiledXQuery)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 QName (org.exist.dom.QName)4 EXistException (org.exist.EXistException)3 Collection (org.exist.collections.Collection)3 DocumentImpl (org.exist.dom.persistent.DocumentImpl)2 LockedDocument (org.exist.dom.persistent.LockedDocument)2