Search in sources :

Example 11 with DefaultDocumentSet

use of org.exist.dom.persistent.DefaultDocumentSet in project exist by eXist-db.

the class AbstractUpdateTest method update.

@Test
public final void update() throws EXistException, DatabaseConfigurationException, LockException, SAXException, PermissionDeniedException, IOException, ParserConfigurationException, XPathException {
    BrokerPool.FORCE_CORRUPTION = true;
    BrokerPool pool = startDb();
    try {
        final TransactionManager transact = pool.getTransactionManager();
        try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
            final DocumentImpl doc = init(broker, transact);
            final MutableDocumentSet docs = new DefaultDocumentSet();
            docs.add(doc);
            doUpdate(broker, transact, docs);
            pool.getJournalManager().get().flush(true, false);
        }
        BrokerPool.FORCE_CORRUPTION = false;
        existEmbeddedServer.restart(false);
        pool = existEmbeddedServer.getBrokerPool();
        read(pool);
    } finally {
        existEmbeddedServer.stopDb(true);
    }
}
Also used : MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) TransactionManager(org.exist.storage.txn.TransactionManager) DocumentImpl(org.exist.dom.persistent.DocumentImpl) Test(org.junit.Test)

Example 12 with DefaultDocumentSet

use of org.exist.dom.persistent.DefaultDocumentSet in project exist by eXist-db.

the class LocalXPathQueryService method beginProtected.

/**
 * Execute all following queries in a protected environment.
 * Protected means: it is guaranteed that documents referenced by the
 * query or the result set are not modified by other threads
 * until {@link #endProtected} is called.
 */
@Override
public void beginProtected() throws XMLDBException {
    try {
        int retries = BEGIN_PROTECTED_MAX_LOCKING_RETRIES == -1 ? -1 : BEGIN_PROTECTED_MAX_LOCKING_RETRIES - 2;
        boolean deadlockCaught;
        do {
            reservedBroker = brokerPool.get(Optional.of(user));
            deadlockCaught = false;
            MutableDocumentSet docs = null;
            try {
                final org.exist.collections.Collection coll = reservedBroker.getCollection(collection.getPathURI());
                lockedDocuments = new LockedDocumentMap();
                docs = new DefaultDocumentSet();
                coll.allDocs(reservedBroker, docs, true, lockedDocuments, LockMode.WRITE_LOCK);
                return;
            } catch (final LockException e) {
                LOG.warn("Deadlock detected. Starting over again. Docs: {}; locked: {}. Cause: {}", docs.getDocumentCount(), lockedDocuments.size(), e.getMessage());
                lockedDocuments.unlock();
                reservedBroker.close();
                deadlockCaught = true;
            } catch (final PermissionDeniedException e) {
                throw new XMLDBException(ErrorCodes.PERMISSION_DENIED, "Permission denied on document");
            }
            retries--;
        } while (deadlockCaught && retries >= -1);
    } catch (final EXistException e) {
        if (reservedBroker != null) {
            reservedBroker.close();
        }
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage());
    }
    throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "Unable to beginProtected after " + BEGIN_PROTECTED_MAX_LOCKING_RETRIES + " retries");
}
Also used : MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) LockException(org.exist.util.LockException) LockedDocumentMap(org.exist.storage.lock.LockedDocumentMap) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException)

Example 13 with DefaultDocumentSet

use of org.exist.dom.persistent.DefaultDocumentSet in project exist by eXist-db.

the class LocalXUpdateQueryService method updateResource.

@Override
public long updateResource(final String id, final String commands) throws XMLDBException {
    return this.<Long>withDb((broker, transaction) -> {
        final long start = System.currentTimeMillis();
        final MutableDocumentSet docs = this.<MutableDocumentSet>read(broker, transaction, collection.getPathURI()).apply((collection, broker1, transaction1) -> {
            MutableDocumentSet d = new DefaultDocumentSet();
            if (id == null) {
                d = collection.allDocs(broker1, d, true);
            } else {
                try {
                    final XmldbURI resourceURI = XmldbURI.xmldbUriFor(id);
                    try (final LockedDocument lockedDocument = collection.getDocumentWithLock(broker1, resourceURI, Lock.LockMode.READ_LOCK)) {
                        // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                        collection.close();
                        final DocumentImpl doc = lockedDocument == null ? null : lockedDocument.getDocument();
                        if (doc == null) {
                            throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Resource not found: " + id);
                        }
                        d.add(doc);
                    }
                } catch (final URISyntaxException e) {
                    throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
                }
            }
            return d;
        });
        try (final Reader reader = new StringReader(commands)) {
            if (processor == null) {
                processor = new XUpdateProcessor(broker, docs);
            } else {
                processor.setBroker(broker);
                processor.setDocumentSet(docs);
            }
            final Modification[] modifications = processor.parse(new InputSource(reader));
            long mods = 0;
            for (Modification modification : modifications) {
                mods += modification.process(transaction);
                broker.flush();
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("xupdate took {}ms.", System.currentTimeMillis() - start);
            }
            return mods;
        } catch (final ParserConfigurationException | SAXException | LockException e) {
            throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
        } finally {
            if (processor != null) {
                processor.reset();
            }
        }
    });
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) Modification(org.exist.xupdate.Modification) InputSource(org.xml.sax.InputSource) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) XMLDBException(org.xmldb.api.base.XMLDBException) Reader(java.io.Reader) StringReader(java.io.StringReader) URISyntaxException(java.net.URISyntaxException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) SAXException(org.xml.sax.SAXException) LockException(org.exist.util.LockException) LockedDocument(org.exist.dom.persistent.LockedDocument) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 14 with DefaultDocumentSet

use of org.exist.dom.persistent.DefaultDocumentSet in project exist by eXist-db.

the class UpdateRecoverTest method store.

private void store(final BrokerPool pool) throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, ParserConfigurationException, XPathException {
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        DocumentImpl doc;
        try (final Txn transaction = transact.beginTransaction()) {
            final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
            assertNotNull(root);
            broker.saveCollection(transaction, root);
            final Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
            assertNotNull(test2);
            broker.saveCollection(transaction, test2);
            broker.storeDocument(transaction, TestConstants.TEST_XML_URI, new StringInputSource(TEST_XML), MimeType.XML_TYPE, test2);
            doc = test2.getDocument(broker, TestConstants.TEST_XML_URI);
            // TODO : unlock the collection here ?
            transact.commit(transaction);
        }
        try (final Txn transaction = transact.beginTransaction()) {
            final MutableDocumentSet docs = new DefaultDocumentSet();
            docs.add(doc);
            final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
            assertNotNull(proc);
            // insert some nodes
            for (int i = 1; i <= 200; i++) {
                final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:insert-before select=\"/products/product[1]\">" + "       <product>" + "           <description>Product " + i + "</description>" + "           <price>" + (i * 2.5) + "</price>" + "           <stock>" + (i * 10) + "</stock>" + "       </product>" + "   </xu:insert-before>" + "</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();
            }
            // add attribute
            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[" + i + "]\">" + "         <xu:attribute name=\"id\">" + i + "</xu:attribute>" + " </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();
            }
            // replace some
            for (int i = 1; i <= 100; i++) {
                final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:replace select=\"/products/product[" + i + "]\">" + "     <product id=\"" + i + "\">" + "         <description>Replaced product</description>" + "         <price>" + (i * 0.75) + "</price>" + "     </product>" + " </xu:replace>" + "</xu:modifications>";
                proc.setBroker(broker);
                proc.setDocumentSet(docs);
                final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                long mods = modifications[0].process(transaction);
                proc.reset();
            }
            // remove some
            for (int i = 1; i <= 100; 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();
            }
            for (int i = 1; i <= 100; 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();
            }
            // rename element "description" to "descript"
            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);
            Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
            assertNotNull(modifications);
            modifications[0].process(transaction);
            proc.reset();
            // update attribute values
            for (int i = 1; i <= 200; i++) {
                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);
                modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                long mods = modifications[0].process(transaction);
                proc.reset();
            }
            // append new element to records
            for (int i = 1; i <= 200; i++) {
                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);
                modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                modifications[0].process(transaction);
                proc.reset();
            }
            // update element content
            for (int i = 1; i <= 200; i++) {
                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);
                modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                long mods = modifications[0].process(transaction);
                proc.reset();
            }
            transact.commit(transaction);
        }
    }
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) Modification(org.exist.xupdate.Modification) StringInputSource(org.exist.util.StringInputSource) InputSource(org.xml.sax.InputSource) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) StringReader(java.io.StringReader) Collection(org.exist.collections.Collection)

Example 15 with DefaultDocumentSet

use of org.exist.dom.persistent.DefaultDocumentSet in project exist by eXist-db.

the class RangeIndexUpdateTest method startDB.

@BeforeClass
public static void startDB() throws EXistException, PermissionDeniedException, IOException, SAXException, CollectionConfigurationException, 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();
        final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI)) {
        broker.saveCollection(transaction, root);
        final CollectionConfigurationManager mgr = pool.getConfigurationManager();
        mgr.addConfiguration(transaction, broker, root, COLLECTION_CONFIG);
        docs = new DefaultDocumentSet();
        broker.storeDocument(transaction, XmldbURI.create("test_string.xml"), new StringInputSource(XML), MimeType.XML_TYPE, root);
        docs.add(root.getDocument(broker, XmldbURI.create("test_string.xml")));
        broker.storeDocument(transaction, XmldbURI.create("test_string2.xml"), new StringInputSource(XML2), MimeType.XML_TYPE, root);
        docs.add(root.getDocument(broker, XmldbURI.create("test_string2.xml")));
        transact.commit(transaction);
    }
}
Also used : DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) CollectionConfigurationManager(org.exist.collections.CollectionConfigurationManager)

Aggregations

DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)15 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)10 TransactionManager (org.exist.storage.txn.TransactionManager)7 Txn (org.exist.storage.txn.Txn)7 DocumentImpl (org.exist.dom.persistent.DocumentImpl)6 CollectionConfigurationManager (org.exist.collections.CollectionConfigurationManager)5 DBBroker (org.exist.storage.DBBroker)5 LockException (org.exist.util.LockException)5 Collection (org.exist.collections.Collection)4 PermissionDeniedException (org.exist.security.PermissionDeniedException)4 BrokerPool (org.exist.storage.BrokerPool)4 StringReader (java.io.StringReader)3 DocumentSet (org.exist.dom.persistent.DocumentSet)3 StringInputSource (org.exist.util.StringInputSource)3 Modification (org.exist.xupdate.Modification)3 XUpdateProcessor (org.exist.xupdate.XUpdateProcessor)3 InputSource (org.xml.sax.InputSource)3 ExtArrayNodeSet (org.exist.dom.persistent.ExtArrayNodeSet)2 XPathException (org.exist.xquery.XPathException)2 SAXException (org.xml.sax.SAXException)2