聊聊SpinalTap的BinlogEvent

本文主要研究一下SpinalTap的BinlogEventjava

BinlogEvent

SpinalTap/spinaltap-mysql/src/main/java/com/airbnb/spinaltap/mysql/event/BinlogEvent.javamysql

@Getter
@ToString
public abstract class BinlogEvent extends SourceEvent {
  private final long tableId;
  private final long serverId;
  private final BinlogFilePos binlogFilePos;

  public BinlogEvent(long tableId, long serverId, long timestamp, BinlogFilePos binlogFilePos) {
    super(timestamp);

    this.tableId = tableId;
    this.serverId = serverId;
    this.binlogFilePos = binlogFilePos;
  }

  public long getOffset() {
    return (binlogFilePos.getFileNumber() << 32) | binlogFilePos.getPosition();
  }

  public boolean isMutation() {
    return this instanceof WriteEvent || this instanceof DeleteEvent || this instanceof UpdateEvent;
  }
}
複製代碼
  • BinlogEvent繼承了SourceEvent,它定義了tableId、serverId、binlogFilePos屬性

StartEvent

SpinalTap/spinaltap-mysql/src/main/java/com/airbnb/spinaltap/mysql/event/StartEvent.javagit

public class StartEvent extends BinlogEvent {
  public StartEvent(long serverId, long timestamp, BinlogFilePos filePos) {
    super(0L, serverId, timestamp, filePos);
  }
}
複製代碼
  • StartEvent繼承了BinlogEvent,其構造器須要serverId、timestamp、filePos參數

TableMapEvent

SpinalTap/spinaltap-mysql/src/main/java/com/airbnb/spinaltap/mysql/event/TableMapEvent.javagithub

@Getter
public final class TableMapEvent extends BinlogEvent {
  private final String database;
  private final String table;
  private final List<ColumnDataType> columnTypes;

  public TableMapEvent(
      long tableId,
      long serverId,
      long timestamp,
      BinlogFilePos filePos,
      String database,
      String table,
      byte[] columnTypeCodes) {
    super(tableId, serverId, timestamp, filePos);

    this.database = database;
    this.table = table;
    this.columnTypes = new ArrayList<>();

    for (byte code : columnTypeCodes) {
      columnTypes.add(ColumnDataType.byCode(code));
    }
  }
}
複製代碼
  • TableMapEvent繼承了BinlogEvent,它定義了database、table、columnTypes屬性

WriteEvent

SpinalTap/spinaltap-mysql/src/main/java/com/airbnb/spinaltap/mysql/event/WriteEvent.javasql

@Getter
public final class WriteEvent extends BinlogEvent {
  private final List<Serializable[]> rows;

  public WriteEvent(
      long tableId,
      long serverId,
      long timestamp,
      BinlogFilePos filePos,
      List<Serializable[]> rows) {
    super(tableId, serverId, timestamp, filePos);

    this.rows = rows;
  }

  @Override
  public int size() {
    return rows.size();
  }
}
複製代碼
  • WriteEvent繼承了BinlogEvent,它定義了類型爲List<Serializable[]>的rows屬性

DeleteEvent

SpinalTap/spinaltap-mysql/src/main/java/com/airbnb/spinaltap/mysql/event/DeleteEvent.javabash

@Getter
public final class DeleteEvent extends BinlogEvent {
  private final List<Serializable[]> rows;

  public DeleteEvent(
      long tableId,
      long serverId,
      long timestamp,
      BinlogFilePos filePos,
      List<Serializable[]> rows) {
    super(tableId, serverId, timestamp, filePos);

    this.rows = rows;
  }

  @Override
  public int size() {
    return rows.size();
  }
}
複製代碼
  • DeleteEvent繼承了BinlogEvent,它定義了類型爲List<Serializable[]>的rows屬性

UpdateEvent

SpinalTap/spinaltap-mysql/src/main/java/com/airbnb/spinaltap/mysql/event/UpdateEvent.javaide

@Getter
public class UpdateEvent extends BinlogEvent {
  private final List<Map.Entry<Serializable[], Serializable[]>> rows;

  public UpdateEvent(
      long tableId,
      long serverId,
      long timestamp,
      BinlogFilePos filePos,
      List<Map.Entry<Serializable[], Serializable[]>> rows) {
    super(tableId, serverId, timestamp, filePos);

    this.rows = rows;
  }

  @Override
  public int size() {
    return rows.size();
  }
}
複製代碼
  • UpdateEvent繼承了BinlogEvent,它定義了類型爲List<Map.Entry<Serializable[], Serializable[]>>的rows屬性

QueryEvent

SpinalTap/spinaltap-mysql/src/main/java/com/airbnb/spinaltap/mysql/event/QueryEvent.javaui

@Getter
public class QueryEvent extends BinlogEvent {
  private final String database;
  private final String sql;

  public QueryEvent(
      long serverId, long timestamp, BinlogFilePos filePos, String database, String sql) {
    super(0l, serverId, timestamp, filePos);

    this.database = database;
    this.sql = sql;
  }
}
複製代碼
  • QueryEvent繼承了BinlogEvent,它定義了database、sql屬性

XidEvent

SpinalTap/spinaltap-mysql/src/main/java/com/airbnb/spinaltap/mysql/event/XidEvent.javathis

@Getter
public class XidEvent extends BinlogEvent {
  private final long xid;

  public XidEvent(long serverId, long timestamp, BinlogFilePos filePos, long xid) {
    super(0l, serverId, timestamp, filePos);

    this.xid = xid;
  }
}
複製代碼
  • XidEvent繼承了BinlogEvent,它定義了xid屬性

GTIDEvent

SpinalTap/spinaltap-mysql/src/main/java/com/airbnb/spinaltap/mysql/event/GTIDEvent.javaspa

@Getter
public class GTIDEvent extends BinlogEvent {
  private final String gtid;

  public GTIDEvent(long serverId, long timestamp, BinlogFilePos filePos, String gtid) {
    super(0, serverId, timestamp, filePos);
    this.gtid = gtid;
  }
}
複製代碼
  • GTIDEvent繼承了BinlogEvent,它定義了gtid屬性

小結

BinlogEvent繼承了SourceEvent,它定義了tableId、serverId、binlogFilePos屬性;其子類主要有StartEvent、TableMapEvent、WriteEvent、DeleteEvent、UpdateEvent、QueryEvent、XidEvent、GTIDEvent

doc

相關文章
相關標籤/搜索