Search in sources :

Example 1 with ManagedDocumentLock

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

the class NodeProxy method lock.

@Override
public ManagedLocks<ManagedDocumentLock> lock(final DBBroker broker, final boolean exclusive) throws LockException {
    final LockManager lockManager = broker.getBrokerPool().getLockManager();
    final ManagedDocumentLock docLock;
    if (exclusive) {
        docLock = lockManager.acquireDocumentWriteLock(doc.getURI());
    } else {
        docLock = lockManager.acquireDocumentReadLock(doc.getURI());
    }
    return new ManagedLocks<>(docLock);
}
Also used : ManagedLocks(org.exist.collections.ManagedLocks) LockManager(org.exist.storage.lock.LockManager) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock)

Example 2 with ManagedDocumentLock

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

the class LockFunction method eval.

/* (non-Javadoc)
     * @see org.exist.xquery.Function#eval(org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
     */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    final Sequence docsArg = getArgument(0).eval(contextSequence, contextItem);
    final DocumentSet docs = docsArg.getDocumentSet();
    try (final ManagedLocks<ManagedDocumentLock> managedLocks = docs.lock(context.getBroker(), exclusive)) {
        return getArgument(1).eval(contextSequence, contextItem);
    } catch (final LockException e) {
        throw new XPathException(this, "Could not lock document set", e);
    }
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockException(org.exist.util.LockException) Sequence(org.exist.xquery.value.Sequence) DocumentSet(org.exist.dom.persistent.DocumentSet)

Example 3 with ManagedDocumentLock

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

the class LocalUserManagementService method listResourcePermissions.

@Override
public Permission[] listResourcePermissions() throws XMLDBException {
    final XmldbURI collectionUri = collection.getPathURI();
    return this.<Permission[]>read(collectionUri).apply((collection, broker, transaction) -> {
        if (!collection.getPermissionsNoLock().validate(user, Permission.READ)) {
            return new Permission[0];
        }
        final Permission[] perms = new Permission[collection.getDocumentCount(broker)];
        final Iterator<DocumentImpl> itDocument = collection.iterator(broker);
        int i = 0;
        while (itDocument.hasNext()) {
            final DocumentImpl document = itDocument.next();
            try (final ManagedDocumentLock documentLock = broker.getBrokerPool().getLockManager().acquireDocumentReadLock(document.getURI())) {
                perms[i++] = document.getPermissions();
            }
        }
        return perms;
    });
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 4 with ManagedDocumentLock

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

the class LocalCollectionManagementService method moveResource.

@Override
public void moveResource(final XmldbURI src, final XmldbURI dest, final XmldbURI name) throws XMLDBException {
    final XmldbURI srcPath = resolve(src);
    final XmldbURI destPath = dest == null ? srcPath.removeLastSegment() : resolve(dest);
    final XmldbURI newName;
    if (name == null) {
        newName = srcPath.lastSegment();
    } else {
        newName = name;
    }
    withDb((broker, transaction) -> modify(broker, transaction, srcPath.removeLastSegment()).apply((sourceCol, b1, t1) -> {
        try (final LockedDocument lockedSource = sourceCol.getDocumentWithLock(b1, srcPath.lastSegment(), Lock.LockMode.WRITE_LOCK)) {
            final DocumentImpl source = lockedSource == null ? null : lockedSource.getDocument();
            if (source == null) {
                // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                sourceCol.close();
                throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE, "Resource " + srcPath + " not found");
            }
            return modify(b1, t1, destPath).apply((destinationCol, b2, t2) -> {
                try (final ManagedDocumentLock lockedDestination = b2.getBrokerPool().getLockManager().acquireDocumentWriteLock(destinationCol.getURI().append(newName))) {
                    b2.moveResource(t2, source, destinationCol, newName);
                    // NOTE: early release of Collection locks inline with Asymmetrical Locking scheme
                    destinationCol.close();
                    sourceCol.close();
                }
                return null;
            });
        }
    }));
}
Also used : XMLDBException(org.xmldb.api.base.XMLDBException) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockedDocument(org.exist.dom.persistent.LockedDocument) BrokerPool(org.exist.storage.BrokerPool) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock) PreserveType(org.exist.storage.DBBroker.PreserveType) LockException(org.exist.util.LockException) Subject(org.exist.security.Subject) Optional(java.util.Optional) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) TriggerException(org.exist.collections.triggers.TriggerException) ErrorCodes(org.xmldb.api.base.ErrorCodes) Lock(org.exist.storage.lock.Lock) Nullable(javax.annotation.Nullable) Collection(org.xmldb.api.base.Collection) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockedDocument(org.exist.dom.persistent.LockedDocument) XMLDBException(org.xmldb.api.base.XMLDBException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 5 with ManagedDocumentLock

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

the class LocalCollection method storeXMLResource.

private void storeXMLResource(final LocalXMLResource res) throws XMLDBException {
    final XmldbURI resURI;
    try {
        resURI = XmldbURI.xmldbUriFor(res.getId());
    } catch (final URISyntaxException e) {
        throw new XMLDBException(ErrorCodes.INVALID_URI, e);
    }
    modify().apply((collection, broker, transaction) -> {
        String uri = null;
        if (res.file != null) {
            uri = res.file.toUri().toASCIIString();
        }
        try (final ManagedDocumentLock documentLock = broker.getBrokerPool().getLockManager().acquireDocumentWriteLock(collection.getURI().append(resURI))) {
            final String strMimeType = res.getMimeType(broker, transaction);
            final MimeType mimeType = strMimeType != null ? MimeTable.getInstance().getContentType(strMimeType) : null;
            if (res.root != null) {
                collection.storeDocument(transaction, broker, resURI, res.root, mimeType, res.datecreated, res.datemodified, null, null, null);
            } else {
                final InputSource source;
                if (uri != null) {
                    source = new InputSource(uri);
                } else if (res.inputSource != null) {
                    source = res.inputSource;
                } else {
                    source = new StringInputSource(res.content);
                }
                final XMLReader reader;
                if (useHtmlReader(broker, transaction, res)) {
                    reader = getHtmlReader();
                } else {
                    reader = null;
                }
                broker.storeDocument(transaction, resURI, source, mimeType, res.datecreated, res.datemodified, null, null, reader, collection);
            }
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            return null;
        } catch (final EXistException | SAXException e) {
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
        }
    });
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) InputSource(org.xml.sax.InputSource) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

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