Class: PEROBS::FlatFileBlobHeader

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

Overview

The FlatFile blob header has the following structure:

1 Byte: Mark byte.

Bit 0: 0 deleted entry, 1 valid entry
Bit 1: 0 unmarked, 1 marked
Bit 2: 0 uncompressed data, 1 compressed data
Bit 3 - 7: reserved, must be 0

8 bytes: Length of the data blob in bytes 8 bytes: ID of the value in the data blob 4 bytes: CRC32 checksum of the data blob

If the bit 0 of the mark byte is 0, only the length is valid. The blob is empty. Only of bit 0 is set then entry is valid.

Constant Summary collapse

FORMAT =

The ‘pack()’ format of the header.

'CQQL'
LENGTH =

The length of the header in bytes.

21

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mark, length, id, crc) ⇒ FlatFileBlobHeader

Create a new FlatFileBlobHeader with the given mark, length, id and crc.

Parameters:

  • mark (Fixnum)

    8 bit number, see above

  • length (Fixnum)

    length of the header in bytes

  • id (Integer)

    ID of the blob entry

  • crc (Fixnum)

    CRC32 checksum of the blob entry



59
60
61
62
63
64
# File 'lib/perobs/FlatFileBlobHeader.rb', line 59

def initialize(mark, length, id, crc)
  @mark = mark
  @length = length
  @id = id
  @crc = crc
end

Instance Attribute Details

#crcObject (readonly)

Returns the value of attribute crc.



52
53
54
# File 'lib/perobs/FlatFileBlobHeader.rb', line 52

def crc
  @crc
end

#idObject (readonly)

Returns the value of attribute id.



52
53
54
# File 'lib/perobs/FlatFileBlobHeader.rb', line 52

def id
  @id
end

#lengthObject (readonly)

Returns the value of attribute length.



52
53
54
# File 'lib/perobs/FlatFileBlobHeader.rb', line 52

def length
  @length
end

#markObject (readonly)

Returns the value of attribute mark.



52
53
54
# File 'lib/perobs/FlatFileBlobHeader.rb', line 52

def mark
  @mark
end

Class Method Details

.read(file) ⇒ Object

Read the header from the given File.

Parameters:

  • file (File)

Returns:

  • FlatFileBlobHeader



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/perobs/FlatFileBlobHeader.rb', line 69

def FlatFileBlobHeader::read(file)
  begin
    buf = file.read(LENGTH)
  rescue IOError => e
    PEROBS.log.fatal "Cannot read blob header in flat file DB: #{e.message}"
  end

  return nil unless buf

  FlatFileBlobHeader.new(*buf.unpack(FORMAT))
end

.read_at(file, addr, id = nil) ⇒ Object

Read the header from the given File.

Parameters:

  • file (File)
  • addr (Integer)

    address in the file to start reading

  • id (Integer) (defaults to: nil)

    Optional ID that the header should have

Returns:

  • FlatFileBlobHeader



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/perobs/FlatFileBlobHeader.rb', line 86

def FlatFileBlobHeader::read_at(file, addr, id = nil)
  buf = nil
  begin
    file.seek(addr)
    buf = file.read(LENGTH)
  rescue IOError => e
    PEROBS.log.fatal "Cannot read blob in flat file DB: #{e.message}"
  end
  if buf.nil? || buf.length != LENGTH
    PEROBS.log.fatal "Cannot read blob header " +
      "#{id ? "for ID #{id} " : ''}at address " +
      "#{addr}"
  end
  header = FlatFileBlobHeader.new(*buf.unpack(FORMAT))
  if id && header.id != id
    PEROBS.log.fatal "Mismatch between FlatFile index and blob file " +
      "found for entry with ID #{id}/#{header.id}"
  end

  return header
end

Instance Method Details

#is_compressed?Boolean

Return true if the blob contains compressed data.

Returns:

  • (Boolean)


130
131
132
# File 'lib/perobs/FlatFileBlobHeader.rb', line 130

def is_compressed?
  bit_set?(2)
end

#is_marked?Boolean

Return true if the blob has been marked.

Returns:

  • (Boolean)


125
126
127
# File 'lib/perobs/FlatFileBlobHeader.rb', line 125

def is_marked?
  bit_set?(1)
end

#is_valid?Boolean

Return true if the header is for a non-empty blob.

Returns:

  • (Boolean)


120
121
122
# File 'lib/perobs/FlatFileBlobHeader.rb', line 120

def is_valid?
  bit_set?(0)
end

#write(file) ⇒ Object

Write the header to a given File.

Parameters:

  • file (File)


110
111
112
113
114
115
116
117
# File 'lib/perobs/FlatFileBlobHeader.rb', line 110

def write(file)
  begin
    file.write([ @mark, @length, @id, @crc].pack(FORMAT))
  rescue IOError => e
    PEROBS.log.fatal "Cannot write blob header into flat file DB: " +
      e.message
  end
end