Search in sources :

Example 96 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class ReplaceTest 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()) {
        String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products\">" + "       <product id=\"1\">" + "           <description>Product 1</description>" + "           <price>24.30</price>" + "           <stock>10</stock>" + "       </product>" + "   </xu:append>" + "</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();
        // append some 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:insert-before select=\"/products/product[1]\">" + "       <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:insert-before>" + "</xu:modifications>";
            proc.setBroker(broker);
            proc.setDocumentSet(docs);
            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();
    // replace elements
    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)));
        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 97 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class ShutdownTest method storeAndShutdown.

public void storeAndShutdown() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, XPathException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        Collection test;
        try (final Txn transaction = transact.beginTransaction()) {
            test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
            assertNotNull(test);
            broker.saveCollection(transaction, test);
            // store some documents.
            for (final String sampleName : SAMPLES.getShakespeareXmlSampleNames()) {
                broker.storeDocument(transaction, XmldbURI.create(sampleName), new InputStreamSupplierInputSource(() -> SAMPLES.getShakespeareSample(sampleName)), MimeType.XML_TYPE, test);
            }
            final XQuery xquery = pool.getXQueryService();
            assertNotNull(xquery);
            final Sequence result = xquery.execute(broker, "//SPEECH[contains(LINE, 'love')]", null);
            assertNotNull(result);
            assertEquals(187, result.getItemCount());
            transact.commit(transaction);
        }
        try (final Txn transaction = transact.beginTransaction()) {
            broker.removeCollection(transaction, test);
            transact.commit(transaction);
        }
    }
}
Also used : 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)

Example 98 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class BinaryDocumentTest method overwriteCollection.

@Test
public void overwriteCollection() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final XmldbURI testCollectionUri = XmldbURI.create("/db/overwrite-collection-test");
    final XmldbURI thingUri = testCollectionUri.append("thing");
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        // create a collection
        final Collection thingCollection = broker.getOrCreateCollection(transaction, thingUri);
        broker.saveCollection(transaction, thingCollection);
        // attempt to create a binary document with the same uri as the thingCollection (should fail)
        final Collection testCollection = broker.getCollection(testCollectionUri);
        try {
            broker.storeDocument(transaction, thingUri.lastSegment(), new StringInputSource("binary-file".getBytes(UTF_8)), MimeType.BINARY_TYPE, testCollection);
            fail("Should not have been able to overwrite Collection with Binary Document");
        } catch (final EXistException e) {
            assertEquals("The collection '" + testCollectionUri.getRawCollectionPath() + "' already has a sub-collection named '" + thingUri.lastSegment().toString() + "', you cannot create a Document with the same name as an existing collection.", e.getMessage());
        }
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 99 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class CopyCollectionRecoveryTest method store.

private void store() throws EXistException, PermissionDeniedException, 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()) {
        final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        broker.saveCollection(transaction, root);
        final Collection test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI.append("test2"));
        broker.saveCollection(transaction, test);
        final String sample = getSampleData();
        broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(sample), MimeType.XML_TYPE, test);
        final Collection dest = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("destination"));
        broker.saveCollection(transaction, dest);
        broker.copyCollection(transaction, test, dest, XmldbURI.create("test3"));
        transact.commit(transaction);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn)

Example 100 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class CopyCollectionRecoveryTest method storeAborted.

private void storeAborted() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        Collection test2;
        try (final Txn transaction = transact.beginTransaction()) {
            final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
            assertNotNull(root);
            broker.saveCollection(transaction, root);
            test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI.append("test2"));
            assertNotNull(test2);
            broker.saveCollection(transaction, test2);
            final String sample = getSampleData();
            broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(sample), MimeType.XML_TYPE, test2);
            transact.commit(transaction);
        }
        final Txn transaction = transact.beginTransaction();
        final Collection dest = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("destination"));
        assertNotNull(dest);
        broker.saveCollection(transaction, dest);
        broker.copyCollection(transaction, test2, dest, XmldbURI.create("test3"));
        // DO NOT COMMIT TRANSACTION
        pool.getJournalManager().get().flush(true, false);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn)

Aggregations

Txn (org.exist.storage.txn.Txn)358 DBBroker (org.exist.storage.DBBroker)215 BrokerPool (org.exist.storage.BrokerPool)179 Collection (org.exist.collections.Collection)162 TransactionManager (org.exist.storage.txn.TransactionManager)129 Sequence (org.exist.xquery.value.Sequence)84 StringInputSource (org.exist.util.StringInputSource)83 Test (org.junit.Test)80 XmldbURI (org.exist.xmldb.XmldbURI)55 EXistException (org.exist.EXistException)50 PermissionDeniedException (org.exist.security.PermissionDeniedException)38 Source (org.exist.source.Source)37 StringSource (org.exist.source.StringSource)37 DocumentImpl (org.exist.dom.persistent.DocumentImpl)35 InputSource (org.xml.sax.InputSource)33 XQuery (org.exist.xquery.XQuery)32 IOException (java.io.IOException)31 LockedDocument (org.exist.dom.persistent.LockedDocument)28 InputStream (java.io.InputStream)27 Path (java.nio.file.Path)24