聊聊puma的ChangedEvent

468 阅读2分钟

本文主要研究一下puma的ChangedEvent

Event

puma/core/src/main/java/com/dianping/puma/core/event/Event.java

public abstract class Event implements Serializable {

	private static final long serialVersionUID = 7986284681273254505L;

	private long seq;
	public void setSeq(long seq) {
		this.seq = seq;
	}
	public long getSeq() {
		return seq;
	}

	public abstract BinlogInfo getBinlogInfo();

	public abstract EventType getEventType();
}
  • Event定义了getBinlogInfo、getEventType抽象方法

ChangedEvent

puma/core/src/main/java/com/dianping/puma/core/event/ChangedEvent.java

public abstract class ChangedEvent extends Event implements Serializable {
	private static final long	serialVersionUID	= -2358086827502066009L;
	protected long					executeTime;
	protected String				database;
	protected String				table;
	protected long					serverId;
	protected BinlogInfo 		binlogInfo;

	//......

}
  • ChangedEvent定义了executeTime、database、table、serverId、binlogInfo属性

DdlEvent

puma/core/src/main/java/com/dianping/puma/core/event/DdlEvent.java

public class DdlEvent extends ChangedEvent implements Serializable {
    private static final long serialVersionUID = -5676914333310337620L;

    private final EventType eventType = EventType.DDL;

    private String sql;

    private DDLType ddlType;

    private DdlEventType ddlEventType;

    private DdlEventSubType ddlEventSubType;

    public DdlEvent() {

    }

    public DdlEvent(long executeTime, long serverId, String binlogFile, long binlogPosition) {
        this.executeTime = executeTime;
        this.serverId = serverId;
        this.binlogInfo = new BinlogInfo(serverId, binlogFile, binlogPosition, 0, executeTime);
    }

    //......
}
  • DdlEvent继承了ChangedEvent,它定义了eventType、sql、ddlType、ddlEventType、ddlEventSubType属性

RowChangedEvent

puma/core/src/main/java/com/dianping/puma/core/event/RowChangedEvent.java

public class RowChangedEvent extends ChangedEvent implements Serializable, Cloneable {

    private final EventType eventType = EventType.DML;

    private static final long serialVersionUID = -3426837914222597530L;

    private DMLType dmlType;

    private boolean isTransactionBegin = false;

    private boolean isTransactionCommit = false;

    private Map<String, ColumnInfo> columns = new HashMap<String, ColumnInfo>();

    public RowChangedEvent() {
    }

    public RowChangedEvent(long executionTime, long serverId, String binlogFile, long binlogPosition) {
        this.executeTime = executionTime;
        this.serverId = serverId;
        this.binlogInfo = new BinlogInfo(serverId, binlogFile, binlogPosition, 0, executionTime);
    }

    //......

}
  • RowChangedEvent继承了ChangedEvent,它定义了eventType、dmlType、isTransactionBegin、isTransactionCommit、columns属性

ColumnInfo

puma/core/src/main/java/com/dianping/puma/core/event/RowChangedEvent.java

    public static class ColumnInfo implements Serializable {
        private static final long serialVersionUID = 8036820944314281838L;

        private boolean isKey;

        private Object oldValue;

        private Object newValue;

        /**
         *
         */
        public ColumnInfo() {
            super();
        }

        /**
         * @param isKey
         * @param oldValue
         * @param newValue
         */
        public ColumnInfo(boolean isKey, Object oldValue, Object newValue) {
            super();
            this.isKey = isKey;
            this.oldValue = oldValue;
            this.newValue = newValue;
        }

        //......

    }
  • ColumnInfo定义了isKey、oldValue、newValue属性

DDLType

puma/core/src/main/java/com/dianping/puma/core/util/sql/DDLType.java

public enum DDLType {

	ALTER_DATABASE(1),
	ALTER_EVENT(2),
	ALTER_LOGFILE_GROUP(3),
	ALTER_FUNCTION(4),
	ALTER_PROCEDURE(5),
	ALTER_SERVER(6),
	ALTER_TABLE(7),
	ALTER_TABLESPACE(8),
	ALTER_VIEW(9),

	CREATE_DATABASE(11),
	CREATE_EVENT(12),
	CREATE_INDEX(13),
	CREATE_LOGFILE_GROUP(14),
	CREATE_FUNCTION(15),
	CREATE_PROCEDURE(16),
	CREATE_SERVER(17),
	CREATE_TABLE(18),
	CREATE_TABLESPACE(19),
	CREATE_TRIGGER(20),
	CREATE_VIEW(21),

	DROP_DATABASE(31),
	DROP_EVENT(32),
	DROP_INDEX(33),
	DROP_LOGFILE_GROUP(34),
	DROP_FUNCTION(35),
	DROP_PROCEDURE(36),
	DROP_SERVER(37),
	DROP_TABLE(38),
	DROP_TABLESPACE(39),
	DROP_TRIGGER(40),
	DROP_VIEW(41),

	RENAME_DATABASE(51),
	RENAME_TABLE(52),

	TRUNCATE_TABLE(61);
	
	private int type;
	
	DDLType(int type){
		this.type = type;
	}
	
	
	public int getDDLType(){
		return this.type;
	}
	
	public static DDLType getDDLType(int type){
		for(DDLType ddlType : DDLType.values()){
			if(ddlType.getDDLType() == type){
				return ddlType;
			}
		}
		
		return null;
	}
}
  • DDLType定义了alter、create、drop、rename、truncate枚举值

DMLType

puma/core/src/main/java/com/dianping/puma/core/util/sql/DMLType.java

public enum DMLType {

    NULL(0), INSERT(1), DELETE(2), UPDATE(3);

    private int type;

    DMLType(int type) {
        this.type = type;
    }

    public int getDMLType() {
        return this.type;
    }

    public static DMLType getDMLType(int type) {
        for (DMLType dmlType : DMLType.values()) {
            if (dmlType.getDMLType() == type) {
                return dmlType;
            }
        }

        return null;
    }
}
  • DMLType定义了NULL、INSERT、DELETE、UPDATE枚举值

小结

ChangedEvent定义了executeTime、database、table、serverId、binlogInfo属性;它有DdlEvent及RowChangedEvent两个子类

doc