/*
 * Copyright (C) 2010-2101 Alibaba Group Holding Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

syntax = "proto3";
package com.alibaba.otter.canal.protocol;

option java_package = "com.alibaba.otter.canal.protocol";
option java_outer_classname = "CanalEntry";
option optimize_for = SPEED;

/****************************************************************
 * message model
 *如果要在Enum中新增类型，确保以前的类型的下标值不变.
 ****************************************************************/
message Entry {
	/**协议头部信息**/
     Header						header 				= 1;
	///**打散后的事件类型**/ [default = ROWDATA]
	oneof entryType_present{
		EntryType					entryType			= 2;
	}

	/**传输的二进制数组**/
	bytes						storeValue			= 3;
}

/**message Header**/
message Header {
	/**协议的版本号**/  //[default = 1]
	oneof version_present {
		int32 					version				= 1;
	}


	/**binlog/redolog 文件名**/
	string					logfileName			= 2;

	/**binlog/redolog 文件的偏移位置**/
	int64 					logfileOffset		= 3;

	/**服务端serverId**/
	int64				serverId         	= 4;

	/** 变更数据的编码 **/
	string					serverenCode		= 5;

	/**变更数据的执行时间 **/
	int64					executeTime			= 6;

	/** 变更数据的来源**/ //[default = MYSQL]
	oneof sourceType_present {
		Type					sourceType			= 7;
	}


	/** 变更数据的schemaname**/
	string					schemaName			= 8;

	/**变更数据的tablename**/
	string					tableName			= 9;

	/**每个event的长度**/
	int64					eventLength         = 10;

	/**数据变更类型**/  // [default = UPDATE]
	oneof eventType_present {
		EventType 				eventType			= 11;
	}


	/**预留扩展**/
	repeated Pair					props				= 12;

    /**当前事务的gitd**/
	string                 gtid                = 13;
}

/**每个字段的数据结构**/
message Column {
	/**字段下标**/
	int32		index			= 		1;

	/**字段java中类型**/
	int32 		sqlType			= 		2;

	/**字段名称(忽略大小写)，在mysql中是没有的**/
	string		name			=		3;

	/**是否是主键**/
	bool 		isKey			= 		4;

	/**如果EventType=UPDATE,用于标识这个字段值是否有修改**/
	bool		updated			= 		5;

	/** 标识是否为空  **/ //[default = false]
	oneof isNull_present {
		bool		isNull			= 		6;
	}


	/**预留扩展**/
	repeated Pair		props			=		7;

	/** 字段值,timestamp,Datetime是一个时间格式的文本 **/
	string		value			= 		8;

	/** 对应数据对象原始长度 **/
	int32		length			= 		9;

	/**字段mysql类型**/
	string		mysqlType		= 		10;
}

message RowData {

	/** 字段信息，增量数据(修改前,删除前) **/
	repeated Column			beforeColumns  	= 		1;

	/** 字段信息，增量数据(修改后,新增后)  **/
	repeated Column			afterColumns	= 		2;

	/**预留扩展**/
	repeated Pair			props			=		3;
}

/**message row 每行变更数据的数据结构**/
message RowChange {

	/**tableId,由数据库产生**/
	int64	 		tableId			=		1;


	/**数据变更类型**/ //[default = UPDATE]
	oneof eventType_present {
		EventType 		eventType		= 		2;
	}


	/** 标识是否是ddl语句  **/ // [default = false]
	oneof isDdl_present {
		bool			isDdl			= 		10;
	}


	/** ddl/query的sql语句  **/
	string			sql 			= 		11;

	/** 一次数据库变更可能存在多行  **/
	repeated RowData		rowDatas		= 		12;

	/**预留扩展**/
	repeated Pair			props			=		13;

	/** ddl/query的schemaName，会存在跨库ddl，需要保留执行ddl的当前schemaName  **/
	string			ddlSchemaName 	= 		14;
}

/**开始事务的一些信息**/
message TransactionBegin{

	/**已废弃，请使用header里的executeTime**/
	int64			executeTime		=		1;

	/**已废弃，Begin里不提供事务id**/
	string			transactionId	=		2;

	/**预留扩展**/
	repeated Pair			props			=		3;

	/**执行的thread Id**/
	int64			threadId		=		4;
}

/**结束事务的一些信息**/
message TransactionEnd{

	/**已废弃，请使用header里的executeTime**/
	int64			executeTime		=		1;

	/**事务号**/
	string			transactionId	=		2;

	/**预留扩展**/
	repeated Pair			props			=		3;
}

/**预留扩展**/
message Pair{
	string 		key				= 			1;
	string 		value			= 			2;
}

/**打散后的事件类型，主要用于标识事务的开始，变更数据，结束**/
enum EntryType{
	ENTRYTYPECOMPATIBLEPROTO2 = 0;
	TRANSACTIONBEGIN 		=		1;
	ROWDATA					=		2;
	TRANSACTIONEND			=		3;
	/** 心跳类型，内部使用，外部暂不可见，可忽略 **/
	ENTRYHEARTBEAT				=		4;
	GTIDLOG                 =       5;
}

/** 事件类型 **/
enum EventType {
	EVENTTYPECOMPATIBLEPROTO2 = 0;
	INSERT 		= 		1;
	UPDATE 		= 		2;
	DELETE 		= 		3;
	CREATE		= 		4;
	ALTER		= 		5;
	ERASE		= 		6;
	QUERY		=		7;
	TRUNCATE	=		8;
	RENAME 		= 		9;
	/**CREATE INDEX**/
	CINDEX		= 		10;
	DINDEX 		= 		11;
	GTID        =       12;
	/** XA **/
	XACOMMIT    =       13;
	XAROLLBACK  =		14;
	/** MASTER HEARTBEAT **/
	MHEARTBEAT  =       15;
}

/**数据库类型**/
enum Type {
	TYPECOMPATIBLEPROTO2 = 0;
	ORACLE		= 		1;
	MYSQL		= 		2;
	PGSQL		= 		3;
}
