Search in sources :

Example 21 with NodeSet

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

Example 22 with NodeSet

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

the class FunDoctype 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());
        }
    }
    final MutableDocumentSet docs = new DefaultDocumentSet();
    for (int i = 0; i < getArgumentCount(); i++) {
        final Sequence seq = getArgument(i).eval(contextSequence, contextItem);
        for (final SequenceIterator j = seq.iterate(); j.hasNext(); ) {
            final String next = j.nextItem().getStringValue();
            try {
                context.getBroker().getXMLResourcesByDoctype(next, docs);
            } catch (final PermissionDeniedException | LockException e) {
                LOG.error(e.getMessage(), e);
                throw new XPathException(this, e);
            }
        }
    }
    final NodeSet result = new ExtArrayNodeSet(1);
    for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
        result.add(new NodeProxy(i.next(), NodeId.DOCUMENT_NODE));
    }
    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) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 23 with NodeSet

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

the class ArrayListValueSequence method toNodeSet.

@Override
public NodeSet toNodeSet() throws XPathException {
    if (isEmpty) {
        return NodeSet.EMPTY_SET;
    }
    // for this method to work, all items have to be nodes
    if (itemType != Type.ANY_TYPE && Type.subTypeOf(itemType, Type.NODE)) {
        final NodeSet set = new NewArrayNodeSet();
        for (int i = 0; i <= values.size(); i++) {
            NodeValue v = (NodeValue) values.get(i);
            if (v.getImplementationType() != NodeValue.PERSISTENT_NODE) {
                // found an in-memory document
                final DocumentImpl doc;
                if (v.getType() == Type.DOCUMENT) {
                    doc = (DocumentImpl) v;
                } else {
                    doc = ((NodeImpl) v).getOwnerDocument();
                }
                if (doc == null) {
                    continue;
                }
                // make this document persistent: doc.makePersistent()
                // returns a map of all root node ids mapped to the corresponding
                // persistent node. We scan the current sequence and replace all
                // in-memory nodes with their new persistent node objects.
                final DocumentImpl expandedDoc = doc.expandRefs(null);
                final org.exist.dom.persistent.DocumentImpl newDoc = expandedDoc.makePersistent();
                if (newDoc != null) {
                    NodeId rootId = newDoc.getBrokerPool().getNodeFactory().createInstance();
                    for (int j = i; j <= values.size(); j++) {
                        v = (NodeValue) values.get(j);
                        if (v.getImplementationType() != NodeValue.PERSISTENT_NODE) {
                            NodeImpl node = (NodeImpl) v;
                            final Document nodeOwnerDoc;
                            if (node.getNodeType() == Node.DOCUMENT_NODE) {
                                nodeOwnerDoc = (Document) node;
                            } else {
                                nodeOwnerDoc = node.getOwnerDocument();
                            }
                            if (nodeOwnerDoc == doc) {
                                if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                                    node = expandedDoc.getAttribute(node.getNodeNumber());
                                } else {
                                    node = expandedDoc.getNode(node.getNodeNumber());
                                }
                                NodeId nodeId = node.getNodeId();
                                if (nodeId == null) {
                                    throw new XPathException("Internal error: nodeId == null");
                                }
                                if (node.getNodeType() == Node.DOCUMENT_NODE) {
                                    nodeId = rootId;
                                } else {
                                    nodeId = rootId.append(nodeId);
                                }
                                final NodeProxy p = new NodeProxy(newDoc, nodeId, node.getNodeType());
                                // replace the node by the NodeProxy
                                values.set(j, p);
                                setHasChanged();
                            }
                        }
                    }
                    set.add((NodeProxy) values.get(i));
                }
            } else {
                set.add((NodeProxy) v);
            }
        }
        // }
        return set;
    } else {
        throw new XPathException("Type error: the sequence cannot be converted into" + " a node set. Item type is " + Type.getTypeName(itemType));
    }
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) Document(org.w3c.dom.Document) DocumentImpl(org.exist.dom.memtree.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) NodeId(org.exist.numbering.NodeId)

Example 24 with NodeSet

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

the class GMLIndexTest method lowLevelSearch.

@Test
public void lowLevelSearch() throws EXistException, SAXException, ParserConfigurationException, SpatialIndexException, IOException {
    GMLHandlerJTS geometryHandler = new GeometryHandler();
    GMLFilterGeometry geometryFilter = new GMLFilterGeometry(geometryHandler);
    GMLFilterDocument handler = new GMLFilterDocument(geometryFilter);
    final BrokerPool pool = server.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        AbstractGMLJDBCIndexWorker indexWorker = (AbstractGMLJDBCIndexWorker) broker.getIndexController().getWorkerByIndexId(AbstractGMLJDBCIndex.ID);
        // Unplugged
        if (indexWorker != null) {
            SAXParserFactory factory = ExistSAXParserFactory.getSAXParserFactory();
            factory.setNamespaceAware(true);
            InputSource src = new InputSource(new StringReader(IN_MEMORY_GML));
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            SAXAdapter adapter = new SAXAdapter();
            reader.setContentHandler(handler);
            reader.setProperty("http://xml.org/sax/properties/lexical-handler", adapter);
            reader.parse(src);
            Geometry EPSG4326_geometry = indexWorker.transformGeometry(currentGeometry, "osgb:BNG", "EPSG:4326");
            assertNotNull(EPSG4326_geometry);
            NodeSet ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.EQUALS);
            assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.DISJOINT);
            assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.INTERSECTS);
            assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.TOUCHES);
            // assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.CROSSES);
            // assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.WITHIN);
            assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.CONTAINS);
            assertTrue(ns.getLength() > 0);
        // ns = ((GMLIndexWorker)index.getWorker()).search(broker, EPSG4326_geometry, SpatialOperator.OVERLAPS);
        // assertTrue(ns.getLength() > 0);
        }
    }
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) FileInputSource(org.exist.util.FileInputSource) GMLHandlerJTS(org.geotools.gml.GMLHandlerJTS) GMLFilterDocument(org.geotools.gml.GMLFilterDocument) Geometry(com.vividsolutions.jts.geom.Geometry) GMLFilterGeometry(org.geotools.gml.GMLFilterGeometry) DBBroker(org.exist.storage.DBBroker) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) SAXAdapter(org.exist.dom.memtree.SAXAdapter) GMLFilterGeometry(org.geotools.gml.GMLFilterGeometry) BrokerPool(org.exist.storage.BrokerPool) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ExistSAXParserFactory(org.exist.util.ExistSAXParserFactory)

Example 25 with NodeSet

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

the class FilteredExpression 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());
        }
    }
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    Sequence result;
    final Sequence seq = expression.eval(contextSequence, contextItem);
    if (seq.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final Predicate pred = predicates.get(0);
        context.setContextSequencePosition(0, seq);
        // If the predicate is known to return a node set, no special treatment is required.
        if (abbreviated && (pred.getExecutionMode() != Predicate.ExecutionMode.NODE || !seq.isPersistentSet())) {
            result = new ValueSequence();
            if (seq.isPersistentSet()) {
                final NodeSet contextSet = seq.toNodeSet();
                final Sequence outerSequence = contextSet.getParents(getExpressionId());
                for (final SequenceIterator i = outerSequence.iterate(); i.hasNext(); ) {
                    final NodeValue node = (NodeValue) i.nextItem();
                    final Sequence newContextSeq = contextSet.selectParentChild((NodeSet) node, NodeSet.DESCENDANT, getExpressionId());
                    final Sequence temp = processPredicate(outerSequence, newContextSeq);
                    result.addAll(temp);
                }
            } else {
                final MemoryNodeSet nodes = seq.toMemNodeSet();
                final Sequence outerSequence = nodes.getParents(new AnyNodeTest());
                for (final SequenceIterator i = outerSequence.iterate(); i.hasNext(); ) {
                    final NodeValue node = (NodeValue) i.nextItem();
                    final Sequence newSet = nodes.getChildrenForParent((NodeImpl) node);
                    final Sequence temp = processPredicate(outerSequence, newSet);
                    result.addAll(temp);
                }
            }
        } else {
            result = processPredicate(contextSequence, seq);
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet)

Aggregations

NodeSet (org.exist.dom.persistent.NodeSet)49 NodeProxy (org.exist.dom.persistent.NodeProxy)18 Sequence (org.exist.xquery.value.Sequence)18 DocumentSet (org.exist.dom.persistent.DocumentSet)14 NewArrayNodeSet (org.exist.dom.persistent.NewArrayNodeSet)12 QName (org.exist.dom.QName)11 ExtArrayNodeSet (org.exist.dom.persistent.ExtArrayNodeSet)10 VirtualNodeSet (org.exist.dom.persistent.VirtualNodeSet)9 ContextItem (org.exist.dom.persistent.ContextItem)8 IOException (java.io.IOException)7 XPathException (org.exist.xquery.XPathException)7 Collator (com.ibm.icu.text.Collator)4 EXistException (org.exist.EXistException)4 DocumentImpl (org.exist.dom.persistent.DocumentImpl)4 EmptyNodeSet (org.exist.dom.persistent.EmptyNodeSet)4 Indexable (org.exist.storage.Indexable)4 SequenceIterator (org.exist.xquery.value.SequenceIterator)4 StringValue (org.exist.xquery.value.StringValue)4 NodeImpl (org.exist.dom.memtree.NodeImpl)3 Match (org.exist.dom.persistent.Match)3