Search in sources :

Example 6 with NewArrayNodeSet

use of org.exist.dom.persistent.NewArrayNodeSet in project exist by eXist-db.

the class NativeStructuralIndexWorker method findElementsByTagName.

public NodeSet findElementsByTagName(byte type, DocumentSet docs, QName qname, NodeSelector selector, Expression parent) {
    final NewArrayNodeSet result = new NewArrayNodeSet();
    final FindElementsCallback callback = new FindElementsCallback(type, qname, result, docs, selector, parent);
    // for each document id range, scan the index to find matches
    for (final Range range : getDocIdRanges(docs)) {
        final byte[] fromKey = computeKey(type, qname, range.start);
        final byte[] toKey = computeKey(type, qname, range.end + 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, callback);
        } catch (final LockException e) {
            NativeStructuralIndex.LOG.warn("Lock problem while searching structural index: {}", e.getMessage(), e);
        } catch (final TerminatedException e) {
            NativeStructuralIndex.LOG.warn("Query was terminated while searching structural index: {}", e.getMessage(), e);
        } catch (final Exception e) {
            NativeStructuralIndex.LOG.error("Error while searching structural index: {}", e.getMessage(), e);
        }
    }
    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) Value(org.exist.storage.btree.Value) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException)

Example 7 with NewArrayNodeSet

use of org.exist.dom.persistent.NewArrayNodeSet in project exist by eXist-db.

the class RangeIndexWorker method queryField.

public NodeSet queryField(int contextId, DocumentSet docs, NodeSet contextSet, Sequence fields, Sequence[] keys, RangeIndex.Operator[] operators, int axis) throws IOException, XPathException {
    return index.withSearcher(searcher -> {
        BooleanQuery query = new BooleanQuery();
        int j = 0;
        for (SequenceIterator i = fields.iterate(); i.hasNext(); j++) {
            String field = i.nextItem().getStringValue();
            if (keys[j].getItemCount() > 1) {
                BooleanQuery bool = new BooleanQuery();
                bool.setMinimumNumberShouldMatch(1);
                for (SequenceIterator ki = keys[j].iterate(); ki.hasNext(); ) {
                    Item key = ki.nextItem();
                    Query q = toQuery(field, null, key.atomize(), operators[j], docs);
                    bool.add(q, BooleanClause.Occur.SHOULD);
                }
                query.add(bool, BooleanClause.Occur.MUST);
            } else {
                Query q = toQuery(field, null, keys[j].itemAt(0).atomize(), operators[j], docs);
                query.add(q, BooleanClause.Occur.MUST);
            }
        }
        Query qu = query;
        BooleanClause[] clauses = query.getClauses();
        if (clauses.length == 1) {
            qu = clauses[0].getQuery();
        }
        final NodeSet resultSet = new NewArrayNodeSet();
        resultSet.addAll(doQuery(contextId, docs, contextSet, axis, searcher.searcher, Node.ELEMENT_NODE, qu, null));
        return resultSet;
    });
}
Also used : NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet)

Example 8 with NewArrayNodeSet

use of org.exist.dom.persistent.NewArrayNodeSet in project exist by eXist-db.

the class RangeIndexWorker method query.

public NodeSet query(int contextId, DocumentSet docs, NodeSet contextSet, List<QName> qnames, AtomicValue[] keys, RangeIndex.Operator operator, int axis) throws IOException, XPathException {
    return index.withSearcher(searcher -> {
        List<QName> definedIndexes = getDefinedIndexes(qnames);
        NodeSet resultSet = new NewArrayNodeSet();
        for (QName qname : definedIndexes) {
            Query query;
            String field = LuceneUtil.encodeQName(qname, index.getBrokerPool().getSymbols());
            if (keys.length > 1) {
                BooleanQuery bool = new BooleanQuery();
                for (AtomicValue key : keys) {
                    bool.add(toQuery(field, qname, key, operator, docs), BooleanClause.Occur.SHOULD);
                }
                query = bool;
            } else {
                query = toQuery(field, qname, keys[0], operator, docs);
            }
            final short nodeType = qname.getNameType() == ElementValue.ATTRIBUTE ? Node.ATTRIBUTE_NODE : Node.ELEMENT_NODE;
            resultSet.addAll(doQuery(contextId, docs, contextSet, axis, searcher.searcher, nodeType, query, null));
        }
        return resultSet;
    });
}
Also used : NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) QName(org.exist.dom.QName)

Example 9 with NewArrayNodeSet

use of org.exist.dom.persistent.NewArrayNodeSet 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)

Example 10 with NewArrayNodeSet

use of org.exist.dom.persistent.NewArrayNodeSet 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)

Aggregations

NewArrayNodeSet (org.exist.dom.persistent.NewArrayNodeSet)14 NodeProxy (org.exist.dom.persistent.NodeProxy)10 NodeSet (org.exist.dom.persistent.NodeSet)9 DocumentImpl (org.exist.dom.persistent.DocumentImpl)6 ReentrantLock (java.util.concurrent.locks.ReentrantLock)4 ContextItem (org.exist.dom.persistent.ContextItem)4 VirtualNodeSet (org.exist.dom.persistent.VirtualNodeSet)4 NodeId (org.exist.numbering.NodeId)4 PermissionDeniedException (org.exist.security.PermissionDeniedException)4 Value (org.exist.storage.btree.Value)4 DatabaseConfigurationException (org.exist.util.DatabaseConfigurationException)4 LockException (org.exist.util.LockException)4 IndexQuery (org.exist.storage.btree.IndexQuery)3 Sequence (org.exist.xquery.value.Sequence)3 SequenceIterator (org.exist.xquery.value.SequenceIterator)3 Collator (com.ibm.icu.text.Collator)2 QName (org.exist.dom.QName)2 TreeSet (java.util.TreeSet)1 EXistException (org.exist.EXistException)1 DocumentImpl (org.exist.dom.memtree.DocumentImpl)1