class PEROBS::IDListPageRecord

The IDListPageRecord class models the elements of the IDList. Each page holds up to a certain number of IDs that can be cached into a file if needed. Each page holds IDs within a given interval. The cache is managed by the IDListPageFile object.

Attributes

max_id[R]
min_id[R]
page_entries[RW]
page_idx[R]

Public Class Methods

new(page_file, min_id, max_id, values = []) click to toggle source

Create a new IDListPageRecord object. @param page_file [IDListPageFile] The page file that manages the cache. @param min_id [Integer] The smallest ID that can be stored in this page @param max_id [Integer] the largest ID that can be stored in this page @param values [Array] An array of IDs to be stored in this page

# File lib/perobs/IDListPageRecord.rb, line 44
def initialize(page_file, min_id, max_id, values = [])
  @page_file = page_file
  @min_id = min_id
  @max_id = max_id
  @page_entries = 0
  @page_idx = @page_file.new_page(self, values)
end

Public Instance Methods

<=>(pr) click to toggle source
# File lib/perobs/IDListPageRecord.rb, line 102
def <=>(pr)
  @min_id <=> pr.min_id
end
check() click to toggle source
# File lib/perobs/IDListPageRecord.rb, line 106
def check
  unless @min_id < @max_id
    raise RuntimeError, "min_id must be smaller than max_id"
  end

  p = page
  values = p.values
  unless @page_entries == values.length
    raise RuntimeError, "Mismatch between node page_entries " +
      "(#{@page_entries}) and number of values (#{p.values.length})"
  end

  values.each do |v|
    if v < @min_id
      raise RuntimeError, "Page value #{v} is smaller than min_id " +
        "#{@min_id}"
    end
    if v > @max_id
      raise RuntimeError, "Page value #{v} is larger than max_id #{@max_id}"
    end
  end

  p.check
end
include?(id) click to toggle source

Check if the given ID is included in this page. @param id [Integer] @return [True of False] Return true if found, false otherwise.

# File lib/perobs/IDListPageRecord.rb, line 55
def include?(id)
  return false if id < @min_id || @max_id < id

  page.include?(id)
end
insert(id) click to toggle source

Insert an ID into the page. @param id [Integer] The ID to store

# File lib/perobs/IDListPageRecord.rb, line 69
def insert(id)
  unless @min_id <= id && id <= @max_id
    raise ArgumentError, "IDs for this page must be between #{@min_id} " +
      "and #{@max_id}. #{id} is outside this range."
  end

  page.insert(id)
end
is_full?() click to toggle source

Check if the page is full and can’t store any more IDs. @return [True or False]

# File lib/perobs/IDListPageRecord.rb, line 63
def is_full?
  page.is_full?
end
split() click to toggle source

Split the current page. This split is done by splitting the ID range in half. This page will keep the first half, the newly created page will get the second half. This may not actually yield an empty page as all values could remain with one of the pages. In this case further splits need to be issued by the caller. @return [IDListPageRecord] A new IDListPageRecord object.

# File lib/perobs/IDListPageRecord.rb, line 84
def split
  # Determine the new max_id for the old page.
  max_id = @min_id + (@max_id - @min_id) / 2
  # Create a new page that stores the upper half of the ID range. Remove
  # all IDs from this page that now belong into the new page and transfer
  # them.
  new_page_record = IDListPageRecord.new(@page_file, max_id + 1, @max_id,
                                         page.delete(max_id))
  # Adjust the max_id of the current page.
  @max_id = max_id

  new_page_record
end
values() click to toggle source
# File lib/perobs/IDListPageRecord.rb, line 98
def values
  page.values
end

Private Instance Methods

page() click to toggle source
# File lib/perobs/IDListPageRecord.rb, line 133
def page
  # The leaf pages reference the IDListPage objects only by their index.
  # This method will convert the index into a reference to the actual
  # object. These references should be very short-lived as a life
  # reference prevents the page object from being collected.
  @page_file.page(self)
end