Class: Innodb::Page::Index

Inherits:
Innodb::Page show all
Defined in:
lib/innodb/page/index.rb

Overview

A specialized class for handling INDEX pages, which contain a portion of the data from exactly one B+tree. These are typically the most common type of page in any database.

The basic structure of an INDEX page is: FIL header, INDEX header, FSEG header, fixed-width system records (infimum and supremum), user records (the actual data) which grow ascending by offset, free space, the page directory which grows descending by offset, and the FIL trailer.

Defined Under Namespace

Classes: RecordCursor

Constant Summary collapse

PAGE_DIRECTION =

Page direction values possible in the page_header’s :direction field.

{
  1 => :left,           # Inserts have been in descending order.
  2 => :right,          # Inserts have been in ascending order.
  3 => :same_rec,       # Unused by InnoDB.
  4 => :same_page,      # Unused by InnoDB.
  5 => :no_direction,   # Inserts have been in random order.
}
RECORD_BITS_SIZE =

The size (in bytes) of the bit-packed fields in each record header.

3
RECORD_NEXT_SIZE =

The size (in bytes) of the “next” pointer in each record header.

2
PAGE_DIR_SLOT_SIZE =

The size (in bytes) of the record pointers in each page directory slot.

2
PAGE_DIR_SLOT_MIN_N_OWNED =

The minimum number of records “owned” by each record with an entry in the page directory.

4
PAGE_DIR_SLOT_MAX_N_OWNED =

The maximum number of records “owned” by each record with an entry in the page directory.

8
RECORD_TYPES =

Record types used in the :type field of the record header.

{
  0 => :conventional,   # A normal user record in a leaf page.
  1 => :node_pointer,   # A node pointer in a non-leaf page.
  2 => :infimum,        # The system "infimum" record.
  3 => :supremum,       # The system "supremum" record.
}
RECORD_INFO_MIN_REC_FLAG =

This record is the minimum record at this level of the B-tree.

1
RECORD_INFO_DELETED_FLAG =

This record has been marked as deleted.

2

Constants inherited from Innodb::Page

PAGE_TYPE, SPECIALIZED_CLASSES

Instance Attribute Summary collapse

Attributes inherited from Innodb::Page

#space

Instance Method Summary collapse

Methods inherited from Innodb::Page

#cursor, #data, #fil_header, #initialize, #inspect, #lsn, maybe_undefined, #next, #offset, parse, #pos_fil_header, #pos_fil_trailer, #prev, #size, #size_fil_header, #size_fil_trailer, #type

Constructor Details

This class inherits a constructor from Innodb::Page

Instance Attribute Details

#record_describerObject

Returns the value of attribute record_describer.



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

def record_describer
  @record_describer
end

Instance Method Details

#directoryObject

Return an array of row offsets for all entries in the page directory.



483
484
485
486
487
488
489
490
491
492
493
# File 'lib/innodb/page/index.rb', line 483

def directory
  return @directory if @directory

  @directory = []
  c = cursor(pos_directory).backward
  directory_slots.times do
    @directory.push c.get_uint16
  end

  @directory
end

#directory_slotsObject

The number of directory slots in use.



83
84
85
# File 'lib/innodb/page/index.rb', line 83

def directory_slots
  page_header[:n_dir_slots]
end

#directory_spaceObject

The amount of space consumed by the page directory.



88
89
90
# File 'lib/innodb/page/index.rb', line 88

def directory_space
  directory_slots * PAGE_DIR_SLOT_SIZE
end

#dumpObject

Dump the contents of a page for debugging purposes.



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/innodb/page/index.rb', line 496

def dump
  super

  puts "page header:"
  pp page_header
  puts

  puts "fseg header:"
  pp fseg_header
  puts

  puts "sizes:"
  puts "  %-15s%5i" % [ "header",     header_space ]
  puts "  %-15s%5i" % [ "trailer",    trailer_space ]
  puts "  %-15s%5i" % [ "directory",  directory_space ]
  puts "  %-15s%5i" % [ "free",       free_space ]
  puts "  %-15s%5i" % [ "used",       used_space ]
  puts "  %-15s%5i" % [ "record",     record_space ]
  puts "  %-15s%5.2f" % [
    "per record",
    (page_header[:n_recs] > 0) ? (record_space / page_header[:n_recs]) : 0
  ]
  puts

  puts "system records:"
  pp infimum
  pp supremum
  puts

  puts "page directory:"
  pp directory
  puts

  puts "records:"
  each_record do |rec|
    pp rec
  end
  puts
end

#each_child_pageObject

Iterate through all child pages of a node (non-leaf) page, which are stored as records with the child page number as the last field in the record.



468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/innodb/page/index.rb', line 468

def each_child_page
  return nil if level == 0

  unless block_given?
    return enum_for(:each_child_page)
  end

  each_record do |rec|
    yield rec[:child_page_number], rec[:key]
  end

  nil
end

#each_recordObject

Iterate through all records.



451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/innodb/page/index.rb', line 451

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

  c = record_cursor(infimum[:next])

  while rec = c.record
    yield rec
  end

  nil
end

#first_recordObject

Return the first record on this page.



445
446
447
448
# File 'lib/innodb/page/index.rb', line 445

def first_record
  first = record(infimum[:next])
  first if first != supremum
end

#free_spaceObject

Return the amount of free space in the page.



98
99
100
101
# File 'lib/innodb/page/index.rb', line 98

def free_space
  page_header[:garbage] +
    (size - size_fil_trailer - directory_space - page_header[:heap_top])
end

#fseg_headerObject

Return the “fseg” header.



166
167
168
169
170
171
172
# File 'lib/innodb/page/index.rb', line 166

def fseg_header
  c = cursor(pos_fseg_header)
  @fseg_header ||= {
    :leaf     => Innodb::FsegEntry.get_inode(@space, c),
    :internal => Innodb::FsegEntry.get_inode(@space, c),
  }
end

#header_spaceObject

The amount of space consumed by the page header.



76
77
78
79
80
# File 'lib/innodb/page/index.rb', line 76

def header_space
  # The end of the supremum system record is the beginning of the space
  # available for user records.
  pos_user_records
end

#infimumObject

Return the infimum record on a page.



318
319
320
# File 'lib/innodb/page/index.rb', line 318

def infimum
  @infimum ||= system_record(pos_infimum)
end

#levelObject

A helper function to return the page level from the “page” header, for easier access.



150
151
152
# File 'lib/innodb/page/index.rb', line 150

def level
  page_header && page_header[:level]
end

#make_record_descriptionObject

Return a set of field objects that describe the record.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/innodb/page/index.rb', line 328

def make_record_description
  description = record_describer.cursor_sendable_description(self)

  fields = []

  (description[:key] + description[:row]).each_with_index do |d, p|
    fields << Innodb::Field.new(p, *d)
  end

  n = description[:key].size

  description[:key] = fields.slice(0 .. n-1)
  description[:row] = fields.slice(n ..  -1)

  return description
end

#page_headerObject

Return the “index” header.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/innodb/page/index.rb', line 129

def page_header
  c = cursor(pos_index_header)
  @page_header ||= {
    :n_dir_slots  => c.get_uint16,
    :heap_top     => c.get_uint16,
    :n_heap       => ((n_heap = c.get_uint16) & (2**15-1)),
    :free         => c.get_uint16,
    :garbage      => c.get_uint16,
    :last_insert  => c.get_uint16,
    :direction    => PAGE_DIRECTION[c.get_uint16],
    :n_direction  => c.get_uint16,
    :n_recs       => c.get_uint16,
    :max_trx_id   => c.get_uint64,
    :level        => c.get_uint16,
    :index_id     => c.get_uint64,
    :format       => (n_heap & 1<<15) == 0 ? :redundant : :compact,
  }
end

#pos_directoryObject

The position of the page directory, which starts at the “fil” trailer and grows backwards from there.



71
72
73
# File 'lib/innodb/page/index.rb', line 71

def pos_directory
  pos_fil_trailer
end

#pos_fseg_headerObject

Return the byte offset of the start of the “fseg” header, which immediately follows the “index” header.



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

def pos_fseg_header
  pos_index_header + size_index_header
end

#pos_index_headerObject

Return the byte offset of the start of the “index” page header, which immediately follows the “fil” header.



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

def pos_index_header
  pos_fil_header + size_fil_header
end

#pos_infimumObject

Return the byte offset of the start of the “origin” of the infimum record, which is always the first record in the singly-linked record chain on any page, and represents a record with a “lower value than any possible user record”. The infimum record immediately follows the page header.



51
52
53
# File 'lib/innodb/page/index.rb', line 51

def pos_infimum
  pos_records + size_record_header + size_record_undefined
end

#pos_recordsObject

Return the byte offset of the start of records within the page (the position immediately after the page header).



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

def pos_records
  size_fil_header + size_index_header + size_fseg_header
end

#pos_supremumObject

Return the byte offset of the start of the “origin” of the supremum record, which is always the last record in the singly-linked record chain on any page, and represents a record with a “higher value than any possible user record”. The supremum record immediately follows the infimum record.



59
60
61
# File 'lib/innodb/page/index.rb', line 59

def pos_supremum
  pos_infimum + size_record_header + size_record_undefined + size_mum_record
end

#pos_user_recordsObject

Return the byte offset of the start of the user records in a page, which immediately follows the supremum record.



65
66
67
# File 'lib/innodb/page/index.rb', line 65

def pos_user_records
  pos_supremum + size_mum_record
end

#record(offset) ⇒ Object

Parse and return a record at a given offset.



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/innodb/page/index.rb', line 353

def record(offset)
  return nil unless offset
  return infimum  if offset == pos_infimum
  return supremum if offset == pos_supremum

  c = cursor(offset).forward

  # There is a header preceding the row itself, so back up and read it.
  header = record_header(offset)

  this_record = {
    :format => page_header[:format],
    :offset => offset,
    :header => header,
    :next => header[:next] == 0 ? nil : (offset + header[:next]),
  }

  if record_format
    this_record[:type] = record_format[:type]

    # Read the key fields present in all types of pages.
    this_record[:key] = []
    record_format[:key].each do |f|
      this_record[:key].push f.read(this_record, c)
    end

    # If this is a leaf page of the clustered index, read InnoDB's internal
    # fields, a transaction ID and roll pointer.
    if level == 0 && record_format[:type] == :clustered
      this_record[:transaction_id] = c.get_hex(6)
      first_byte = c.get_uint8
      this_record[:roll_pointer]   = {
        :is_insert  => (first_byte & 0x80) == 0x80,
        :rseg_id    => first_byte & 0x7f,
        :undo_log => {
          :page       => c.get_uint32,
          :offset     => c.get_uint16,
        }
      }
    end

    # If this is a leaf page of the clustered index, or any page of a
    # secondary index, read the non-key fields.
    if (level == 0 && record_format[:type] == :clustered) ||
      (record_format[:type] == :secondary)
      # Read the non-key fields.
      this_record[:row] = []
      record_format[:row].each do |f|
        this_record[:row].push f.read(this_record, c)
      end
    end

    # If this is a node (non-leaf) page, it will have a child page number
    # (or "node pointer") stored as the last field.
    if level > 0
      # Read the node pointer in a node (non-leaf) page.
      this_record[:child_page_number] = c.get_uint32
    end
  end

  this_record
end

#record_bytesObject

Return the actual bytes of the portion of the page which is used to store user records (eliminate the headers and trailer from the page).



115
116
117
# File 'lib/innodb/page/index.rb', line 115

def record_bytes
  data(pos_user_records, page_header[:heap_top] - pos_user_records)
end

#record_cursor(offset) ⇒ Object

Return a RecordCursor starting at offset.



440
441
442
# File 'lib/innodb/page/index.rb', line 440

def record_cursor(offset)
  RecordCursor.new(self, offset)
end

#record_formatObject

Return (and cache) the record format provided by an external class.



346
347
348
349
350
# File 'lib/innodb/page/index.rb', line 346

def record_format
  if record_describer
    @record_format ||= make_record_description()
  end
end

#record_header(offset) ⇒ Object

Return the header from a record.



227
228
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
# File 'lib/innodb/page/index.rb', line 227

def record_header(offset)
  c = cursor(offset).backward
  case page_header[:format]
  when :compact
    header = {}
    header[:next] = c.get_sint16
    bits1 = c.get_uint16
    header[:type] = RECORD_TYPES[bits1 & 0x07]
    header[:order] = (bits1 & 0xf8) >> 3
    bits2 = c.get_uint8
    header[:n_owned] = bits2 & 0x0f
    info = (bits2 & 0xf0) >> 4
    header[:min_rec] = (info & RECORD_INFO_MIN_REC_FLAG) != 0
    header[:deleted] = (info & RECORD_INFO_DELETED_FLAG) != 0
    case header[:type]
    when :conventional, :node_pointer
      # The variable-length part of the record header contains a
      # bit vector indicating NULL fields and the length of each
      # non-NULL variable-length field.
      if record_format
        header[:null_bitmap] = nbmap = record_null_bitmap(c)
        header[:variable_length] = record_variable_length(c, nbmap)
      end
    end
    header
  when :redundant
    raise "Not implemented"
  end
end

#record_null_bitmap(cursor) ⇒ Object

Return an array indicating which fields are null.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/innodb/page/index.rb', line 258

def record_null_bitmap(cursor)
  fields = (record_format[:key] + record_format[:row])

  # The number of bits in the bitmap is the number of nullable fields.
  size = fields.count do |f| f.nullable end

  # There is no bitmap if there are no nullable fields.
  return nil unless size > 0

  # To simplify later checks, expand bitmap to one for each field.
  bitmap = Array.new(fields.size, false)

  null_bit_array = cursor.get_bit_array(size).reverse!

  # For every nullable field, set whether the field is actually null.
  fields.each do |f|
    bitmap[f.position] = f.nullable ? (null_bit_array.shift == 1) : false
  end

  return bitmap
end

#record_spaceObject

Return the amount of space occupied by records in the page.



109
110
111
# File 'lib/innodb/page/index.rb', line 109

def record_space
  used_space - header_space - directory_space - trailer_space
end

#record_variable_length(cursor, null_bitmap) ⇒ Object

Return an array containing the length of each variable-length field.



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/innodb/page/index.rb', line 281

def record_variable_length(cursor, null_bitmap)
  fields = (record_format[:key] + record_format[:row])

  len_array = Array.new(fields.size, 0)

  # For each non-NULL variable-length field, the record header contains
  # the length in one or two bytes.
  fields.each do |f|
    next if f.fixed_len > 0 or null_bitmap[f.position]

    len = cursor.get_uint8

    # Two bytes are used only if the length exceeds 127 bytes and the
    # maximum length exceeds 255 bytes.
    if len > 127 and f.variable_len > 255
      len = ((len & 0x3f) << 8) + cursor.get_uint8
    end

    len_array[f.position] = len
  end

  return len_array
end

#recordsObject

A helper function to return the number of records.



155
156
157
# File 'lib/innodb/page/index.rb', line 155

def records
  page_header && page_header[:n_recs]
end

#root?Boolean

A helper function to identify root index pages; they must be the only pages at their level.

Returns:

  • (Boolean)


161
162
163
# File 'lib/innodb/page/index.rb', line 161

def root?
  self.prev.nil? && self.next.nil?
end

#size_fseg_headerObject

The size of the “fseg” header.



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

def size_fseg_header
  2 * Innodb::FsegEntry::SIZE
end

#size_index_headerObject

The size of the “index” header.



21
22
23
# File 'lib/innodb/page/index.rb', line 21

def size_index_header
  36
end

#size_mum_recordObject

The size of the data from the supremum or infimum records.



43
44
45
# File 'lib/innodb/page/index.rb', line 43

def size_mum_record
  8
end

#size_record_headerObject

Return the size of the header for each record.



192
193
194
195
196
197
198
199
# File 'lib/innodb/page/index.rb', line 192

def size_record_header
  case page_header[:format]
  when :compact
    RECORD_BITS_SIZE + RECORD_NEXT_SIZE
  when :redundant
    RECORD_BITS_SIZE + RECORD_NEXT_SIZE + 1
  end
end

#size_record_undefinedObject

Return the size of a field in the record header for which no description could be found (but must be skipped anyway).



203
204
205
206
207
208
209
210
# File 'lib/innodb/page/index.rb', line 203

def size_record_undefined
  case page_header[:format]
  when :compact
    0
  when :redundant
    1
  end
end

#supremumObject

Return the supremum record on a page.



323
324
325
# File 'lib/innodb/page/index.rb', line 323

def supremum
  @supremum ||= system_record(pos_supremum)
end

#system_record(offset) ⇒ Object

Parse and return simple fixed-format system records, such as InnoDB’s internal infimum and supremum records.



307
308
309
310
311
312
313
314
315
# File 'lib/innodb/page/index.rb', line 307

def system_record(offset)
  header = record_header(offset)
  {
    :offset => offset,
    :header => header,
    :next => offset + header[:next],
    :data => cursor(offset).get_bytes(size_mum_record),
  }
end

#trailer_spaceObject

The amount of space consumed by the trailers in the page.



93
94
95
# File 'lib/innodb/page/index.rb', line 93

def trailer_space
  size_fil_trailer
end

#used_spaceObject

Return the amount of used space in the page.



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

def used_space
  size - free_space
end