Class: PEROBS::IDListPageRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/perobs/IDListPageRecord.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_file, min_id, max_id, values = []) ⇒ IDListPageRecord

Create a new IDListPageRecord object.

Parameters:

  • page_file (IDListPageFile)

    The page file that manages the cache.

  • min_id (Integer)

    The smallest ID that can be stored in this page

  • max_id (Integer)

    the largest ID that can be stored in this page

  • values (Array) (defaults to: [])

    An array of IDs to be stored in this page



44
45
46
47
48
49
50
# 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

Instance Attribute Details

#max_idObject (readonly)

Returns the value of attribute max_id.



36
37
38
# File 'lib/perobs/IDListPageRecord.rb', line 36

def max_id
  @max_id
end

#min_idObject (readonly)

Returns the value of attribute min_id.



36
37
38
# File 'lib/perobs/IDListPageRecord.rb', line 36

def min_id
  @min_id
end

#page_entriesObject

Returns the value of attribute page_entries.



37
38
39
# File 'lib/perobs/IDListPageRecord.rb', line 37

def page_entries
  @page_entries
end

#page_idxObject (readonly)

Returns the value of attribute page_idx.



36
37
38
# File 'lib/perobs/IDListPageRecord.rb', line 36

def page_idx
  @page_idx
end

Instance Method Details

#<=>(pr) ⇒ Object



102
103
104
# File 'lib/perobs/IDListPageRecord.rb', line 102

def <=>(pr)
  @min_id <=> pr.min_id
end

#checkObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ True of False

Check if the given ID is included in this page.

Parameters:

  • id (Integer)

Returns:

  • (True of False)

    Return true if found, false otherwise.



55
56
57
58
59
# 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) ⇒ Object

Insert an ID into the page.

Parameters:

  • ID (Integer)

    The ID to store



69
70
71
72
73
74
75
76
# 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?True or False

Check if the page is full and can’t store any more IDs.

Returns:

  • (True or False)


63
64
65
# File 'lib/perobs/IDListPageRecord.rb', line 63

def is_full?
  page.is_full?
end

#splitIDListPageRecord

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.

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

#valuesObject



98
99
100
# File 'lib/perobs/IDListPageRecord.rb', line 98

def values
  page.values
end