Search in sources :

Example 36 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class RawNodeIterator method seek.

@Override
public final void seek(final NodeHandle node) throws IOException {
    try (final ManagedLock<ReentrantLock> domFileLock = lockManager.acquireBtreeReadLock(db.getLockName())) {
        RecordPos rec = null;
        if (StorageAddress.hasAddress(node.getInternalAddress())) {
            rec = db.findRecord(node.getInternalAddress());
        }
        if (rec == null) {
            try {
                final long address = db.findValue(broker, new NodeProxy(node));
                if (address == BTree.KEY_NOT_FOUND) {
                    throw new IOException("Node not found.");
                }
                rec = db.findRecord(address);
            } catch (final BTreeException e) {
                throw new IOException("Node not found: " + e.getMessage());
            }
        }
        pageNum = rec.getPage().getPageNum();
        // Position the stream at the very beginning of the record
        offset = rec.offset - DOMFile.LENGTH_TID;
        page = rec.getPage();
    } catch (final LockException e) {
        throw new IOException("Exception while scanning document: " + e.getMessage());
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) BTreeException(org.exist.storage.btree.BTreeException) LockException(org.exist.util.LockException) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 37 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class ExistDocument method resourceCopyMove.

/**
 * Copy document or collection in database.
 */
void resourceCopyMove(XmldbURI destCollectionUri, String newName, Mode mode) throws EXistException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("{} {} to {} named {}", String.valueOf(mode), xmldbUri, destCollectionUri, newName);
    }
    XmldbURI newNameUri = null;
    try {
        newNameUri = XmldbURI.xmldbUriFor(newName);
    } catch (URISyntaxException ex) {
        LOG.error(ex);
        throw new EXistException(ex.getMessage());
    }
    // use WRITE_LOCK if moving or if src and dest collection are the same
    final LockMode srcCollectionLockMode = mode == Mode.MOVE || destCollectionUri.equals(xmldbUri.removeLastSegment()) ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
    DocumentImpl srcDocument = null;
    // Need to split path into collection and document name
    final XmldbURI srcCollectionUri = xmldbUri.removeLastSegment();
    final XmldbURI srdDocumentUri = xmldbUri.lastSegment();
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection srcCollection = broker.openCollection(srcCollectionUri, srcCollectionLockMode)) {
        // Open collection if possible, else abort
        if (srcCollection == null) {
            txnManager.abort(txn);
            // TODO throw
            return;
        }
        // Open document if possible, else abort
        srcDocument = srcCollection.getDocument(broker, srdDocumentUri);
        if (srcDocument == null) {
            LOG.debug("No resource found for path: {}", xmldbUri);
            txnManager.abort(txn);
            return;
        }
        // Open collection if possible, else abort
        try (final Collection destCollection = broker.openCollection(destCollectionUri, LockMode.WRITE_LOCK)) {
            if (destCollection == null) {
                LOG.debug("Destination collection {} does not exist.", xmldbUri);
                txnManager.abort(txn);
                return;
            }
            // Perform actial move/copy
            if (mode == Mode.COPY) {
                broker.copyResource(txn, srcDocument, destCollection, newNameUri);
            } else {
                broker.moveResource(txn, srcDocument, destCollection, newNameUri);
            }
            // Commit change
            txnManager.commit(txn);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Document {}d successfully", mode);
            }
        }
    } catch (LockException e) {
        LOG.error("Resource is locked.", e);
        throw new EXistException(e.getMessage());
    } catch (EXistException e) {
        LOG.error(e);
        throw e;
    } catch (IOException | PermissionDeniedException | TriggerException e) {
        LOG.error(e);
        throw new EXistException(e.getMessage());
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished {}", mode);
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) LockMode(org.exist.storage.lock.Lock.LockMode) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) DBBroker(org.exist.storage.DBBroker) LockException(org.exist.util.LockException) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 38 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class NativeStructuralIndexWorker method removeQNamesForDoc.

protected void removeQNamesForDoc(DocumentImpl doc) {
    final byte[] fromKey = computeDocKey(doc.getDocId());
    final byte[] toKey = computeDocKey(doc.getDocId() + 1);
    final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
    try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
        index.btree.remove(query, null);
    } catch (final LockException e) {
        NativeStructuralIndex.LOG.warn("Failed to lock structural index: {}", e.getMessage(), e);
    } catch (final Exception e) {
        NativeStructuralIndex.LOG.warn("Exception caught while reading structural index for document {}: {}", doc.getURI(), e.getMessage(), e);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) LockException(org.exist.util.LockException) Value(org.exist.storage.btree.Value) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException)

Example 39 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class NativeStructuralIndexWorker method removeCollection.

@Override
public void removeCollection(Collection collection, DBBroker broker, boolean reindex) throws PermissionDeniedException {
    try {
        for (final Iterator<DocumentImpl> i = collection.iterator(broker); i.hasNext(); ) {
            final DocumentImpl doc = i.next();
            removeDocument(doc);
        }
    } catch (final LockException e) {
        LOG.error(e);
    }
}
Also used : LockException(org.exist.util.LockException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 40 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class NativeStructuralIndexWorker method findDescendantsByTagName.

public NodeSet findDescendantsByTagName(byte type, QName qname, int axis, DocumentSet docs, NodeSet contextSet, int contextId, Expression parent) {
    final NewArrayNodeSet result = new NewArrayNodeSet();
    final FindDescendantsCallback callback = new FindDescendantsCallback(type, axis, qname, contextId, result, parent);
    try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
        for (final NodeProxy ancestor : contextSet) {
            final DocumentImpl doc = ancestor.getOwnerDocument();
            final NodeId ancestorId = ancestor.getNodeId();
            callback.setAncestor(doc, ancestor);
            final byte[] fromKey;
            final byte[] toKey;
            if (ancestorId == NodeId.DOCUMENT_NODE) {
                fromKey = computeKey(type, qname, doc.getDocId());
                toKey = computeKey(type, qname, doc.getDocId() + 1);
            } else {
                fromKey = computeKey(type, qname, doc.getDocId(), ancestorId);
                toKey = computeKey(type, qname, doc.getDocId(), ancestorId.nextSibling());
            }
            final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
            try {
                index.btree.query(query, callback);
            } catch (final Exception e) {
                NativeStructuralIndex.LOG.error("Error while searching structural index: {}", e.getMessage(), e);
            }
        }
    } catch (final LockException e) {
        NativeStructuralIndex.LOG.warn("Lock problem while searching structural index: {}", e.getMessage(), e);
    }
    result.updateNoSort();
    return result;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) IndexQuery(org.exist.storage.btree.IndexQuery) LockException(org.exist.util.LockException) NodeId(org.exist.numbering.NodeId) Value(org.exist.storage.btree.Value) NodeProxy(org.exist.dom.persistent.NodeProxy) DocumentImpl(org.exist.dom.persistent.DocumentImpl) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException)

Aggregations

LockException (org.exist.util.LockException)72 PermissionDeniedException (org.exist.security.PermissionDeniedException)44 EXistException (org.exist.EXistException)29 DocumentImpl (org.exist.dom.persistent.DocumentImpl)26 XmldbURI (org.exist.xmldb.XmldbURI)26 ReentrantLock (java.util.concurrent.locks.ReentrantLock)19 Collection (org.exist.collections.Collection)16 IOException (java.io.IOException)12 Tuple3 (com.evolvedbinary.j8fu.tuple.Tuple3)11 LockedDocument (org.exist.dom.persistent.LockedDocument)11 Value (org.exist.storage.btree.Value)11 Txn (org.exist.storage.txn.Txn)11 TriggerException (org.exist.collections.triggers.TriggerException)10 NodeProxy (org.exist.dom.persistent.NodeProxy)10 DatabaseConfigurationException (org.exist.util.DatabaseConfigurationException)10 QName (org.exist.dom.QName)9 LockMode (org.exist.storage.lock.Lock.LockMode)9 XPathException (org.exist.xquery.XPathException)9 SAXException (org.xml.sax.SAXException)9 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)8