Search in sources :

Example 1 with ExtArrayNodeSet

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

the class ValueComparison method nodeSetCompare.

protected Sequence nodeSetCompare(NodeSet nodes, Sequence contextSequence) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().message(this, Profiler.OPTIMIZATION_FLAGS, "OPTIMIZATION CHOICE", "nodeSetCompare");
    }
    final NodeSet result = new ExtArrayNodeSet();
    final Collator collator = getCollator(contextSequence);
    if (contextSequence != null && !contextSequence.isEmpty()) {
        for (final NodeProxy current : nodes) {
            ContextItem context = current.getContext();
            if (context == null) {
                throw new XPathException(this, ErrorCodes.XPDY0002, "Context is missing for node set comparison");
            }
            do {
                final AtomicValue lv = current.atomize();
                final Sequence rs = getRight().eval(context.getNode().toSequence());
                if (!rs.hasOne()) {
                    throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: sequence with less or more than one item is not allowed here");
                }
                if (compareAtomic(collator, lv, rs.itemAt(0).atomize(), StringTruncationOperator.NONE, relation)) {
                    result.add(current);
                }
            } while ((context = context.getNextDirect()) != null);
        }
    } else {
        final Sequence rs = getRight().eval(null);
        if (!rs.hasOne()) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: sequence with less or more than one item is not allowed here");
        }
        final AtomicValue rv = rs.itemAt(0).atomize();
        for (final NodeProxy current : nodes) {
            final AtomicValue lv = current.atomize();
            if (compareAtomic(collator, lv, rv, StringTruncationOperator.NONE, Comparison.EQ)) {
                result.add(current);
            }
        }
    }
    return result;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) ContextItem(org.exist.dom.persistent.ContextItem) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) AtomicValue(org.exist.xquery.value.AtomicValue) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) Collator(com.ibm.icu.text.Collator)

Example 2 with ExtArrayNodeSet

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

the class FunRemove method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    Sequence result;
    Sequence seq = getArgument(0).eval(contextSequence, contextItem);
    if (seq.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        // TODO : explain this Double conversion -pb
        int pos = ((DoubleValue) getArgument(1).eval(contextSequence, contextItem).convertTo(Type.DOUBLE)).getInt();
        if (pos < 1 || pos > seq.getItemCount()) {
            result = seq;
        } else {
            pos--;
            if (seq instanceof NodeSet) {
                result = new ExtArrayNodeSet();
                result.addAll((NodeSet) seq);
                result = ((NodeSet) result).except((NodeSet) seq.itemAt(pos));
            } else {
                result = new ValueSequence();
                for (int i = 0; i < seq.getItemCount(); i++) {
                    if (i != pos) {
                        result.add(seq.itemAt(i));
                    }
                }
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) DoubleValue(org.exist.xquery.value.DoubleValue) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence)

Example 3 with ExtArrayNodeSet

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

the class FunRoot method eval.

/* (non-Javadoc)
         * @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
         */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    Sequence seq;
    Sequence result;
    Item item;
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    if (contextSequence == null || contextSequence.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    }
    // If we have one argumment, we take it into account
    if (getSignature().getArgumentCount() > 0) {
        seq = getArgument(0).eval(contextSequence, contextItem);
    } else // Otherwise, we take the context sequence and we iterate over it
    {
        seq = contextSequence;
    }
    if (seq == null) {
        throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
    }
    if (seq.isPersistentSet()) {
        result = new ExtArrayNodeSet(seq.getItemCount());
    } else {
        result = new ValueSequence(seq.getItemCount());
    }
    for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
        item = i.nextItem();
        if (!Type.subTypeOf(item.getType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "Item is not a node; got '" + item + "'", seq);
        }
        final Sequence s = item.toSequence();
        if (s.isPersistentSet()) {
            final NodeProxy p = s.toNodeSet().get(0);
            result.add(new NodeProxy(p.getOwnerDocument()));
        } else {
            if (seq.hasOne() && item.getType() == Type.ATTRIBUTE) {
                result.add(item);
            } else if (item.getType() == Type.DOCUMENT) {
                result.add((DocumentImpl) item);
            } else {
                result.add(((NodeImpl) item).getOwnerDocument());
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) DocumentImpl(org.exist.dom.memtree.DocumentImpl)

Example 4 with ExtArrayNodeSet

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

the class NGramIndexWorker method search.

public NodeSet search(final int contextId, final DocumentSet docs, final List<QName> qnames, final String query, final String ngram, final XQueryContext context, final NodeSet contextSet, final int axis) throws XPathException {
    final List<QName> searchQnames;
    if (qnames == null || qnames.isEmpty()) {
        searchQnames = getDefinedIndexes(context.getBroker(), docs);
    } else {
        searchQnames = qnames;
    }
    final NodeSet result = new ExtArrayNodeSet(docs.getDocumentCount(), 250);
    for (final Iterator<Collection> iter = docs.getCollectionIterator(); iter.hasNext(); ) {
        final int collectionId = iter.next().getId();
        for (final QName qname : searchQnames) {
            final NGramQNameKey key = new NGramQNameKey(collectionId, qname, index.getBrokerPool().getSymbols(), query);
            try (final ManagedLock<ReentrantLock> dbLock = lockManager.acquireBtreeReadLock(index.db.getLockName())) {
                final SearchCallback cb = new SearchCallback(contextId, query, ngram, docs, contextSet, context, result, axis == NodeSet.ANCESTOR);
                final int op = query.codePointCount(0, query.length()) < getN() ? IndexQuery.TRUNC_RIGHT : IndexQuery.EQ;
                index.db.query(new IndexQuery(op, key), cb);
            } catch (final LockException e) {
                LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(index.db.getFile()), e);
            } catch (final IOException | BTreeException e) {
                LOG.error("{} in '{}'", e.getMessage(), FileUtils.fileName(index.db.getFile()), e);
            }
        }
    }
    // ensure result is ready to use
    result.iterate();
    return result;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) QName(org.exist.dom.QName) IOException(java.io.IOException) BTreeException(org.exist.storage.btree.BTreeException) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) Collection(org.exist.collections.Collection)

Example 5 with ExtArrayNodeSet

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

the class AlternativeStrings method eval.

@Override
public NodeSet eval(final NGramIndexWorker index, final DocumentSet docs, final List<QName> qnames, final NodeSet nodeSet, final int axis, final int expressionId) throws XPathException {
    NodeSet result = new ExtArrayNodeSet();
    for (String s : strings) {
        result.addAll(nGramSearch.fixedStringSearch(index, docs, qnames, s, nodeSet, axis));
    }
    // ensure result is ready to use
    result.iterate();
    return result;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet)

Aggregations

ExtArrayNodeSet (org.exist.dom.persistent.ExtArrayNodeSet)10 NodeSet (org.exist.dom.persistent.NodeSet)8 NodeProxy (org.exist.dom.persistent.NodeProxy)6 Sequence (org.exist.xquery.value.Sequence)4 ValueSequence (org.exist.xquery.value.ValueSequence)3 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)2 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)2 XPathException (org.exist.xquery.XPathException)2 SequenceIterator (org.exist.xquery.value.SequenceIterator)2 Collator (com.ibm.icu.text.Collator)1 IOException (java.io.IOException)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Collection (org.exist.collections.Collection)1 QName (org.exist.dom.QName)1 DocumentImpl (org.exist.dom.memtree.DocumentImpl)1 NodeImpl (org.exist.dom.memtree.NodeImpl)1 ContextItem (org.exist.dom.persistent.ContextItem)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 DocumentSet (org.exist.dom.persistent.DocumentSet)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1