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.



635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/innodb/page/index.rb', line 635

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

#current_record ⇒ Object

Return the current record, mostly as a helper.



653
654
655
# File 'lib/innodb/page/index.rb', line 653

def current_record
  @record
end

#each_record ⇒ Object

Iterate through all records in the cursor.



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

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

  while rec = record
    yield rec
  end
end

#next_record ⇒ Object

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



659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/innodb/page/index.rb', line 659

def next_record
  Innodb::Stats.increment :page_record_cursor_next_record

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

  if @record == @page.supremum
    # We've reached the end of the linked list at supremum.
    nil
  else
    @record
  end
end

#prev_record ⇒ Object

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



674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# File 'lib/innodb/page/index.rb', line 674

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

#record ⇒ Object

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



696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/innodb/page/index.rb', line 696

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

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