class MysqlBinlog::BinlogFileReader

Read a binary log from a file on disk.

Constants

MAGIC_SIZE
MAGIC_VALUE

Attributes

tail[RW]

Public Class Methods

new(filename) click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 9
def initialize(filename)
  @tail = false
  open_file(filename)
end

Public Instance Methods

end?() click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 66
def end?
  return false if tail
  @binlog.eof?
end
filename() click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 46
def filename
  @filename
end
open_file(filename) click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 20
def open_file(filename)
  @dirname  = File.dirname(filename)
  @filename = File.basename(filename)
  @binlog   = File.open(filename, "r:BINARY")

  verify_magic
end
position() click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 50
def position
  @binlog.tell
end
read(length) click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 79
def read(length)
  if tail
    needed_position = position + length
    while @binlog.stat.size < needed_position
      sleep 0.02
    end
  end
  return "" if length == 0
  data = @binlog.read(length)
  if !data
    raise MalformedBinlogException.new
  elsif data.length == 0
    raise ZeroReadException.new
  elsif data.length < length
    raise ShortReadException.new
  end
  data
end
remaining(header) click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 71
def remaining(header)
  header[:payload_end] - @binlog.tell
end
rewind() click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 54
def rewind
  seek(MAGIC_SIZE)
end
rotate(filename, position) click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 28
def rotate(filename, position)
  retries = 10
  begin
    open_file(@dirname + "/" + filename)
    seek(position)
  rescue Errno::ENOENT
    # A rotate event will be seen in the previous log file before the
    # new file exists. Retry a few times with a little sleep to give
    # the server a chance to create the new file.
    if (retries -= 1) > 0
      sleep 0.01
      retry
    else
      raise
    end
  end
end
seek(pos) click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 58
def seek(pos)
  @binlog.seek(pos)
end
skip(header) click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 75
def skip(header)
  seek(header[:next_position])
end
unget(char) click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 62
def unget(char)
  @binlog.ungetc(char)
end
verify_magic() click to toggle source
# File lib/mysql_binlog/reader/binlog_file_reader.rb, line 14
def verify_magic
  if (magic = read(MAGIC_SIZE).unpack("V").first) != MAGIC_VALUE
    raise MalformedBinlogException.new("Magic number #{magic} is incorrect")
  end
end