Class: MysqlBinlog::BinlogFileReader
- Inherits:
-
Object
- Object
- MysqlBinlog::BinlogFileReader
- Defined in:
- lib/mysql_binlog/reader/binlog_file_reader.rb
Overview
Read a binary log from a file on disk.
Constant Summary collapse
- MAGIC_SIZE =
4
- MAGIC_VALUE =
1852400382
Instance Attribute Summary collapse
-
#tail ⇒ Object
Returns the value of attribute tail.
Instance Method Summary collapse
- #end? ⇒ Boolean
- #filename ⇒ Object
-
#initialize(filename) ⇒ BinlogFileReader
constructor
A new instance of BinlogFileReader.
- #open_file(filename) ⇒ Object
- #position ⇒ Object
- #read(length) ⇒ Object
- #remaining(header) ⇒ Object
- #rewind ⇒ Object
- #rotate(filename, position) ⇒ Object
- #seek(pos) ⇒ Object
- #skip(header) ⇒ Object
- #unget(char) ⇒ Object
- #verify_magic ⇒ Object
Constructor Details
#initialize(filename) ⇒ BinlogFileReader
Returns a new instance of BinlogFileReader.
9 10 11 12 |
# File 'lib/mysql_binlog/reader/binlog_file_reader.rb', line 9 def initialize(filename) @tail = false open_file(filename) end |
Instance Attribute Details
#tail ⇒ Object
Returns the value of attribute tail.
7 8 9 |
# File 'lib/mysql_binlog/reader/binlog_file_reader.rb', line 7 def tail @tail end |
Instance Method Details
#end? ⇒ Boolean
66 67 68 69 |
# File 'lib/mysql_binlog/reader/binlog_file_reader.rb', line 66 def end? return false if tail @binlog.eof? end |
#filename ⇒ Object
46 47 48 |
# File 'lib/mysql_binlog/reader/binlog_file_reader.rb', line 46 def filename @filename end |
#open_file(filename) ⇒ Object
20 21 22 23 24 25 26 |
# 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, mode="r") verify_magic end |
#position ⇒ Object
50 51 52 |
# File 'lib/mysql_binlog/reader/binlog_file_reader.rb', line 50 def position @binlog.tell end |
#read(length) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# 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) ⇒ Object
71 72 73 |
# File 'lib/mysql_binlog/reader/binlog_file_reader.rb', line 71 def remaining(header) header[:next_position] - @binlog.tell end |
#rewind ⇒ Object
54 55 56 |
# File 'lib/mysql_binlog/reader/binlog_file_reader.rb', line 54 def rewind seek(MAGIC_SIZE) end |
#rotate(filename, position) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# 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) ⇒ Object
58 59 60 |
# File 'lib/mysql_binlog/reader/binlog_file_reader.rb', line 58 def seek(pos) @binlog.seek(pos) end |
#skip(header) ⇒ Object
75 76 77 |
# File 'lib/mysql_binlog/reader/binlog_file_reader.rb', line 75 def skip(header) seek(header[:next_position]) end |
#unget(char) ⇒ Object
62 63 64 |
# File 'lib/mysql_binlog/reader/binlog_file_reader.rb', line 62 def unget(char) @binlog.ungetc(char) end |
#verify_magic ⇒ Object
14 15 16 17 18 |
# 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 |