Search in sources :

Example 41 with LockedDocument

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

the class RecoveryTest method verify.

private void verify(final BrokerPool pool) throws EXistException, PermissionDeniedException, SAXException, XPathException, IOException, BTreeException, LockException {
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final Serializer serializer = broker.borrowSerializer();
        try {
            try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/hamlet.xml"), LockMode.READ_LOCK)) {
                assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/hamlet.xml' should not be null", lockedDoc);
                final String data = serializer.serialize(lockedDoc.getDocument());
                assertNotNull(data);
            }
            try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/test_string.xml"), LockMode.READ_LOCK)) {
                assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/test_string.xml' should not be null", lockedDoc);
                final String data = serializer.serialize(lockedDoc.getDocument());
                assertNotNull(data);
            }
            final String lastSampleName = SAMPLES.getShakespeareXmlSampleNames()[SAMPLES.getShakespeareXmlSampleNames().length - 1];
            try (final LockedDocument lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(lastSampleName), LockMode.READ_LOCK)) {
                assertNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/'" + lastSampleName + " should not exist anymore", lockedDoc);
            }
            final XQuery xquery = pool.getXQueryService();
            assertNotNull(xquery);
            final Sequence seq = xquery.execute(broker, "//SPEECH[contains(LINE, 'king')]", null);
            assertNotNull(seq);
            for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
                final Item next = i.nextItem();
                final String value = serializer.serialize((NodeValue) next);
            }
        } finally {
            broker.returnSerializer(serializer);
        }
        try (final LockedDocument lockedBinDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(TestConstants.TEST_BINARY_URI), LockMode.READ_LOCK)) {
            assertNotNull("Binary document is null", lockedBinDoc);
            final BinaryDocument binDoc = (BinaryDocument) lockedBinDoc.getDocument();
            try (final InputStream is = broker.getBinaryResource(binDoc)) {
                final byte[] bdata = new byte[(int) binDoc.getContentLength()];
                is.read(bdata);
                final String data = new String(bdata);
                assertNotNull(data);
            }
        }
        final DOMFile domDb = ((NativeBroker) broker).getDOMFile();
        assertNotNull(domDb);
        try (final Writer writer = new StringWriter()) {
            domDb.dump(writer);
        }
        final TransactionManager transact = pool.getTransactionManager();
        try (final Txn transaction = transact.beginTransaction()) {
            try (final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
                assertNotNull(root);
                transaction.acquireCollectionLock(() -> broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(root.getURI()));
                broker.removeCollection(transaction, root);
            }
            transact.commit(transaction);
        }
    }
}
Also used : XQuery(org.exist.xquery.XQuery) InputStream(java.io.InputStream) Sequence(org.exist.xquery.value.Sequence) DOMFile(org.exist.storage.dom.DOMFile) Txn(org.exist.storage.txn.Txn) BinaryDocument(org.exist.dom.persistent.BinaryDocument) Item(org.exist.xquery.value.Item) SequenceIterator(org.exist.xquery.value.SequenceIterator) StringWriter(java.io.StringWriter) TransactionManager(org.exist.storage.txn.TransactionManager) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Serializer(org.exist.storage.serializers.Serializer)

Example 42 with LockedDocument

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

the class FluentBrokerAPITest method collectionThenCollectionAndDoc.

@Test
public void collectionThenCollectionAndDoc() throws PermissionDeniedException, EXistException, LockException {
    final XmldbURI docUri = uri("all-test.xml");
    final long collectionCreated = 1234;
    final IMocksControl ctrl = createStrictControl();
    ctrl.checkOrder(true);
    final BrokerPool mockBrokerPool = ctrl.createMock(BrokerPool.class);
    final DBBroker mockBroker = ctrl.createMock(DBBroker.class);
    final Collection mockCollection = ctrl.createMock(Collection.class);
    final LockedDocument mockLockedDocument = ctrl.createMock(LockedDocument.class);
    final DocumentImpl mockDocument = ctrl.createMock(DocumentImpl.class);
    expect(mockBrokerPool.getBroker()).andReturn(mockBroker);
    expect(mockBroker.openCollection(TEST_COLLECTION_URI, READ_LOCK)).andReturn(mockCollection);
    expect(mockCollection.getCreated()).andReturn(collectionCreated);
    expect(mockCollection.getDocumentWithLock(mockBroker, docUri, READ_LOCK)).andReturn(mockLockedDocument);
    expect(mockLockedDocument.getDocument()).andReturn(mockDocument);
    expect(mockCollection.getURI()).andReturn(TEST_COLLECTION_URI);
    expect(mockDocument.getFileURI()).andReturn(docUri);
    // NOTE: checks that Collection lock is release before Document lock
    mockCollection.close();
    mockLockedDocument.close();
    mockBroker.close();
    ctrl.replay();
    final Function<Collection, Long> collectionOp = collection -> collection.getCreated();
    final BiFunction<Collection, DocumentImpl, String> collectionDocOp = (collection, doc) -> collection.getURI().append(doc.getFileURI()).toString();
    final Tuple2<Long, String> result = FluentBrokerAPI.builder(mockBrokerPool).withCollection(TEST_COLLECTION_URI, READ_LOCK).execute(collectionOp).withDocument(collection -> new Tuple2<>(docUri, READ_LOCK)).execute(collectionDocOp).doAll();
    assertEquals(collectionCreated, result._1.longValue());
    assertEquals(TEST_COLLECTION_URI.append(docUri), result._2);
    ctrl.verify();
}
Also used : Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) IMocksControl(org.easymock.IMocksControl) LockMode(org.exist.storage.lock.Lock.LockMode) LockedDocument(org.exist.dom.persistent.LockedDocument) EasyMock.createStrictControl(org.easymock.EasyMock.createStrictControl) BiFunction(java.util.function.BiFunction) Test(org.junit.Test) PermissionDeniedException(org.exist.security.PermissionDeniedException) EasyMock.expect(org.easymock.EasyMock.expect) Function(java.util.function.Function) FluentBrokerAPI.uri(org.exist.storage.FluentBrokerAPI.uri) LockException(org.exist.util.LockException) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) Lock(org.exist.storage.lock.Lock) Assert.assertEquals(org.junit.Assert.assertEquals) DocumentImpl(org.exist.dom.persistent.DocumentImpl) IMocksControl(org.easymock.IMocksControl) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 43 with LockedDocument

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

the class LargeValuesTest method restart.

/**
 * Just recover.
 */
private void restart() throws EXistException, DatabaseConfigurationException, PermissionDeniedException, IOException, SAXException, LockException {
    BrokerPool.FORCE_CORRUPTION = false;
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.READ_LOCK)) {
        assertNotNull(root);
        try (final LockedDocument lockedDoc = root.getDocumentWithLock(broker, XmldbURI.create("test.xml"), LockMode.READ_LOCK)) {
            assertNotNull(lockedDoc);
            final Path tempFile = Files.createTempFile("eXist", ".xml");
            final Serializer serializer = broker.borrowSerializer();
            try (final Writer writer = Files.newBufferedWriter(tempFile, UTF_8)) {
                serializer.serialize(lockedDoc.getDocument(), writer);
            } finally {
                broker.returnSerializer(serializer);
            }
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            root.close();
            FileUtils.deleteQuietly(tempFile);
        // XQuery xquery = broker.getXQueryService();
        // DocumentSet docs = broker.getAllXMLResources(new DefaultDocumentSet());
        // Sequence result = xquery.execute(broker, "//key/@id/string()", docs.docsToNodeSet(), AccessContext.TEST);
        // assertEquals(KEY_COUNT, result.getItemCount());
        // for (SequenceIterator i = result.iterate(); i.hasNext();) {
        // Item item = i.nextItem();
        // String s = item.getStringValue();
        // assertTrue(s.length() > 0);
        // if (s.length() == 0)
        // break;
        // }
        }
    }
}
Also used : Path(java.nio.file.Path) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Writer(java.io.Writer) Serializer(org.exist.storage.serializers.Serializer)

Example 44 with LockedDocument

use of org.exist.dom.persistent.LockedDocument 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 45 with LockedDocument

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

the class RewriteConfig method configure.

private void configure(final String controllerConfig) throws ServletException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Loading XQueryURLRewrite configuration from {}", controllerConfig);
    }
    if (controllerConfig.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
        try (final DBBroker broker = urlRewrite.getBrokerPool().get(Optional.ofNullable(urlRewrite.getDefaultUser()))) {
            try (final LockedDocument lockedDocument = broker.getXMLResource(XmldbURI.create(controllerConfig), LockMode.READ_LOCK)) {
                final DocumentImpl doc = lockedDocument == null ? null : lockedDocument.getDocument();
                if (doc != null) {
                    parse(doc);
                }
            }
        } catch (final EXistException | PermissionDeniedException e) {
            throw new ServletException("Failed to parse controller.xml: " + e.getMessage(), e);
        }
    } else {
        try {
            final Path d = Paths.get(urlRewrite.getConfig().getServletContext().getRealPath("/")).normalize();
            final Path configFile = d.resolve(controllerConfig);
            if (Files.isReadable(configFile)) {
                final Document doc = parseConfig(configFile);
                parse(doc);
            }
        } catch (final ParserConfigurationException | IOException | SAXException e) {
            throw new ServletException("Failed to parse controller.xml: " + e.getMessage(), e);
        }
    }
    urlRewrite.clearCaches();
}
Also used : Path(java.nio.file.Path) EXistException(org.exist.EXistException) IOException(java.io.IOException) Document(org.w3c.dom.Document) LockedDocument(org.exist.dom.persistent.LockedDocument) DocumentImpl(org.exist.dom.persistent.DocumentImpl) SAXException(org.xml.sax.SAXException) ServletException(javax.servlet.ServletException) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

LockedDocument (org.exist.dom.persistent.LockedDocument)91 DocumentImpl (org.exist.dom.persistent.DocumentImpl)40 XmldbURI (org.exist.xmldb.XmldbURI)39 DBBroker (org.exist.storage.DBBroker)36 PermissionDeniedException (org.exist.security.PermissionDeniedException)30 Collection (org.exist.collections.Collection)28 Txn (org.exist.storage.txn.Txn)27 BrokerPool (org.exist.storage.BrokerPool)21 EXistException (org.exist.EXistException)20 Test (org.junit.Test)20 IOException (java.io.IOException)18 BinaryDocument (org.exist.dom.persistent.BinaryDocument)18 Serializer (org.exist.storage.serializers.Serializer)15 XPathException (org.exist.xquery.XPathException)14 URISyntaxException (java.net.URISyntaxException)13 InputStream (java.io.InputStream)11 LockException (org.exist.util.LockException)11 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)9 Lock (org.exist.storage.lock.Lock)9 TransactionManager (org.exist.storage.txn.TransactionManager)9