Class: Innodb::UndoRecord

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

Overview

A single undo log record.

Constant Summary collapse

TYPE =

Possible undo record types.

{
  11 => :insert,
  12 => :update_existing,
  13 => :update_deleted,
  14 => :delete,
}
TYPE_MASK =
0x0f
COMPILATION_INFO_MASK =
0x70
COMPILATION_INFO_SHIFT =
4
COMPILATION_INFO_NO_ORDER_CHANGE_BV =
1
COMPILATION_INFO_NO_SIZE_CHANGE_BV =
2
EXTERN_FLAG =
0x80

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(undo_page, position) ⇒ UndoRecord

Returns a new instance of UndoRecord.



11
12
13
14
15
16
17
# File 'lib/innodb/undo_record.rb', line 11

def initialize(undo_page, position)
  @undo_page = undo_page
  @position = position

  @undo_log = nil
  @index_page = nil
end

Instance Attribute Details

#index_pageObject

Returns the value of attribute index_page.



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

def index_page
  @index_page
end

#positionObject (readonly)

Returns the value of attribute position.



6
7
8
# File 'lib/innodb/undo_record.rb', line 6

def position
  @position
end

#undo_logObject

Returns the value of attribute undo_log.



8
9
10
# File 'lib/innodb/undo_record.rb', line 8

def undo_log
  @undo_log
end

#undo_pageObject (readonly)

Returns the value of attribute undo_page.



5
6
7
# File 'lib/innodb/undo_record.rb', line 5

def undo_page
  @undo_page
end

Instance Method Details

#cursor(position) ⇒ Object

Return a BufferCursor starting before the header.



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

def cursor(position)
  new_cursor = @undo_page.cursor(position)
  if @undo_log
    new_cursor.push_name("undo_log[#{@undo_log.position}]")
  end
  new_cursor.push_name("undo_record[#{@position}]")
  new_cursor
end

#get(prev_or_next) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/innodb/undo_record.rb', line 95

def get(prev_or_next)
  if header[prev_or_next] != 0
    new_undo_record = new_subordinate(@undo_page, header[prev_or_next])
    if new_undo_record.type
      new_undo_record
    end
  end
end

#has_previous_version?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/innodb/undo_record.rb', line 91

def has_previous_version?
  [:update_existing, :update_deleted, :delete].include?(type)
end

#headerObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/innodb/undo_record.rb', line 67

def header
  @header ||= cursor(pos_header).name("header") do |c|
    header = {
      :prev => c.name("prev") { c.get_uint16 },
      :next => c.name("next") { c.get_uint16 },
    }

    info = c.name("info") { c.get_uint8 }
    cmpl = (info & COMPILATION_INFO_MASK) >> COMPILATION_INFO_SHIFT
    header[:type] = TYPE[info & TYPE_MASK]
    header[:extern_flag] = (info & EXTERN_FLAG) != 0
    header[:info] = {
      :order_may_change => (cmpl & COMPILATION_INFO_NO_ORDER_CHANGE_BV) == 0,
      :size_may_change  => (cmpl & COMPILATION_INFO_NO_SIZE_CHANGE_BV) == 0,
    }

    header
  end
end

#keyObject



204
205
206
# File 'lib/innodb/undo_record.rb', line 204

def key
  undo_record[:key]
end

#key_stringObject



208
209
210
# File 'lib/innodb/undo_record.rb', line 208

def key_string
  key && key.map { |r| "%s=%s" % [r[:name], r[:value].inspect] }.join(", ")
end

#new_subordinate(undo_page, position) ⇒ Object



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

def new_subordinate(undo_page, position)
  new_undo_record = self.class.new(undo_page, position)
  new_undo_record.undo_log = undo_log
  new_undo_record.index_page = index_page

  new_undo_record
end

#nextObject



108
109
110
# File 'lib/innodb/undo_record.rb', line 108

def next
  get(:next)
end

#pos_headerObject

The header really starts 2 bytes before the undo record position, as the pointer to the previous record is written there.



29
30
31
# File 'lib/innodb/undo_record.rb', line 29

def pos_header
  @position - 2
end

#pos_recordObject



38
39
40
# File 'lib/innodb/undo_record.rb', line 38

def pos_record
  pos_header + size_header
end

#prevObject



104
105
106
# File 'lib/innodb/undo_record.rb', line 104

def prev
  get(:prev)
end

#prev_by_historyObject

Find the previous row version by following the roll_ptr from one undo record to the next (backwards through the record version history). Since we are operating without the benefit of knowing about active transactions and without protection from purge, check that everything looks sane before returning it.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/innodb/undo_record.rb', line 229

def prev_by_history
  unless has_previous_version?
    # This undo record type has no previous version information.
    return nil
  end

  undo_log = roll_ptr[:undo_log]
  older_undo_page = @undo_page.space.page(undo_log[:page])

  unless older_undo_page and older_undo_page.is_a?(Innodb::Page::UndoLog)
    # The page was probably re-used for something else.
    return nil
  end

  older_undo_record = new_subordinate(older_undo_page,
                                      undo_log[:offset])

  unless older_undo_record and table_id == older_undo_record.table_id
    # The record space was probably re-used for something else.
    return nil
  end

  unless older_undo_record.trx_id.nil? or trx_id >= older_undo_record.trx_id
    # The trx_id should not be newer; but may be absent (for insert).
    return nil
  end

  older_undo_record
end

#read_recordObject



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
# File 'lib/innodb/undo_record.rb', line 116

def read_record
  cursor(pos_record).name("record") do |c|
    this_record = {
      :page => undo_page.offset,
      :offset => position,
      :header => header,
      :undo_no => c.name("undo_no") { c.get_imc_uint64 },
      :table_id => c.name("table_id") { c.get_imc_uint64 },
    }

    if has_previous_version?
      this_record[:info_bits] = c.name("info_bits") { c.get_uint8 }
      this_record[:trx_id] = c.name("trx_id") { c.get_ic_uint64 }
      this_record[:roll_ptr] = c.name("roll_ptr") {
        Innodb::DataType::RollPointerType.parse_roll_pointer(c.get_ic_uint64)
      }
    end

    if index_page
      read_record_fields(this_record, c)
    else
      # Slurp up the remaining data as a string.
      this_record[:data] = c.get_bytes(header[:next] - c.position - 2)
    end

    this_record
  end
end

#read_record_fields(this_record, c) ⇒ Object



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
# File 'lib/innodb/undo_record.rb', line 145

def read_record_fields(this_record, c)
  this_record[:key] = []
  index_page.record_format[:key].each do |field|
    this_record[:key][field.position] = {
      :name => field.name,
      :type => field.data_type.name,
      :value => c.name(field.name) {
        field_length = c.name("field_length") { c.get_ic_uint32 }
        field.value_by_length(c, field_length)
      }
    }
  end

  if has_previous_version?
    field_count = c.name("field_count") { c.get_ic_uint32 }
    this_record[:row] = Array.new(index_page.record_format[:row].size)
    field_count.times do
      field_number = c.name("field_number[#{field_count}]") { c.get_ic_uint32 }
      field = nil
      field_index = nil
      index_page.record_format[:row].each_with_index do |candidate_field, index|
        if candidate_field.position == field_number
          field = candidate_field
          field_index = index
        end
      end
      raise "Unknown field #{field_number}" unless field
      this_record[:row][field_index] = {
        :name => field.name,
        :type => field.data_type.name,
        :value => c.name(field.name) {
          field_length = c.name("field_length") { c.get_ic_uint32 }
          field.value_by_length(c, field_length)
        }
      }
    end
  end
end

#record_sizeObject



112
113
114
# File 'lib/innodb/undo_record.rb', line 112

def record_size
  header[:next] - @position - size_header
end

#roll_ptrObject



200
201
202
# File 'lib/innodb/undo_record.rb', line 200

def roll_ptr
  undo_record[:roll_ptr]
end

#rowObject



212
213
214
# File 'lib/innodb/undo_record.rb', line 212

def row
  undo_record[:row]
end

#row_stringObject



216
217
218
# File 'lib/innodb/undo_record.rb', line 216

def row_string
  row && row.select { |r| !r.nil? }.map { |r| r && "%s=%s" % [r[:name], r[:value].inspect] }.join(", ")
end

#size_headerObject

The size of the header.



34
35
36
# File 'lib/innodb/undo_record.rb', line 34

def size_header
  2 + 2 + 1
end

#stringObject



220
221
222
# File 'lib/innodb/undo_record.rb', line 220

def string
  "(%s) → (%s)" % [key_string, row_string]
end

#table_idObject



192
193
194
# File 'lib/innodb/undo_record.rb', line 192

def table_id
  undo_record[:table_id]
end

#trx_idObject



196
197
198
# File 'lib/innodb/undo_record.rb', line 196

def trx_id
  undo_record[:trx_id]
end

#typeObject



87
88
89
# File 'lib/innodb/undo_record.rb', line 87

def type
  header[:type]
end

#undo_noObject



188
189
190
# File 'lib/innodb/undo_record.rb', line 188

def undo_no
  undo_record[:undo_no]
end

#undo_recordObject



184
185
186
# File 'lib/innodb/undo_record.rb', line 184

def undo_record
  @undo_record ||= read_record
end