Search in sources :

Example 6 with OCollate

use of com.orientechnologies.orient.core.collate.OCollate in project orientdb by orientechnologies.

the class OIndexDefinitionFactory method createMultipleFieldIndexDefinition.

private static OIndexDefinition createMultipleFieldIndexDefinition(final OClass oClass, final List<String> fieldsToIndex, final List<OType> types, List<OCollate> collates, String indexKind, String algorithm) {
    final OIndexFactory factory = OIndexes.getFactory(indexKind, algorithm);
    final String className = oClass.getName();
    final OCompositeIndexDefinition compositeIndex = new OCompositeIndexDefinition(className);
    for (int i = 0, fieldsToIndexSize = fieldsToIndex.size(); i < fieldsToIndexSize; i++) {
        OCollate collate = null;
        if (collates != null)
            collate = collates.get(i);
        compositeIndex.addIndex(createSingleFieldIndexDefinition(oClass, fieldsToIndex.get(i), types.get(i), collate, indexKind, algorithm));
    }
    return compositeIndex;
}
Also used : OCollate(com.orientechnologies.orient.core.collate.OCollate)

Example 7 with OCollate

use of com.orientechnologies.orient.core.collate.OCollate in project orientdb by orientechnologies.

the class OCompositeCollate method transform.

@SuppressWarnings("unchecked")
@Override
public Object transform(final Object obj) {
    final List<Object> keys;
    if (obj instanceof OCompositeKey) {
        final OCompositeKey compositeKey = (OCompositeKey) obj;
        keys = compositeKey.getKeys();
    } else if (obj instanceof List) {
        keys = (List<Object>) obj;
    } else {
        throw new OIndexException("Impossible add as key of a CompositeIndex a value of type " + obj.getClass());
    }
    final OCompositeKey transformedKey = new OCompositeKey();
    final int size = Math.min(keys.size(), collates.size());
    for (int i = 0; i < size; i++) {
        final Object key = keys.get(i);
        final OCollate collate = collates.get(i);
        transformedKey.addKey(collate.transform(key));
    }
    for (int i = size; i < keys.size(); i++) transformedKey.addKey(keys.get(i));
    return transformedKey;
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) OCollate(com.orientechnologies.orient.core.collate.OCollate)

Example 8 with OCollate

use of com.orientechnologies.orient.core.collate.OCollate in project orientdb by orientechnologies.

the class OPropertyImpl method setCollateInternal.

private OProperty setCollateInternal(String iCollate) {
    acquireSchemaWriteLock();
    try {
        checkEmbedded();
        final OCollate oldCollate = this.collate;
        if (iCollate == null)
            iCollate = ODefaultCollate.NAME;
        collate = OSQLEngine.getCollate(iCollate);
        if ((this.collate != null && !this.collate.equals(oldCollate)) || (this.collate == null && oldCollate != null)) {
            final Set<OIndex<?>> indexes = owner.getClassIndexes();
            final List<OIndex<?>> indexesToRecreate = new ArrayList<OIndex<?>>();
            for (OIndex<?> index : indexes) {
                OIndexDefinition definition = index.getDefinition();
                final List<String> fields = definition.getFields();
                if (fields.contains(getName()))
                    indexesToRecreate.add(index);
            }
            if (!indexesToRecreate.isEmpty()) {
                OLogManager.instance().info(this, "Collate value was changed, following indexes will be rebuilt %s", indexesToRecreate);
                final ODatabaseDocument database = getDatabase();
                final OIndexManager indexManager = database.getMetadata().getIndexManager();
                for (OIndex<?> indexToRecreate : indexesToRecreate) {
                    final OIndexMetadata indexMetadata = indexToRecreate.getInternal().loadMetadata(indexToRecreate.getConfiguration());
                    final ODocument metadata = indexToRecreate.getMetadata();
                    final List<String> fields = indexMetadata.getIndexDefinition().getFields();
                    final String[] fieldsToIndex = fields.toArray(new String[fields.size()]);
                    indexManager.dropIndex(indexMetadata.getName());
                    owner.createIndex(indexMetadata.getName(), indexMetadata.getType(), null, metadata, indexMetadata.getAlgorithm(), fieldsToIndex);
                }
            }
        }
    } finally {
        releaseSchemaWriteLock();
    }
    return this;
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OCollate(com.orientechnologies.orient.core.collate.OCollate) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 9 with OCollate

use of com.orientechnologies.orient.core.collate.OCollate in project orientdb by orientechnologies.

the class OBinaryComparatorV0 method compare.

/**
   * Compares two values executing also conversion between types.
   *
   * @param iField1
   *          First value to compare
   * @param iField2
   *          Second value to compare
   * @return 0 if they matches, >0 if first value is major than second, <0 in case is minor
   */
@Override
public int compare(final OBinaryField iField1, final OBinaryField iField2) {
    final BytesContainer fieldValue1 = iField1.bytes;
    final int offset1 = fieldValue1.offset;
    final BytesContainer fieldValue2 = iField2.bytes;
    final int offset2 = fieldValue2.offset;
    try {
        switch(iField1.type) {
            case INTEGER:
                {
                    final int value1 = OVarIntSerializer.readAsInteger(fieldValue1);
                    switch(iField2.type) {
                        case INTEGER:
                            {
                                final int value2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case LONG:
                        case DATETIME:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DATE:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2) * ORecordSerializerBinaryV0.MILLISEC_PER_DAY;
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case SHORT:
                            {
                                final short value2 = OVarIntSerializer.readAsShort(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case BYTE:
                            {
                                final byte value2 = ORecordSerializerBinaryV0.readByte(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case FLOAT:
                            {
                                final float value2 = Float.intBitsToFloat(ORecordSerializerBinaryV0.readInteger(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DOUBLE:
                            {
                                final double value2 = Double.longBitsToDouble(ORecordSerializerBinaryV0.readLong(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case STRING:
                            {
                                final String value2 = ORecordSerializerBinaryV0.readString(fieldValue2);
                                return Integer.toString(value1).compareTo(value2);
                            }
                        case DECIMAL:
                            {
                                final int value2 = ODecimalSerializer.INSTANCE.deserialize(fieldValue2.bytes, fieldValue2.offset).intValue();
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                    }
                    break;
                }
            case LONG:
                {
                    final long value1 = OVarIntSerializer.readAsLong(fieldValue1);
                    switch(iField2.type) {
                        case INTEGER:
                            {
                                final int value2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case LONG:
                        case DATETIME:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DATE:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2) * ORecordSerializerBinaryV0.MILLISEC_PER_DAY;
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case SHORT:
                            {
                                final short value2 = OVarIntSerializer.readAsShort(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case BYTE:
                            {
                                final byte value2 = ORecordSerializerBinaryV0.readByte(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case FLOAT:
                            {
                                final float value2 = Float.intBitsToFloat(ORecordSerializerBinaryV0.readInteger(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DOUBLE:
                            {
                                final double value2 = Double.longBitsToDouble(ORecordSerializerBinaryV0.readLong(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case STRING:
                            {
                                final String value2 = ORecordSerializerBinaryV0.readString(fieldValue2);
                                return Long.toString(value1).compareTo(value2);
                            }
                        case DECIMAL:
                            {
                                final long value2 = ODecimalSerializer.INSTANCE.deserialize(fieldValue2.bytes, fieldValue2.offset).longValue();
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                    }
                    break;
                }
            case SHORT:
                {
                    final short value1 = OVarIntSerializer.readAsShort(fieldValue1);
                    switch(iField2.type) {
                        case INTEGER:
                            {
                                final int value2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case LONG:
                        case DATETIME:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DATE:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2) * ORecordSerializerBinaryV0.MILLISEC_PER_DAY;
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case SHORT:
                            {
                                final short value2 = OVarIntSerializer.readAsShort(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case BYTE:
                            {
                                final byte value2 = ORecordSerializerBinaryV0.readByte(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case FLOAT:
                            {
                                final float value2 = Float.intBitsToFloat(ORecordSerializerBinaryV0.readInteger(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DOUBLE:
                            {
                                final double value2 = Double.longBitsToDouble(ORecordSerializerBinaryV0.readLong(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case STRING:
                            {
                                final String value2 = ORecordSerializerBinaryV0.readString(fieldValue2);
                                return Short.toString(value1).compareTo(value2);
                            }
                        case DECIMAL:
                            {
                                final short value2 = ODecimalSerializer.INSTANCE.deserialize(fieldValue2.bytes, fieldValue2.offset).shortValue();
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                    }
                    break;
                }
            case STRING:
                {
                    final String value1 = ORecordSerializerBinaryV0.readString(fieldValue1);
                    switch(iField2.type) {
                        case INTEGER:
                            {
                                final int value2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                return value1.compareTo(Integer.toString(value2));
                            }
                        case LONG:
                        case DATETIME:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2);
                                return value1.compareTo(Long.toString(value2));
                            }
                        case DATE:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2) * ORecordSerializerBinaryV0.MILLISEC_PER_DAY;
                                return value1.compareTo(Long.toString(value2));
                            }
                        case SHORT:
                            {
                                final short value2 = OVarIntSerializer.readAsShort(fieldValue2);
                                return value1.compareTo(Short.toString(value2));
                            }
                        case BYTE:
                            {
                                final byte value2 = ORecordSerializerBinaryV0.readByte(fieldValue2);
                                return value1.compareTo(Byte.toString(value2));
                            }
                        case FLOAT:
                            {
                                final float value2 = Float.intBitsToFloat(ORecordSerializerBinaryV0.readInteger(fieldValue2));
                                return value1.compareTo(Float.toString(value2));
                            }
                        case DOUBLE:
                            {
                                final double value2 = Double.longBitsToDouble(ORecordSerializerBinaryV0.readLong(fieldValue2));
                                return value1.compareTo(Double.toString(value2));
                            }
                        case STRING:
                            {
                                final String value2 = ORecordSerializerBinaryV0.readString(fieldValue2);
                                final OCollate collate = (iField1.collate != null && !ODefaultCollate.NAME.equals(iField1.collate.getName())) ? iField1.collate : (iField2.collate != null && !ODefaultCollate.NAME.equals(iField2.collate.getName()) ? iField2.collate : null);
                                if (collate != null) {
                                    final String str1 = (String) collate.transform(value1);
                                    final String str2 = (String) collate.transform(value2);
                                    return str1.compareTo(str2);
                                }
                                return value1.compareTo(value2);
                            }
                        case BOOLEAN:
                            {
                                final boolean value2 = ORecordSerializerBinaryV0.readByte(fieldValue2) == 1;
                                return value1.compareTo(Boolean.toString(value2));
                            }
                        case DECIMAL:
                            {
                                final BigDecimal value2 = ODecimalSerializer.INSTANCE.deserialize(fieldValue2.bytes, fieldValue2.offset);
                                return new BigDecimal(value1).compareTo(value2);
                            }
                    }
                    break;
                }
            case DOUBLE:
                {
                    final double value1 = Double.longBitsToDouble(ORecordSerializerBinaryV0.readLong(fieldValue1));
                    switch(iField2.type) {
                        case INTEGER:
                            {
                                final int value2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case LONG:
                        case DATETIME:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case SHORT:
                            {
                                final short value2 = OVarIntSerializer.readAsShort(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case BYTE:
                            {
                                final byte value2 = ORecordSerializerBinaryV0.readByte(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case FLOAT:
                            {
                                final float value2 = Float.intBitsToFloat(ORecordSerializerBinaryV0.readInteger(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DOUBLE:
                            {
                                final double value2 = Double.longBitsToDouble(ORecordSerializerBinaryV0.readLong(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case STRING:
                            {
                                final String value2 = ORecordSerializerBinaryV0.readString(fieldValue2);
                                return Double.toString(value1).compareTo(value2);
                            }
                        case DECIMAL:
                            {
                                final double value2 = ODecimalSerializer.INSTANCE.deserialize(fieldValue2.bytes, fieldValue2.offset).doubleValue();
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                    }
                    break;
                }
            case FLOAT:
                {
                    final float value1 = Float.intBitsToFloat(ORecordSerializerBinaryV0.readInteger(fieldValue1));
                    switch(iField2.type) {
                        case INTEGER:
                            {
                                final int value2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case LONG:
                        case DATETIME:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case SHORT:
                            {
                                final short value2 = OVarIntSerializer.readAsShort(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case BYTE:
                            {
                                final byte value2 = ORecordSerializerBinaryV0.readByte(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case FLOAT:
                            {
                                final float value2 = Float.intBitsToFloat(ORecordSerializerBinaryV0.readInteger(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DOUBLE:
                            {
                                final double value2 = Double.longBitsToDouble(ORecordSerializerBinaryV0.readLong(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case STRING:
                            {
                                final String value2 = ORecordSerializerBinaryV0.readString(fieldValue2);
                                return Float.toString(value1).compareTo(value2);
                            }
                        case DECIMAL:
                            {
                                final String value2 = ORecordSerializerBinaryV0.readString(fieldValue2);
                                return Float.toString(value1).compareTo(value2);
                            }
                    }
                    break;
                }
            case BYTE:
                {
                    final byte value1 = ORecordSerializerBinaryV0.readByte(fieldValue1);
                    switch(iField2.type) {
                        case INTEGER:
                            {
                                final int value2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case LONG:
                        case DATETIME:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case SHORT:
                            {
                                final short value2 = OVarIntSerializer.readAsShort(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case BYTE:
                            {
                                final byte value2 = ORecordSerializerBinaryV0.readByte(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case FLOAT:
                            {
                                final float value2 = Float.intBitsToFloat(ORecordSerializerBinaryV0.readInteger(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DOUBLE:
                            {
                                final double value2 = Double.longBitsToDouble(ORecordSerializerBinaryV0.readLong(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case STRING:
                            {
                                final String value2 = ORecordSerializerBinaryV0.readString(fieldValue2);
                                return Byte.toString(value1).compareTo(value2);
                            }
                        case DECIMAL:
                            {
                                final byte value2 = ODecimalSerializer.INSTANCE.deserialize(fieldValue2.bytes, fieldValue2.offset).byteValue();
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                    }
                    break;
                }
            case BOOLEAN:
                {
                    final boolean value1 = ORecordSerializerBinaryV0.readByte(fieldValue1) == 1;
                    switch(iField2.type) {
                        case BOOLEAN:
                            {
                                final boolean value2 = ORecordSerializerBinaryV0.readByte(fieldValue2) == 1;
                                return (value1 == value2) ? 0 : value1 ? 1 : -1;
                            }
                        case STRING:
                            {
                                final boolean value2 = Boolean.parseBoolean(ORecordSerializerBinaryV0.readString(fieldValue2));
                                return (value1 == value2) ? 0 : value1 ? 1 : -1;
                            }
                    }
                    break;
                }
            case DATETIME:
                {
                    final long value1 = OVarIntSerializer.readAsLong(fieldValue1);
                    switch(iField2.type) {
                        case INTEGER:
                            {
                                final int value2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case LONG:
                        case DATETIME:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DATE:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2) * ORecordSerializerBinaryV0.MILLISEC_PER_DAY;
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case SHORT:
                            {
                                final short value2 = OVarIntSerializer.readAsShort(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case BYTE:
                            {
                                final byte value2 = ORecordSerializerBinaryV0.readByte(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case FLOAT:
                            {
                                final float value2 = Float.intBitsToFloat(ORecordSerializerBinaryV0.readInteger(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DOUBLE:
                            {
                                final double value2 = Double.longBitsToDouble(ORecordSerializerBinaryV0.readLong(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case STRING:
                            {
                                final String value2AsString = ORecordSerializerBinaryV0.readString(fieldValue2);
                                if (OIOUtils.isLong(value2AsString)) {
                                    final long value2 = Long.parseLong(value2AsString);
                                    return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                                }
                                final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
                                try {
                                    final SimpleDateFormat dateFormat = db != null ? db.getStorage().getConfiguration().getDateTimeFormatInstance() : new SimpleDateFormat(OStorageConfiguration.DEFAULT_DATETIME_FORMAT);
                                    final Date value2AsDate = dateFormat.parse(value2AsString);
                                    final long value2 = value2AsDate.getTime();
                                    return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                                } catch (ParseException e) {
                                    try {
                                        final SimpleDateFormat dateFormat = db != null ? db.getStorage().getConfiguration().getDateFormatInstance() : new SimpleDateFormat(OStorageConfiguration.DEFAULT_DATE_FORMAT);
                                        final Date value2AsDate = dateFormat.parse(value2AsString);
                                        final long value2 = value2AsDate.getTime();
                                        return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                                    } catch (ParseException e1) {
                                        return new Date(value1).toString().compareTo(value2AsString);
                                    }
                                }
                            }
                        case DECIMAL:
                            {
                                final long value2 = ODecimalSerializer.INSTANCE.deserialize(fieldValue2.bytes, fieldValue2.offset).longValue();
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                    }
                    break;
                }
            case DATE:
                {
                    final long value1 = OVarIntSerializer.readAsLong(fieldValue1) * ORecordSerializerBinaryV0.MILLISEC_PER_DAY;
                    switch(iField2.type) {
                        case INTEGER:
                            {
                                final int value2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case LONG:
                        case DATETIME:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DATE:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2) * ORecordSerializerBinaryV0.MILLISEC_PER_DAY;
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case SHORT:
                            {
                                final short value2 = OVarIntSerializer.readAsShort(fieldValue2);
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case FLOAT:
                            {
                                final float value2 = Float.intBitsToFloat(ORecordSerializerBinaryV0.readInteger(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case DOUBLE:
                            {
                                final double value2 = Double.longBitsToDouble(ORecordSerializerBinaryV0.readLong(fieldValue2));
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                        case STRING:
                            {
                                final String value2AsString = ORecordSerializerBinaryV0.readString(fieldValue2);
                                if (OIOUtils.isLong(value2AsString)) {
                                    final long value2 = Long.parseLong(value2AsString);
                                    return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                                }
                                final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
                                try {
                                    final SimpleDateFormat dateFormat = db != null ? db.getStorage().getConfiguration().getDateFormatInstance() : new SimpleDateFormat(OStorageConfiguration.DEFAULT_DATE_FORMAT);
                                    final Date value2AsDate = dateFormat.parse(value2AsString);
                                    long value2 = value2AsDate.getTime();
                                    value2 = ORecordSerializerBinaryV0.convertDayToTimezone(ODateHelper.getDatabaseTimeZone(), TimeZone.getTimeZone("GMT"), value2);
                                    return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                                } catch (ParseException e) {
                                    try {
                                        final SimpleDateFormat dateFormat = db != null ? db.getStorage().getConfiguration().getDateFormatInstance() : new SimpleDateFormat(OStorageConfiguration.DEFAULT_DATETIME_FORMAT);
                                        final Date value2AsDate = dateFormat.parse(value2AsString);
                                        long value2 = value2AsDate.getTime();
                                        value2 = ORecordSerializerBinaryV0.convertDayToTimezone(ODateHelper.getDatabaseTimeZone(), TimeZone.getTimeZone("GMT"), value2);
                                        return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                                    } catch (ParseException e1) {
                                        return new Date(value1).toString().compareTo(value2AsString);
                                    }
                                }
                            }
                        case DECIMAL:
                            {
                                final long value2 = ODecimalSerializer.INSTANCE.deserialize(fieldValue2.bytes, fieldValue2.offset).longValue();
                                return (value1 < value2) ? -1 : ((value1 == value2) ? 0 : 1);
                            }
                    }
                    break;
                }
            case BINARY:
                {
                    switch(iField2.type) {
                        case BINARY:
                            {
                                final int length1 = OVarIntSerializer.readAsInteger(fieldValue1);
                                final int length2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                final int max = Math.min(length1, length2);
                                for (int i = 0; i < max; ++i) {
                                    final byte b1 = fieldValue1.bytes[fieldValue1.offset + i];
                                    final byte b2 = fieldValue2.bytes[fieldValue2.offset + i];
                                    if (b1 > b2)
                                        return 1;
                                    else if (b2 > b1)
                                        return -1;
                                }
                                if (length1 > length2)
                                    return 1;
                                else if (length2 > length1)
                                    return -1;
                                // EQUALS
                                return 0;
                            }
                    }
                    break;
                }
            case LINK:
                {
                    switch(iField2.type) {
                        case LINK:
                            {
                                final int clusterId1 = OVarIntSerializer.readAsInteger(fieldValue1);
                                final int clusterId2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                if (clusterId1 > clusterId2)
                                    return 1;
                                else if (clusterId1 < clusterId2)
                                    return -1;
                                else {
                                    final long clusterPos1 = OVarIntSerializer.readAsLong(fieldValue1);
                                    final long clusterPos2 = OVarIntSerializer.readAsLong(fieldValue2);
                                    if (clusterPos1 > clusterPos2)
                                        return 1;
                                    else if (clusterPos1 < clusterPos2)
                                        return -1;
                                    return 0;
                                }
                            }
                        case STRING:
                            {
                                return ORecordSerializerBinaryV0.readOptimizedLink(fieldValue1).compareTo(new ORecordId(ORecordSerializerBinaryV0.readString(fieldValue2)));
                            }
                    }
                    break;
                }
            case DECIMAL:
                {
                    final BigDecimal value1 = ODecimalSerializer.INSTANCE.deserialize(fieldValue1.bytes, fieldValue1.offset);
                    switch(iField2.type) {
                        case INTEGER:
                            {
                                final int value2 = OVarIntSerializer.readAsInteger(fieldValue2);
                                return value1.compareTo(new BigDecimal(value2));
                            }
                        case LONG:
                        case DATETIME:
                            {
                                final long value2 = OVarIntSerializer.readAsLong(fieldValue2);
                                return value1.compareTo(new BigDecimal(value2));
                            }
                        case SHORT:
                            {
                                final short value2 = OVarIntSerializer.readAsShort(fieldValue2);
                                return value1.compareTo(new BigDecimal(value2));
                            }
                        case FLOAT:
                            {
                                final float value2 = Float.intBitsToFloat(ORecordSerializerBinaryV0.readInteger(fieldValue2));
                                return value1.compareTo(new BigDecimal(value2));
                            }
                        case DOUBLE:
                            {
                                final double value2 = Double.longBitsToDouble(ORecordSerializerBinaryV0.readLong(fieldValue2));
                                return value1.compareTo(new BigDecimal(value2));
                            }
                        case STRING:
                            {
                                final String value2 = ORecordSerializerBinaryV0.readString(fieldValue2);
                                return value1.toString().compareTo(value2);
                            }
                        case DECIMAL:
                            {
                                final BigDecimal value2 = ODecimalSerializer.INSTANCE.deserialize(fieldValue2.bytes, fieldValue2.offset);
                                return value1.compareTo(value2);
                            }
                        case BYTE:
                            {
                                final byte value2 = ORecordSerializerBinaryV0.readByte(fieldValue2);
                                return value1.compareTo(new BigDecimal(value2));
                            }
                    }
                    break;
                }
        }
    } finally {
        fieldValue1.offset = offset1;
        fieldValue2.offset = offset2;
    }
    // NO COMPARE SUPPORTED, RETURN NON EQUALS
    return 1;
}
Also used : BigDecimal(java.math.BigDecimal) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) Date(java.util.Date) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OCollate(com.orientechnologies.orient.core.collate.OCollate) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 10 with OCollate

use of com.orientechnologies.orient.core.collate.OCollate 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)

Aggregations

OCollate (com.orientechnologies.orient.core.collate.OCollate)10 OType (com.orientechnologies.orient.core.metadata.schema.OType)3 ParseException (java.text.ParseException)3 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)2 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)2 OBinaryField (com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField)2 BigDecimal (java.math.BigDecimal)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 OException (com.orientechnologies.common.exception.OException)1 OCollateFactory (com.orientechnologies.orient.core.collate.OCollateFactory)1 OQueryParsingException (com.orientechnologies.orient.core.exception.OQueryParsingException)1 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 OQueryRuntimeValueMulti (com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 BytesContainer (com.orientechnologies.orient.core.serialization.serializer.record.binary.BytesContainer)1 OQueryOperatorMatches (com.orientechnologies.orient.core.sql.operator.OQueryOperatorMatches)1