Search in sources :

Example 6 with Occurrences

use of org.exist.util.Occurrences in project exist by eXist-db.

the class IndexKeys method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    int arg = 0;
    final String field = args[arg++].getStringValue();
    String start = null;
    if (args.length == 4) {
        start = args[arg++].getStringValue();
    }
    try (final FunctionReference ref = (FunctionReference) args[arg++].itemAt(0)) {
        int max = -1;
        if (!args[arg].isEmpty()) {
            max = ((IntegerValue) args[arg].itemAt(0)).getInt();
        }
        final Sequence result = new ValueSequence();
        final RangeIndexWorker worker = (RangeIndexWorker) context.getBroker().getIndexController().getWorkerByIndexName("range-index");
        Occurrences[] occur = worker.scanIndexByField(field, contextSequence == null ? context.getStaticallyKnownDocuments() : contextSequence.getDocumentSet(), start, max);
        final int len = (max != -1 && occur.length > max ? max : occur.length);
        final Sequence[] params = new Sequence[2];
        ValueSequence data = new ValueSequence();
        for (int j = 0; j < len; 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 : RangeIndexWorker(org.exist.indexing.range.RangeIndexWorker) Occurrences(org.exist.util.Occurrences)

Example 7 with Occurrences

use of org.exist.util.Occurrences in project exist by eXist-db.

the class RemoteIndexQueryService method getIndexedElements.

@Override
public Occurrences[] getIndexedElements(final boolean inclusive) throws XMLDBException {
    final List<Object> params = new ArrayList<>();
    params.add(collection.getPath());
    params.add(inclusive);
    final Object[] result = (Object[]) collection.execute("getIndexedElements", params);
    final Stream<Occurrences> occurrences = Arrays.stream(result).map(o -> (Object[]) o).map(row -> new Occurrences(new QName(row[0].toString(), row[1].toString(), row[2].toString()), (Integer) row[3]));
    return occurrences.toArray(Occurrences[]::new);
}
Also used : XmlRpcException(org.apache.xmlrpc.XmlRpcException) XMLDBException(org.xmldb.api.base.XMLDBException) Arrays(java.util.Arrays) List(java.util.List) Stream(java.util.stream.Stream) Occurrences(org.exist.util.Occurrences) QName(org.exist.dom.QName) URISyntaxException(java.net.URISyntaxException) XmlRpcClient(org.apache.xmlrpc.client.XmlRpcClient) ErrorCodes(org.xmldb.api.base.ErrorCodes) ArrayList(java.util.ArrayList) Collection(org.xmldb.api.base.Collection) QName(org.exist.dom.QName) ArrayList(java.util.ArrayList) Occurrences(org.exist.util.Occurrences)

Example 8 with Occurrences

use of org.exist.util.Occurrences 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 9 with Occurrences

use of org.exist.util.Occurrences 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 10 with Occurrences

use of org.exist.util.Occurrences 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)

Aggregations

Occurrences (org.exist.util.Occurrences)11 QName (org.exist.dom.QName)5 DocumentSet (org.exist.dom.persistent.DocumentSet)4 HashMap (java.util.HashMap)3 NodeSet (org.exist.dom.persistent.NodeSet)3 IndexWorker (org.exist.indexing.IndexWorker)3 OrderedValuesIndex (org.exist.indexing.OrderedValuesIndex)3 ValueOccurrences (org.exist.util.ValueOccurrences)3 DocumentImpl (org.exist.dom.persistent.DocumentImpl)2 Indexable (org.exist.storage.Indexable)2 XPathException (org.exist.xquery.XPathException)2 IntegerValue (org.exist.xquery.value.IntegerValue)2 Sequence (org.exist.xquery.value.Sequence)2 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Stream (java.util.stream.Stream)1 Bits (org.apache.lucene.util.Bits)1