Search in sources :

Example 6 with StructuralIndex

use of org.exist.indexing.StructuralIndex in project exist by eXist-db.

the class LocationStep method getChildren.

private Sequence getChildren(final XQueryContext context, final Sequence contextSequence) throws XPathException {
    if (!contextSequence.isPersistentSet()) {
        final MemoryNodeSet nodes = contextSequence.toMemNodeSet();
        return nodes.getChildren(test);
    }
    final NodeSet contextSet = contextSequence.toNodeSet();
    // similar way ? -pb
    if ((!hasPreloadedData() && test.isWildcardTest()) || test.getType() == Type.PROCESSING_INSTRUCTION) {
        // test is one out of *, text(), node() including
        // processing-instruction(targetname)
        final VirtualNodeSet vset = new VirtualNodeSet(context.getBroker(), axis, test, contextId, contextSet);
        vset.setInPredicate(Expression.NO_CONTEXT_ID != contextId);
        return vset;
    }
    if (useDirectChildSelect) {
        final NewArrayNodeSet result = new NewArrayNodeSet();
        for (final NodeProxy p : contextSet) {
            result.addAll(p.directSelectChild(test.getName(), contextId));
        }
        return result;
    } else if (hasPreloadedData()) {
        final DocumentSet docs = getDocumentSet(contextSet);
        synchronized (context) {
            // ones
            if (currentSet == null || currentDocs == null || (!optimized && !(docs == currentDocs || docs.equalDocs(currentDocs)))) {
                final StructuralIndex index = context.getBroker().getStructuralIndex();
                if (context.getProfiler().isEnabled()) {
                    context.getProfiler().message(this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using structural index '" + index.toString() + "'");
                }
                currentSet = index.findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), null, this);
                currentDocs = docs;
                registerUpdateListener();
            }
            return currentSet.selectParentChild(contextSet, NodeSet.DESCENDANT, contextId);
        }
    } else {
        final DocumentSet docs = getDocumentSet(contextSet);
        final StructuralIndex index = context.getBroker().getStructuralIndex();
        if (context.getProfiler().isEnabled()) {
            context.getProfiler().message(this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using structural index '" + index.toString() + "'");
        }
        if (!contextSet.getProcessInReverseOrder() && !(contextSet instanceof VirtualNodeSet) && contextSet.getLength() < INDEX_SCAN_THRESHOLD) {
            return index.findDescendantsByTagName(ElementValue.ELEMENT, test.getName(), axis, docs, contextSet, contextId, parent);
        } else {
            // if (contextSet instanceof VirtualNodeSet)
            // ((VirtualNodeSet)contextSet).realize();
            final NodeSelector selector = new ChildSelector(contextSet, contextId);
            return index.findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), selector, this);
        }
    }
}
Also used : InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) StructuralIndex(org.exist.indexing.StructuralIndex)

Example 7 with StructuralIndex

use of org.exist.indexing.StructuralIndex in project exist by eXist-db.

the class LocationStep method getSelf.

private Sequence getSelf(final XQueryContext context, final Sequence contextSequence) throws XPathException {
    if (!contextSequence.isPersistentSet()) {
        final MemoryNodeSet nodes = contextSequence.toMemNodeSet();
        return nodes.getSelf(test);
    }
    if (hasPreloadedData() && !test.isWildcardTest()) {
        final NodeSet ns;
        if (contextSequence instanceof NodeSet) {
            ns = (NodeSet) contextSequence;
        } else {
            ns = null;
        }
        for (final NodeProxy p : currentSet) {
            p.addContextNode(contextId, p);
            if (ns != null) {
                final NodeProxy np = ns.get(p);
                if (np != null && np.getMatches() != null) {
                    p.addMatch(np.getMatches());
                }
            }
        }
        return currentSet;
    }
    final NodeSet contextSet = contextSequence.toNodeSet();
    if (test.isWildcardTest()) {
        if (nodeTestType == null) {
            nodeTestType = test.getType();
        }
        if (Type.subTypeOf(nodeTestType, Type.NODE)) {
            if (Expression.NO_CONTEXT_ID != contextId) {
                if (contextSet instanceof VirtualNodeSet) {
                    ((VirtualNodeSet) contextSet).setInPredicate(true);
                    ((VirtualNodeSet) contextSet).setContextId(contextId);
                    ((VirtualNodeSet) contextSet).setSelfIsContext();
                } else if (Type.subTypeOf(contextSet.getItemType(), Type.NODE)) {
                    for (final NodeProxy p : contextSet) {
                        if (test.matches(p)) {
                            p.addContextNode(contextId, p);
                        }
                    }
                }
                return contextSet;
            } else {
                final NewArrayNodeSet results = new NewArrayNodeSet();
                for (final NodeProxy p : contextSet) {
                    if (test.matches(p)) {
                        results.add(p);
                    }
                }
                return results;
            }
        } else {
            final VirtualNodeSet vset = new VirtualNodeSet(context.getBroker(), axis, test, contextId, contextSet);
            vset.setInPredicate(Expression.NO_CONTEXT_ID != contextId);
            return vset;
        }
    } else {
        final DocumentSet docs = getDocumentSet(contextSet);
        final StructuralIndex index = context.getBroker().getStructuralIndex();
        if (context.getProfiler().isEnabled()) {
            context.getProfiler().message(this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using structural index '" + index.toString() + "'");
        }
        final NodeSelector selector = new SelfSelector(contextSet, contextId);
        return index.findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), selector, this);
    }
}
Also used : InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) StructuralIndex(org.exist.indexing.StructuralIndex)

Example 8 with StructuralIndex

use of org.exist.indexing.StructuralIndex in project exist by eXist-db.

the class LocationStep method getDescendants.

private Sequence getDescendants(final XQueryContext context, final Sequence contextSequence) throws XPathException {
    if (!contextSequence.isPersistentSet()) {
        final MemoryNodeSet nodes = contextSequence.toMemNodeSet();
        return nodes.getDescendants(axis == Constants.DESCENDANT_SELF_AXIS, test);
    }
    final NodeSet contextSet = contextSequence.toNodeSet();
    // similar way ? -pb
    if ((!hasPreloadedData() && test.isWildcardTest()) || test.getType() == Type.PROCESSING_INSTRUCTION) {
        // test is one out of *, text(), node() including
        // processing-instruction(targetname)
        final VirtualNodeSet vset = new VirtualNodeSet(context.getBroker(), axis, test, contextId, contextSet);
        vset.setInPredicate(Expression.NO_CONTEXT_ID != contextId);
        return vset;
    } else if (hasPreloadedData()) {
        final DocumentSet docs = getDocumentSet(contextSet);
        synchronized (context) {
            // ones
            if (currentSet == null || currentDocs == null || (!optimized && !(docs == currentDocs || docs.equalDocs(currentDocs)))) {
                final StructuralIndex index = context.getBroker().getStructuralIndex();
                if (context.getProfiler().isEnabled()) {
                    context.getProfiler().message(this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using structural index '" + index.toString() + "'");
                }
                currentSet = index.findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), null, this);
                currentDocs = docs;
                registerUpdateListener();
            }
            switch(axis) {
                case Constants.DESCENDANT_SELF_AXIS:
                    return currentSet.selectAncestorDescendant(contextSet, NodeSet.DESCENDANT, true, contextId, true);
                case Constants.DESCENDANT_AXIS:
                    return currentSet.selectAncestorDescendant(contextSet, NodeSet.DESCENDANT, false, contextId, true);
                default:
                    throw new IllegalArgumentException("Unsupported axis specified");
            }
        }
    } else {
        final DocumentSet docs = contextSet.getDocumentSet();
        final StructuralIndex index = context.getBroker().getStructuralIndex();
        if (context.getProfiler().isEnabled()) {
            context.getProfiler().message(this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using structural index '" + index.toString() + "'");
        }
        if (!contextSet.getProcessInReverseOrder() && (contextSet instanceof VirtualNodeSet || contextSet.getLength() < INDEX_SCAN_THRESHOLD)) {
            return index.findDescendantsByTagName(ElementValue.ELEMENT, test.getName(), axis, docs, contextSet, contextId, this);
        } else {
            final NodeSelector selector;
            switch(axis) {
                case Constants.DESCENDANT_SELF_AXIS:
                    selector = new DescendantOrSelfSelector(contextSet, contextId);
                    break;
                case Constants.DESCENDANT_AXIS:
                    selector = new DescendantSelector(contextSet, contextId);
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported axis specified");
            }
            return index.findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), selector, this);
        }
    }
}
Also used : InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) StructuralIndex(org.exist.indexing.StructuralIndex)

Example 9 with StructuralIndex

use of org.exist.indexing.StructuralIndex in project exist by eXist-db.

the class LocationStep method getPrecedingOrFollowing.

/**
 * Get the preceding or following axis nodes
 *
 * @param context the xquery context
 * @param contextSequence the context sequence
 *
 * @return the nodes from the preceding or following axis
 *
 * @throws XPathException if an error occurs
 */
private Sequence getPrecedingOrFollowing(final XQueryContext context, final Sequence contextSequence) throws XPathException {
    final int position = computeLimit();
    // process an in-memory node set
    if (!contextSequence.isPersistentSet()) {
        final MemoryNodeSet nodes = contextSequence.toMemNodeSet();
        if (position > -1) {
            applyPredicate = false;
        }
        if (axis == Constants.PRECEDING_AXIS) {
            return nodes.getPreceding(test, position);
        } else {
            return nodes.getFollowing(test, position);
        }
    }
    final NodeSet contextSet = contextSequence.toNodeSet();
    // similar way ? -pb
    if (test.getType() == Type.PROCESSING_INSTRUCTION) {
        final VirtualNodeSet vset = new VirtualNodeSet(context.getBroker(), axis, test, contextId, contextSet);
        vset.setInPredicate(Expression.NO_CONTEXT_ID != contextId);
        return vset;
    }
    // handle node(), * etc.
    if (test.isWildcardTest()) {
        try {
            final NodeSet result = new NewArrayNodeSet();
            for (final NodeProxy next : contextSet) {
                final NodeList cl = next.getOwnerDocument().getChildNodes();
                for (int j = 0; j < cl.getLength(); j++) {
                    final NodeHandle node = (NodeHandle) cl.item(j);
                    final NodeProxy root = new NodeProxy(node);
                    final StreamFilter filter;
                    if (axis == Constants.PRECEDING_AXIS) {
                        filter = new PrecedingFilter(test, root, next, result, contextId);
                    } else {
                        filter = new FollowingFilter(test, root, next, result, contextId, position);
                    }
                    final IEmbeddedXMLStreamReader reader = context.getBroker().getXMLStreamReader(root, false);
                    reader.filter(filter);
                }
            }
            return result;
        } catch (final XMLStreamException | IOException e) {
            throw new XPathException(this, e);
        }
    } else {
        // TODO : no test on preloaded data ?
        final DocumentSet docs = getDocumentSet(contextSet);
        synchronized (context) {
            if (currentSet == null || currentDocs == null || !(docs.equalDocs(currentDocs))) {
                final StructuralIndex index = context.getBroker().getStructuralIndex();
                if (context.getProfiler().isEnabled()) {
                    context.getProfiler().message(this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using structural index '" + index.toString() + "'");
                }
                currentSet = index.findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), null, this);
                currentDocs = docs;
                registerUpdateListener();
            }
            if (position > -1) {
                try {
                    applyPredicate = false;
                    if (axis == Constants.PRECEDING_AXIS) {
                        return currentSet.selectPreceding(contextSet, position, contextId);
                    } else {
                        return currentSet.selectFollowing(contextSet, position, contextId);
                    }
                } catch (final UnsupportedOperationException e) {
                    if (axis == Constants.PRECEDING_AXIS) {
                        return currentSet.selectPreceding(contextSet, contextId);
                    } else {
                        return currentSet.selectFollowing(contextSet, contextId);
                    }
                }
            } else {
                if (axis == Constants.PRECEDING_AXIS) {
                    return currentSet.selectPreceding(contextSet, contextId);
                } else {
                    return currentSet.selectFollowing(contextSet, contextId);
                }
            }
        }
    }
}
Also used : InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) StructuralIndex(org.exist.indexing.StructuralIndex) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) StreamFilter(javax.xml.stream.StreamFilter) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 10 with StructuralIndex

use of org.exist.indexing.StructuralIndex in project exist by eXist-db.

the class LocationStep method getParents.

/**
 * Get the parent axis nodes
 *
 * @param context the xquery context
 * @param contextSequence the context sequence
 *
 * @return the parent nodes
 *
 * @throws XPathException if an error occurs
 */
protected Sequence getParents(final XQueryContext context, final Sequence contextSequence) throws XPathException {
    if (!contextSequence.isPersistentSet()) {
        final MemoryNodeSet nodes = contextSequence.toMemNodeSet();
        return nodes.getParents(test);
    }
    final NodeSet contextSet = contextSequence.toNodeSet();
    if (test.isWildcardTest()) {
        final NodeSet temp = contextSet.getParents(contextId);
        final NodeSet result = new NewArrayNodeSet();
        for (final NodeProxy p : temp) {
            if (test.matches(p)) {
                result.add(p);
            }
        }
        return result;
    } else if (hasPreloadedData()) {
        final DocumentSet docs = getDocumentSet(contextSet);
        synchronized (context) {
            if (currentSet == null || currentDocs == null || (!optimized && !(docs == currentDocs || docs.equalDocs(currentDocs)))) {
                final StructuralIndex index = context.getBroker().getStructuralIndex();
                if (context.getProfiler().isEnabled()) {
                    context.getProfiler().message(this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using structural index '" + index.toString() + "'");
                }
                currentSet = index.findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), null, this);
                currentDocs = docs;
                registerUpdateListener();
            }
            return contextSet.selectParentChild(currentSet, NodeSet.ANCESTOR);
        }
    } else {
        final DocumentSet docs = getDocumentSet(contextSet);
        final StructuralIndex index = context.getBroker().getStructuralIndex();
        if (context.getProfiler().isEnabled()) {
            context.getProfiler().message(this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using structural index '" + index.toString() + "'");
        }
        return index.findAncestorsByTagName(ElementValue.ELEMENT, test.getName(), Constants.PARENT_AXIS, docs, contextSet, contextId);
    }
}
Also used : InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) StructuralIndex(org.exist.indexing.StructuralIndex)

Aggregations

StructuralIndex (org.exist.indexing.StructuralIndex)11 InMemoryNodeSet (org.exist.dom.memtree.InMemoryNodeSet)8 IOException (java.io.IOException)2 StreamFilter (javax.xml.stream.StreamFilter)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 QName (org.exist.dom.QName)2 NodeId (org.exist.numbering.NodeId)2 NodeSet (org.exist.dom.persistent.NodeSet)1 NodeSelector (org.exist.xquery.NodeSelector)1 Sequence (org.exist.xquery.value.Sequence)1 NodeList (org.w3c.dom.NodeList)1