Class: Innodb::Page::Index::RecordCursor

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

Overview

A class for cursoring through records starting from an arbitrary point.

Instance Method Summary collapse

Constructor Details

#initialize(page, offset, direction) ⇒ RecordCursor

Returns a new instance of RecordCursor.



715
716
717
718
719
720
721
722
# File 'lib/innodb/page/index.rb', line 715

def initialize(page, offset, direction)
  Innodb::Stats.increment :page_record_cursor_create

  @initial = true
  @page = page
  @direction = direction
  @record = initial_record(offset)
end

Instance Method Details

#each_recordObject

Iterate through all records in the cursor.



789
790
791
792
793
794
795
# File 'lib/innodb/page/index.rb', line 789

def each_record
  return enum_for(:each_record) unless block_given?

  while (rec = record)
    yield rec
  end
end

#initial_record(offset) ⇒ Object



724
725
726
727
728
729
730
731
732
733
734
# File 'lib/innodb/page/index.rb', line 724

def initial_record(offset)
  case offset
  when :min
    @page.min_record
  when :max
    @page.max_record
  else
    # Offset is a byte offset of a record (hopefully).
    @page.record(offset)
  end
end

#next_recordObject

Return the next record, and advance the cursor. Return nil when the end of records (supremum) is reached.



738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/innodb/page/index.rb', line 738

def next_record
  Innodb::Stats.increment :page_record_cursor_next_record

  rec = @page.record(@record.next)

  # The garbage record list's end is self-linked, so we must check for
  # both supremum and the current record's offset.
  if rec == @page.supremum || rec.offset == @record.offset
    # We've reached the end of the linked list at supremum.
    nil
  else
    @record = rec
  end
end

#prev_recordObject

Return the previous record, and advance the cursor. Return nil when the end of records (infimum) is reached.



755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'lib/innodb/page/index.rb', line 755

def prev_record
  Innodb::Stats.increment :page_record_cursor_prev_record

  slot = @page.directory_slot_for_record(@record)
  raise "Could not find slot for record" unless slot

  search_cursor = @page.record_cursor(@page.directory[slot - 1])
  raise "Could not position search cursor" unless search_cursor

  while (rec = search_cursor.record) && rec.offset != @record.offset
    next unless rec.next == @record.offset

    return if rec == @page.infimum

    return @record = rec
  end
end

#recordObject

Return the next record in the order defined when the cursor was created.



774
775
776
777
778
779
780
781
782
783
784
785
786
# File 'lib/innodb/page/index.rb', line 774

def record
  if @initial
    @initial = false
    return @record
  end

  case @direction
  when :forward
    next_record
  when :backward
    prev_record
  end
end