Class: Innodb::Record

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, record) ⇒ Record

Returns a new instance of Record.



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

def initialize(page, record)
  @page = page
  @record = record
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



4
5
6
# File 'lib/innodb/record.rb', line 4

def page
  @page
end

#recordObject

Returns the value of attribute record.



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

def record
  @record
end

Instance Method Details

#child_page_numberObject



103
104
105
# File 'lib/innodb/record.rb', line 103

def child_page_number
  record[:child_page_number]
end

#compare_key(other_key) ⇒ Object

Compare two arrays of fields to determine if they are equal. This follows the same comparison rules as strcmp and others:

0 = a is equal to b
-1 = a is less than b
+1 = a is greater than b


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/innodb/record.rb', line 136

def compare_key(other_key)
  Innodb::Stats.increment :compare_key

  return 0 if other_key.nil? && key.nil?
  return -1 if other_key.nil? || (!key.nil? && other_key.size < key.size)
  return +1 if key.nil? || (!other_key.nil? && other_key.size > key.size)

  key.each_index do |i|
    Innodb::Stats.increment :compare_key_field_comparison
    return -1 if other_key[i] < key[i][:value]
    return +1 if other_key[i] > key[i][:value]
  end

  return 0
end

#deleted?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/innodb/record.rb', line 32

def deleted?
  header[:deleted]
end

#dumpObject



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
198
199
# File 'lib/innodb/record.rb', line 152

def dump
  puts "Record at offset %i" % offset
  puts

  puts "Header:"
  puts "  %-20s: %i" % ["Next record offset", header[:next]]
  puts "  %-20s: %i" % ["Heap number", header[:heap_number]]
  puts "  %-20s: %s" % ["Type", header[:type]]
  puts "  %-20s: %s" % ["Deleted", header[:deleted]]
  puts "  %-20s: %s" % ["Length", header[:length]]
  puts

  if page.leaf?
    puts "System fields:"
    puts "  Transaction ID: %s" % transaction_id
    puts "  Roll Pointer:"
    puts "    Undo Log: page %i, offset %i" % [
      roll_pointer[:undo_log][:page],
      roll_pointer[:undo_log][:offset],
    ]
    puts "    Rollback Segment ID: %i" % roll_pointer[:rseg_id]
    puts "    Insert: %s" % roll_pointer[:is_insert]
    puts
  end

  puts "Key fields:"
  key.each do |field|
    puts "  %s: %s" % [
      field[:name],
      field[:value].inspect,
    ]
  end
  puts

  if page.leaf?
    puts "Non-key fields:"
    row.each do |field|
      puts "  %s: %s" % [
        field[:name],
        field[:value].inspect,
      ]
    end
    puts
  else
    puts "Child page number: %i" % child_page_number
    puts
  end
end

#each_undo_recordObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/innodb/record.rb', line 89

def each_undo_record
  unless block_given?
    return enum_for(:each_undo_record)
  end

  undo_record = undo
  while undo_record
    yield undo_record
    undo_record = undo_record.prev_by_history
  end

  nil
end

#fieldsObject



127
128
129
# File 'lib/innodb/record.rb', line 127

def fields
  @fields ||= uncached_fields
end

#headerObject



12
13
14
# File 'lib/innodb/record.rb', line 12

def header
  record[:header]
end

#heap_numberObject



24
25
26
# File 'lib/innodb/record.rb', line 24

def heap_number
  header[:heap_number]
end

#keyObject



52
53
54
# File 'lib/innodb/record.rb', line 52

def key
  record[:key]
end

#key_stringObject



56
57
58
# File 'lib/innodb/record.rb', line 56

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

#lengthObject



44
45
46
# File 'lib/innodb/record.rb', line 44

def length
  record[:length]
end

#min_rec?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/innodb/record.rb', line 36

def min_rec?
  header[:min_rec]
end

#n_ownedObject



28
29
30
# File 'lib/innodb/record.rb', line 28

def n_owned
  header[:n_owned]
end

#nextObject



16
17
18
# File 'lib/innodb/record.rb', line 16

def next
  header[:next]
end

#offsetObject



40
41
42
# File 'lib/innodb/record.rb', line 40

def offset
  record[:offset]
end

#roll_pointerObject



72
73
74
# File 'lib/innodb/record.rb', line 72

def roll_pointer
  record[:roll_pointer]
end

#rowObject



60
61
62
# File 'lib/innodb/record.rb', line 60

def row
  record[:row]
end

#row_stringObject



64
65
66
# File 'lib/innodb/record.rb', line 64

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

#stringObject



107
108
109
110
111
112
113
# File 'lib/innodb/record.rb', line 107

def string
  if child_page_number
    "(%s) → #%s" % [key_string, child_page_number]
  else
    "(%s) → (%s)" % [key_string, row_string]
  end
end

#transaction_idObject



68
69
70
# File 'lib/innodb/record.rb', line 68

def transaction_id
  record[:transaction_id]
end

#typeObject



20
21
22
# File 'lib/innodb/record.rb', line 20

def type
  header[:type]
end

#uncached_fieldsObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/innodb/record.rb', line 115

def uncached_fields
  fields_hash = {}
  [:key, :row].each do |group|
    if record[group]
      record[group].each do |column|
        fields_hash[column[:name]] = column[:value]
      end
    end
  end
  fields_hash
end

#undoObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/innodb/record.rb', line 76

def undo
  return nil unless roll_pointer

  if innodb_system = @page.space.innodb_system
    undo_space = innodb_system.system_space
    if undo_page = undo_space.page(roll_pointer[:undo_log][:page])
      new_undo_record = Innodb::UndoRecord.new(undo_page, roll_pointer[:undo_log][:offset])
      new_undo_record.index_page = page
      new_undo_record
    end
  end
end