Class: MysqlBinlog::Binlog

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_binlog/binlog.rb

Overview

Read a binary log, parsing and returning events.

Examples

A basic example of using the Binlog class:

require 'mysql_binlog'
include MysqlBinlog

# Open a binary log from a file on disk.
binlog = Binlog.new(BinlogFileReader.new("mysql-bin.000001"))

# Iterate over all events from the log, printing the event type (such
# as :query_event, :write_rows_event, etc.)
binlog.each_event do |event|
  puts event[:type]
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader) ⇒ Binlog

Returns a new instance of Binlog.



55
56
57
58
59
60
61
62
63
64
# File 'lib/mysql_binlog/binlog.rb', line 55

def initialize(reader)
  @reader = reader
  @field_parser = BinlogFieldParser.new(self)
  @event_parser = BinlogEventParser.new(self)
  @fde = nil
  @filter_event_types = nil
  @filter_flags = nil
  @ignore_rotate = false
  @max_query_length = 1048576
end

Instance Attribute Details

#event_parserObject

Returns the value of attribute event_parser.



49
50
51
# File 'lib/mysql_binlog/binlog.rb', line 49

def event_parser
  @event_parser
end

#fdeObject (readonly)

Returns the value of attribute fde.



46
47
48
# File 'lib/mysql_binlog/binlog.rb', line 46

def fde
  @fde
end

#field_parserObject

Returns the value of attribute field_parser.



48
49
50
# File 'lib/mysql_binlog/binlog.rb', line 48

def field_parser
  @field_parser
end

#filter_event_typesObject

Returns the value of attribute filter_event_types.



50
51
52
# File 'lib/mysql_binlog/binlog.rb', line 50

def filter_event_types
  @filter_event_types
end

#filter_flagsObject

Returns the value of attribute filter_flags.



51
52
53
# File 'lib/mysql_binlog/binlog.rb', line 51

def filter_flags
  @filter_flags
end

#ignore_rotateObject

Returns the value of attribute ignore_rotate.



52
53
54
# File 'lib/mysql_binlog/binlog.rb', line 52

def ignore_rotate
  @ignore_rotate
end

#max_query_lengthObject

Returns the value of attribute max_query_length.



53
54
55
# File 'lib/mysql_binlog/binlog.rb', line 53

def max_query_length
  @max_query_length
end

#readerObject

Returns the value of attribute reader.



47
48
49
# File 'lib/mysql_binlog/binlog.rb', line 47

def reader
  @reader
end

Instance Method Details

#each_eventObject

Iterate through all events.



185
186
187
188
189
190
191
192
193
# File 'lib/mysql_binlog/binlog.rb', line 185

def each_event
  unless block_given?
    return Enumerable::Enumerator.new(self, :each_event)
  end

  while event = read_event
    yield event
  end
end

#read_eventObject

Scan events until finding one that isn’t rejected by the filter rules. If there are no filter rules, this will return the next event provided by the reader.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/mysql_binlog/binlog.rb', line 109

def read_event
  while true
    skip_this_event = false
    return nil if reader.end?

    filename = reader.filename
    position = reader.position

    # Read the common header for an event. Every event has a header.
    unless header = event_parser.event_header
      return nil
    end

    if @filter_event_types
      unless @filter_event_types.include? header[:event_type]
        skip_this_event = true
      end
    end
    
    if @filter_flags
      unless @filter_flags.include? header[:flags]
        skip_this_event = true
      end
    end

    # Never skip over rotate_event or format_description_event as they
    # are critical to understanding the format of this event stream.
    if skip_this_event
      unless [:rotate_event, :format_description_event].include? header[:event_type]
        skip_event(header)
        next
      end
    end

    fields = read_event_fields(header)

    case header[:event_type]
    when :rotate_event
      unless ignore_rotate
        reader.rotate(fields[:name], fields[:pos])
      end
    when :format_description_event
      process_fde(fields)
    end

    break
  end

  {
    :type     => header[:event_type],
    :filename => filename,
    :position => position,
    :header   => header,
    :event    => fields,
  }
end

#rewindObject

Rewind to the beginning of the log, if supported by the reader. The reader may throw an exception if rewinding is not supported (e.g. for a stream-based reader).



69
70
71
# File 'lib/mysql_binlog/binlog.rb', line 69

def rewind
  reader.rewind
end