class Innodb::Index::IndexCursor

A cursor to walk the index (cursor) forwards or backward starting with a given record, or the minimum (:min) or maximum (:max) record in the index.

Public Class Methods

new(index, record, direction) click to toggle source
# File lib/innodb/index.rb, line 245
def initialize(index, record, direction)
  Innodb::Stats.increment :index_cursor_create
  @index = index
  @direction = direction
  case record
  when :min
    # Start at the minimum record on the minimum page in the index.
    @page = index.min_page_at_level(0)
    @page_cursor = @page.record_cursor(:min, direction)
  when :max
    # Start at the maximum record on the maximum page in the index.
    @page = index.max_page_at_level(0)
    @page_cursor = @page.record_cursor(:max, direction)
  else
    # Start at the record provided.
    @page = record.page
    @page_cursor = @page.record_cursor(record.offset, direction)
  end
end

Public Instance Methods

each_record() { |rec| ... } click to toggle source

Iterate through all records in the cursor.

# File lib/innodb/index.rb, line 280
def each_record
  return enum_for(:each_record) unless block_given?

  while (rec = record)
    yield rec
  end
end
record() click to toggle source

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

# File lib/innodb/index.rb, line 266
def record
  if (rec = @page_cursor.record)
    return rec
  end

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

Private Instance Methods

move_cursor(page, record) click to toggle source

Move the cursor to a new starting position in a given page.

# File lib/innodb/index.rb, line 291
def move_cursor(page, record)
  raise "Failed to load page" unless (@page = @index.page(page))
  raise "Failed to position cursor" unless (@page_cursor = @page.record_cursor(record, @direction))
end
next_record() click to toggle source

Move to the next record in the forward direction and return it.

# File lib/innodb/index.rb, line 297
def next_record
  Innodb::Stats.increment :index_cursor_next_record

  if (rec = @page_cursor.record)
    return rec
  end

  return unless (next_page = @page.next)

  move_cursor(next_page, :min)

  @page_cursor.record
end
prev_record() click to toggle source

Move to the previous record in the backward direction and return it.

# File lib/innodb/index.rb, line 312
def prev_record
  Innodb::Stats.increment :index_cursor_prev_record

  if (rec = @page_cursor.record)
    return rec
  end

  return unless (prev_page = @page.prev)

  move_cursor(prev_page, :max)

  @page_cursor.record
end