聊聊EventDeserializer的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).
         *
         * <p>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).
         *
         * <p>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<T extends EventData> implements EventDataDeserializer<T> {

	//......

    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构造返回

原文 

https://juejin.im/post/5ea99255f265da7bdb320b91

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 聊聊EventDeserializer的CompatibilityMode

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址