Class: MysqlBinlog::Binlog
- Inherits:
-
Object
- Object
- MysqlBinlog::Binlog
- 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
-
#checksum ⇒ Object
Returns the value of attribute checksum.
-
#event_parser ⇒ Object
Returns the value of attribute event_parser.
-
#fde ⇒ Object
readonly
Returns the value of attribute fde.
-
#field_parser ⇒ Object
Returns the value of attribute field_parser.
-
#filter_event_types ⇒ Object
Returns the value of attribute filter_event_types.
-
#filter_flags ⇒ Object
Returns the value of attribute filter_flags.
-
#ignore_rotate ⇒ Object
Returns the value of attribute ignore_rotate.
-
#max_query_length ⇒ Object
Returns the value of attribute max_query_length.
-
#reader ⇒ Object
Returns the value of attribute reader.
Instance Method Summary collapse
- #checksum_length ⇒ Object
-
#each_event ⇒ Object
Iterate through all events.
-
#initialize(reader) ⇒ Binlog
constructor
A new instance of Binlog.
- #payload_length(header) ⇒ Object
-
#read_event ⇒ Object
Scan events until finding one that isn’t rejected by the filter rules.
-
#rewind ⇒ Object
Rewind to the beginning of the log, if supported by the reader.
Constructor Details
#initialize(reader) ⇒ Binlog
Returns a new instance of Binlog.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mysql_binlog/binlog.rb', line 56 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 @checksum = :nil end |
Instance Attribute Details
#checksum ⇒ Object
Returns the value of attribute checksum.
54 55 56 |
# File 'lib/mysql_binlog/binlog.rb', line 54 def checksum @checksum end |
#event_parser ⇒ Object
Returns the value of attribute event_parser.
49 50 51 |
# File 'lib/mysql_binlog/binlog.rb', line 49 def event_parser @event_parser end |
#fde ⇒ Object (readonly)
Returns the value of attribute fde.
46 47 48 |
# File 'lib/mysql_binlog/binlog.rb', line 46 def fde @fde end |
#field_parser ⇒ Object
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_types ⇒ Object
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_flags ⇒ Object
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_rotate ⇒ Object
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_length ⇒ Object
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 |
#reader ⇒ Object
Returns the value of attribute reader.
47 48 49 |
# File 'lib/mysql_binlog/binlog.rb', line 47 def reader @reader end |
Instance Method Details
#checksum_length ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/mysql_binlog/binlog.rb', line 114 def checksum_length case @checksum when :crc32 4 else 0 end end |
#each_event ⇒ Object
Iterate through all events.
218 219 220 221 222 223 224 225 226 |
# File 'lib/mysql_binlog/binlog.rb', line 218 def each_event unless block_given? return Enumerable::Enumerator.new(self, :each_event) end while event = read_event yield event end end |
#payload_length(header) ⇒ Object
123 124 125 |
# File 'lib/mysql_binlog/binlog.rb', line 123 def payload_length(header) @fde ? (header[:event_length] - @fde[:header_length] - checksum_length) : 0 end |
#read_event ⇒ Object
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.
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mysql_binlog/binlog.rb', line 130 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 # Skip the remaining part of the header which might not have been # parsed. if @fde reader.seek(position + @fde[:header_length]) header[:payload_length] = payload_length(header) header[:payload_end] = position + @fde[:header_length] + payload_length(header) else header[:payload_length] = 0 header[:payload_end] = header[:next_position] 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 |
#rewind ⇒ Object
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).
71 72 73 |
# File 'lib/mysql_binlog/binlog.rb', line 71 def rewind reader.rewind end |