Class: Innodb::LogReader

Inherits:
Object
  • Object
show all
Defined in:
lib/innodb/log_reader.rb

Overview

Representation of the log group as a seekable stream of log records.

Defined Under Namespace

Classes: ChecksumError, EOFError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lsn, group) ⇒ LogReader

Returns a new instance of LogReader.



11
12
13
14
15
# File 'lib/innodb/log_reader.rb', line 11

def initialize(lsn, group)
  @group = group
  @context = OpenStruct.new(:buffer => String.new,
    :buffer_lsn => lsn.dup, :record_lsn => lsn.dup)
end

Instance Attribute Details

#checksumObject

Whether to checksum blocks.



9
10
11
# File 'lib/innodb/log_reader.rb', line 9

def checksum
  @checksum
end

Instance Method Details

#each_record(follow, wait = 0.5) ⇒ Object

Call the given block once for each record in the log until the end of the log (or a corrupted block) is reached. If the follow argument is true, retry.



43
44
45
46
47
48
49
# File 'lib/innodb/log_reader.rb', line 43

def each_record(follow, wait=0.5)
  begin
    loop { yield record }
  rescue EOFError, ChecksumError
    sleep(wait) and retry if follow
  end
end

#recordObject

Read a record.



32
33
34
35
36
37
38
# File 'lib/innodb/log_reader.rb', line 32

def record
  cursor = BufferCursor.new(self, 0)
  record = Innodb::LogRecord.new
  record.read(cursor)
  record.lsn = reposition(cursor.position)
  record
end

#seek(lsn_no) ⇒ Object

Seek to record starting position.



18
19
20
21
22
23
24
# File 'lib/innodb/log_reader.rb', line 18

def seek(lsn_no)
  check_lsn_no(lsn_no)
  @context.buffer = String.new
  @context.buffer_lsn.reposition(lsn_no, @group)
  @context.record_lsn = @context.buffer_lsn.dup
  self
end

#slice(position, length) ⇒ Object

Read a slice of log data (that is, log data used for records).



52
53
54
55
56
57
58
59
60
61
# File 'lib/innodb/log_reader.rb', line 52

def slice(position, length)
  buffer = @context.buffer
  length = position + length

  if length > buffer.size
    preload(length)
  end

  buffer.slice(position, length - position)
end

#tellObject

Returns the current LSN starting position.



27
28
29
# File 'lib/innodb/log_reader.rb', line 27

def tell
  @context.record_lsn.no
end