binlog

package
v0.0.0-...-1e4b8bd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2023 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidHeader is returned when event header cannot be parsed.
	ErrInvalidHeader = errors.New("Header is invalid")
)

Functions

func RowsEventHasExtraData

func RowsEventHasExtraData(et EventType) bool

RowsEventHasExtraData returns true if given event is of rows type and contains extra data.

func RowsEventHasSecondBitmap

func RowsEventHasSecondBitmap(et EventType) bool

RowsEventHasSecondBitmap returns true if given event is of rows type and contains a second bitmap.

func RowsEventVersion

func RowsEventVersion(et EventType) int

RowsEventVersion returns rows event versions. If event is not a rows type -1 is returned.

Types

type ChecksumAlgorithm

type ChecksumAlgorithm byte

ChecksumAlgorithm is a checksum algorithm is the one used by the server.

const (
	// FlavorMySQL is the MySQL db flavor.
	FlavorMySQL = "MySQL"

	// ChecksumAlgorithmNone means no checksum appened.
	ChecksumAlgorithmNone ChecksumAlgorithm = 0x00
	// ChecksumAlgorithmCRC32 used to append a 4 byte checksum at the end.
	ChecksumAlgorithmCRC32 ChecksumAlgorithm = 0x01
	// ChecksumAlgorithmUndefined is used when checksum algorithm is not known.
	ChecksumAlgorithmUndefined ChecksumAlgorithm = 0xFF
)

func (ChecksumAlgorithm) String

func (ca ChecksumAlgorithm) String() string

type EventHeader

type EventHeader struct {
	Timestamp    uint32
	Type         EventType
	ServerID     uint32
	EventLen     uint32
	NextOffset   uint32
	Flags        uint16
	ExtraHeaders []byte
}

EventHeader represents binlog event header.

func (*EventHeader) Decode

func (h *EventHeader) Decode(connBuff []byte, fd FormatDescription) error

Decode decodes given buffer into event header. Spec: https://dev.mysql.com/doc/internals/en/event-header-fields.html

type EventType

type EventType byte

EventType defines a binary log event type.

const (
	// EventTypeUnknown is an event that should never occur.
	EventTypeUnknown EventType = 0
	// EventTypeStartV3 is the Start_event of binlog format 3.
	EventTypeStartV3 EventType = 1
	// EventTypeQuery is created for each query that modifies the database,
	// unless the query is logged row-based.
	EventTypeQuery EventType = 2
	// EventTypeStop is written to the log files under these circumstances:
	// A master writes the event to the binary log when it shuts down.
	// A slave writes the event to the relay log when it shuts down or when a
	// RESET SLAVE statement is executed.
	EventTypeStop EventType = 3
	// EventTypeRotate is written at the end of the file that points to the next
	// file in the squence. It is written when a binary log file exceeds a size
	// limit.
	EventTypeRotate EventType = 4
	// EventTypeIntvar will be created just before a Query_event, if the query
	// uses one of the variables LAST_INSERT_ID or INSERT_ID.
	EventTypeIntvar EventType = 5
	// EventTypeLoad ...
	EventTypeLoad EventType = 6
	// EventTypeSlave ...
	EventTypeSlave EventType = 7
	// EventTypeCreateFile ...
	EventTypeCreateFile EventType = 8
	// EventTypeAppendBlock is created to contain the file data.
	EventTypeAppendBlock EventType = 9
	// EventTypeExecLoad ...
	EventTypeExecLoad EventType = 10
	// EventTypeDeleteFile occurs when the LOAD DATA failed on the master.
	// This event notifies the slave not to do the load and to delete the
	// temporary file.
	EventTypeDeleteFile EventType = 11
	// EventTypeNewLoad ...
	EventTypeNewLoad EventType = 12
	// EventTypeRand logs random seed used by the next RAND(), and by PASSWORD()
	// in 4.1.0.
	EventTypeRand EventType = 13
	// EventTypeUserVar is written every time a statement uses a user variable;
	// precedes other events for the statement. Indicates the value to use for
	// the user variable in the next statement. This is written only before a
	// QUERY_EVENT and is not used with row-based logging.
	EventTypeUserVar EventType = 14
	// EventTypeFormatDescription is saved by threads which read it, as they
	// need it for future use (to decode the ordinary events).
	EventTypeFormatDescription EventType = 15
	// EventTypeXID is generated for a commit of a transaction that modifies one
	// or more tables of an XA-capable storage engine.
	EventTypeXID EventType = 16
	// EventTypeBeginLoadQuery is for the first block of file to be loaded, its
	// only difference from Append_block event is that this event creates or
	// truncates existing file before writing data.
	EventTypeBeginLoadQuery EventType = 17
	// EventTypeExecuteLoadQuery is responsible for LOAD DATA execution, it
	// similar to Query_event but before executing the query it substitutes
	// original filename in LOAD DATA query with name of temporary file.
	EventTypeExecuteLoadQuery EventType = 18
	// EventTypeTableMap is used in row-based mode where it preceeds every row
	// operation event and maps a table definition to a number. The table
	// definition consists of database name, table name, and column definitions.
	EventTypeTableMap EventType = 19
	// EventTypeWriteRowsV0 represents inserted rows. Used in MySQL 5.1.0 to
	// 5.1.15.
	EventTypeWriteRowsV0 EventType = 20
	// EventTypeUpdateRowsV0 represents updated rows. It contains both old and
	// new versions. Used in MySQL 5.1.0 to 5.1.15.
	EventTypeUpdateRowsV0 EventType = 21
	// EventTypeDeleteRowsV0 represents deleted rows. Used in MySQL 5.1.0 to
	// 5.1.15.
	EventTypeDeleteRowsV0 EventType = 22
	// EventTypeWriteRowsV1 represents inserted rows. Used in MySQL 5.1.15 to
	// 5.6.
	EventTypeWriteRowsV1 EventType = 23
	// EventTypeUpdateRowsV1 represents updated rows. It contains both old and
	// new versions. Used in MySQL 5.1.15 to 5.6.
	EventTypeUpdateRowsV1 EventType = 24
	// EventTypeDeleteRowsV1 represents deleted rows. Used in MySQL 5.1.15 to
	// 5.6.
	EventTypeDeleteRowsV1 EventType = 25
	// EventTypeIncident represents an incident, an occurance out of the
	// ordinary, that happened on the master. The event is used to inform the
	// slave that something out of the ordinary happened on the master that
	// might cause the database to be in an inconsistent state.
	EventTypeIncident EventType = 26
	// EventTypeHeartbeet is a replication event used to ensure to slave that
	// master is alive. The event is originated by master's dump thread and sent
	// straight to slave without being logged. Slave itself does not store it in
	// relay log but rather uses a data for immediate checks and throws away the
	// event.
	EventTypeHeartbeet EventType = 27
	// EventTypeIgnorable is a kind of event that could be ignored.
	EventTypeIgnorable EventType = 28
	// EventTypeRowsQuery is a subclass of the IgnorableEvent, to record the
	// original query for the rows events in RBR.
	EventTypeRowsQuery EventType = 29
	// EventTypeWriteRowsV2 represents inserted rows. Used starting from MySQL
	// 5.6.
	EventTypeWriteRowsV2 EventType = 30
	// EventTypeUpdateRowsV2 represents updated rows. It contains both old and
	// new versions. Used starting from MySQL 5.6.
	EventTypeUpdateRowsV2 EventType = 31
	// EventTypeDeleteRowsV2 represents deleted rows. Used starting from MySQL
	// 5.6.
	EventTypeDeleteRowsV2 EventType = 32
	// EventTypeGTID is an event that contains latest GTID.
	// GTID stands for Global Transaction IDentifier It is composed of two
	// parts:
	// * SID for Source Identifier, and
	// * GNO for Group Number. The basic idea is to associate an identifier, the
	// Global Transaction IDentifier or GTID, to every transaction. When a
	// transaction is copied to a slave, re-executed on the slave, and written
	// to the slave's binary log, the GTID is preserved. When a slave connects
	// to a master, the slave uses GTIDs instead of (file, offset).
	EventTypeGTID EventType = 33
	// EventTypeAnonymousGTID is a subclass of GTIDEvent.
	EventTypeAnonymousGTID EventType = 34
	// EventTypePreviousGTIDs is a subclass of GTIDEvent.
	EventTypePreviousGTIDs EventType = 35
)

Spec: https://dev.mysql.com/doc/internals/en/event-classes-and-types.html

func (EventType) String

func (et EventType) String() string

type Flavor

type Flavor string

Flavor defines the specific kind of MySQL-like database.

type FormatDescription

type FormatDescription struct {
	Version                uint16
	ServerVersion          string
	CreateTimestamp        uint32
	EventHeaderLength      uint8
	EventTypeHeaderLengths []uint8
	ServerDetails          ServerDetails
}

FormatDescription is a description of binary log format.

func (FormatDescription) HeaderLen

func (fd FormatDescription) HeaderLen() int

HeaderLen returns length of event header.

func (FormatDescription) PostHeaderLen

func (fd FormatDescription) PostHeaderLen(et EventType) int

PostHeaderLen returns length of a post-header for a given event type.

func (FormatDescription) TableIDSize

func (fd FormatDescription) TableIDSize(et EventType) int

TableIDSize returns table ID size for a given event type.

type FormatDescriptionEvent

type FormatDescriptionEvent struct {
	FormatDescription
}

FormatDescriptionEvent contains server details and binary log format description. It is usually the first event in a log file.

func (*FormatDescriptionEvent) Decode

func (e *FormatDescriptionEvent) Decode(data []byte) error

Decode decodes given buffer into a format description event. Spec: https://dev.mysql.com/doc/internals/en/format-description-event.html

type Position

type Position struct {
	File   string
	Offset uint64
}

Position is a pair of log file name and a binary offset in it that is used to represent the beginning of the event description.

type QueryEvent

type QueryEvent struct {
	SlaveProxyID  uint32
	ExecutionTime uint32
	ErrorCode     uint16
	StatusVars    []byte
	Schema        []byte
	Query         []byte
}

QueryEvent contains query details.

func (*QueryEvent) Decode

func (e *QueryEvent) Decode(connBuff []byte)

Decode given buffer into a qeury event. Spec: https://dev.mysql.com/doc/internals/en/query-event.html

type RotateEvent

type RotateEvent struct {
	NextFile Position
}

RotateEvent is written at the end of the file that points to the next file in the squence. It is written when a binary log file exceeds a size limit.

func (*RotateEvent) Decode

func (e *RotateEvent) Decode(connBuff []byte, fd FormatDescription) error

Decode decodes given buffer into a rotate event. Spec: https://dev.mysql.com/doc/internals/en/rotate-event.html

type RowsEvent

type RowsEvent struct {
	Type          EventType
	TableID       uint64
	Flags         RowsFlag
	ExtraData     []byte
	ColumnCount   uint64
	ColumnBitmap1 []byte
	ColumnBitmap2 []byte
	Rows          [][]interface{}
}

RowsEvent contains a Rows Event.

func (*RowsEvent) Decode

func (e *RowsEvent) Decode(connBuff []byte, fd FormatDescription, td TableDescription) (err error)

Decode decodes given buffer into a rows event event.

func (*RowsEvent) PeekTableIDAndFlags

func (e *RowsEvent) PeekTableIDAndFlags(connBuff []byte, fd FormatDescription) (uint64, RowsFlag)

PeekTableIDAndFlags returns table ID and flags without decoding whole event.

type RowsFlag

type RowsFlag uint16

RowsFlag is bitmask of flags.

const (
	// RowsFlagEndOfStatement is used to clear old table mappings.
	RowsFlagEndOfStatement RowsFlag = 0x0001
)

type ServerDetails

type ServerDetails struct {
	Flavor            Flavor
	Version           int
	ChecksumAlgorithm ChecksumAlgorithm
}

ServerDetails contains server feature details.

type TableDescription

type TableDescription struct {
	Flags       uint16
	SchemaName  string
	TableName   string
	ColumnCount uint64
	ColumnTypes []byte
	ColumnMeta  []uint16
	NullBitmask []byte
}

TableDescription contains table details required to process rows events.

type TableMapEvent

type TableMapEvent struct {
	TableID uint64
	TableDescription
}

TableMapEvent contains table description alongside an ID that would be used to reference the table in the following rows events.

func (*TableMapEvent) Decode

func (e *TableMapEvent) Decode(connBuff []byte, fd FormatDescription) error

Decode decodes given buffer into a table map event. Spec: https://dev.mysql.com/doc/internals/en/table-map-event.html

type XIDEvent

type XIDEvent struct {
	XID uint64
}

XIDEvent contains an XID (XA transaction identifier) https://dev.mysql.com/doc/refman/5.7/en/xa.html

func (*XIDEvent) Decode

func (e *XIDEvent) Decode(connBuff []byte)

Decode decodes given buffer into an XID event. Spec: https://dev.mysql.com/doc/internals/en/xid-event.html

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL