Search in sources :

Example 26 with LockedDocument

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

the class LocalIndexQueryService method reindexDocument.

private void reindexDocument(final XmldbURI col, final String docName) throws XMLDBException {
    final XmldbURI collectionPath = resolve(col);
    withDb((broker, transaction) -> {
        try (final LockedDocument lockedDoc = broker.getXMLResource(collectionPath.append(docName), LockMode.READ_LOCK)) {
            if (lockedDoc != null) {
                broker.reindexXMLResource(transaction, lockedDoc.getDocument(), DBBroker.IndexMode.STORE);
                broker.sync(Sync.MAJOR);
            }
            return null;
        }
    });
}
Also used : LockedDocument(org.exist.dom.persistent.LockedDocument)

Example 27 with LockedDocument

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

the class DocUtils method getDocumentByPathFromDB.

private static Sequence getDocumentByPathFromDB(final XQueryContext context, final String path) throws XPathException, PermissionDeniedException {
    // check if the loaded documents should remain locked
    final LockMode lockType = context.lockDocumentsOnLoad() ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
    try {
        final XmldbURI baseURI = context.getBaseURI().toXmldbURI();
        final XmldbURI pathUri;
        if (baseURI != null && !(baseURI.equals("") || baseURI.equals("/db"))) {
            // relative collection Path: add the current base URI
            pathUri = baseURI.resolveCollectionPath(XmldbURI.xmldbUriFor(path, false));
        } else {
            pathUri = XmldbURI.xmldbUriFor(path, false);
        }
        // relative collection Path: add the current module call URI if applicable
        final XmldbURI resourceUri = Optional.ofNullable(context.getModuleLoadPath()).filter(moduleLoadPath -> !moduleLoadPath.isEmpty()).flatMap(moduleLoadPath -> Try(() -> XmldbURI.xmldbUriFor(moduleLoadPath)).toOption()).map(moduleLoadPath -> moduleLoadPath.resolveCollectionPath(pathUri)).orElse(pathUri);
        // try to open the document and acquire a lock
        try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(resourceUri, lockType)) {
            if (lockedDoc == null) {
                return Sequence.EMPTY_SEQUENCE;
            } else {
                final DocumentImpl doc = lockedDoc.getDocument();
                if (!doc.getPermissions().validate(context.getSubject(), Permission.READ)) {
                    throw new PermissionDeniedException("Insufficient privileges to read resource " + path);
                }
                if (doc.getResourceType() == DocumentImpl.BINARY_FILE) {
                    throw new XPathException("Document " + path + " is a binary resource, not an XML document. Please consider using the function util:binary-doc() to retrieve a reference to it.");
                }
                return new NodeProxy(doc);
            }
        }
    } catch (final URISyntaxException e) {
        throw new XPathException(e);
    }
}
Also used : LockMode(org.exist.storage.lock.Lock.LockMode) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) BrokerPool(org.exist.storage.BrokerPool) XMLReaderPool(org.exist.util.XMLReaderPool) ErrorCodes(org.exist.xquery.ErrorCodes) NodeProxy(org.exist.dom.persistent.NodeProxy) PermissionDeniedException(org.exist.security.PermissionDeniedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) XMLReader(org.xml.sax.XMLReader) Source(org.exist.source.Source) java.net(java.net) Namespaces(org.exist.Namespaces) Document(org.w3c.dom.Document) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) Permission(org.exist.security.Permission) XQueryContext(org.exist.xquery.XQueryContext) Nullable(javax.annotation.Nullable) InputSource(org.xml.sax.InputSource) LockedDocument(org.exist.dom.persistent.LockedDocument) AnyURIValue(org.exist.xquery.value.AnyURIValue) IOException(java.io.IOException) Try(com.evolvedbinary.j8fu.Try.Try) URLSource(org.exist.source.URLSource) SAXException(org.xml.sax.SAXException) SourceFactory(org.exist.source.SourceFactory) Optional(java.util.Optional) Sequence(org.exist.xquery.value.Sequence) Pattern(java.util.regex.Pattern) SAXAdapter(org.exist.dom.memtree.SAXAdapter) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockMode(org.exist.storage.lock.Lock.LockMode) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) XmldbURI(org.exist.xmldb.XmldbURI)

Example 28 with LockedDocument

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

the class RemoveCollectionTest method recover.

public void recover(final BrokerPool pool, final boolean checkResource) throws EXistException, PermissionDeniedException, DatabaseConfigurationException, IOException {
    LockedDocument lockedDoc = null;
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        if (checkResource) {
            lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI.append("hamlet.xml"), LockMode.READ_LOCK);
            assertNull("Resource should have been removed", lockedDoc);
        }
    } finally {
        if (lockedDoc != null) {
            lockedDoc.close();
        }
    }
}
Also used : LockedDocument(org.exist.dom.persistent.LockedDocument)

Example 29 with LockedDocument

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

the class ConcurrentBrokerPoolTest method validateStoredDoc.

private void validateStoredDoc(final Tuple2<Path, UUID> pathUuid) throws EXistException, IOException, DatabaseConfigurationException, PermissionDeniedException, URISyntaxException {
    final Path dataDir = pathUuid._1;
    assertTrue(Files.exists(dataDir));
    final UUID uuid = pathUuid._2;
    final Properties config = new Properties();
    config.put(BrokerPool.PROPERTY_DATA_DIR, dataDir);
    config.put(Journal.PROPERTY_RECOVERY_JOURNAL_DIR, dataDir);
    final ExistEmbeddedServer server = new ExistEmbeddedServer("validate-" + uuid.toString(), getConfigFile(getClass()), config, true, false);
    server.startDb();
    try {
        try (final DBBroker broker = server.getBrokerPool().getBroker()) {
            try (final LockedDocument doc = broker.getXMLResource(XmldbURI.DB.append(docName(uuid)), Lock.LockMode.READ_LOCK)) {
                assertNotNull(doc);
                final Source expected = Input.fromString(docContent(uuid)).build();
                final Source actual = Input.fromNode(doc.getDocument()).build();
                final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
                // ASSERT
                assertFalse(diff.toString(), diff.hasDifferences());
            }
        }
    } finally {
        server.stopDb();
        // clear temp files
        FileUtils.deleteQuietly(dataDir);
    }
}
Also used : Path(java.nio.file.Path) Diff(org.xmlunit.diff.Diff) ExistEmbeddedServer(org.exist.test.ExistEmbeddedServer) LockedDocument(org.exist.dom.persistent.LockedDocument) UUID(java.util.UUID) Properties(java.util.Properties) Source(javax.xml.transform.Source)

Example 30 with LockedDocument

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

the class SourceFactoryTest method getSourceFromXmldb.

@Test
public void getSourceFromXmldb() throws IOException, PermissionDeniedException {
    final String contextPath = "xmldb:exist:///db";
    final String location = "library.xqm";
    final DBBroker mockBroker = createMock(DBBroker.class);
    final LockedDocument mockLockedDoc = createMock(LockedDocument.class);
    final BinaryDocument mockBinDoc = createMock(BinaryDocument.class);
    expect(mockBroker.getXMLResource(anyObject(), anyObject())).andReturn(mockLockedDoc);
    expect(mockLockedDoc.getDocument()).andReturn(mockBinDoc);
    expect(mockBinDoc.getResourceType()).andReturn(BinaryDocument.BINARY_FILE);
    expect(mockBinDoc.getURI()).andReturn(XmldbURI.create(contextPath).append(location)).times(2);
    expect(mockBinDoc.getLastModified()).andReturn(123456789l);
    /*expect*/
    mockLockedDoc.close();
    replay(mockBroker, mockLockedDoc, mockBinDoc);
    final Source libSource = SourceFactory.getSource(mockBroker, contextPath, location, false);
    assertTrue(libSource instanceof DBSource);
    assertEquals(XmldbURI.create(contextPath).append(location), ((DBSource) libSource).getDocumentPath());
    verify(mockBroker, mockLockedDoc, mockBinDoc);
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Test(org.junit.Test)

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