Search in sources :

Example 21 with LockException

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

the class NativeStructuralIndexWorker method removeSome.

protected void removeSome() {
    if (pending.size() == 0) {
        return;
    }
    try {
        for (final Map.Entry<QName, List<NodeProxy>> entry : pending.entrySet()) {
            final QName qname = entry.getKey();
            try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
                final List<NodeProxy> nodes = entry.getValue();
                for (final NodeProxy proxy : nodes) {
                    final NodeId nodeId = proxy.getNodeId();
                    final byte[] key = computeKey(qname.getNameType(), qname, document.getDocId(), nodeId);
                    index.btree.removeValue(new Value(key));
                }
            } 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 writing to structural index: {}", e.getMessage(), e);
            }
        }
    } finally {
        pending.clear();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) QName(org.exist.dom.QName) NodeProxy(org.exist.dom.persistent.NodeProxy) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) LockException(org.exist.util.LockException) NodeId(org.exist.numbering.NodeId) Value(org.exist.storage.btree.Value) NodeList(org.w3c.dom.NodeList)

Example 22 with LockException

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

the class NativeStructuralIndexWorker method scanIndex.

/**
 * Collect index statistics. Used by functions like util:index-keys.
 *
 * @param context the xquery context
 * @param docs The documents to which the index entries belong
 * @param contextSet ignored by this index
 * @param hints Some "hints" for retrieving the index entries. See such hints in
 * {@link org.exist.indexing.OrderedValuesIndex} and {@link org.exist.indexing.QNamedKeysIndex}.
 * @return the matching occurrences
 */
public Occurrences[] scanIndex(XQueryContext context, DocumentSet docs, NodeSet contextSet, Map hints) {
    final Map<String, Occurrences> occurrences = new TreeMap<>();
    for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
        final DocumentImpl doc = i.next();
        final List<QName> qnames = getQNamesForDoc(doc);
        for (final QName qname : qnames) {
            final String name;
            if (qname.getNameType() == ElementValue.ATTRIBUTE) {
                name = "@" + qname.getLocalPart();
            } else {
                name = qname.getLocalPart();
            }
            final byte[] fromKey = computeKey(qname.getNameType(), qname, doc.getDocId());
            final byte[] toKey = computeKey(qname.getNameType(), qname, doc.getDocId() + 1);
            final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
            try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
                index.btree.query(query, (value, pointer) -> {
                    Occurrences oc = occurrences.get(name);
                    if (oc == null) {
                        oc = new Occurrences(name);
                        occurrences.put(name, oc);
                        oc.addDocument(doc);
                        oc.addOccurrences(1);
                    } else {
                        oc.addOccurrences(1);
                        oc.addDocument(doc);
                    }
                    return true;
                });
            } 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);
            }
        }
    }
    final Occurrences[] result = new Occurrences[occurrences.size()];
    int i = 0;
    for (Occurrences occ : occurrences.values()) {
        result[i++] = occ;
    }
    return result;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) QName(org.exist.dom.QName) Occurrences(org.exist.util.Occurrences) DocumentImpl(org.exist.dom.persistent.DocumentImpl) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) LockException(org.exist.util.LockException) Value(org.exist.storage.btree.Value)

Example 23 with LockException

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

the class NativeStructuralIndexWorker method processPending.

/**
 * Process the map of pending entries and store them into the btree.
 */
private void processPending() {
    if (pending.size() == 0 || index.btree == null) {
        return;
    }
    try {
        for (final Map.Entry<QName, List<NodeProxy>> entry : pending.entrySet()) {
            final QName qname = entry.getKey();
            try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
                final List<NodeProxy> nodes = entry.getValue();
                for (final NodeProxy proxy : nodes) {
                    final NodeId nodeId = proxy.getNodeId();
                    final byte[] key = computeKey(qname.getNameType(), qname, document.getDocId(), nodeId);
                    index.btree.addValue(new Value(key), computeValue(proxy));
                }
                final Value docKey = new Value(computeDocKey(qname.getNameType(), document.getDocId(), qname));
                if (index.btree.findValue(docKey) == -1) {
                    index.btree.addValue(docKey, 0);
                }
            } catch (final LockException e) {
                NativeStructuralIndex.LOG.warn("Failed to lock structural index: {}", e.getMessage(), e);
            // } catch (ReadOnlyException e) {
            // NativeStructuralIndex.LOG.warn("Read-only error: " + e.getMessage(), e);
            } catch (final Exception e) {
                NativeStructuralIndex.LOG.warn("Exception caught while writing to structural index: {}", e.getMessage(), e);
            }
        }
    } finally {
        pending.clear();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) QName(org.exist.dom.QName) NodeProxy(org.exist.dom.persistent.NodeProxy) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) LockException(org.exist.util.LockException) NodeId(org.exist.numbering.NodeId) Value(org.exist.storage.btree.Value) NodeList(org.w3c.dom.NodeList)

Example 24 with LockException

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

the class LockManager method acquireDocumentWriteLock.

/**
 * Acquire a WRITE_LOCK on a Document
 *
 * @param documentPath The URI of the Document within the database
 *
 * @return the lock for the Document
 *
 * @throws LockException if the lock could not be acquired
 */
public ManagedDocumentLock acquireDocumentWriteLock(final XmldbURI documentPath) throws LockException {
    if (usePathLocksForDocuments) {
        final LockGroup lockGroup = acquirePathWriteLock(LockType.DOCUMENT, documentPath, false);
        return new ManagedDocumentLock(documentPath, Arrays.stream(lockGroup.locks).map(Tuple3::get_1).toArray(MultiLock[]::new), () -> unlockAll(lockGroup.locks, l -> lockTable.released(lockGroup.groupId, l._3, LockType.DOCUMENT, l._2)));
    } else {
        final long groupId = System.nanoTime();
        final String path = documentPath.toString();
        final MultiLock lock = getDocumentLock(path);
        lockTable.attempt(groupId, path, LockType.DOCUMENT, Lock.LockMode.WRITE_LOCK);
        if (lock(lock, Lock.LockMode.WRITE_LOCK)) {
            lockTable.acquired(groupId, path, LockType.DOCUMENT, Lock.LockMode.WRITE_LOCK);
        } else {
            lockTable.attemptFailed(groupId, path, LockType.DOCUMENT, Lock.LockMode.WRITE_LOCK);
            throw new LockException("Unable to acquire WRITE_LOCK for: " + path);
        }
        return new ManagedDocumentLock(documentPath, lock, () -> {
            lock.asWriteLock().unlock();
            lockTable.released(groupId, path, LockType.DOCUMENT, Lock.LockMode.WRITE_LOCK);
        });
    }
}
Also used : Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) Consumer(java.util.function.Consumer) Arrays(java.util.Arrays) Logger(org.apache.logging.log4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) WeakLazyStripes(org.exist.util.WeakLazyStripes) LockException(org.exist.util.LockException) XmldbURI(org.exist.xmldb.XmldbURI) LockType(org.exist.storage.lock.Lock.LockType) Configuration(org.exist.util.Configuration) MultiLock(uk.ac.ic.doc.slurp.multilock.MultiLock) LogManager(org.apache.logging.log4j.LogManager) LockException(org.exist.util.LockException) Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) MultiLock(uk.ac.ic.doc.slurp.multilock.MultiLock)

Example 25 with LockException

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

the class LockManager method acquireDocumentReadLock.

/**
 * Acquire a READ_LOCK on a Document
 *
 * @param documentPath The URI of the Document within the database
 *
 * @return the lock for the Document
 *
 * @throws LockException if the lock could not be acquired
 */
public ManagedDocumentLock acquireDocumentReadLock(final XmldbURI documentPath) throws LockException {
    if (usePathLocksForDocuments) {
        final LockGroup lockGroup = acquirePathReadLock(LockType.DOCUMENT, documentPath);
        return new ManagedDocumentLock(documentPath, Arrays.stream(lockGroup.locks).map(Tuple3::get_1).toArray(MultiLock[]::new), () -> unlockAll(lockGroup.locks, l -> lockTable.released(lockGroup.groupId, l._3, LockType.DOCUMENT, l._2)));
    } else {
        final long groupId = System.nanoTime();
        final String path = documentPath.toString();
        final MultiLock lock = getDocumentLock(path);
        lockTable.attempt(groupId, path, LockType.DOCUMENT, Lock.LockMode.READ_LOCK);
        if (lock(lock, Lock.LockMode.READ_LOCK)) {
            lockTable.acquired(groupId, path, LockType.DOCUMENT, Lock.LockMode.READ_LOCK);
        } else {
            lockTable.attemptFailed(groupId, path, LockType.DOCUMENT, Lock.LockMode.READ_LOCK);
            throw new LockException("Unable to acquire READ_LOCK for: " + path);
        }
        return new ManagedDocumentLock(documentPath, lock, () -> {
            lock.asReadLock().unlock();
            lockTable.released(groupId, path, LockType.DOCUMENT, Lock.LockMode.READ_LOCK);
        });
    }
}
Also used : Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) Consumer(java.util.function.Consumer) Arrays(java.util.Arrays) Logger(org.apache.logging.log4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) WeakLazyStripes(org.exist.util.WeakLazyStripes) LockException(org.exist.util.LockException) XmldbURI(org.exist.xmldb.XmldbURI) LockType(org.exist.storage.lock.Lock.LockType) Configuration(org.exist.util.Configuration) MultiLock(uk.ac.ic.doc.slurp.multilock.MultiLock) LogManager(org.apache.logging.log4j.LogManager) LockException(org.exist.util.LockException) Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) MultiLock(uk.ac.ic.doc.slurp.multilock.MultiLock)

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