Module: Pocolog::Format::V2
- Defined in:
- lib/pocolog/format/v2.rb
Defined Under Namespace
Classes: IndexStreamInfo
Constant Summary
- MAGIC =
The magic code present at the beginning of each pocolog file
'POCOSIM'
- VERSION =
Format version ID. Increment this when the file format changes in a non-backward-compatible way
2
- PROLOGUE_SIZE =
The size of the file's prologue
MAGIC.size + 9
- INDEX_MAGIC =
The magic code at the beginning of a pocolog index
'POCOSIM_INDEX'
- INDEX_VERSION =
The current index version. Unlike with the format version, a changing index version will only cause rebuilding the index
(i.e. this can change without changing the overall format version)
3
- INDEX_PROLOGUE_SIZE =
Size of the index prologue
INDEX_MAGIC.size + 20
- INDEX_STREAM_DESCRIPTION_SIZE =
Size of a stream description in the index
8 * 8
- INDEX_STREAM_ENTRY_SIZE =
Size of an entry in the index table
8 * 3
- BLOCK_HEADER_SIZE =
The size of the generic block header
8
- TIME_SIZE =
The size of a time in a block header
8
- DATA_BLOCK_HEADER_SIZE =
The size of a data header, excluding the generic block header
TIME_SIZE * 2 + 5
- STREAM_BLOCK_DECLARATION_HEADER_SIZE_MIN =
The size of a stream block declaration header
Stream declarations contain variable-length strings, we can only have a min
9
Class Method Summary collapse
-
.index_contents_from_stream(stream_info, index_data_pos) ⇒ Object
private
Helper method that prepares index contents for a given stream.
-
.index_file_valid?(index_path, file_path) ⇒ Boolean
Tests whether the index whose path is given is valid for the given log file.
-
.read_index(index_io, expected_file_size: nil, expected_mtime: nil) ⇒ Array<StreamInfo>
Read the information contained in a file index.
-
.read_index_prologue(index_io, validate_version: true, expected_mtime: nil, expected_file_size: nil) ⇒ Integer
Read the prologue of an index file.
-
.read_index_stream_info(index_io, expected_file_size: nil, expected_mtime: nil) ⇒ Array<IndexStreamInfo>
Read basic stream information from an index file.
-
.read_minimal_stream_info(index_io, file_io) ⇒ Array<(BlockStream::StreamBlock,IndexStreamInfo)>
Read the stream information, but not the actual block index, from an index file.
-
.read_prologue(io, validate_version: true) ⇒ (Integer,Boolean)
Read a file's prologue.
-
.rebuild_index_file(io, index_path) ⇒ Array<StreamInfo,nil>
Rebuild a pocolog file's index and saves it to file.
- .valid_file?(file) ⇒ Boolean
-
.validate_version(version) ⇒ Object
Verify that the given version is compatible with this format version.
-
.write_index(index_io, file_io, streams, version: INDEX_VERSION) ⇒ Object
Write an index file for a given file.
-
.write_index_prologue(index_io, size, mtime, version: INDEX_VERSION) ⇒ Object
Write a prologue on an index file.
-
.write_prologue(io, big_endian = Pocolog.big_endian?) ⇒ Object
Write a v2 file prologue.
Instance Method Summary collapse
-
#declaration_pos ⇒ Integer
The position in the pocolog file of the stream declaration block.
-
#index_pos ⇒ Integer
The position in the index file of the stream index data.
-
#stream_size ⇒ Integer
The number of samples in the stream.
Class Method Details
.index_contents_from_stream(stream_info, index_data_pos) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Helper method that prepares index contents for a given stream
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/pocolog/format/v2.rb', line 359 def self.index_contents_from_stream(stream_info, index_data_pos) interval_rt = stream_info.interval_rt.dup interval_lg = stream_info.interval_lg.dup base_time = stream_info.index.base_time index_stream_info = [ stream_info.declaration_blocks.first, index_data_pos, base_time || 0, stream_info.size, interval_rt[0] || 0, interval_rt[1] || 0, interval_lg[0] || 0, interval_lg[1] || 0 ] [index_stream_info, stream_info.index.index_map.flatten] end |
.index_file_valid?(index_path, file_path) ⇒ Boolean
Tests whether the index whose path is given is valid for the given log file
209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/pocolog/format/v2.rb', line 209 def self.index_file_valid?(index_path, file_path) stat = file_path.stat begin File.open(index_path) do |index_io| read_index_stream_info(index_io, expected_file_size: stat.size) end true rescue Errno::ENOENT false end rescue InvalidIndex false end |
.read_index(index_io, expected_file_size: nil, expected_mtime: nil) ⇒ Array<StreamInfo>
Read the information contained in a file index
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/pocolog/format/v2.rb', line 168 def self.read_index(index_io, expected_file_size: nil, expected_mtime: nil) minimal_stream_info = read_index_stream_info( index_io, expected_file_size: expected_file_size, expected_mtime: expected_mtime ) minimal_stream_info.map do |info| index_size = info.stream_size * INDEX_STREAM_ENTRY_SIZE index_io.seek(info.index_pos) index_data = index_io.read(index_size) if index_data.size != index_size raise InvalidIndex, 'not enough or too much data in index' end index_data = index_data.unpack('Q>*') .each_slice(3).to_a StreamInfo.from_raw_data( info.declaration_pos, info.interval_rt, info.base_time, index_data ) end end |
.read_index_prologue(index_io, validate_version: true, expected_mtime: nil, expected_file_size: nil) ⇒ Integer
Read the prologue of an index file
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/pocolog/format/v2.rb', line 104 def self.read_index_prologue( index_io, validate_version: true, expected_mtime: nil, expected_file_size: nil ) if index_io.size < INDEX_PROLOGUE_SIZE raise InvalidIndex, 'index file too small to contain a valid index' end header = index_io.read(INDEX_MAGIC.size + 4) magic = header[0, INDEX_MAGIC.size] if magic != INDEX_MAGIC = if magic "wrong index magic in #{index_io.path}, "\ 'probably an old index' else "#{index_io.path} is empty" end raise MissingIndexPrologue, end index_version = Integer(header[INDEX_MAGIC.size, 4].unpack('L>').first) if validate_version if index_version < INDEX_VERSION raise ObsoleteIndexVersion, "old format #{index_version}, "\ "current format is #{INDEX_VERSION}" elsif index_version > INDEX_VERSION raise InvalidIndex, "old format #{index_version}, "\ "current format is #{INDEX_VERSION}" end end index_size, index_mtime = index_io.read(16).unpack('Q>Q>') if expected_file_size && expected_file_size != index_size raise InvalidIndex, "file size in index (#{index_size}) and actual file "\ "size (#{expected_file_size}) mismatch" end if expected_mtime expected_mtime_i = StreamIndex.time_to_internal(expected_mtime, 0) if expected_mtime_i != index_mtime raise InvalidIndex, "mtime in index (#{index_mtime}) and actual mtime "\ "(#{expected_mtime_i}) mismatch" end end [index_version, index_size, StreamIndex.time_from_internal(index_mtime, 0)] end |
.read_index_stream_info(index_io, expected_file_size: nil, expected_mtime: nil) ⇒ Array<IndexStreamInfo>
Read basic stream information from an index file
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/pocolog/format/v2.rb', line 228 def self.read_index_stream_info(index_io, expected_file_size: nil, expected_mtime: nil) read_index_prologue(index_io, validate_version: true, expected_mtime: expected_mtime, expected_file_size: expected_file_size) index_size = index_io.size if index_size < INDEX_PROLOGUE_SIZE + 8 raise InvalidIndex, 'index file too small' end stream_count = index_io.read(8).unpack('Q>').first minimum_index_size = INDEX_PROLOGUE_SIZE + 8 + INDEX_STREAM_DESCRIPTION_SIZE * stream_count if index_size < minimum_index_size raise InvalidIndex, 'index file too small' end expected_file_size = [] streams = [] stream_count.times do values = index_io.read(INDEX_STREAM_DESCRIPTION_SIZE).unpack('Q>*') # This is (declaration_pos, index_pos, stream_size) declaration_pos, index_pos, base_time, stream_size, interval_rt_min, interval_rt_max, interval_lg_min, interval_lg_max = *values index_size = stream_size * INDEX_STREAM_ENTRY_SIZE expected_file_size << index_size + index_pos if stream_size == 0 base_time = nil interval_rt = [] interval_lg = [] else interval_rt = [interval_rt_min, interval_rt_max] interval_lg = [interval_lg_min, interval_lg_max] end streams << IndexStreamInfo.new( declaration_pos, index_pos, base_time, stream_size, interval_rt, interval_lg ) end expected_file_size = expected_file_size.max if index_io.size != expected_file_size raise InvalidIndex, "index file should be of size #{expected_file_size} "\ "but is of size #{index_io.size}" end streams end |
.read_minimal_stream_info(index_io, file_io) ⇒ Array<(BlockStream::StreamBlock,IndexStreamInfo)>
Read the stream information, but not the actual block index, from an index file
289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/pocolog/format/v2.rb', line 289 def self.read_minimal_stream_info(index_io, file_io) index_stream_info = read_index_stream_info( index_io, expected_file_size: file_io.size ) index_stream_info.map do |info| file_io.seek(info.declaration_pos) block_stream = BlockStream.new(file_io) block_stream.read_next_block_header stream_block = block_stream.read_stream_block [stream_block, info] end end |
.read_prologue(io, validate_version: true) ⇒ (Integer,Boolean)
Read a file's prologue
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pocolog/format/v2.rb', line 48 def self.read_prologue(io, validate_version: true) header = io.read(PROLOGUE_SIZE) || '' if !header || (header.size < PROLOGUE_SIZE) raise MissingPrologue, "#{io.path} too small" end magic = header[0, MAGIC.size] if magic != MAGIC raise MissingPrologue, "#{io.path} is not a pocolog log file. "\ "Got #{magic} at #{io.tell}, but was expecting #{MAGIC}" end format_version, big_endian = header[MAGIC.size, 9].unpack('xVV') validate_version(format_version) if validate_version [format_version, big_endian] end |
.rebuild_index_file(io, index_path) ⇒ Array<StreamInfo,nil>
Rebuild a pocolog file's index and saves it to file
310 311 312 313 314 315 316 317 318 319 |
# File 'lib/pocolog/format/v2.rb', line 310 def self.rebuild_index_file(io, index_path) block_stream = BlockStream.new(io) block_stream.read_prologue stream_info = Pocolog.file_index_builder(block_stream) FileUtils.mkdir_p(File.dirname(index_path)) File.open(index_path, 'w') do |index_io| write_index(index_io, io, stream_info) end stream_info end |
.valid_file?(file) ⇒ Boolean
84 85 86 87 88 89 90 91 |
# File 'lib/pocolog/format/v2.rb', line 84 def self.valid_file?(file) File.open(file) do |io| read_prologue(io) true end rescue InvalidFile false end |
.validate_version(version) ⇒ Object
Verify that the given version is compatible with this format version
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/pocolog/format/v2.rb', line 71 def self.validate_version(version) if version < VERSION raise ObsoleteVersion, "old format #{version}, current format "\ "is #{VERSION}. Convert it using the "\ '--to-new-format of pocolog' elsif version > VERSION raise InvalidFile, "this file is in v#{version} which is "\ "newer that the one we know #{VERSION}. Update pocolog" end end |
.write_index(index_io, file_io, streams, version: INDEX_VERSION) ⇒ Object
Write an index file for a given file
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/pocolog/format/v2.rb', line 329 def self.write_index(index_io, file_io, streams, version: INDEX_VERSION) if index_io.path == file_io.path raise ArgumentError, 'attempting to overwrite the file by its index' end write_index_prologue(index_io, file_io.stat.size, file_io.stat.mtime, version: version) index_io.write([streams.size].pack('Q>')) index_list_pos = index_io.tell index_data_pos = INDEX_STREAM_DESCRIPTION_SIZE * streams.size + index_list_pos streams.each do |stream_info| index_stream_info, index_data = index_contents_from_stream(stream_info, index_data_pos) index_io.seek(index_list_pos) index_io.write(index_stream_info.pack('Q>*')) index_io.seek(index_data_pos) index_io.write(index_data.pack('Q>*')) index_list_pos += INDEX_STREAM_DESCRIPTION_SIZE index_data_pos += index_data.size * 8 end end |
.write_index_prologue(index_io, size, mtime, version: INDEX_VERSION) ⇒ Object
Write a prologue on an index file
158 159 160 161 162 |
# File 'lib/pocolog/format/v2.rb', line 158 def self.write_index_prologue(index_io, size, mtime, version: INDEX_VERSION) index_io.write(INDEX_MAGIC) data = [version, size, StreamIndex.time_to_internal(mtime, 0)] index_io.write(data.pack('L>Q>Q>')) end |
.write_prologue(io, big_endian = Pocolog.big_endian?) ⇒ Object
Write a v2 file prologue
94 95 96 97 |
# File 'lib/pocolog/format/v2.rb', line 94 def self.write_prologue(io, big_endian = Pocolog.big_endian?) io.write(MAGIC) io.write(*[VERSION, big_endian ? 1 : 0].pack('xVV')) end |
Instance Method Details
#declaration_pos ⇒ Integer
Returns the position in the pocolog file of the stream declaration block
202 203 204 205 |
# File 'lib/pocolog/format/v2.rb', line 202 IndexStreamInfo = Struct.new( :declaration_pos, :index_pos, :base_time, :stream_size, :interval_rt, :interval_lg ) |
#index_pos ⇒ Integer
Returns the position in the index file of the stream index data
202 203 204 205 |
# File 'lib/pocolog/format/v2.rb', line 202 IndexStreamInfo = Struct.new( :declaration_pos, :index_pos, :base_time, :stream_size, :interval_rt, :interval_lg ) |
#stream_size ⇒ Integer
Returns the number of samples in the stream
202 203 204 205 |
# File 'lib/pocolog/format/v2.rb', line 202 IndexStreamInfo = Struct.new( :declaration_pos, :index_pos, :base_time, :stream_size, :interval_rt, :interval_lg ) |