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.



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

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.



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

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.



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

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.



705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/innodb/page/index.rb', line 705

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.



727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/innodb/page/index.rb', line 727

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

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