Search in sources :

Example 11 with AtomicValue

use of org.exist.xquery.value.AtomicValue in project exist by eXist-db.

the class NativeValueIndexTest method convertToAtomicNull.

@Test
public void convertToAtomicNull() {
    final AtomicValue result = NativeValueIndex.convertToAtomic(type, null);
    assertNull(result);
}
Also used : AtomicValue(org.exist.xquery.value.AtomicValue) Test(org.junit.Test)

Example 12 with AtomicValue

use of org.exist.xquery.value.AtomicValue in project exist by eXist-db.

the class NativeValueIndexTest method convertToAtomicEmptyString.

@Test
public void convertToAtomicEmptyString() {
    final AtomicValue result = NativeValueIndex.convertToAtomic(type, "");
    assertNull(result);
}
Also used : AtomicValue(org.exist.xquery.value.AtomicValue) Test(org.junit.Test)

Example 13 with AtomicValue

use of org.exist.xquery.value.AtomicValue in project exist by eXist-db.

the class RangeIndexConfigAttributeCondition method findAtomicValue.

private AtomicValue findAtomicValue(Expression expr) {
    if (expr instanceof AtomicValue) {
        return (AtomicValue) expr;
    } else if (expr instanceof LiteralValue) {
        return ((LiteralValue) expr).getValue();
    } else if (expr instanceof VariableReference || expr instanceof Function) {
        try {
            final Sequence contextSequence;
            final ContextItemDeclaration cid = expr.getContext().getContextItemDeclartion();
            if (cid != null) {
                contextSequence = cid.eval(null);
            } else {
                contextSequence = null;
            }
            final Sequence result = expr.eval(contextSequence);
            if (result instanceof AtomicValue) {
                return (AtomicValue) result;
            }
        } catch (XPathException e) {
            RangeIndex.LOG.error(e);
        }
    }
    return null;
}
Also used : AtomicValue(org.exist.xquery.value.AtomicValue) Sequence(org.exist.xquery.value.Sequence)

Example 14 with AtomicValue

use of org.exist.xquery.value.AtomicValue in project exist by eXist-db.

the class RangeIndexConfigAttributeCondition method find.

@Override
public boolean find(Predicate predicate) {
    final Expression inner = this.getInnerExpression(predicate);
    Operator operator;
    Expression lhe;
    Expression rhe;
    // get the type of the expression inside the predicate and determine right and left hand arguments
    if (inner instanceof GeneralComparison) {
        final GeneralComparison comparison = (GeneralComparison) inner;
        operator = RangeQueryRewriter.getOperator(inner);
        lhe = comparison.getLeft();
        rhe = comparison.getRight();
    } else if (inner instanceof InternalFunctionCall) {
        // calls to matches() will not have been rewritten to a comparison, so check for function call
        final Function func = ((InternalFunctionCall) inner).getFunction();
        if (func.isCalledAs("matches")) {
            operator = Operator.MATCH;
            lhe = func.getArgument(0);
            rhe = func.getArgument(1);
            lhe = unwrapSubExpression(lhe);
            rhe = unwrapSubExpression(rhe);
        } else {
            // predicate expression cannot be parsed as condition
            return false;
        }
    } else {
        // predicate expression cannot be parsed as condition
        return false;
    }
    // find the attribute name and value pair from the predicate to check against
    // first assume attribute is on the left and value is on the right
    LocationStep testStep = findLocationStep(lhe);
    AtomicValue testValue = findAtomicValue(rhe);
    switch(this.operator) {
        case EQ:
        case NE:
            // check the other way around
            if (testStep == null && testValue == null) {
                testStep = findLocationStep(rhe);
                testValue = findAtomicValue(lhe);
            }
        case GT:
        case LT:
        case GE:
        case LE:
            // but the operator has to be inverted
            if (testStep == null && testValue == null) {
                testStep = findLocationStep(rhe);
                testValue = findAtomicValue(lhe);
                operator = invertOrdinalOperator(operator);
            }
    }
    if (testStep != null && testValue != null) {
        final QName qname = testStep.getTest().getName();
        Comparable foundValue;
        Comparable requiredValue;
        boolean valueTypeMatches;
        try {
            if (this.numericComparison) {
                valueTypeMatches = testValue instanceof NumericValue;
                requiredValue = this.numericValue;
                foundValue = testValue.toJavaObject(Double.class);
            } else {
                valueTypeMatches = testValue instanceof StringValue;
                if (this.caseSensitive) {
                    requiredValue = this.getLowercaseValue();
                    foundValue = testValue.getStringValue().toLowerCase();
                } else {
                    requiredValue = this.value;
                    foundValue = testValue.getStringValue();
                }
            }
            if (qname.getNameType() == ElementValue.ATTRIBUTE && operator.equals(this.operator) && qname.equals(this.attribute) && valueTypeMatches && foundValue.equals(requiredValue)) {
                return true;
            }
        } catch (XPathException e) {
            RangeIndex.LOG.error("Value conversion error when testing predicate for condition, value: {}", testValue.toString());
            RangeIndex.LOG.error(e);
        }
    }
    return false;
}
Also used : Operator(org.exist.indexing.range.RangeIndex.Operator) QName(org.exist.dom.QName) AtomicValue(org.exist.xquery.value.AtomicValue) NumericValue(org.exist.xquery.value.NumericValue) StringValue(org.exist.xquery.value.StringValue)

Example 15 with AtomicValue

use of org.exist.xquery.value.AtomicValue in project exist by eXist-db.

the class MapExpr method eval.

@Override
public Sequence eval(Sequence contextSequence, final Item contextItem) throws XPathException {
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    final IMap<AtomicValue, Sequence> map = newLinearMap(null);
    boolean firstType = true;
    int prevType = AbstractMapType.UNKNOWN_KEY_TYPE;
    for (final Mapping mapping : this.mappings) {
        final Sequence key = mapping.key.eval(contextSequence);
        if (key.getItemCount() != 1) {
            throw new XPathException(this, MapErrorCode.EXMPDY001, "Expected single value for key, got " + key.getItemCount());
        }
        final AtomicValue atomic = key.itemAt(0).atomize();
        final Sequence value = mapping.value.eval(contextSequence);
        if (map.contains(atomic)) {
            throw new XPathException(this, ErrorCodes.XQDY0137, "Key \"" + atomic.getStringValue() + "\" already exists in map.");
        }
        map.put(atomic, value);
        final int thisType = atomic.getType();
        if (firstType) {
            prevType = thisType;
            firstType = false;
        } else {
            if (thisType != prevType) {
                prevType = AbstractMapType.MIXED_KEY_TYPES;
            }
        }
    }
    return new MapType(context, map.forked(), prevType);
}
Also used : AtomicValue(org.exist.xquery.value.AtomicValue) Sequence(org.exist.xquery.value.Sequence)

Aggregations

AtomicValue (org.exist.xquery.value.AtomicValue)26 Sequence (org.exist.xquery.value.Sequence)16 Collator (com.ibm.icu.text.Collator)9 Item (org.exist.xquery.value.Item)9 SequenceIterator (org.exist.xquery.value.SequenceIterator)9 XPathException (org.exist.xquery.XPathException)6 NumericValue (org.exist.xquery.value.NumericValue)6 StringValue (org.exist.xquery.value.StringValue)5 NodeProxy (org.exist.dom.persistent.NodeProxy)4 ComputableValue (org.exist.xquery.value.ComputableValue)4 IOException (java.io.IOException)3 QName (org.exist.dom.QName)3 QNameValue (org.exist.xquery.value.QNameValue)3 ValueSequence (org.exist.xquery.value.ValueSequence)3 Test (org.junit.Test)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 EXistException (org.exist.EXistException)2 Collection (org.exist.collections.Collection)2 ContextItem (org.exist.dom.persistent.ContextItem)2 NodeSet (org.exist.dom.persistent.NodeSet)2