Search in sources :

Example 11 with NodeProxy

use of org.exist.dom.persistent.NodeProxy 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 12 with NodeProxy

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

the class IntersectTest method memtree_intersect_persistent.

/**
 * Tests the XQuery `intersect` operator against an
 * in-memory node on the left and a persistent node on the right
 */
@Test
public void memtree_intersect_persistent() throws XPathException, NoSuchMethodException {
    final XQueryContext mockContext = createMock(XQueryContext.class);
    final PathExpr mockLeft = createMock(PathExpr.class);
    final PathExpr mockRight = createMock(PathExpr.class);
    final Sequence mockContextSequence = createMock(Sequence.class);
    final Item mockContextItem = createMock(Item.class);
    final Profiler mockProfiler = createMock(Profiler.class);
    final DocumentImpl mockPersistentDoc = createMock(DocumentImpl.class);
    final NodeProxy mockPersistentNode = createMockBuilder(NodeProxy.class).withConstructor(DocumentImpl.class, NodeId.class).withArgs(mockPersistentDoc, new DLN(1)).addMockedMethods(NodeProxy.class.getMethod("isEmpty", new Class[] {}), NodeProxy.class.getMethod("getItemType", new Class[] {}), NodeProxy.class.getMethod("equals", new Class[] { Object.class })).createMock();
    expect(mockContext.nextExpressionId()).andReturn(1);
    expect(mockContext.getProfiler()).andReturn(mockProfiler);
    // memtree node
    expect(mockLeft.eval(mockContextSequence, mockContextItem)).andReturn((org.exist.dom.memtree.ElementImpl) createInMemoryDocument().getDocumentElement());
    // persistent node
    expect(mockRight.eval(mockContextSequence, mockContextItem)).andReturn(mockPersistentNode);
    expect(mockPersistentNode.isEmpty()).andReturn(false);
    expect(mockPersistentNode.getItemType()).andReturn(Type.NODE);
    expect(mockContext.getProfiler()).andReturn(mockProfiler);
    replay(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
    // test
    final Intersect intersect = new Intersect(mockContext, mockLeft, mockRight);
    final Sequence result = intersect.eval(mockContextSequence, mockContextItem);
    assertEquals(0, ((ValueSequence) result).size());
    verify(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
}
Also used : Item(org.exist.xquery.value.Item) DLN(org.exist.numbering.DLN) NodeId(org.exist.numbering.NodeId) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) Test(org.junit.Test)

Example 13 with NodeProxy

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

the class LuceneMatchListener method reset.

protected void reset(final DBBroker broker, final NodeProxy proxy) {
    this.broker = broker;
    this.match = proxy.getMatches();
    setNextInChain(null);
    final IndexSpec indexConf = proxy.getOwnerDocument().getCollection().getIndexConfiguration(broker);
    if (indexConf != null) {
        config = (LuceneConfig) indexConf.getCustomIndexSpec(LuceneIndex.ID);
    } else {
        config = LuceneConfig.DEFAULT_CONFIG;
    }
    getTerms();
    nodesWithMatch = new TreeMap<>();
    /* Check if an index is defined on an ancestor of the current node.
        * If yes, scan the ancestor to get the offset of the first character
        * in the current node. For example, if the indexed node is &lt;a>abc&lt;b>de&lt;/b></a>
        * and we query for //a[text:ngram-contains(., 'de')]/b, proxy will be a &lt;b> node, but
        * the offsets of the matches are relative to the start of &lt;a>.
        */
    NodeSet ancestors = null;
    Match nextMatch = this.match;
    while (nextMatch != null) {
        if (proxy.getNodeId().isDescendantOf(nextMatch.getNodeId())) {
            if (ancestors == null) {
                ancestors = new NewArrayNodeSet();
            }
            ancestors.add(new NodeProxy(proxy.getOwnerDocument(), nextMatch.getNodeId()));
        }
        nextMatch = nextMatch.getNextMatch();
    }
    if (ancestors != null && !ancestors.isEmpty()) {
        for (final NodeProxy p : ancestors) {
            scanMatches(p);
        }
    }
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) IndexSpec(org.exist.storage.IndexSpec) NodeProxy(org.exist.dom.persistent.NodeProxy) Match(org.exist.dom.persistent.Match)

Example 14 with NodeProxy

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

the class Score method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    NodeValue nodeValue = (NodeValue) args[0].itemAt(0);
    if (nodeValue.getImplementationType() != NodeValue.PERSISTENT_NODE) {
        return Sequence.EMPTY_SEQUENCE;
    }
    NodeProxy proxy = (NodeProxy) nodeValue;
    Match match = proxy.getMatches();
    float score = 0.0f;
    while (match != null) {
        if (match.getIndexId().equals(LuceneIndex.ID)) {
            float currentScore = ((LuceneMatch) match).getScore();
            score += currentScore;
        }
        match = match.getNextMatch();
    }
    return new FloatValue(score);
}
Also used : LuceneMatch(org.exist.indexing.lucene.LuceneMatch) NodeProxy(org.exist.dom.persistent.NodeProxy) Match(org.exist.dom.persistent.Match) LuceneMatch(org.exist.indexing.lucene.LuceneMatch)

Example 15 with NodeProxy

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

the class Facets method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String dimension = args[1].getStringValue();
    int count = Integer.MAX_VALUE;
    if (getArgumentCount() == 3 && args[2].hasOne()) {
        count = ((IntegerValue) args[2].itemAt(0)).getInt();
    }
    String[] paths = null;
    if (getArgumentCount() == 4 && !args[3].isEmpty()) {
        paths = new String[args[3].getItemCount()];
        int j = 0;
        for (SequenceIterator i = args[3].unorderedIterator(); i.hasNext(); j++) {
            paths[j] = i.nextItem().getStringValue();
        }
    }
    // Find all lucene queries referenced from the input sequence and remember
    // the first match for each. Every query will have its own facets attached,
    // so we have to merge them below.
    final Map<Query, LuceneMatch> luceneQueries = new IdentityHashMap<>();
    for (final SequenceIterator i = args[0].unorderedIterator(); i.hasNext(); ) {
        final NodeValue nv = (NodeValue) i.nextItem();
        if (nv.getImplementationType() == NodeValue.PERSISTENT_NODE) {
            final NodeProxy proxy = (NodeProxy) nv;
            Match match = proxy.getMatches();
            while (match != null) {
                if (match.getIndexId().equals(LuceneIndex.ID)) {
                    final LuceneMatch luceneMatch = (LuceneMatch) match;
                    luceneQueries.putIfAbsent(luceneMatch.getQuery(), luceneMatch);
                }
                match = match.getNextMatch();
            }
        }
    }
    // Iterate the found queries/matches and collect facets for each
    final IMap<AtomicValue, Sequence> map = newLinearMap(null);
    for (LuceneMatch match : luceneQueries.values()) {
        try {
            addFacetsToMap(map, dimension, count, paths, match);
        } catch (IOException e) {
            throw new XPathException(this, LuceneModule.EXXQDYFT0002, e.getMessage());
        }
    }
    return new MapType(context, map.forked(), Type.STRING);
}
Also used : Query(org.apache.lucene.search.Query) IdentityHashMap(java.util.IdentityHashMap) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) MapType(org.exist.xquery.functions.map.MapType) Match(org.exist.dom.persistent.Match) LuceneMatch(org.exist.indexing.lucene.LuceneMatch) LuceneMatch(org.exist.indexing.lucene.LuceneMatch)

Aggregations

NodeProxy (org.exist.dom.persistent.NodeProxy)79 DocumentImpl (org.exist.dom.persistent.DocumentImpl)18 Sequence (org.exist.xquery.value.Sequence)17 NodeSet (org.exist.dom.persistent.NodeSet)16 NodeId (org.exist.numbering.NodeId)16 XPathException (org.exist.xquery.XPathException)16 IOException (java.io.IOException)12 NewArrayNodeSet (org.exist.dom.persistent.NewArrayNodeSet)10 PermissionDeniedException (org.exist.security.PermissionDeniedException)10 LockException (org.exist.util.LockException)10 NodeValue (org.exist.xquery.value.NodeValue)10 Node (org.w3c.dom.Node)9 Document (org.w3c.dom.Document)8 SAXException (org.xml.sax.SAXException)8 NodeImpl (org.exist.dom.memtree.NodeImpl)7 ExtArrayNodeSet (org.exist.dom.persistent.ExtArrayNodeSet)7 SequenceIterator (org.exist.xquery.value.SequenceIterator)7 ReentrantLock (java.util.concurrent.locks.ReentrantLock)6 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)6 Match (org.exist.dom.persistent.Match)6