Search in sources :

Example 6 with Modification

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

the class AppendTest method doUpdate.

@Override
protected void doUpdate(final DBBroker broker, final TransactionManager transact, final MutableDocumentSet docs) throws ParserConfigurationException, IOException, SAXException, LockException, XPathException, PermissionDeniedException, EXistException {
    final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
    try (final Txn transaction = transact.beginTransaction()) {
        // append some new element to records
        for (int i = 1; i <= 50; i++) {
            final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products\">" + "       <product>" + "           <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>" + "           <description>Product " + i + "</description>" + "           <price>" + (i * 2.5) + "</price>" + "           <stock>" + (i * 10) + "</stock>" + "       </product>" + "   </xu:append>" + "</xu:modifications>";
            proc.setBroker(broker);
            proc.setDocumentSet(docs);
            final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
            modifications[0].process(transaction);
            proc.reset();
        }
        transact.commit(transaction);
    }
    // the following transaction will not be committed and thus undone during recovery
    final Txn transaction = transact.beginTransaction();
    // append new element
    for (int i = 1; i <= 50; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products/product[" + i + "]\">" + "       <date><xu:value-of select=\"current-dateTime()\"/></date>" + "   </xu:append>" + "</xu:modifications>";
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        modifications[0].process(transaction);
        proc.reset();
    }
// DO NOT COMMIT TRANSACTION!
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) Modification(org.exist.xupdate.Modification) InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) Txn(org.exist.storage.txn.Txn)

Example 7 with Modification

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

the class UpdateAttributeTest method doUpdate.

@Override
protected void doUpdate(final DBBroker broker, final TransactionManager transact, final MutableDocumentSet docs) throws ParserConfigurationException, IOException, SAXException, LockException, XPathException, PermissionDeniedException, EXistException {
    final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
    assertNotNull(proc);
    try (final Txn transaction = transact.beginTransaction()) {
        // append some new element to records
        for (int i = 1; i <= 200; i++) {
            final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products\">" + "       <product>" + "           <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>" + "           <description>Product " + i + "</description>" + "           <price>" + (i * 2.5) + "</price>" + "           <stock>" + (i * 10) + "</stock>" + "       </product>" + "   </xu:append>" + "</xu:modifications>";
            proc.setBroker(broker);
            proc.setDocumentSet(docs);
            final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
            assertNotNull(modifications);
            modifications[0].process(transaction);
            proc.reset();
        }
        transact.commit(transaction);
    }
    // the following transaction will not be committed and thus undone during recovery
    final Txn transaction = transact.beginTransaction();
    assertNotNull(transaction);
    // update attributes
    for (int i = 1; i <= 200; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:update select=\"/products/product[" + i + "]/@id\">" + i + "u</xu:update>" + "</xu:modifications>";
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
    }
// DO NOT COMMIT TRANSACTION
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) Modification(org.exist.xupdate.Modification) InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) Txn(org.exist.storage.txn.Txn)

Example 8 with Modification

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

the class UpdateTest method doUpdate.

@Override
protected void doUpdate(final DBBroker broker, final TransactionManager transact, final MutableDocumentSet docs) throws ParserConfigurationException, IOException, SAXException, LockException, XPathException, PermissionDeniedException, EXistException {
    final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
    assertNotNull(proc);
    try (final Txn transaction = transact.beginTransaction()) {
        assertNotNull(transaction);
        // append some new element to records
        for (int i = 1; i <= 200; i++) {
            final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products\">" + "       <product>" + "           <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>" + "           <description>Product " + i + "</description>" + "           <price>" + (i * 2.5) + "</price>" + "           <stock>" + (i * 10) + "</stock>" + "       </product>" + "   </xu:append>" + "</xu:modifications>";
            proc.setBroker(broker);
            proc.setDocumentSet(docs);
            final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
            assertNotNull(modifications);
            modifications[0].process(transaction);
            proc.reset();
        }
        transact.commit(transaction);
    }
    // the following transaction will not be committed and thus undone during recovery
    final Txn transaction = transact.beginTransaction();
    assertNotNull(transaction);
    // update elements
    for (int i = 1; i <= 200; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:update select=\"/products/product[" + i + "]/price\">19.99</xu:update>" + "</xu:modifications>";
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(transaction);
        modifications[0].process(transaction);
        proc.reset();
    }
// DO NOT COMMIT TRANSACTION
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) Modification(org.exist.xupdate.Modification) InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) Txn(org.exist.storage.txn.Txn)

Example 9 with Modification

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

the class CustomIndexTest method xupdateRemove.

/**
 * Remove nodes from different levels of the tree and check if the index is
 * correctly updated.
 */
@Test
public void xupdateRemove() throws EXistException, PermissionDeniedException, XPathException, ParserConfigurationException, IOException, SAXException, LockException {
    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:remove select=\"//item[@id='2']/price\"/>" + XUPDATE_END;
        Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(broker, docs, "892", 0);
        checkIndex(broker, docs, "tab", 1);
        checkIndex(broker, docs, "le8", 0);
        checkIndex(broker, docs, "cab", 1);
        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();
        checkIndex(broker, docs, "cab", 0);
        checkIndex(broker, docs, "att", 1);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "   <xu:remove select=\"//item[@id='1']/@attr\"/>" + 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, "cha", 1);
        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();
        checkIndex(broker, docs, "cha", 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 10 with Modification

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

the class CustomIndexTest method xupdateUpdate.

@Test
public void xupdateUpdate() 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:update select=\"//item[@id = '1']/description\">wardrobe</xu:update>" + XUPDATE_END;
        Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(broker, docs, "war", 1);
        checkIndex(broker, docs, "cha", 0);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "   <xu:update select=\"//item[@id = '1']/description/text()\">Wheelchair</xu:update>" + XUPDATE_END;
        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:update select=\"//item[@id = '1']/@attr\">abc</xu:update>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        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)

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