Search in sources :

Example 21 with Modification

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

the class RemoveTest 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 <= 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)));
            assertNotNull(modifications);
            modifications[0].process(transaction);
            proc.reset();
        }
        transact.commit(transaction);
    }
    final Serializer serializer = broker.borrowSerializer();
    try (final LockedDocument lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(TestConstants.TEST_XML_URI), LockMode.READ_LOCK)) {
        assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/test.xml' should not be null", lockedDoc);
        final String data = serializer.serialize(lockedDoc.getDocument());
    } finally {
        broker.returnSerializer(serializer);
    }
    // the following transaction will not be committed and thus undone during recovery
    final Txn transaction = transact.beginTransaction();
    // remove elements
    for (int i = 1; i <= 25; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:remove select=\"/products/product[last()]\"/>" + "</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) LockedDocument(org.exist.dom.persistent.LockedDocument) Txn(org.exist.storage.txn.Txn) Serializer(org.exist.storage.serializers.Serializer)

Example 22 with Modification

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

the class RenameTest 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();
    // rename elements
    final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:rename select=\"/products/product/description\">descript</xu:rename>" + "</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 23 with Modification

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

the class LuceneIndexTest method xupdateUpdate.

@Test
public void xupdateUpdate() 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());
        // Update element content
        final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
        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(docs, broker, new QName[] { new QName("description") }, null, 3);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 5);
        checkIndex(docs, broker, new QName[] { new QName("description") }, "chair", 0);
        checkIndex(docs, broker, new QName[] { new QName("item") }, "chair", 0);
        Occurrences[] o = checkIndex(docs, broker, new QName[] { new QName("description") }, "wardrobe", 1);
        assertEquals("wardrobe", o[0].getTerm());
        // Update text node
        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(docs, broker, new QName[] { new QName("description") }, null, 3);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 5);
        checkIndex(docs, broker, new QName[] { new QName("description") }, "wardrobe", 0);
        checkIndex(docs, broker, new QName[] { new QName("item") }, "wardrobe", 0);
        o = checkIndex(docs, broker, new QName[] { new QName("description") }, "wheelchair", 1);
        assertEquals("wheelchair", o[0].getTerm());
        // Update attribute value
        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();
        final 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)

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