Class: PEROBS::FlatFileBlobHeader
- Inherits:
-
Object
- Object
- PEROBS::FlatFileBlobHeader
- 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
-
#crc ⇒ Object
readonly
Returns the value of attribute crc.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#mark ⇒ Object
readonly
Returns the value of attribute mark.
Class Method Summary collapse
-
.read(file) ⇒ Object
Read the header from the given File.
-
.read_at(file, addr, id = nil) ⇒ Object
Read the header from the given File.
Instance Method Summary collapse
-
#initialize(mark, length, id, crc) ⇒ FlatFileBlobHeader
constructor
Create a new FlatFileBlobHeader with the given mark, length, id and crc.
-
#is_compressed? ⇒ Boolean
Return true if the blob contains compressed data.
-
#is_marked? ⇒ Boolean
Return true if the blob has been marked.
-
#is_valid? ⇒ Boolean
Return true if the header is for a non-empty blob.
-
#write(file) ⇒ Object
Write the header to a given File.
Constructor Details
#initialize(mark, length, id, crc) ⇒ FlatFileBlobHeader
Create a new FlatFileBlobHeader with the given mark, length, id and crc.
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
#crc ⇒ Object (readonly)
Returns the value of attribute crc.
52 53 54 |
# File 'lib/perobs/FlatFileBlobHeader.rb', line 52 def crc @crc end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
52 53 54 |
# File 'lib/perobs/FlatFileBlobHeader.rb', line 52 def id @id end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
52 53 54 |
# File 'lib/perobs/FlatFileBlobHeader.rb', line 52 def length @length end |
#mark ⇒ Object (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.
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.}" 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.
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.}" 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.
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.
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.
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.
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. end end |