Search in sources :

Example 41 with LockException

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

the class NativeStructuralIndexWorker method scanByType.

public NodeSet scanByType(byte type, int axis, NodeTest test, boolean useSelfAsContext, DocumentSet docs, NodeSet contextSet, int contextId) {
    final NewArrayNodeSet result = new NewArrayNodeSet();
    final FindDescendantsCallback callback = new FindDescendantsCallback(type, axis, null, contextId, useSelfAsContext, result, null);
    for (final NodeProxy ancestor : contextSet) {
        final DocumentImpl doc = ancestor.getOwnerDocument();
        final NodeId ancestorId = ancestor.getNodeId();
        final List<QName> qnames = getQNamesForDoc(doc);
        try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
            for (final QName qname : qnames) {
                if (test.getName() == null || test.matches(qname)) {
                    callback.setAncestor(doc, ancestor);
                    byte[] fromKey, 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) QName(org.exist.dom.QName) 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) LockException(org.exist.util.LockException) NodeId(org.exist.numbering.NodeId) Value(org.exist.storage.btree.Value)

Example 42 with LockException

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

the class NativeStructuralIndexWorker method getQNamesForDoc.

protected List<QName> getQNamesForDoc(DocumentImpl doc) {
    final List<QName> qnames = new ArrayList<>();
    if (index.btree == null) {
        return qnames;
    }
    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.query(query, (value, pointer) -> {
            final QName qname = readQName(value.getData());
            qnames.add(qname);
            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);
    }
    return qnames;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) LockException(org.exist.util.LockException) QName(org.exist.dom.QName) Value(org.exist.storage.btree.Value) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException)

Example 43 with LockException

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

the class NativeStructuralIndexWorker method findAncestorsByTagName.

public NodeSet findAncestorsByTagName(byte type, QName qname, int axis, DocumentSet docs, NodeSet contextSet, int contextId) {
    final NewArrayNodeSet result = new NewArrayNodeSet();
    try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
        for (final NodeProxy descendant : contextSet) {
            NodeId parentId;
            if (axis == Constants.ANCESTOR_SELF_AXIS || axis == Constants.SELF_AXIS) {
                parentId = descendant.getNodeId();
            } else {
                parentId = descendant.getNodeId().getParentId();
            }
            final DocumentImpl doc = descendant.getOwnerDocument();
            while (parentId != NodeId.DOCUMENT_NODE) {
                final byte[] key = computeKey(type, qname, doc.getDocId(), parentId);
                final long address = index.btree.findValue(new Value(key));
                if (address != -1) {
                    final NodeProxy storedNode = new NodeProxy(doc, parentId, type == ElementValue.ATTRIBUTE ? Node.ATTRIBUTE_NODE : Node.ELEMENT_NODE, address);
                    result.add(storedNode);
                    if (Expression.NO_CONTEXT_ID != contextId) {
                        storedNode.deepCopyContext(descendant, contextId);
                    } else {
                        storedNode.copyContext(descendant);
                    }
                    if (contextSet.getTrackMatches()) {
                        storedNode.addMatches(descendant);
                    }
                }
                // stop after first iteration if we are on the self axis
                if (axis == Constants.SELF_AXIS || axis == Constants.PARENT_AXIS) {
                    break;
                }
                // continue with the parent of the parent
                parentId = parentId.getParentId();
            }
        }
    } catch (final LockException e) {
        NativeStructuralIndex.LOG.warn("Lock problem while searching structural index: {}", e.getMessage(), e);
    } catch (final Exception e) {
        NativeStructuralIndex.LOG.error("Error while searching structural index: {}", e.getMessage(), e);
    }
    result.sort(true);
    return result;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) 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)

Example 44 with LockException

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

the class CreateOrderIndex method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (args[1].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    final String id = args[0].getStringValue();
    // check how the function was called and prepare callback
    FunctionReference call = null;
    if (isCalledAs("create-index-callback")) {
        call = (FunctionReference) args[2].itemAt(0);
    } else if (args[2].getItemCount() != args[1].getItemCount())
        throw new XPathException(this, "$nodes and $values sequences need to have the same length.");
    // options
    if (args[3].getItemCount() > 0) {
        final NodeValue optionValue = (NodeValue) args[3].itemAt(0);
        final Element options = (Element) optionValue.getNode();
        String option = options.getAttribute("order");
        if (option != null) {
            descending = option.equalsIgnoreCase("descending");
        }
        option = options.getAttribute("empty");
        if (option != null) {
            emptyLeast = option.equalsIgnoreCase("least");
        }
    }
    // create the input list to be sorted below
    final List<SortItem> items = new ArrayList<>(args[1].getItemCount());
    final Sequence[] params = new Sequence[1];
    SequenceIterator valuesIter = null;
    if (call == null)
        valuesIter = args[2].iterate();
    int c = 0;
    final int len = args[1].getItemCount();
    final int logChunk = 1 + (len / 20);
    for (final SequenceIterator nodesIter = args[1].iterate(); nodesIter.hasNext(); ) {
        final NodeValue nv = (NodeValue) nodesIter.nextItem();
        if (nv.getImplementationType() == NodeValue.IN_MEMORY_NODE)
            throw new XPathException(this, "Cannot create order-index on an in-memory node");
        final NodeProxy node = (NodeProxy) nv;
        final SortItem si = new SortItemImpl(node);
        if (LOG.isDebugEnabled() && ++c % logChunk == 0) {
            LOG.debug("Storing item {} out of {} to sort index.", c, len);
        }
        if (call != null) {
            // call the callback function to get value
            params[0] = node;
            final Sequence r = call.evalFunction(contextSequence, null, params);
            if (!r.isEmpty()) {
                AtomicValue v = r.itemAt(0).atomize();
                if (v.getType() == Type.UNTYPED_ATOMIC)
                    v = v.convertTo(Type.STRING);
                si.setValue(v);
            }
        } else {
            // no callback, take value from second sequence
            AtomicValue v = valuesIter.nextItem().atomize();
            if (v.getType() == Type.UNTYPED_ATOMIC)
                v = v.convertTo(Type.STRING);
            si.setValue(v);
        }
        items.add(si);
    }
    // sort the set
    FastQSort.sort(items, 0, items.size() - 1);
    // create the index
    final SortIndexWorker index = (SortIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(SortIndex.ID);
    try {
        index.createIndex(id, items);
    } catch (final EXistException e) {
        throw new XPathException(this, e.getMessage(), e);
    } catch (final LockException e) {
        throw new XPathException(this, "Caught lock error while creating index. Giving up.", e);
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : SortIndexWorker(org.exist.indexing.sort.SortIndexWorker) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) EXistException(org.exist.EXistException) NodeProxy(org.exist.dom.persistent.NodeProxy) SortItem(org.exist.indexing.sort.SortItem) LockException(org.exist.util.LockException)

Example 45 with LockException

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

the class AbstractCompressFunction method compressFromUri.

private void compressFromUri(OutputStream os, URI uri, boolean useHierarchy, String stripOffset, String method, String resourceName) throws XPathException {
    try {
        if ("file".equals(uri.getScheme())) {
            if (!context.getSubject().hasDbaRole()) {
                XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to call this function.");
                logger.error("Invalid user", xPathException);
                throw xPathException;
            }
            // got a file
            Path file = Paths.get(uri.getPath());
            compressFile(os, file, useHierarchy, stripOffset, method, resourceName);
        } else {
            final XmldbURI xmldburi = XmldbURI.create(uri);
            // try for a collection
            try (final Collection collection = context.getBroker().openCollection(xmldburi, LockMode.READ_LOCK)) {
                if (collection != null) {
                    compressCollection(os, collection, useHierarchy, stripOffset);
                    return;
                }
            } catch (final PermissionDeniedException | LockException | SAXException | IOException pde) {
                throw new XPathException(this, pde.getMessage());
            }
            // otherwise, try for a doc
            try (final Collection collection = context.getBroker().openCollection(xmldburi.removeLastSegment(), LockMode.READ_LOCK)) {
                if (collection == null) {
                    throw new XPathException(this, "Invalid URI: " + uri.toString());
                }
                try (final LockedDocument doc = collection.getDocumentWithLock(context.getBroker(), xmldburi.lastSegment(), LockMode.READ_LOCK)) {
                    // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                    collection.close();
                    if (doc == null) {
                        throw new XPathException(this, "Invalid URI: " + uri.toString());
                    }
                    compressResource(os, doc.getDocument(), useHierarchy, stripOffset, method, resourceName);
                    return;
                }
            } catch (final PermissionDeniedException | LockException | SAXException | IOException pde) {
                throw new XPathException(this, pde.getMessage());
            }
        }
    } catch (final IOException e) {
        throw new XPathException(this, e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) LockException(org.exist.util.LockException) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI) SAXException(org.xml.sax.SAXException)

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