登录
首页 >  数据库 >  MySQL

聊聊EventDeserializer的CompatibilityMode

来源:SegmentFault

时间:2023-02-24 19:51:34 413浏览 收藏

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《聊聊EventDeserializer的CompatibilityMode》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

本文主要研究一下EventDeserializer的CompatibilityMode

CompatibilityMode

mysql-binlog-connector-java-0.20.1/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/EventDeserializer.java

    public enum CompatibilityMode {
        /**
         * Return DATETIME/DATETIME_V2/TIMESTAMP/TIMESTAMP_V2/DATE/TIME/TIME_V2 values as long|s
         * (number of milliseconds since the epoch (00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970,
         * not counting leap seconds)) (instead of java.util.Date/java.sql.Timestamp/java.sql.Date/new java.sql.Time).
         *
         * 

This option is going to be enabled by default starting from mysql-binlog-connector-java@1.0.0. */ DATE_AND_TIME_AS_LONG, /** * Same as {@link CompatibilityMode#DATE_AND_TIME_AS_LONG} but values are returned in microseconds. */ DATE_AND_TIME_AS_LONG_MICRO, /** * Return 0 instead of null if year/month/day is 0. * Affects DATETIME/DATETIME_V2/DATE/TIME/TIME_V2. */ INVALID_DATE_AND_TIME_AS_ZERO, /** * Return -1 instead of null if year/month/day is 0. * Affects DATETIME/DATETIME_V2/DATE/TIME/TIME_V2. * * @deprecated */ INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE, /** * Return Long.MIN_VALUE instead of null if year/month/day is 0. * Affects DATETIME/DATETIME_V2/DATE/TIME/TIME_V2. */ INVALID_DATE_AND_TIME_AS_MIN_VALUE, /** * Return CHAR/VARCHAR/BINARY/VARBINARY values as byte[]|s (instead of String|s). * *

This option is going to be enabled by default starting from mysql-binlog-connector-java@1.0.0. */ CHAR_AND_BINARY_AS_BYTE_ARRAY }

  • EventDeserializer的CompatibilityMode定义了DATE_AND_TIME_AS_LONG、DATE_AND_TIME_AS_LONG_MICRO、INVALID_DATE_AND_TIME_AS_ZERO、INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE、INVALID_DATE_AND_TIME_AS_MIN_VALUE、CHAR_AND_BINARY_AS_BYTE_ARRAY这几个枚举值

ensureCompatibility

mysql-binlog-connector-java-0.20.1/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/EventDeserializer.java

public class EventDeserializer {

    //......

    private void ensureCompatibility(EventDataDeserializer eventDataDeserializer) {
        if (eventDataDeserializer instanceof AbstractRowsEventDataDeserializer) {
            AbstractRowsEventDataDeserializer deserializer =
                (AbstractRowsEventDataDeserializer) eventDataDeserializer;
            boolean deserializeDateAndTimeAsLong =
                compatibilitySet.contains(CompatibilityMode.DATE_AND_TIME_AS_LONG) ||
                compatibilitySet.contains(CompatibilityMode.DATE_AND_TIME_AS_LONG_MICRO);
            deserializer.setDeserializeDateAndTimeAsLong(deserializeDateAndTimeAsLong);
            deserializer.setMicrosecondsPrecision(
                compatibilitySet.contains(CompatibilityMode.DATE_AND_TIME_AS_LONG_MICRO)
            );
            if (compatibilitySet.contains(CompatibilityMode.INVALID_DATE_AND_TIME_AS_ZERO)) {
                deserializer.setInvalidDateAndTimeRepresentation(0L);
            }
            if (compatibilitySet.contains(CompatibilityMode.INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE)) {
                if (!deserializeDateAndTimeAsLong) {
                    throw new IllegalArgumentException("INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE requires " +
                        "DATE_AND_TIME_AS_LONG or DATE_AND_TIME_AS_LONG_MICRO");
                }
                deserializer.setInvalidDateAndTimeRepresentation(-1L);
            }
            if (compatibilitySet.contains(CompatibilityMode.INVALID_DATE_AND_TIME_AS_MIN_VALUE)) {
                if (!deserializeDateAndTimeAsLong) {
                    throw new IllegalArgumentException("INVALID_DATE_AND_TIME_AS_MIN_VALUE requires " +
                        "DATE_AND_TIME_AS_LONG or DATE_AND_TIME_AS_LONG_MICRO");
                }
                deserializer.setInvalidDateAndTimeRepresentation(Long.MIN_VALUE);
            }
            deserializer.setDeserializeCharAndBinaryAsByteArray(
                compatibilitySet.contains(CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY)
            );
        }
    }

    //......

}
  • EventDeserializer的ensureCompatibility方法在compatibilitySet包含DATE_AND_TIME_AS_LONG或者DATE_AND_TIME_AS_LONG_MICRO时,设置deserializeDateAndTimeAsLong为true;在compatibilitySet包含DATE_AND_TIME_AS_LONG_MICRO时设置microsecondsPrecision为true;在compatibilitySet包含INVALID_DATE_AND_TIME_AS_ZERO时设置invalidDateAndTimeRepresentation为0L;在compatibilitySet包含INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE时设置invalidDateAndTimeRepresentation为-1;在compatibilitySet包含INVALID_DATE_AND_TIME_AS_MIN_VALUE时设置invalidDateAndTimeRepresentation为Long.MIN_VALUE;在compatibilitySet包含CHAR_AND_BINARY_AS_BYTE_ARRAY时设置deserializeCharAndBinaryAsByteArray为true

AbstractRowsEventDataDeserializer

mysql-binlog-connector-java-0.20.1/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/AbstractRowsEventDataDeserializer.java

public abstract class AbstractRowsEventDataDeserializer implements EventDataDeserializer {

    //......

    private Long castTimestamp(Long timestamp, int fsp) {
        if (microsecondsPrecision && timestamp != null && !timestamp.equals(invalidDateAndTimeRepresentation)) {
            return timestamp * 1000 + fsp % 1000;
        }
        return timestamp;
    }

    protected Long asUnixTime(int year, int month, int day, int hour, int minute, int second, int millis) {
        // https://dev.mysql.com/doc/refman/5.0/en/datetime.html
        if (year == 0 || month == 0 || day == 0) {
            return invalidDateAndTimeRepresentation;
        }
        return UnixTime.from(year, month, day, hour, minute, second, millis);
    }

    //......

}
  • AbstractRowsEventDataDeserializer的castTimestamp方法根据microsecondsPrecision来决定是否设置精度;asUnixTime方法则在year或者month或者day为0时返回invalidDateAndTimeRepresentation,其余的通过UnixTime构造返回

小结

EventDeserializer的CompatibilityMode定义了DATE_AND_TIME_AS_LONG、DATE_AND_TIME_AS_LONG_MICRO、INVALID_DATE_AND_TIME_AS_ZERO、INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE、INVALID_DATE_AND_TIME_AS_MIN_VALUE、CHAR_AND_BINARY_AS_BYTE_ARRAY这几个枚举值

doc

本篇关于《聊聊EventDeserializer的CompatibilityMode》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于数据库的相关知识,请关注golang学习网公众号!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>