Class: Innodb::Index::IndexCursor

Inherits:
Object
  • Object
show all
Defined in:
lib/innodb/index.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(index, record, direction) ⇒ IndexCursor

Returns a new instance of IndexCursor.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/innodb/index.rb', line 262

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

Instance Method Details

#each_recordObject

Iterate through all records in the cursor.



297
298
299
300
301
302
303
304
305
# File 'lib/innodb/index.rb', line 297

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

  while rec = record
    yield rec
  end
end

#recordObject

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



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/innodb/index.rb', line 283

def record
  if rec = @page_cursor.record
    return rec
  end

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