Search in sources :

Example 41 with DocumentSet

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

the class FunDoc 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;
    final Sequence arg = getArgument(0).eval(contextSequence, contextItem);
    if (arg.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final String path = arg.itemAt(0).getStringValue();
        // }
        try {
            result = DocUtils.getDocument(this.context, path);
            if (result.isEmpty() && context.isRaiseErrorOnFailedRetrieval()) {
                throw new XPathException(this, ErrorCodes.FODC0002, "Can not access '" + path + "'", arg);
            }
            // TODO: we still need a final decision about this. Also check base-uri.
            // if (result == Sequence.EMPTY_SEQUENCE)
            // throw new XPathException(this, path + " is not an XML document");
            final DocumentSet docs = result.getDocumentSet();
            if (docs != null && DocumentSet.EMPTY_DOCUMENT_SET != docs) {
                // only cache node sets (which have a non-empty document set)
                registerUpdateListener();
            }
        } catch (final Exception e) {
            throw new XPathException(this, ErrorCodes.FODC0005, e.getMessage(), arg, e);
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Sequence(org.exist.xquery.value.Sequence) DocumentSet(org.exist.dom.persistent.DocumentSet)

Example 42 with DocumentSet

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

the class IndexKeyDocuments method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) 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);
        }
    }
    Sequence result;
    if (args[0].isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final NodeSet nodes = args[0].toNodeSet();
        final DocumentSet docs = nodes.getDocumentSet();
        if (this.getArgumentCount() == 3) {
            final IndexWorker indexWorker = context.getBroker().getIndexController().getWorkerByIndexName(args[2].itemAt(0).getStringValue());
            // IndexWorker indexWorker = context.getBroker().getBrokerPool().getIndexManager().getIndexByName(args[2].itemAt(0).getStringValue()).getWorker();
            if (indexWorker == null) {
                throw new XPathException(this, "Unknown index: " + args[2].itemAt(0).getStringValue());
            }
            final Map<String, Object> hints = new HashMap<>();
            if (indexWorker instanceof OrderedValuesIndex) {
                hints.put(OrderedValuesIndex.START_VALUE, args[1]);
            } else {
                logger.warn("{} isn't an instance of org.exist.indexing.OrderedIndexWorker. Start value '{}' ignored.", indexWorker.getClass().getName(), args[1]);
            }
            final Occurrences[] occur = indexWorker.scanIndex(context, docs, nodes, hints);
            if (occur.length == 0) {
                result = Sequence.EMPTY_SEQUENCE;
            } else {
                result = new IntegerValue(occur[0].getDocuments());
            }
        } else {
            final ValueOccurrences[] occur = context.getBroker().getValueIndex().scanIndexKeys(docs, nodes, (Indexable) args[1]);
            if (occur.length == 0) {
                result = Sequence.EMPTY_SEQUENCE;
            } else {
                result = new IntegerValue(occur[0].getDocuments());
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) XPathException(org.exist.xquery.XPathException) HashMap(java.util.HashMap) IntegerValue(org.exist.xquery.value.IntegerValue) ValueOccurrences(org.exist.util.ValueOccurrences) OrderedValuesIndex(org.exist.indexing.OrderedValuesIndex) Occurrences(org.exist.util.Occurrences) ValueOccurrences(org.exist.util.ValueOccurrences) Sequence(org.exist.xquery.value.Sequence) IndexWorker(org.exist.indexing.IndexWorker) DocumentSet(org.exist.dom.persistent.DocumentSet)

Example 43 with DocumentSet

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

the class IndexKeyOccurrences method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) 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);
        }
    }
    Sequence result;
    if (args[0].isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final NodeSet nodes = args[0].toNodeSet();
        final DocumentSet docs = nodes.getDocumentSet();
        if (this.getArgumentCount() == 3) {
            final IndexWorker indexWorker = context.getBroker().getIndexController().getWorkerByIndexName(args[2].itemAt(0).getStringValue());
            // IndexWorker indexWorker = context.getBroker().getBrokerPool().getIndexManager().getIndexByName(args[2].itemAt(0).getStringValue()).getWorker();
            if (indexWorker == null) {
                throw new XPathException(this, "Unknown index: " + args[2].itemAt(0).getStringValue());
            }
            final Map<String, Object> hints = new HashMap<>();
            if (indexWorker instanceof OrderedValuesIndex) {
                hints.put(OrderedValuesIndex.START_VALUE, args[1]);
            } else {
                logger.warn("{} isn't an instance of org.exist.indexing.OrderedIndexWorker. Start value '{}' ignored.", indexWorker.getClass().getName(), args[1]);
            }
            final Occurrences[] occur = indexWorker.scanIndex(context, docs, nodes, hints);
            if (occur.length == 0) {
                result = Sequence.EMPTY_SEQUENCE;
            } else {
                result = new IntegerValue(occur[0].getOccurrences());
            }
        } else {
            ValueOccurrences[] occur = context.getBroker().getValueIndex().scanIndexKeys(docs, nodes, (Indexable) (args[1].itemAt(0)));
            if (occur.length == 0) {
                occur = context.getBroker().getValueIndex().scanIndexKeys(docs, nodes, null, (Indexable) (args[1].itemAt(0)));
            }
            if (occur.length == 0) {
                result = Sequence.EMPTY_SEQUENCE;
            } else {
                result = new IntegerValue(occur[0].getOccurrences());
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) XPathException(org.exist.xquery.XPathException) HashMap(java.util.HashMap) IntegerValue(org.exist.xquery.value.IntegerValue) ValueOccurrences(org.exist.util.ValueOccurrences) OrderedValuesIndex(org.exist.indexing.OrderedValuesIndex) Occurrences(org.exist.util.Occurrences) ValueOccurrences(org.exist.util.ValueOccurrences) Sequence(org.exist.xquery.value.Sequence) IndexWorker(org.exist.indexing.IndexWorker) Indexable(org.exist.storage.Indexable) DocumentSet(org.exist.dom.persistent.DocumentSet)

Example 44 with DocumentSet

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

the class LuceneIndexKeys method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String fieldName = args[0].getStringValue();
    final DocumentSet docs = contextSequence == null ? context.getStaticallyKnownDocuments() : contextSequence.getDocumentSet();
    final Map<String, Object> hints = new HashMap<>();
    if (!args[1].isEmpty()) {
        hints.put(OrderedValuesIndex.START_VALUE, args[1].getStringValue());
    }
    IntegerValue max = null;
    if (args[3].hasOne()) {
        max = ((IntegerValue) args[3].itemAt(0));
    }
    if (max != null && max.getInt() > -1) {
        hints.put(IndexWorker.VALUE_COUNT, max);
    }
    final Sequence result = new ValueSequence();
    try (final FunctionReference ref = (FunctionReference) args[2].itemAt(0)) {
        final LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        final Occurrences[] occur = index.scanIndexByField(fieldName, docs, hints);
        final Sequence[] params = new Sequence[2];
        final ValueSequence data = new ValueSequence();
        for (int j = 0; j < occur.length; j++) {
            params[0] = new StringValue(occur[j].getTerm().toString());
            data.add(new IntegerValue(occur[j].getOccurrences(), Type.UNSIGNED_INT));
            data.add(new IntegerValue(occur[j].getDocuments(), Type.UNSIGNED_INT));
            data.add(new IntegerValue(j + 1, Type.UNSIGNED_INT));
            params[1] = data;
            result.addAll(ref.evalFunction(Sequence.EMPTY_SEQUENCE, null, params));
            data.clear();
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Occurrences(org.exist.util.Occurrences) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) DocumentSet(org.exist.dom.persistent.DocumentSet)

Example 45 with DocumentSet

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

the class QueryField method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (contextItem != null)
        contextSequence = contextItem.toSequence();
    NodeSet result;
    if (preselectResult == null) {
        long start = System.currentTimeMillis();
        String field = getArgument(0).eval(contextSequence).getStringValue();
        Item query = getKey(contextSequence, null);
        DocumentSet docs = null;
        if (contextSequence == null)
            docs = context.getStaticallyKnownDocuments();
        else
            docs = contextSequence.getDocumentSet();
        NodeSet contextSet = null;
        if (contextSequence != null)
            contextSet = contextSequence.toNodeSet();
        LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        QueryOptions options = parseOptions(this, contextSequence, contextItem, 3);
        try {
            if (Type.subTypeOf(query.getType(), Type.ELEMENT))
                result = index.queryField(getExpressionId(), docs, contextSet, field, (Element) ((NodeValue) query).getNode(), NodeSet.ANCESTOR, options);
            else
                result = index.queryField(context, getExpressionId(), docs, contextSet, field, query.getStringValue(), NodeSet.ANCESTOR, options);
        } catch (IOException e) {
            throw new XPathException(this, e.getMessage());
        }
        if (context.getProfiler().traceFunctions()) {
            context.getProfiler().traceIndexUsage(context, "lucene", this, PerformanceStats.BASIC_INDEX, System.currentTimeMillis() - start);
        }
    } else {
        result = preselectResult.selectAncestorDescendant(contextSequence.toNodeSet(), NodeSet.DESCENDANT, true, getContextId(), true);
    }
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) DocumentSet(org.exist.dom.persistent.DocumentSet) IOException(java.io.IOException) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker)

Aggregations

DocumentSet (org.exist.dom.persistent.DocumentSet)50 QName (org.exist.dom.QName)20 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)18 Sequence (org.exist.xquery.value.Sequence)18 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)16 NodeSet (org.exist.dom.persistent.NodeSet)14 DBBroker (org.exist.storage.DBBroker)14 BrokerPool (org.exist.storage.BrokerPool)13 CompiledXQuery (org.exist.xquery.CompiledXQuery)12 XQuery (org.exist.xquery.XQuery)12 IOException (java.io.IOException)9 Txn (org.exist.storage.txn.Txn)9 TransactionManager (org.exist.storage.txn.TransactionManager)8 Test (org.junit.Test)7 DocumentImpl (org.exist.dom.persistent.DocumentImpl)6 InputSource (org.xml.sax.InputSource)6 StringReader (java.io.StringReader)5 LuceneIndexWorker (org.exist.indexing.lucene.LuceneIndexWorker)5 XPathException (org.exist.xquery.XPathException)5 VirtualNodeSet (org.exist.dom.persistent.VirtualNodeSet)4