Search in sources :

Example 11 with LockManager

use of org.exist.storage.lock.LockManager in project exist by eXist-db.

the class ExtCollection method toSequence.

private Sequence toSequence(final DocumentSet docs) throws XPathException {
    final Sequence result = new ValueSequence();
    final LockManager lockManager = context.getBroker().getBrokerPool().getLockManager();
    for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
        final DocumentImpl doc = i.next();
        // filter out binary documents, fn:collection should only return XML documents
        if (doc.getResourceType() == DocumentImpl.XML_FILE) {
            ManagedDocumentLock dlock = null;
            try {
                if (!context.inProtectedMode()) {
                    dlock = lockManager.acquireDocumentReadLock(doc.getURI());
                }
                result.add(new NodeProxy(doc));
            } catch (final LockException e) {
                throw new XPathException(this, ErrorCodes.FODC0002, e);
            } finally {
                if (dlock != null) {
                    dlock.close();
                }
            }
        }
    }
    return result;
}
Also used : LockManager(org.exist.storage.lock.LockManager) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockException(org.exist.util.LockException)

Example 12 with LockManager

use of org.exist.storage.lock.LockManager in project exist by eXist-db.

the class InMemoryOutputStream method stream.

public void stream(final XmldbURL xmldbURL, final InputStream is, @Deprecated final int length) throws IOException {
    BrokerPool db;
    try {
        db = BrokerPool.getInstance();
    } catch (EXistException e) {
        throw new IOException(e);
    }
    try (final DBBroker broker = db.getBroker()) {
        final XmldbURI collectionUri = XmldbURI.create(xmldbURL.getCollection());
        final XmldbURI documentUri = XmldbURI.create(xmldbURL.getDocumentName());
        final TransactionManager transact = db.getTransactionManager();
        try (final Txn txn = transact.beginTransaction();
            final Collection collection = broker.getOrCreateCollection(txn, collectionUri)) {
            if (collection == null) {
                throw new IOException("Resource " + collectionUri.toString() + " is not a collection.");
            }
            final LockManager lockManager = db.getLockManager();
            txn.acquireCollectionLock(() -> lockManager.acquireCollectionWriteLock(collectionUri));
            if (collection.hasChildCollection(broker, documentUri)) {
                throw new IOException("Resource " + documentUri.toString() + " is a collection.");
            }
            try (final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) broker.getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), is);
                final CachingFilterInputStream cfis = new CachingFilterInputStream(cache)) {
                final MimeType mime = MimeTable.getInstance().getContentTypeFor(documentUri);
                try (final ManagedDocumentLock lock = lockManager.acquireDocumentWriteLock(documentUri)) {
                    broker.storeDocument(txn, documentUri, new CachingFilterInputStreamInputSource(cfis), mime, collection);
                }
            }
            txn.commit();
        }
    } catch (final IOException ex) {
        LOG.debug(ex);
        throw ex;
    } catch (final Exception ex) {
        LOG.debug(ex);
        throw new IOException(ex.getMessage(), ex);
    }
}
Also used : EXistException(org.exist.EXistException) IOException(java.io.IOException) Txn(org.exist.storage.txn.Txn) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache) CachingFilterInputStreamInputSource(org.exist.util.CachingFilterInputStreamInputSource) MimeType(org.exist.util.MimeType) IOException(java.io.IOException) EXistException(org.exist.EXistException) LockManager(org.exist.storage.lock.LockManager) DBBroker(org.exist.storage.DBBroker) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Example 13 with LockManager

use of org.exist.storage.lock.LockManager in project exist by eXist-db.

the class NewArrayNodeSet method lock.

@Override
public ManagedLocks<ManagedDocumentLock> lock(final DBBroker broker, final boolean exclusive) throws LockException {
    sort();
    final LockManager lockManager = broker.getBrokerPool().getLockManager();
    final ManagedDocumentLock[] managedDocumentLocks = new ManagedDocumentLock[documentCount];
    try {
        for (int idx = 0; idx < documentCount; idx++) {
            final DocumentImpl doc = nodes[documentNodesOffset[idx]].getOwnerDocument();
            final ManagedDocumentLock managedDocumentLock;
            if (exclusive) {
                managedDocumentLock = lockManager.acquireDocumentWriteLock(doc.getURI());
            } else {
                managedDocumentLock = lockManager.acquireDocumentReadLock(doc.getURI());
            }
            managedDocumentLocks[idx] = managedDocumentLock;
        }
        return new ManagedLocks<>(managedDocumentLocks);
    } catch (final LockException e) {
        // unlock any previously locked documents
        new ManagedLocks<>(managedDocumentLocks).close();
        throw e;
    }
}
Also used : ManagedLocks(org.exist.collections.ManagedLocks) LockManager(org.exist.storage.lock.LockManager) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockException(org.exist.util.LockException)

Example 14 with LockManager

use of org.exist.storage.lock.LockManager in project exist by eXist-db.

the class ExtArrayNodeSet method lock.

@Override
public ManagedLocks<ManagedDocumentLock> lock(final DBBroker broker, final boolean exclusive) throws LockException {
    final LockManager lockManager = broker.getBrokerPool().getLockManager();
    final ManagedDocumentLock[] managedDocumentLocks = new ManagedDocumentLock[partCount];
    try {
        for (int i = 0; i < partCount; i++) {
            final DocumentImpl doc = parts[i].getOwnerDocument();
            final ManagedDocumentLock docLock;
            if (exclusive) {
                docLock = lockManager.acquireDocumentWriteLock(doc.getURI());
            } else {
                docLock = lockManager.acquireDocumentReadLock(doc.getURI());
            }
            managedDocumentLocks[i] = docLock;
        }
        return new ManagedLocks<>(managedDocumentLocks);
    } catch (final LockException e) {
        // unlock any previously locked documents
        new ManagedLocks<>(managedDocumentLocks).close();
        throw e;
    }
}
Also used : ManagedLocks(org.exist.collections.ManagedLocks) LockManager(org.exist.storage.lock.LockManager) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockException(org.exist.util.LockException)

Aggregations

LockManager (org.exist.storage.lock.LockManager)14 ManagedDocumentLock (org.exist.storage.lock.ManagedDocumentLock)9 DBBroker (org.exist.storage.DBBroker)6 XmldbURI (org.exist.xmldb.XmldbURI)6 EXistException (org.exist.EXistException)4 Collection (org.exist.collections.Collection)4 Txn (org.exist.storage.txn.Txn)4 LockException (org.exist.util.LockException)4 IOException (java.io.IOException)3 ManagedLocks (org.exist.collections.ManagedLocks)3 URISyntaxException (java.net.URISyntaxException)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 HttpSession (javax.servlet.http.HttpSession)2 HttpSessionEvent (javax.servlet.http.HttpSessionEvent)2 LockedDocument (org.exist.dom.persistent.LockedDocument)2 TransactionException (org.exist.storage.txn.TransactionException)2 Test (org.junit.Test)2 AtomicFSM (com.evolvedbinary.j8fu.fsm.AtomicFSM)1 FSM (com.evolvedbinary.j8fu.fsm.FSM)1 TransitionTable.transitionTable (com.evolvedbinary.j8fu.fsm.TransitionTable.transitionTable)1