Search in sources :

Example 1 with SortIndexWorker

use of org.exist.indexing.sort.SortIndexWorker in project exist by eXist-db.

the class RemoveIndex method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final SortIndexWorker index = (SortIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(SortIndex.ID);
    final String id = args[0].getStringValue();
    try {
        if (getArgumentCount() == 2) {
            final NodeValue nv = (NodeValue) args[1].itemAt(0);
            if (nv.getImplementationType() == NodeValue.IN_MEMORY_NODE)
                throw new XPathException(this, "Second argument to remove should be a persistent node, not " + "an in-memory node.");
            final NodeProxy proxy = (NodeProxy) nv;
            index.remove(id, proxy.getOwnerDocument());
        } else {
            index.remove(id);
        }
    } catch (final EXistException e) {
        throw new XPathException(this, e.getMessage(), e);
    } catch (final LockException e) {
        throw new XPathException(this, "Caught lock error while removing index. Giving up.", e);
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : SortIndexWorker(org.exist.indexing.sort.SortIndexWorker) LockException(org.exist.util.LockException) EXistException(org.exist.EXistException) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 2 with SortIndexWorker

use of org.exist.indexing.sort.SortIndexWorker in project exist by eXist-db.

the class GetIndex method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (args[1].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    final String id = args[0].getStringValue();
    final NodeProxy node = (NodeProxy) args[1].itemAt(0);
    final SortIndexWorker index = (SortIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(SortIndex.ID);
    long pos = 0;
    try {
        pos = index.getIndex(id, node);
    } catch (final EXistException e) {
        throw new XPathException(this, e.getMessage(), e);
    } catch (final LockException e) {
        throw new XPathException(this, "Caught lock error while searching index. Giving up.", e);
    }
    return pos < 0 ? Sequence.EMPTY_SEQUENCE : new IntegerValue(pos, Type.LONG);
}
Also used : SortIndexWorker(org.exist.indexing.sort.SortIndexWorker) LockException(org.exist.util.LockException) EXistException(org.exist.EXistException) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 3 with SortIndexWorker

use of org.exist.indexing.sort.SortIndexWorker in project exist by eXist-db.

the class CreateOrderIndex method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (args[1].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    final String id = args[0].getStringValue();
    // check how the function was called and prepare callback
    FunctionReference call = null;
    if (isCalledAs("create-index-callback")) {
        call = (FunctionReference) args[2].itemAt(0);
    } else if (args[2].getItemCount() != args[1].getItemCount())
        throw new XPathException(this, "$nodes and $values sequences need to have the same length.");
    // options
    if (args[3].getItemCount() > 0) {
        final NodeValue optionValue = (NodeValue) args[3].itemAt(0);
        final Element options = (Element) optionValue.getNode();
        String option = options.getAttribute("order");
        if (option != null) {
            descending = option.equalsIgnoreCase("descending");
        }
        option = options.getAttribute("empty");
        if (option != null) {
            emptyLeast = option.equalsIgnoreCase("least");
        }
    }
    // create the input list to be sorted below
    final List<SortItem> items = new ArrayList<>(args[1].getItemCount());
    final Sequence[] params = new Sequence[1];
    SequenceIterator valuesIter = null;
    if (call == null)
        valuesIter = args[2].iterate();
    int c = 0;
    final int len = args[1].getItemCount();
    final int logChunk = 1 + (len / 20);
    for (final SequenceIterator nodesIter = args[1].iterate(); nodesIter.hasNext(); ) {
        final NodeValue nv = (NodeValue) nodesIter.nextItem();
        if (nv.getImplementationType() == NodeValue.IN_MEMORY_NODE)
            throw new XPathException(this, "Cannot create order-index on an in-memory node");
        final NodeProxy node = (NodeProxy) nv;
        final SortItem si = new SortItemImpl(node);
        if (LOG.isDebugEnabled() && ++c % logChunk == 0) {
            LOG.debug("Storing item {} out of {} to sort index.", c, len);
        }
        if (call != null) {
            // call the callback function to get value
            params[0] = node;
            final Sequence r = call.evalFunction(contextSequence, null, params);
            if (!r.isEmpty()) {
                AtomicValue v = r.itemAt(0).atomize();
                if (v.getType() == Type.UNTYPED_ATOMIC)
                    v = v.convertTo(Type.STRING);
                si.setValue(v);
            }
        } else {
            // no callback, take value from second sequence
            AtomicValue v = valuesIter.nextItem().atomize();
            if (v.getType() == Type.UNTYPED_ATOMIC)
                v = v.convertTo(Type.STRING);
            si.setValue(v);
        }
        items.add(si);
    }
    // sort the set
    FastQSort.sort(items, 0, items.size() - 1);
    // create the index
    final SortIndexWorker index = (SortIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(SortIndex.ID);
    try {
        index.createIndex(id, items);
    } catch (final EXistException e) {
        throw new XPathException(this, e.getMessage(), e);
    } catch (final LockException e) {
        throw new XPathException(this, "Caught lock error while creating index. Giving up.", e);
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : SortIndexWorker(org.exist.indexing.sort.SortIndexWorker) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) EXistException(org.exist.EXistException) NodeProxy(org.exist.dom.persistent.NodeProxy) SortItem(org.exist.indexing.sort.SortItem) LockException(org.exist.util.LockException)

Example 4 with SortIndexWorker

use of org.exist.indexing.sort.SortIndexWorker in project exist by eXist-db.

the class HasIndex method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final String id = args[0].getStringValue();
    final SortIndexWorker index = (SortIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(SortIndex.ID);
    try {
        return BooleanValue.valueOf(index.hasIndex(id));
    } catch (final EXistException e) {
        throw new XPathException(this, e.getMessage(), e);
    } catch (final LockException e) {
        throw new XPathException(this, "Caught lock error while searching index. Giving up.", e);
    }
}
Also used : SortIndexWorker(org.exist.indexing.sort.SortIndexWorker) LockException(org.exist.util.LockException) EXistException(org.exist.EXistException)

Aggregations

EXistException (org.exist.EXistException)4 SortIndexWorker (org.exist.indexing.sort.SortIndexWorker)4 LockException (org.exist.util.LockException)4 NodeProxy (org.exist.dom.persistent.NodeProxy)3 ArrayList (java.util.ArrayList)1 SortItem (org.exist.indexing.sort.SortItem)1 Element (org.w3c.dom.Element)1