Class: MysqlReplicator::Binlogs::EventParser

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_replicator/binlogs/event_parser.rb

Instance Method Summary collapse

Constructor Details

#initializeEventParser

Returns a new instance of EventParser.



31
32
33
# File 'lib/mysql_replicator/binlogs/event_parser.rb', line 31

def initialize
  @stored_table_map = {}
end

Instance Method Details

#execute(payload, connection, checksum_enabled) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mysql_replicator/binlogs/event_parser.rb', line 39

def execute(payload, connection, checksum_enabled)
  offset = 0

  timestamp = Time.at(MysqlReplicator::StringUtil.read_uint32(payload[offset, 4]))
  offset += 4
  event_type = readable_event_type(MysqlReplicator::StringUtil.read_uint8(payload[offset]))
  offset += 1
  server_id = MysqlReplicator::StringUtil.read_uint32(payload[offset, 4])
  offset += 4
  event_length = MysqlReplicator::StringUtil.read_uint32(payload[offset, 4])
  offset += 4
  next_position = MysqlReplicator::StringUtil.read_uint32(payload[offset, 4])
  offset += 4
  flags = MysqlReplicator::StringUtil.read_uint16(payload[offset, 2])
  offset += 2

  payload_length = event_length - offset

  execution = parse_execution_data(
    event_type,
    MysqlReplicator::StringUtil.read_str(payload[offset, payload_length]),
    connection,
    checksum_enabled
  )

  {
    timestamp: timestamp,
    event_type: event_type,
    server_id: server_id,
    length: event_length,
    next_position: next_position,
    flags: flags,
    execution: execution
  }
end

#parse_execution_data(event_type, payload, connection, checksum_enabled) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mysql_replicator/binlogs/event_parser.rb', line 107

def parse_execution_data(event_type, payload, connection, checksum_enabled)
  case event_type
  when :QUERY
    MysqlReplicator::Binlogs::QueryEventParser.parse(payload, checksum_enabled)
  when :ROTATE
    MysqlReplicator::Binlogs::RotateEventParser.parse(payload, checksum_enabled)
  when :FORMAT_DESCRIPTION
    MysqlReplicator::Binlogs::FormatDescriptionEventParser.parse(payload)
  when :TABLE_MAP
    result = MysqlReplicator::Binlogs::TableMapEventParser.parse(payload, connection)
    # Store in table map for future row events
    @stored_table_map[result[:table_id]] = result
    result
  when :WRITE_ROWS
    MysqlReplicator::Binlogs::RowsEventParser.parse(:WRITE_ROWS, payload, checksum_enabled, @stored_table_map)
  when :UPDATE_ROWS
    MysqlReplicator::Binlogs::RowsEventParser.parse(:UPDATE_ROWS, payload, checksum_enabled, @stored_table_map)
  when :DELETE_ROWS
    MysqlReplicator::Binlogs::RowsEventParser.parse(:DELETE_ROWS, payload, checksum_enabled, @stored_table_map)
  when :XID
    MysqlReplicator::Binlogs::XidEventParser.parse(payload)
  else
    {}
  end
end

#readable_event_type(event_type) ⇒ Object

Basic event type identification



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mysql_replicator/binlogs/event_parser.rb', line 79

def readable_event_type(event_type)
  case event_type
  when 2
    :QUERY
  when 4
    :ROTATE
  when 15
    :FORMAT_DESCRIPTION
  when 16
    :XID
  when 19
    :TABLE_MAP
  when 30
    :WRITE_ROWS
  when 31
    :UPDATE_ROWS
  when 32
    :DELETE_ROWS
  else
    :UNKNOWN
  end
end