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;
}
}
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);
}
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;
}
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;
}
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;
}
Aggregations