Search in sources :

Example 51 with TransactionManager

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

the class MoveCollectionTest method moveDeepWithSubCollections.

/**
 * Test move collection /db/a/b/c/d/e/f/g/h/i/j/k to /db/z/y/x/w/v/u/k
 *
 * Note that the collection /db/a/b/c/d/e/f/g/h/i/j/k has the sub-collections (sub-1 and sub-2),
 * this test checks that the sub-collections are correctly preserved.
 */
@Test
public void moveDeepWithSubCollections() throws EXistException, IOException, PermissionDeniedException, TriggerException, 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 XmldbURI srcUri = XmldbURI.create("/db/a/b/c/d/e/f/g/h/i/j/k");
        final XmldbURI srcSubCol1Uri = srcUri.append("sub-1");
        final XmldbURI srcSubCol2Uri = srcUri.append("sub-2");
        final XmldbURI destUri = XmldbURI.create("/db/z/y/x/w/v/u");
        // create src collection
        try (final Collection src = broker.getOrCreateCollection(transaction, srcUri)) {
            assertNotNull(src);
            broker.saveCollection(transaction, src);
        }
        // create src sub-collections
        try (final Collection srcColSubCol1 = broker.getOrCreateCollection(transaction, srcSubCol1Uri)) {
            assertNotNull(srcColSubCol1);
            broker.saveCollection(transaction, srcColSubCol1);
        }
        try (final Collection srcColSubCol2 = broker.getOrCreateCollection(transaction, srcSubCol2Uri)) {
            assertNotNull(srcColSubCol2);
            broker.saveCollection(transaction, srcColSubCol2);
        }
        // create dst collection
        try (final Collection dst = broker.getOrCreateCollection(transaction, destUri)) {
            assertNotNull(dst);
            broker.saveCollection(transaction, dst);
        }
        try (final Collection src = broker.openCollection(srcUri, Lock.LockMode.WRITE_LOCK);
            final Collection dst = broker.openCollection(destUri, Lock.LockMode.WRITE_LOCK)) {
            broker.moveCollection(transaction, src, dst, src.getURI().lastSegment());
        }
        transact.commit(transaction);
    }
}
Also used : TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 52 with TransactionManager

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

the class MoveCollectionTest method rename.

/**
 * Test rename collection /db/move-collection-test-rename/before to /db/move-collection-test-rename/after
 */
@Test
public void rename() throws EXistException, PermissionDeniedException, IOException, TriggerException, 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 XmldbURI testColUri = XmldbURI.create("/db/move-collection-test-rename");
        final XmldbURI srcColUri = testColUri.append("before");
        final XmldbURI newName = XmldbURI.create("after");
        try (final Collection testCol = broker.getOrCreateCollection(transaction, testColUri)) {
            assertNotNull(testCol);
            broker.saveCollection(transaction, testCol);
        }
        try (final Collection srcCol = broker.getOrCreateCollection(transaction, srcColUri)) {
            assertNotNull(srcCol);
            broker.saveCollection(transaction, srcCol);
        }
        try (final Collection src = broker.openCollection(srcColUri, Lock.LockMode.WRITE_LOCK);
            final Collection testCol = broker.openCollection(testColUri, Lock.LockMode.WRITE_LOCK)) {
            broker.moveCollection(transaction, src, testCol, newName);
            assertFalse(testCol.hasChildCollection(broker, srcColUri.lastSegment()));
            assertTrue(testCol.hasChildCollection(broker, newName));
        }
        transact.commit(transaction);
    }
}
Also used : TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 53 with TransactionManager

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

the class MoveCollectionTest method renameWithSubCollections.

/**
 * Test rename collection /db/move-collection-test-rename/before to /db/move-collection-test-rename/after
 *
 * Note that the collection /db/move-collection-test-rename/before has the sub-collections (sub-1 and sub-2),
 * this test checks that the sub-collections are correctly preserved.
 */
@Test
public void renameWithSubCollections() throws EXistException, PermissionDeniedException, IOException, TriggerException, 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 XmldbURI testColUri = XmldbURI.create("/db/move-collection-test-rename");
        final XmldbURI srcColUri = testColUri.append("before");
        final XmldbURI srcColSubCol1Uri = srcColUri.append("sub-1");
        final XmldbURI srcColSubCol2Uri = srcColUri.append("sub-2");
        final XmldbURI newName = XmldbURI.create("after");
        // create test collection
        try (final Collection testCol = broker.getOrCreateCollection(transaction, testColUri)) {
            assertNotNull(testCol);
            broker.saveCollection(transaction, testCol);
        }
        // create src collection
        try (final Collection srcCol = broker.getOrCreateCollection(transaction, srcColUri)) {
            assertNotNull(srcCol);
            broker.saveCollection(transaction, srcCol);
        }
        // create src sub-collections
        try (final Collection srcColSubCol1 = broker.getOrCreateCollection(transaction, srcColSubCol1Uri)) {
            assertNotNull(srcColSubCol1);
            broker.saveCollection(transaction, srcColSubCol1);
        }
        try (final Collection srcColSubCol2 = broker.getOrCreateCollection(transaction, srcColSubCol2Uri)) {
            assertNotNull(srcColSubCol2);
            broker.saveCollection(transaction, srcColSubCol2);
        }
        try (final Collection src = broker.openCollection(srcColUri, Lock.LockMode.WRITE_LOCK);
            final Collection testCol = broker.openCollection(testColUri, Lock.LockMode.WRITE_LOCK)) {
            broker.moveCollection(transaction, src, testCol, newName);
            assertFalse(testCol.hasChildCollection(broker, srcColUri.lastSegment()));
            assertTrue(testCol.hasChildCollection(broker, newName));
        }
        transact.commit(transaction);
    }
}
Also used : TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 54 with TransactionManager

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

the class ResourceTest method read2.

private void read2(final BrokerPool pool) throws EXistException, PermissionDeniedException, IOException, TriggerException {
    final TransactionManager transact = pool.getTransactionManager();
    byte[] data = null;
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        final XmldbURI docPath = TestConstants.TEST_COLLECTION_URI.append(DOCUMENT_NAME_URI);
        try (final LockedDocument lockedDoc = broker.getXMLResource(docPath, LockMode.READ_LOCK)) {
            // if document is not present, null is returned
            if (lockedDoc == null) {
                fail("Binary document '" + docPath + " does not exist.");
            } else {
                final BinaryDocument binDoc = (BinaryDocument) lockedDoc.getDocument();
                try (final InputStream is = broker.getBinaryResource(transaction, binDoc)) {
                    data = new byte[(int) binDoc.getContentLength()];
                    is.read(data);
                }
            }
        }
        try (final Collection collection = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
            broker.removeCollection(transaction, collection);
        }
        transact.commit(transaction);
    }
    assertEquals(0, data.length);
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) TransactionManager(org.exist.storage.txn.TransactionManager) InputStream(java.io.InputStream) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI)

Example 55 with TransactionManager

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

the class StoreBinaryTest method removeTestResources.

@After
public void removeTestResources() throws EXistException, PermissionDeniedException, IOException, TriggerException {
    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 testCollection = broker.getCollection(TestConstants.TEST_COLLECTION_URI);
        broker.removeCollection(transaction, testCollection);
        transaction.commit();
    }
}
Also used : TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn)

Aggregations

TransactionManager (org.exist.storage.txn.TransactionManager)131 Txn (org.exist.storage.txn.Txn)128 Collection (org.exist.collections.Collection)86 DBBroker (org.exist.storage.DBBroker)70 BrokerPool (org.exist.storage.BrokerPool)61 StringInputSource (org.exist.util.StringInputSource)27 EXistException (org.exist.EXistException)21 XmldbURI (org.exist.xmldb.XmldbURI)20 Sequence (org.exist.xquery.value.Sequence)20 XQuery (org.exist.xquery.XQuery)18 InputSource (org.xml.sax.InputSource)18 DocumentImpl (org.exist.dom.persistent.DocumentImpl)17 Test (org.junit.Test)16 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)15 InputStream (java.io.InputStream)14 StringReader (java.io.StringReader)13 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)13 PermissionDeniedException (org.exist.security.PermissionDeniedException)13 IOException (java.io.IOException)12 Modification (org.exist.xupdate.Modification)12