其他分享
首页 > 其他分享> > 聊聊SpinalTap的BinlogEvent

聊聊SpinalTap的BinlogEvent

作者:互联网

本文主要研究一下SpinalTap的BinlogEvent

BinlogEvent

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

@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;
  }
}

StartEvent

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

public class StartEvent extends BinlogEvent {
  public StartEvent(long serverId, long timestamp, BinlogFilePos filePos) {
    super(0L, serverId, timestamp, filePos);
  }
}

TableMapEvent

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

@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));
    }
  }
}

WriteEvent

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

@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();
  }
}

DeleteEvent

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

@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();
  }
}

UpdateEvent

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

@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();
  }
}

QueryEvent

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

@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;
  }
}

XidEvent

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

@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;
  }
}

GTIDEvent

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

@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;
  }
}

小结

BinlogEvent继承了SourceEvent,它定义了tableId、serverId、binlogFilePos属性;其子类主要有StartEvent、TableMapEvent、WriteEvent、DeleteEvent、UpdateEvent、QueryEvent、XidEvent、GTIDEvent,然后就ojbk了,文章分享完毕,希望我的分享对大家有所帮助。更多学习技巧也可参阅:Python

http://www.fu-w.com/a/63599.html

标签:timestamp,long,SpinalTap,聊聊,mysql,serverId,public,BinlogEvent
来源: https://www.cnblogs.com/meilideni/p/12992014.html