Class: Resilience::Page

Inherits:
Object
  • Object
show all
Includes:
OnImage
Defined in:
lib/resilience/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OnImage

#image, image, included, restore_pos, #restore_pos, store_pos, #store_pos

Constructor Details

#initializePage

Returns a new instance of Page.



22
23
24
# File 'lib/resilience/page.rb', line 22

def initialize
  @attributes ||= []
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



17
18
19
# File 'lib/resilience/page.rb', line 17

def attributes
  @attributes
end

#contentsObject

Returns the value of attribute contents.



12
13
14
# File 'lib/resilience/page.rb', line 12

def contents
  @contents
end

#entriesObject

Returns the value of attribute entries.



20
21
22
# File 'lib/resilience/page.rb', line 20

def entries
  @entries
end

#idObject

Returns the value of attribute id.



11
12
13
# File 'lib/resilience/page.rb', line 11

def id
  @id
end

#object_idObject

Returns the value of attribute object_id.



19
20
21
# File 'lib/resilience/page.rb', line 19

def object_id
  @object_id
end

#sequenceObject

Returns the value of attribute sequence.



14
15
16
# File 'lib/resilience/page.rb', line 14

def sequence
  @sequence
end

#virtual_page_numberObject

Returns the value of attribute virtual_page_number.



15
16
17
# File 'lib/resilience/page.rb', line 15

def virtual_page_number
  @virtual_page_number
end

Class Method Details

.extract_allObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/resilience/page.rb', line 26

def self.extract_all
  page_id     = PAGES[:first]
  pages       = Pages.new

  image.seek(page_id * PAGE_SIZE)
  while contents = image.read(PAGE_SIZE)
    # only pull out metadata pages currently
    extracted_id   = id_from_contents(contents)
        = extracted_id == page_id
    pages[page_id] = Page.parse(page_id, contents) if 
    page_id       += 1
  end

  pages
end

.id_from_contents(contents) ⇒ Object



42
43
44
# File 'lib/resilience/page.rb', line 42

def self.id_from_contents(contents)
  contents.unpack('S').first
end

.parse(id, contents) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/resilience/page.rb', line 62

def self.parse(id, contents)
  store_pos

  page          = new
  page.id       = id
  page.contents = contents

  image.seek(page.offset + ADDRESSES[:page_sequence])
  page.sequence = image.read(4).unpack('L').first

  image.seek(page.offset + ADDRESSES[:virtual_page_number])
  page.virtual_page_number = image.read(4).unpack('L').first

  unless page.root? || page.object_table?
    # TODO:
    #page.parse_attributes
    #page.parse_metadata
  end

  restore_pos

  page
end

Instance Method Details

#attr_startObject



50
51
52
# File 'lib/resilience/page.rb', line 50

def attr_start
  offset + ADDRESSES[:first_attr]
end

#has_attributes?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/resilience/page.rb', line 86

def has_attributes?
  !@attributes.nil? && !@attributes.empty?
end

#object_table?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/resilience/page.rb', line 58

def object_table?
  virtual_page_number == PAGES[:object_table]
end

#offsetObject



46
47
48
# File 'lib/resilience/page.rb', line 46

def offset
  id * PAGE_SIZE
end

#parse_attributesObject



90
91
92
93
94
95
96
97
# File 'lib/resilience/page.rb', line 90

def parse_attributes
  image.seek(attr_start)
  while true
    attr = Attribute.read
    break if attr.empty?
    @attributes << attr
  end
end

#parse_metadataObject



99
100
101
102
# File 'lib/resilience/page.rb', line 99

def 
  @object_id = @attributes.first.unpack("C*")[ADDRESSES[:object_id]]
  @entries   = @attributes.first.unpack("C*")[ADDRESSES[:num_objects]]
end

#root?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/resilience/page.rb', line 54

def root?
  virtual_page_number == PAGES[:root]
end