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: Flags 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: 0 current entry, 1 outdated entry
Bit 4 - 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 flags 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
VALID_FLAG_BIT =
0
MARK_FLAG_BIT =
1
COMPRESSED_FLAG_BIT =
2
OUTDATED_FLAG_BIT =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, addr, flags, length, id, crc) ⇒ FlatFileBlobHeader

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



66
67
68
69
70
71
72
73
# File 'lib/perobs/FlatFileBlobHeader.rb', line 66

def initialize(file, addr, flags, length, id, crc)
  @file = file
  @addr = addr
  @flags = flags
  @length = length
  @id = id
  @crc = crc
end

Instance Attribute Details

#addrObject (readonly)

Returns the value of attribute addr.



57
58
59
# File 'lib/perobs/FlatFileBlobHeader.rb', line 57

def addr
  @addr
end

#crcObject (readonly)

Returns the value of attribute crc.



57
58
59
# File 'lib/perobs/FlatFileBlobHeader.rb', line 57

def crc
  @crc
end

#flagsObject (readonly)

Returns the value of attribute flags.



57
58
59
# File 'lib/perobs/FlatFileBlobHeader.rb', line 57

def flags
  @flags
end

#idObject (readonly)

Returns the value of attribute id.



57
58
59
# File 'lib/perobs/FlatFileBlobHeader.rb', line 57

def id
  @id
end

#lengthObject (readonly)

Returns the value of attribute length.



57
58
59
# File 'lib/perobs/FlatFileBlobHeader.rb', line 57

def length
  @length
end

Class Method Details

.read(file) ⇒ Object

Read the header from the given File.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/perobs/FlatFileBlobHeader.rb', line 78

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

  return nil unless buf

  if buf.length != LENGTH
    PEROBS.log.error "Incomplete FlatFileBlobHeader: Only #{buf.length} " +
      "bytes of #{LENGTH} could be read"
    return nil
  end

  FlatFileBlobHeader.new(file, addr, *buf.unpack(FORMAT))
end

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

Read the header from the given File.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/perobs/FlatFileBlobHeader.rb', line 103

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(file, addr, *buf.unpack(FORMAT))
  if id && header.id != id
    PEROBS.log.fatal "Mismatch between FlatFile index and blob file " +
      "found. FlatFile has entry with ID #{header.id} at address " +
      "#{addr}. Index has ID #{id} for this address."
  end

  return header
end

Instance Method Details

#clear_flagsObject

Reset all the flags bit to 0. This marks the blob as invalid.



141
142
143
144
# File 'lib/perobs/FlatFileBlobHeader.rb', line 141

def clear_flags
  @flags = 0
  write_flags
end

#clear_mark_flagObject

Clear the mark bit.



163
164
165
166
# File 'lib/perobs/FlatFileBlobHeader.rb', line 163

def clear_mark_flag
  clear_flag(MARK_FLAG_BIT)
  write_flags
end

#is_compressed?Boolean

Return true if the blob contains compressed data.



169
170
171
# File 'lib/perobs/FlatFileBlobHeader.rb', line 169

def is_compressed?
  bit_set?(COMPRESSED_FLAG_BIT)
end

#is_marked?Boolean

Return true if the blob has been marked.



152
153
154
# File 'lib/perobs/FlatFileBlobHeader.rb', line 152

def is_marked?
  bit_set?(MARK_FLAG_BIT)
end

#is_outdated?Boolean

Return true if the blob contains outdated data.



181
182
183
# File 'lib/perobs/FlatFileBlobHeader.rb', line 181

def is_outdated?
  bit_set?(OUTDATED_FLAG_BIT)
end

#is_valid?Boolean

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



147
148
149
# File 'lib/perobs/FlatFileBlobHeader.rb', line 147

def is_valid?
  bit_set?(VALID_FLAG_BIT)
end

#set_mark_flagObject

Set the mark bit.



157
158
159
160
# File 'lib/perobs/FlatFileBlobHeader.rb', line 157

def set_mark_flag
  set_flag(MARK_FLAG_BIT)
  write_flags
end

#set_outdated_flagObject

Set the outdated bit. The entry will be invalid as soon as the current transaction has been completed.



175
176
177
178
# File 'lib/perobs/FlatFileBlobHeader.rb', line 175

def set_outdated_flag
  set_flag(OUTDATED_FLAG_BIT)
  write_flags
end

#writeObject

Write the header to a given File.



128
129
130
131
132
133
134
135
136
# File 'lib/perobs/FlatFileBlobHeader.rb', line 128

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