Search in sources :

Example 1 with CodecException

use of com.pingcap.tikv.exception.CodecException in project tispark by pingcap.

the class RowEncoderV2 method encodeBit.

private void encodeBit(CodecDataOutput cdo, Object value) {
    long s = 0;
    if (value instanceof Long) {
        s = (long) value;
    } else if (value instanceof byte[]) {
        for (byte b : (byte[]) value) {
            s <<= 8;
            s |= b;
        }
    } else {
        throw new CodecException("invalid bytes type " + value.getClass());
    }
    encodeInt(cdo, s);
}
Also used : CodecException(com.pingcap.tikv.exception.CodecException)

Example 2 with CodecException

use of com.pingcap.tikv.exception.CodecException in project tispark by pingcap.

the class RowEncoderV2 method encodeTimestamp.

private void encodeTimestamp(CodecDataOutput cdo, Object value, DateTimeZone tz) {
    if (value instanceof Timestamp) {
        ExtendedDateTime extendedDateTime = Converter.convertToDateTime(value);
        long t = DateTimeCodec.toPackedLong(extendedDateTime, tz);
        encodeInt(cdo, t);
    } else if (value instanceof Date) {
        ExtendedDateTime extendedDateTime = Converter.convertToDateTime(value);
        long t = DateTimeCodec.toPackedLong(extendedDateTime, tz);
        encodeInt(cdo, t);
    } else {
        throw new CodecException("invalid timestamp type " + value.getClass());
    }
}
Also used : ExtendedDateTime(com.pingcap.tikv.ExtendedDateTime) CodecException(com.pingcap.tikv.exception.CodecException) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Example 3 with CodecException

use of com.pingcap.tikv.exception.CodecException in project tispark by pingcap.

the class RowEncoderV2 method encodeValue.

private void encodeValue(CodecDataOutput cdo, Object value, DataType tp) {
    switch(tp.getType()) {
        case TypeLonglong:
        case TypeLong:
        case TypeInt24:
        case TypeShort:
        case TypeTiny:
            // TODO: encode consider unsigned
            encodeInt(cdo, (long) value);
            break;
        case TypeFloat:
        case TypeDouble:
            if (value instanceof Double) {
                encodeDouble(cdo, value);
            } else if (value instanceof Float) {
                encodeFloat(cdo, value);
            } else {
                throw new TypeException("type does not match in encoding, should be float/double");
            }
            break;
        case TypeString:
        case TypeVarString:
        case TypeVarchar:
        case TypeBlob:
        case TypeTinyBlob:
        case TypeMediumBlob:
        case TypeLongBlob:
            encodeString(cdo, value);
            break;
        case TypeNewDecimal:
            DecimalType decimalType = (DecimalType) tp;
            encodeDecimal(cdo, value, (int) decimalType.getLength(), decimalType.getDecimal());
            break;
        case TypeBit:
            encodeBit(cdo, value);
            break;
        case TypeTimestamp:
            encodeTimestamp(cdo, value, DateTimeZone.UTC);
            break;
        case TypeDate:
        case TypeDatetime:
            encodeTimestamp(cdo, value, Converter.getLocalTimezone());
            break;
        case TypeDuration:
        case TypeYear:
            encodeInt(cdo, (long) value);
            break;
        case TypeEnum:
            encodeEnum(cdo, value, tp.getElems());
            break;
        case TypeSet:
            encodeSet(cdo, value, tp.getElems());
            break;
        case TypeJSON:
            encodeJson(cdo, value);
            break;
        case TypeNull:
        // ??
        case TypeDecimal:
        case TypeGeometry:
        case TypeNewDate:
            throw new CodecException("type should not appear in encoding");
        default:
            throw new CodecException("invalid data type: " + tp.getType().name());
    }
}
Also used : DecimalType(com.pingcap.tikv.types.DecimalType) TypeException(com.pingcap.tikv.exception.TypeException) CodecException(com.pingcap.tikv.exception.CodecException)

Example 4 with CodecException

use of com.pingcap.tikv.exception.CodecException in project tispark by pingcap.

the class Codec method decodeOne.

public static Object decodeOne(byte[] colData) {
    if (colData.length <= 1) {
        throw new CodecException("invalid encoded column data, length <=1");
    }
    int flag = colData[0];
    DataType tp;
    switch(flag) {
        case INT_FLAG:
        case UINT_FLAG:
        case VARINT_FLAG:
        case UVARINT_FLAG:
            tp = IntegerType.BIGINT;
            break;
        case FLOATING_FLAG:
            tp = RealType.DOUBLE;
            break;
        case BYTES_FLAG:
        case COMPACT_BYTES_FLAG:
            tp = BytesType.TEXT;
            break;
        case DECIMAL_FLAG:
            tp = DecimalType.DECIMAL;
            break;
        case DURATION_FLAG:
            tp = TimeType.TIME;
            break;
        case JSON_FLAG:
            tp = JsonType.JSON;
            break;
        default:
            throw new CodecException("Unknown type");
    }
    return tp.decode(new CodecDataInput(colData));
}
Also used : DataType(com.pingcap.tikv.types.DataType) CodecException(com.pingcap.tikv.exception.CodecException)

Example 5 with CodecException

use of com.pingcap.tikv.exception.CodecException in project tispark by pingcap.

the class RowEncoderV2 method encodeDecimal.

private void encodeDecimal(CodecDataOutput cdo, Object value, int precision, int frac) {
    if (value instanceof MyDecimal) {
        MyDecimal dec = (MyDecimal) value;
        DecimalCodec.writeDecimal(cdo, dec, precision, frac);
    } else if (value instanceof BigDecimal) {
        MyDecimal dec = new MyDecimal();
        dec.fromString(((BigDecimal) value).toPlainString());
        DecimalCodec.writeDecimal(cdo, dec, precision, frac);
    } else {
        throw new CodecException("invalid decimal type " + value.getClass());
    }
}
Also used : CodecException(com.pingcap.tikv.exception.CodecException) BigDecimal(java.math.BigDecimal)

Aggregations

CodecException (com.pingcap.tikv.exception.CodecException)5 ExtendedDateTime (com.pingcap.tikv.ExtendedDateTime)1 TypeException (com.pingcap.tikv.exception.TypeException)1 DataType (com.pingcap.tikv.types.DataType)1 DecimalType (com.pingcap.tikv.types.DecimalType)1 BigDecimal (java.math.BigDecimal)1 Date (java.sql.Date)1 Timestamp (java.sql.Timestamp)1