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.



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/innodb/page/index.rb', line 649

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

  @initial = true
  @page = page
  @direction = direction
  case offset
  when :min
    @record = @page.min_record
  when :max
    @record = @page.max_record
  else
    # Offset is a byte offset of a record (hopefully).
    @record = @page.record(offset)
  end
end

Instance Method Details

#each_recordObject

Iterate through all records in the cursor.



722
723
724
725
726
727
728
729
730
# File 'lib/innodb/page/index.rb', line 722

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

  while rec = record
    yield rec
  end
end

#next_recordObject

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



668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'lib/innodb/page/index.rb', line 668

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.



685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/innodb/page/index.rb', line 685

def prev_record
  Innodb::Stats.increment :page_record_cursor_prev_record

  unless slot = @page.directory_slot_for_record(@record)
    raise "Couldn't find slot for record"
  end

  unless search_cursor = @page.record_cursor(@page.directory[slot-1])
    raise "Couldn't position search cursor"
  end

  while rec = search_cursor.record and rec.offset != @record.offset
    if rec.next == @record.offset
      if rec == @page.infimum
        return nil
      end
      return @record = rec
    end
  end
end

#recordObject

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



707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/innodb/page/index.rb', line 707

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

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