Class: PEROBS::IDListPageRecord
- Inherits:
-
Object
- Object
- PEROBS::IDListPageRecord
- 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
-
#max_id ⇒ Object
readonly
Returns the value of attribute max_id.
-
#min_id ⇒ Object
readonly
Returns the value of attribute min_id.
-
#page_entries ⇒ Object
Returns the value of attribute page_entries.
-
#page_idx ⇒ Object
readonly
Returns the value of attribute page_idx.
Instance Method Summary collapse
- #<=>(pr) ⇒ Object
- #check ⇒ Object
-
#include?(id) ⇒ True of False
Check if the given ID is included in this page.
-
#initialize(page_file, min_id, max_id, values = []) ⇒ IDListPageRecord
constructor
Create a new IDListPageRecord object.
-
#insert(id) ⇒ Object
Insert an ID into the page.
-
#is_full? ⇒ True or False
Check if the page is full and can’t store any more IDs.
-
#split ⇒ IDListPageRecord
Split the current page.
- #values ⇒ Object
Constructor Details
#initialize(page_file, min_id, max_id, values = []) ⇒ IDListPageRecord
Create a new IDListPageRecord object.
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_id ⇒ Object (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_id ⇒ Object (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_entries ⇒ Object
Returns the value of attribute page_entries.
37 38 39 |
# File 'lib/perobs/IDListPageRecord.rb', line 37 def page_entries @page_entries end |
#page_idx ⇒ Object (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 |
#check ⇒ Object
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.
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.
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.
63 64 65 |
# File 'lib/perobs/IDListPageRecord.rb', line 63 def is_full? page.is_full? end |
#split ⇒ IDListPageRecord
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.
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 |
#values ⇒ Object
98 99 100 |
# File 'lib/perobs/IDListPageRecord.rb', line 98 def values page.values end |