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.



644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/innodb/page/index.rb', line 644

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.



717
718
719
720
721
722
723
724
725
# File 'lib/innodb/page/index.rb', line 717

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.



663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'lib/innodb/page/index.rb', line 663

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.



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/innodb/page/index.rb', line 680

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.



702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/innodb/page/index.rb', line 702

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

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