Class: Innodb::Record

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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.



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

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

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

#recordObject

Returns the value of attribute record.



10
11
12
# File 'lib/innodb/record.rb', line 10

def record
  @record
end

Instance Method Details

#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


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/innodb/record.rb', line 103

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

  0
end

#dumpObject



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/innodb/record.rb', line 119

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
  else
    puts "Child page number: %i" % child_page_number
  end
  puts
end

#each_undo_recordObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/innodb/record.rb', line 64

def each_undo_record
  return enum_for(:each_undo_record) unless block_given?

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

  nil
end

#fieldsObject



94
95
96
# File 'lib/innodb/record.rb', line 94

def fields
  @fields ||= uncached_fields
end

#full_value_with_externs_for_field(field) ⇒ Object



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

def full_value_with_externs_for_field(field)
  blob_value = field.value
  extern_page = field.extern && page.space.page(field.extern.page_number)
  while extern_page
    blob_value += extern_page.blob_data
    extern_page = extern_page.next_blob_page
  end
  blob_value
end

#key_stringObject



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

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

#row_stringObject



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

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

#stringObject



76
77
78
79
80
81
82
# File 'lib/innodb/record.rb', line 76

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

#uncached_fieldsObject



84
85
86
87
88
89
90
91
92
# File 'lib/innodb/record.rb', line 84

def uncached_fields
  fields_hash = {}
  %i[key row].each do |group|
    record[group]&.each do |column|
      fields_hash[column.name] = column.value
    end
  end
  fields_hash
end

#undoObject



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

def undo
  return nil unless roll_pointer
  return unless (innodb_system = @page.space.innodb_system)

  undo_page = innodb_system.system_space.page(roll_pointer.undo_log.page)
  return unless undo_page

  new_undo_record = Innodb::UndoRecord.new(undo_page, roll_pointer.undo_log.offset)
  new_undo_record.index_page = page
  new_undo_record
end