Search in sources :

Example 1 with OBinaryField

use of com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField in project orientdb by orientechnologies.

the class OQueryOperatorEquality method evaluateRecord.

@Override
public Object evaluateRecord(final OIdentifiable iRecord, ODocument iCurrentResult, final OSQLFilterCondition iCondition, final Object iLeft, final Object iRight, OCommandContext iContext) {
    if (iLeft instanceof OBinaryField && iRight instanceof OBinaryField)
        // BINARY COMPARISON
        return evaluate((OBinaryField) iLeft, (OBinaryField) iRight, iContext);
    else if (iLeft instanceof OQueryRuntimeValueMulti) {
        // LEFT = MULTI
        final OQueryRuntimeValueMulti left = (OQueryRuntimeValueMulti) iLeft;
        if (left.getValues().length == 0)
            return false;
        if (left.getDefinition().getRoot().startsWith(OSQLFilterItemFieldAll.NAME)) {
            // ALL VALUES
            for (int i = 0; i < left.getValues().length; ++i) {
                Object v = left.getValues()[i];
                Object r = iRight;
                final OCollate collate = left.getCollate(i);
                if (collate != null) {
                    v = collate.transform(v);
                    r = collate.transform(iRight);
                }
                if (v == null || !evaluateExpression(iRecord, iCondition, v, r, iContext))
                    return false;
            }
            return true;
        } else {
            // ANY VALUES
            for (int i = 0; i < left.getValues().length; ++i) {
                Object v = left.getValues()[i];
                Object r = iRight;
                final OCollate collate = left.getCollate(i);
                if (collate != null) {
                    v = collate.transform(v);
                    r = collate.transform(iRight);
                }
                if (v != null && evaluateExpression(iRecord, iCondition, v, r, iContext))
                    return true;
            }
            return false;
        }
    } else if (iRight instanceof OQueryRuntimeValueMulti) {
        // RIGHT = MULTI
        final OQueryRuntimeValueMulti right = (OQueryRuntimeValueMulti) iRight;
        if (right.getValues().length == 0)
            return false;
        if (right.getDefinition().getRoot().startsWith(OSQLFilterItemFieldAll.NAME)) {
            // ALL VALUES
            for (int i = 0; i < right.getValues().length; ++i) {
                Object v = right.getValues()[i];
                Object l = iLeft;
                final OCollate collate = right.getCollate(i);
                if (collate != null) {
                    v = collate.transform(v);
                    l = collate.transform(iLeft);
                }
                if (v == null || !evaluateExpression(iRecord, iCondition, l, v, iContext))
                    return false;
            }
            return true;
        } else {
            // ANY VALUES
            for (int i = 0; i < right.getValues().length; ++i) {
                Object v = right.getValues()[i];
                Object l = iLeft;
                final OCollate collate = right.getCollate(i);
                if (collate != null) {
                    v = collate.transform(v);
                    l = collate.transform(iLeft);
                }
                if (v != null && evaluateExpression(iRecord, iCondition, l, v, iContext))
                    return true;
            }
            return false;
        }
    } else {
        // SINGLE SIMPLE ITEM
        return evaluateExpression(iRecord, iCondition, iLeft, iRight, iContext);
    }
}
Also used : OBinaryField(com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField) OCollate(com.orientechnologies.orient.core.collate.OCollate) OQueryRuntimeValueMulti(com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti)

Example 2 with OBinaryField

use of com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField in project orientdb by orientechnologies.

the class AbstractComparatorTest method field.

protected OBinaryField field(final OType type, final Object value, OCollate collate) {
    BytesContainer bytes = new BytesContainer();
    bytes.offset = serializer.serializeValue(bytes, value, type, null);
    return new OBinaryField(null, type, bytes, collate);
}
Also used : BytesContainer(com.orientechnologies.orient.core.serialization.serializer.record.binary.BytesContainer) OBinaryField(com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField)

Example 3 with OBinaryField

use of com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField in project orientdb by orientechnologies.

the class BinaryComparatorEqualsTest method testBinaryFieldCopy.

@Test
public void testBinaryFieldCopy() {
    final OBinaryField f = field(OType.BYTE, 10, new OCaseInsensitiveCollate()).copy();
    Assert.assertEquals(f.type, OType.BYTE);
    Assert.assertNotNull(f.bytes);
    Assert.assertEquals(f.collate.getName(), OCaseInsensitiveCollate.NAME);
}
Also used : OCaseInsensitiveCollate(com.orientechnologies.orient.core.collate.OCaseInsensitiveCollate) OBinaryField(com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField) Test(org.testng.annotations.Test)

Example 4 with OBinaryField

use of com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField in project orientdb by orientechnologies.

the class OSQLFilterCondition method evaluate.

public Object evaluate(final OIdentifiable iCurrentRecord, final ODocument iCurrentResult, final OCommandContext iContext) {
    boolean binaryEvaluation = operator != null && operator.isSupportingBinaryEvaluate() && iCurrentRecord != null && iCurrentRecord.getIdentity().isPersistent();
    if (left instanceof OSQLQuery<?>)
        // EXECUTE SUB QUERIES ONLY ONCE
        left = ((OSQLQuery<?>) left).setContext(iContext).execute();
    Object l = evaluate(iCurrentRecord, iCurrentResult, left, iContext, binaryEvaluation);
    if (operator == null || operator.canShortCircuit(l))
        return l;
    if (right instanceof OSQLQuery<?>)
        // EXECUTE SUB QUERIES ONLY ONCE
        right = ((OSQLQuery<?>) right).setContext(iContext).execute();
    Object r = evaluate(iCurrentRecord, iCurrentResult, right, iContext, binaryEvaluation);
    if (binaryEvaluation && l instanceof OBinaryField) {
        if (r != null && !(r instanceof OBinaryField)) {
            final OType type = OType.getTypeByValue(r);
            if (ORecordSerializerBinary.INSTANCE.getCurrentSerializer().getComparator().isBinaryComparable(type)) {
                final BytesContainer bytes = new BytesContainer();
                ORecordSerializerBinary.INSTANCE.getCurrentSerializer().serializeValue(bytes, r, type, null);
                bytes.offset = 0;
                final OCollate collate = r instanceof OSQLFilterItemField ? ((OSQLFilterItemField) r).getCollate(iCurrentRecord) : null;
                r = new OBinaryField(null, type, bytes, collate);
                if (!(right instanceof OSQLFilterItem || right instanceof OSQLFilterCondition))
                    // FIXED VALUE, REPLACE IT
                    right = r;
            }
        } else if (r instanceof OBinaryField)
            // GET THE COPY OR MT REASONS
            r = ((OBinaryField) r).copy();
    }
    if (binaryEvaluation && r instanceof OBinaryField) {
        if (l != null && !(l instanceof OBinaryField)) {
            final OType type = OType.getTypeByValue(l);
            if (ORecordSerializerBinary.INSTANCE.getCurrentSerializer().getComparator().isBinaryComparable(type)) {
                final BytesContainer bytes = new BytesContainer();
                ORecordSerializerBinary.INSTANCE.getCurrentSerializer().serializeValue(bytes, l, type, null);
                bytes.offset = 0;
                final OCollate collate = l instanceof OSQLFilterItemField ? ((OSQLFilterItemField) l).getCollate(iCurrentRecord) : null;
                l = new OBinaryField(null, type, bytes, collate);
                if (!(left instanceof OSQLFilterItem || left instanceof OSQLFilterCondition))
                    // FIXED VALUE, REPLACE IT
                    left = l;
            }
        } else if (l instanceof OBinaryField)
            // GET THE COPY OR MT REASONS
            l = ((OBinaryField) l).copy();
    }
    if (binaryEvaluation)
        binaryEvaluation = l instanceof OBinaryField && r instanceof OBinaryField;
    if (!binaryEvaluation) {
        // no collate for regular expressions, otherwise quotes will result in no match
        final OCollate collate = operator instanceof OQueryOperatorMatches ? null : getCollate(iCurrentRecord);
        final Object[] convertedValues = checkForConversion(iCurrentRecord, l, r, collate);
        if (convertedValues != null) {
            l = convertedValues[0];
            r = convertedValues[1];
        }
    }
    Object result;
    try {
        result = operator.evaluateRecord(iCurrentRecord, iCurrentResult, this, l, r, iContext);
    } catch (OCommandExecutionException e) {
        throw e;
    } catch (Exception e) {
        if (OLogManager.instance().isDebugEnabled())
            OLogManager.instance().debug(this, "Error on evaluating expression (%s)", e, toString());
        result = Boolean.FALSE;
    }
    return result;
}
Also used : OBinaryField(com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField) OType(com.orientechnologies.orient.core.metadata.schema.OType) OException(com.orientechnologies.common.exception.OException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) ParseException(java.text.ParseException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) BytesContainer(com.orientechnologies.orient.core.serialization.serializer.record.binary.BytesContainer) OQueryOperatorMatches(com.orientechnologies.orient.core.sql.operator.OQueryOperatorMatches) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OCollate(com.orientechnologies.orient.core.collate.OCollate) OSQLQuery(com.orientechnologies.orient.core.sql.query.OSQLQuery)

Example 5 with OBinaryField

use of com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField in project orientdb by orientechnologies.

the class OSQLFilterCondition method evaluate.

protected Object evaluate(OIdentifiable iCurrentRecord, final ODocument iCurrentResult, final Object iValue, final OCommandContext iContext, final boolean binaryEvaluation) {
    if (iValue == null)
        return null;
    if (iValue instanceof BytesContainer)
        return iValue;
    if (iCurrentRecord != null) {
        iCurrentRecord = iCurrentRecord.getRecord();
        if (iCurrentRecord != null && ((ORecord) iCurrentRecord).getInternalStatus() == ORecordElement.STATUS.NOT_LOADED) {
            try {
                iCurrentRecord = iCurrentRecord.getRecord().load();
            } catch (ORecordNotFoundException e) {
                return null;
            }
        }
    }
    if (binaryEvaluation && iValue instanceof OSQLFilterItemField) {
        final OBinaryField bField = ((OSQLFilterItemField) iValue).getBinaryField(iCurrentRecord);
        if (bField != null)
            return bField;
    }
    if (iValue instanceof OSQLFilterItem) {
        return ((OSQLFilterItem) iValue).getValue(iCurrentRecord, iCurrentResult, iContext);
    }
    if (iValue instanceof OSQLFilterCondition) {
        // NESTED CONDITION: EVALUATE IT RECURSIVELY
        return ((OSQLFilterCondition) iValue).evaluate(iCurrentRecord, iCurrentResult, iContext);
    }
    if (iValue instanceof OSQLFunctionRuntime) {
        // STATELESS FUNCTION: EXECUTE IT
        final OSQLFunctionRuntime f = (OSQLFunctionRuntime) iValue;
        return f.execute(iCurrentRecord, iCurrentRecord, iCurrentResult, iContext);
    }
    if (OMultiValue.isMultiValue(iValue)) {
        final Iterable<?> multiValue = OMultiValue.getMultiValueIterable(iValue, false);
        // MULTI VALUE: RETURN A COPY
        final ArrayList<Object> result = new ArrayList<Object>(OMultiValue.getSize(iValue));
        for (final Object value : multiValue) {
            if (value instanceof OSQLFilterItem) {
                result.add(((OSQLFilterItem) value).getValue(iCurrentRecord, iCurrentResult, iContext));
            } else {
                result.add(value);
            }
        }
        return result;
    }
    // SIMPLE VALUE: JUST RETURN IT
    return iValue;
}
Also used : BytesContainer(com.orientechnologies.orient.core.serialization.serializer.record.binary.BytesContainer) OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) ORecord(com.orientechnologies.orient.core.record.ORecord) OBinaryField(com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException)

Aggregations

OBinaryField (com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField)5 BytesContainer (com.orientechnologies.orient.core.serialization.serializer.record.binary.BytesContainer)3 OCollate (com.orientechnologies.orient.core.collate.OCollate)2 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)2 OException (com.orientechnologies.common.exception.OException)1 OCaseInsensitiveCollate (com.orientechnologies.orient.core.collate.OCaseInsensitiveCollate)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 OQueryParsingException (com.orientechnologies.orient.core.exception.OQueryParsingException)1 OType (com.orientechnologies.orient.core.metadata.schema.OType)1 OQueryRuntimeValueMulti (com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 OSQLFunctionRuntime (com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime)1 OQueryOperatorMatches (com.orientechnologies.orient.core.sql.operator.OQueryOperatorMatches)1 OSQLQuery (com.orientechnologies.orient.core.sql.query.OSQLQuery)1 ParseException (java.text.ParseException)1 Test (org.testng.annotations.Test)1