Search in sources :

Example 36 with TransactionManager

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

the class DOMFileRecoverTest method add.

@Test
public void add() throws EXistException, ReadOnlyException, TerminatedException, IOException, BTreeException {
    BrokerPool.FORCE_CORRUPTION = false;
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final NodeIdFactory idFact = pool.getNodeFactory();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        // Add some random data and force db corruption
        broker.flush();
        final DOMFile domDb = ((NativeBroker) broker).getDOMFile();
        domDb.setOwnerObject(this);
        final TransactionManager mgr = pool.getTransactionManager();
        long firstToRemove = -1;
        try (final Txn txn = mgr.beginTransaction()) {
            // put 1000 values into the btree
            for (int i = 1; i <= 10000; i++) {
                byte[] data = ("Value" + i).getBytes();
                NodeId id = idFact.createInstance(i);
                long addr = domDb.put(txn, new NativeBroker.NodeRef(500, id), data);
                // TODO : test addr ?
                if (i == 1)
                    firstToRemove = addr;
            }
            domDb.closeDocument();
            // remove all
            NativeBroker.NodeRef ref = new NativeBroker.NodeRef(500);
            assertNotNull(ref);
            IndexQuery idx = new IndexQuery(IndexQuery.TRUNC_RIGHT, ref);
            assertNotNull(idx);
            domDb.remove(txn, idx, null);
            domDb.removeAll(txn, firstToRemove);
            // put some more
            for (int i = 1; i <= 10000; i++) {
                byte[] data = ("Value" + i).getBytes();
                @SuppressWarnings("unused") long addr = domDb.put(txn, new NativeBroker.NodeRef(500, idFact.createInstance(i)), data);
            // TODO : test addr ?
            }
            domDb.closeDocument();
            mgr.commit(txn);
        }
        try (final Txn txn = mgr.beginTransaction()) {
            // put 1000 new values into the btree
            for (int i = 1; i <= 1000; i++) {
                byte[] data = ("Value" + i).getBytes();
                long addr = domDb.put(txn, new NativeBroker.NodeRef(501, idFact.createInstance(i)), data);
                // TODO : test addr ?
                if (i == 1)
                    firstToRemove = addr;
            }
            domDb.closeDocument();
            mgr.commit(txn);
        }
        // the following transaction is not committed and will be rolled back during recovery
        try (final Txn txn = mgr.beginTransaction()) {
            for (int i = 1; i <= 200; i++) {
                domDb.remove(txn, new NativeBroker.NodeRef(500, idFact.createInstance(i)));
            }
            final IndexQuery idx = new IndexQuery(IndexQuery.TRUNC_RIGHT, new NativeBroker.NodeRef(501));
            domDb.remove(txn, idx, null);
            domDb.removeAll(txn, firstToRemove);
            // Don't commit...
            mgr.commit(txn);
        }
        pool.getJournalManager().get().flush(true, false);
        Writer writer = new StringWriter();
        domDb.dump(writer);
    }
}
Also used : IndexQuery(org.exist.storage.btree.IndexQuery) DOMFile(org.exist.storage.dom.DOMFile) Txn(org.exist.storage.txn.Txn) NodeIdFactory(org.exist.numbering.NodeIdFactory) StringWriter(java.io.StringWriter) TransactionManager(org.exist.storage.txn.TransactionManager) NodeId(org.exist.numbering.NodeId) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 37 with TransactionManager

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

the class MoveResourceRecoveryTest method storeAborted.

private void storeAborted() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, URISyntaxException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        try (final Txn transaction = transact.beginTransaction()) {
            final Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
            assertNotNull(test2);
            broker.saveCollection(transaction, test2);
            final String sample;
            try (final InputStream is = SAMPLES.getRomeoAndJulietSample()) {
                sample = InputStreamUtil.readString(is, UTF_8);
            }
            broker.storeDocument(transaction, XmldbURI.create("new_test2.xml"), new StringInputSource(sample), MimeType.XML_TYPE, test2);
            transact.commit(transaction);
        }
        final Txn transaction = transact.beginTransaction();
        final Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
        final DocumentImpl doc = test2.getDocument(broker, XmldbURI.create("new_test2.xml"));
        assertNotNull(doc);
        final Collection test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        broker.moveResource(transaction, doc, test, XmldbURI.create("new_test2.xml"));
        broker.saveCollection(transaction, test);
        // NOTE: do not commit the transaction
        pool.getJournalManager().get().flush(true, false);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) InputStream(java.io.InputStream) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 38 with TransactionManager

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

the class MoveResourceRecoveryTest method store.

private void store() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, URISyntaxException {
    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 test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        assertNotNull(test);
        broker.saveCollection(transaction, test);
        final Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
        assertNotNull(test2);
        broker.saveCollection(transaction, test2);
        final String sample;
        try (final InputStream is = SAMPLES.getRomeoAndJulietSample()) {
            sample = InputStreamUtil.readString(is, UTF_8);
        }
        broker.storeDocument(transaction, TestConstants.TEST_XML_URI, new StringInputSource(sample), MimeType.XML_TYPE, test2);
        final DocumentImpl doc = test2.getDocument(broker, TestConstants.TEST_XML_URI);
        assertNotNull(doc);
        broker.moveResource(transaction, doc, test, XmldbURI.create("new_test.xml"));
        broker.saveCollection(transaction, test);
        transact.commit(transaction);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) InputStream(java.io.InputStream) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 39 with TransactionManager

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

the class CopyCollectionTest method copyDeepWithSubCollections.

/**
 * Test copy 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 copyDeepWithSubCollections() throws EXistException, IOException, PermissionDeniedException, TriggerException, LockException {
    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");
    final XmldbURI newName = srcUri.lastSegment();
    final BrokerPool pool = existWebServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        // 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 (Collection dst = broker.getOrCreateCollection(transaction, destUri)) {
            assertNotNull(dst);
            broker.saveCollection(transaction, dst);
        }
        try (final Collection src = broker.openCollection(srcUri, LockMode.WRITE_LOCK);
            final Collection dst = broker.openCollection(destUri, LockMode.WRITE_LOCK)) {
            broker.copyCollection(transaction, src, dst, newName);
        }
        transact.commit(transaction);
    }
    // check that the source still exists
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        try (final Collection src = broker.openCollection(srcUri, LockMode.READ_LOCK)) {
            assertNotNull(src);
        }
        // check that the source sub-collections still exist
        try (final Collection srcSubCol1 = broker.openCollection(srcSubCol1Uri, LockMode.READ_LOCK)) {
            assertNotNull(srcSubCol1);
        }
        try (final Collection srcSubCol2 = broker.openCollection(srcSubCol2Uri, LockMode.READ_LOCK)) {
            assertNotNull(srcSubCol2);
        }
        transaction.commit();
    }
    // check that the new copy exists
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        final XmldbURI copyUri = destUri.append(newName);
        try (final Collection copy = broker.openCollection(copyUri, LockMode.READ_LOCK)) {
            assertNotNull(copy);
        }
        // check that the new copy has sub-collection copies
        final XmldbURI copySubCol1Uri = copyUri.append(srcSubCol1Uri.lastSegment());
        try (final Collection copySubCol1 = broker.openCollection(copySubCol1Uri, LockMode.READ_LOCK)) {
            assertNotNull(copySubCol1);
        }
        final XmldbURI copySubCol2Uri = copyUri.append(srcSubCol2Uri.lastSegment());
        try (final Collection copySubCol2 = broker.openCollection(copySubCol2Uri, LockMode.READ_LOCK)) {
            assertNotNull(copySubCol2);
        }
        transaction.commit();
    }
}
Also used : TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI)

Example 40 with TransactionManager

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

the class BFileRecoverTest method add.

@Test
public void add() throws EXistException, IOException, BTreeException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager mgr = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        broker.flush();
        broker.sync(Sync.MAJOR);
        final BFile collectionsDb = (BFile) ((NativeBroker) broker).getStorage(NativeBroker.COLLECTIONS_DBX_ID);
        BrokerPool.FORCE_CORRUPTION = true;
        try (final Txn txn = mgr.beginTransaction()) {
            for (int i = 1; i < 1001; i++) {
                String value = "test" + i;
                byte[] data = value.getBytes(UTF_8);
                collectionsDb.put(txn, new Value(data), new FixedByteArray(data, 0, data.length), true);
            }
            byte[] replacement = "new value".getBytes(UTF_8);
            for (int i = 1; i < 101; i++) {
                String value = "test" + i;
                byte[] data = value.getBytes(UTF_8);
                collectionsDb.put(txn, new Value(data), new FixedByteArray(replacement, 0, replacement.length), true);
            }
            mgr.commit(txn);
        }
        Writer writer = new StringWriter();
        collectionsDb.dump(writer);
    }
}
Also used : StringWriter(java.io.StringWriter) TransactionManager(org.exist.storage.txn.TransactionManager) FixedByteArray(org.exist.util.FixedByteArray) Value(org.exist.storage.btree.Value) Txn(org.exist.storage.txn.Txn) BFile(org.exist.storage.index.BFile) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

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