Method: PEROBS::SpaceManager#check

Defined in:
lib/perobs/SpaceManager.rb

#check(flat_file = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/perobs/SpaceManager.rb', line 163

def check(flat_file = nil)
  sync
  return false unless @index.check
  return false unless @list.check

  smallest_space = nil
  largest_space = nil
  total_space_bytes = 0
  space_distribution = ::Hash.new(0)

  @index.each do |length, list_entry_addr|
    if list_entry_addr <= 0
      PEROBS.log.error "list_entry_addr (#{list_entry_addr}) " +
        "must be positive"
      return false
    end

    # Detect smallest and largest space
    if smallest_space.nil? || length < smallest_space
      smallest_space = length
    end
    if largest_space.nil? || length > largest_space
      largest_space = length
    end

    known_addresses = [ list_entry_addr ]
    entries = 0
    while list_entry_addr > 0
      entries += 1
      unless (blob = @list.retrieve_blob(list_entry_addr))
        PEROBS.log.error "SpaceManager points to non-existing " +
          "space list entry at address #{list_entry_addr}"
        return false
      end
      space_address, next_entry_addr = blob.unpack('QQ')

      if known_addresses.include?(next_entry_addr)
        PEROBS.log.error "Space list is cyclic: "
          "#{known_addresses + next_entry_addr}"
        return false
      end
      if flat_file &&
          !flat_file.has_space?(space_address, length)
        PEROBS.log.error "SpaceManager has space at offset " +
          "#{space_address} of size #{length} that isn't " +
          "available in the FlatFile."
        return false
      end
      list_entry_addr = next_entry_addr
    end

    total_space_bytes += length * entries
    space_distribution[msb(length)] += entries
  end

  PEROBS.log.info "SpaceManager stats: smallest: #{smallest_space}; " +
    "largest: #{largest_space}; total bytes: #{total_space_bytes}; " +
    "distribution: " +
    "#{space_distribution.map { |l, c| "#{2 ** (l - 1)}-#{2 ** l - 1}:#{c}; " }}"

  true
end