Search in sources :

Example 6 with ManagedDocumentLock

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

the class Txn method acquireDocumentLock.

public void acquireDocumentLock(final SupplierE<ManagedDocumentLock, LockException> fnLockAcquire) throws LockException {
    final ManagedDocumentLock lock = fnLockAcquire.get();
    locksHeld.add(new LockInfo(lock, lock::close));
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock)

Example 7 with ManagedDocumentLock

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

the class RpcConnection method listDocumentPermissions.

private Map<String, List> listDocumentPermissions(final XmldbURI collUri) throws EXistException, PermissionDeniedException {
    return this.<Map<String, List>>readCollection(collUri).apply((collection, broker, transaction) -> {
        final Map<String, List> result = new HashMap<>(collection.getDocumentCount(broker));
        for (final Iterator<DocumentImpl> i = collection.iterator(broker); i.hasNext(); ) {
            final DocumentImpl doc = i.next();
            try (final ManagedDocumentLock documentLock = broker.getBrokerPool().getLockManager().acquireDocumentReadLock(doc.getURI())) {
                final Permission perm = doc.getPermissions();
                result.put(doc.getFileURI().toString(), toList(perm));
            }
        }
        return result;
    });
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) ACLPermission(org.exist.security.ACLPermission) Permission(org.exist.security.Permission)

Example 8 with ManagedDocumentLock

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

the class RpcConnection method storeBinary.

private boolean storeBinary(final byte[] data, final XmldbURI docUri, final String mimeType, final int overwrite, final Date created, final Date modified) throws EXistException, PermissionDeniedException {
    return this.<Boolean>writeCollection(docUri.removeLastSegment()).apply((collection, broker, transaction) -> {
        // keep a write lock in the transaction
        transaction.acquireCollectionLock(() -> broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collection.getURI()));
        try (final ManagedDocumentLock lockedDocument = broker.getBrokerPool().getLockManager().acquireDocumentWriteLock(docUri)) {
            if (overwrite == 0) {
                // NOTE: we have the document write lock above
                final DocumentImpl old = collection.getDocument(broker, docUri.lastSegment());
                if (old != null) {
                    // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                    collection.close();
                    throw new PermissionDeniedException("Old document exists and overwrite is not allowed");
                }
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Storing binary resource to collection {}", collection.getURI());
            }
            broker.storeDocument(transaction, docUri.lastSegment(), new StringInputSource(data), MimeTable.getInstance().getContentType(mimeType), created, modified, null, null, null, collection);
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            return true;
        }
    });
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 9 with ManagedDocumentLock

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

the class RpcConnection method parse.

private boolean parse(final byte[] xml, final XmldbURI docUri, final int overwrite, @Nullable final Date created, @Nullable final Date modified) throws EXistException, PermissionDeniedException {
    return this.<Boolean>writeCollection(docUri.removeLastSegment()).apply((collection, broker, transaction) -> {
        try (final ManagedDocumentLock lockedDocument = broker.getBrokerPool().getLockManager().acquireDocumentWriteLock(docUri)) {
            if (overwrite == 0) {
                // NOTE: we have the document write lock above
                final DocumentImpl old = collection.getDocument(broker, docUri.lastSegment());
                if (old != null) {
                    // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                    collection.close();
                    throw new PermissionDeniedException("Document exists and overwrite is not allowed");
                }
            }
            final InputSource source = new StringInputSource(xml);
            final long startTime = System.currentTimeMillis();
            final MimeType mime = MimeTable.getInstance().getContentTypeFor(docUri.lastSegment());
            broker.storeDocument(transaction, docUri.lastSegment(), source, mime, created, modified, null, null, null, collection);
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            if (LOG.isDebugEnabled()) {
                LOG.debug("parsing {} took {}ms.", docUri, System.currentTimeMillis() - startTime);
            }
            return true;
        }
    });
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) InputSource(org.xml.sax.InputSource) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 10 with ManagedDocumentLock

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

the class DocumentNameOrId method getResourceByAbsoluteId.

private Sequence getResourceByAbsoluteId(final IntegerValue ivAbsoluteId) throws XPathException, PermissionDeniedException, LockException, IOException {
    final BigInteger absoluteId = ivAbsoluteId.toJavaObject(BigInteger.class);
    final Tuple3<Integer, Integer, Byte> decoded = decodeAbsoluteResourceId(absoluteId);
    final DocumentImpl doc = context.getBroker().getResourceById(decoded._1, decoded._3, decoded._2);
    if (doc != null) {
        try (final ManagedDocumentLock docLock = context.getBroker().getBrokerPool().getLockManager().acquireDocumentReadLock(doc.getURI())) {
            if (doc instanceof BinaryDocument) {
                final BinaryDocument bin = (BinaryDocument) doc;
                final InputStream is = context.getBroker().getBinaryResource(bin);
                return Base64BinaryDocument.getInstance(context, is);
            } else {
                return new NodeProxy(doc);
            }
        }
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : BigInteger(java.math.BigInteger) BinaryDocument(org.exist.dom.persistent.BinaryDocument) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) InputStream(java.io.InputStream) BigInteger(java.math.BigInteger) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy)

Aggregations

ManagedDocumentLock (org.exist.storage.lock.ManagedDocumentLock)23 LockManager (org.exist.storage.lock.LockManager)10 LockException (org.exist.util.LockException)7 EXistException (org.exist.EXistException)6 URISyntaxException (java.net.URISyntaxException)5 DocumentImpl (org.exist.dom.persistent.DocumentImpl)5 XmldbURI (org.exist.xmldb.XmldbURI)5 ManagedLocks (org.exist.collections.ManagedLocks)4 PermissionDeniedException (org.exist.security.PermissionDeniedException)4 DBBroker (org.exist.storage.DBBroker)4 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)3 Nullable (javax.annotation.Nullable)3 Collection (org.exist.collections.Collection)3 TriggerException (org.exist.collections.triggers.TriggerException)3 LockedDocument (org.exist.dom.persistent.LockedDocument)3 ACLPermission (org.exist.security.ACLPermission)3 Permission (org.exist.security.Permission)3 BrokerPool (org.exist.storage.BrokerPool)3 Lock (org.exist.storage.lock.Lock)3 ManagedCollectionLock (org.exist.storage.lock.ManagedCollectionLock)3