evio  6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BlockHeaderV2.h
Go to the documentation of this file.
1 //
2 // Copyright 2020, Jefferson Science Associates, LLC.
3 // Subject to the terms in the LICENSE file found in the top-level directory.
4 //
5 // EPSCI Group
6 // Thomas Jefferson National Accelerator Facility
7 // 12000, Jefferson Ave, Newport News, VA 23606
8 // (757)-269-7100
9 
10 
11 #ifndef EVIO_6_0_BLOCKHEADERV2_H
12 #define EVIO_6_0_BLOCKHEADERV2_H
13 
14 
15 #include <string>
16 #include <stdexcept>
17 
18 
19 #include "ByteOrder.h"
20 #include "IBlockHeader.h"
21 
22 
23 namespace evio {
24 
74  class BlockHeaderV2 : public IBlockHeader {
75 
76  public:
77 
83  static const uint32_t MAX_BLOCK_SIZE = 32768;
84 
85  private:
86 
88  uint32_t size = 0;
89 
91  uint32_t number = 1;
92 
94  uint32_t headerLength = 8;
95 
102  uint32_t start = 8;
103 
110  uint32_t end = 0;
111 
113  uint32_t version = 2;
114 
121  uint32_t reserved1 = 0;
122 
124  uint32_t magicNumber = MAGIC_NUMBER;
125 
128 
133  uint64_t bufferStartingPosition = 0L;
134 
135  public:
136 
138  BlockHeaderV2() = default;
139 
140 
156  BlockHeaderV2(uint32_t sz, uint32_t num) {
157  size = sz;
158  number = num;
159  end = size;
160  }
161 
162 
168  explicit BlockHeaderV2(std::shared_ptr<BlockHeaderV2> & blkHeader) {
169  copy(blkHeader);
170  }
171 
172 
177  void copy(std::shared_ptr<BlockHeaderV2> & blkHeader) {
178  size = blkHeader->size;
179  number = blkHeader->number;
180  headerLength = blkHeader->headerLength;
181  version = blkHeader->version;
182  end = blkHeader->end;
183  start = blkHeader->start;
184  reserved1 = blkHeader->reserved1;
185  byteOrder = blkHeader->byteOrder;
186  magicNumber = blkHeader->magicNumber;
187  bufferStartingPosition = blkHeader->bufferStartingPosition;
188  }
189 
190 
192  bool hasDictionary() override {return false;}
193 
194 
196  bool isLastBlock() override {return false;}
197 
198 
200  bool isCompressed() override {return false;}
201 
202 
205 
206 
208  uint32_t getSize() override {return size;}
209 
210 
216  void setSize(uint32_t sz) {
217  if ((sz < 8) || (sz > MAX_BLOCK_SIZE)) {
218  throw EvioException("Bad value for size in block (physical record) header: " + std::to_string(sz));
219  }
220  size = sz;
221  }
222 
223 
234  uint32_t getStart() const {return start;}
235 
236 
249  void setStart(uint32_t strt) {
250  if ((strt < 0) || (strt > MAX_BLOCK_SIZE)) {
251  throw EvioException("Bad value for start in block (physical record) header: " + std::to_string(strt));
252  }
253  start = strt;
254  }
255 
256 
266  uint32_t getEnd() const {return end;}
267 
268 
279  void setEnd(uint32_t endd) {
280  if ((endd < 8) || (endd > MAX_BLOCK_SIZE)) {
281  throw EvioException("Bad value for end in block (physical record) header: " + std::to_string(endd));
282  }
283  end = endd;
284  }
285 
286 
288  uint32_t getNumber() override {return number;}
289 
290 
296  void setNumber(uint32_t num) {number = num;}
297 
298 
303  uint32_t getHeaderLength() const {return headerLength;}
304 
305 
307  uint32_t getHeaderWords() override {return headerLength;}
308 
309 
317  void setHeaderLength(uint32_t len) {
318  if (len != 8) {
319  throw EvioException("Bad Block (Physical Record) Header Length: " + std::to_string(len));
320  }
321  headerLength = len;
322  }
323 
324 
326  bool hasFirstEvent() override {return false;}
327 
328 
330  uint32_t getEventType() override {return 0;}
331 
332 
334  uint32_t getVersion() override {return version;}
335 
336 
341  void setVersion(uint32_t ver) {version = ver;}
342 
343 
345  uint32_t getSourceId() override {return reserved1;}
346 
347 
352  uint32_t getReserved1() const {return reserved1;}
353 
354 
359  void setReserved1(uint32_t r1) {reserved1 = r1;}
360 
361 
363  uint32_t getMagicNumber() override {return magicNumber;}
364 
365 
376  void setMagicNumber(uint32_t magicNum) {
377  if (magicNum != MAGIC_NUMBER) {
378  throw EvioException("Value for magicNumber, " + std::to_string(magicNum) +
379  " does not match MAGIC_NUMBER 0xc0da0100");
380  }
381  magicNumber = MAGIC_NUMBER;
382  }
383 
384 
386  ByteOrder & getByteOrder() override {return byteOrder;}
387 
388 
393  void setByteOrder(ByteOrder & order) {byteOrder = order;}
394 
395 
397  std::string toString() override {
398  std::stringstream ss;
399 
400  ss << "block size: " << size << std::endl;
401  ss << "number: " << number << std::endl;
402  ss << "headerLen: " << headerLength << std::endl;
403  ss << "start: " << start << std::endl;
404  ss << "end: " << end << std::endl;
405  ss << "version: " << version << std::endl;
406  ss << "reserved1: " << reserved1 << std::endl;
407  ss << "magicNumber: " << magicNumber << std::endl;
408  ss << " *buffer start: " << getBufferStartingPosition() << std::endl;
409  ss << " *next start: " << nextBufferStartingPosition() << std::endl;
410 
411  return ss.str();
412  }
413 
414 
416  size_t getBufferEndingPosition() override {return bufferStartingPosition + 4*end;}
417 
418 
420  size_t getBufferStartingPosition() override {return bufferStartingPosition;}
421 
422 
424  void setBufferStartingPosition(size_t pos) override {bufferStartingPosition = pos;}
425 
426 
428  size_t nextBufferStartingPosition() override {return bufferStartingPosition + 4*size;}
429 
430 
432  size_t firstEventStartingPosition() override {
433  if (start == 0) {
434  return 0UL;
435  }
436  return bufferStartingPosition + 4*start;
437  }
438 
439 
441  size_t bytesRemaining(size_t position) override {
442  if (position < bufferStartingPosition) {
443  throw EvioException("Provided position is less than buffer starting position.");
444  }
445 
446  size_t nextBufferStart = nextBufferStartingPosition();
447  if (position > nextBufferStart) {
448  throw EvioException("Provided position beyond buffer end position.");
449  }
450 
451  return nextBufferStart - position;
452  }
453 
454 
456  size_t write(ByteBuffer & byteBuffer) override {
457 
458  if (byteBuffer.remaining() < 32) {
459  throw std::overflow_error("not enough room in buffer to write");
460  }
461  byteBuffer.putInt(size);
462  byteBuffer.putInt(number);
463  byteBuffer.putInt(headerLength); // should always be 8
464  byteBuffer.putInt(start);
465  byteBuffer.putInt(end);
466  byteBuffer.putInt(version);
467  byteBuffer.putInt(reserved1);
468  byteBuffer.putInt(magicNumber);
469  return 32;
470  }
471  };
472 
473 
474 }
475 
476 #endif //EVIO_6_0_BLOCKHEADERV2_H
void setVersion(uint32_t ver)
Sets the evio version.
Definition: BlockHeaderV2.h:341
void setReserved1(uint32_t r1)
Sets the value of reserved1.
Definition: BlockHeaderV2.h:359
This class is copied from one of the same name in the Java programming language.
Definition: ByteBuffer.h:42
void setByteOrder(ByteOrder &order)
Sets the byte order of data being read.
Definition: BlockHeaderV2.h:393
uint32_t getNumber() override
Get the block number for this block (record).In a file, this is usually sequential, starting at 1. the block number for this block (record).
Definition: BlockHeaderV2.h:288
void setEnd(uint32_t endd)
Set the ending position of the block (physical record.) This is the number of valid words (header + d...
Definition: BlockHeaderV2.h:279
uint32_t getHeaderWords() override
Get the block (record) header length, in 32 bit words.block (record) header length, in 32 bit words.
Definition: BlockHeaderV2.h:307
bool hasDictionary() override
Does this block contain an evio dictionary?true if this block contains an evio dictionary, else false. Always returns false for versions 1-3 (not implemented).
Definition: BlockHeaderV2.h:192
std::string toString() override
Get the string representation of the block (record) header.string representation of the block (record...
Definition: BlockHeaderV2.h:397
bool isLastBlock() override
Is this the last block in the file or being sent over the network?true if this is the last block in t...
Definition: BlockHeaderV2.h:196
void copy(std::shared_ptr< BlockHeaderV2 > &blkHeader)
This method copies another header&#39;s contents.
Definition: BlockHeaderV2.h:177
void setBufferStartingPosition(size_t pos) override
Set the starting position in the buffer (bytes) from which this header was read–if that happened...
Definition: BlockHeaderV2.h:424
Make a common interface for different versions of the BlockHeader arising from different evio version...
Definition: IBlockHeader.h:37
size_t remaining() const
Returns the number of bytes from the current position to the end of the data.
Definition: ByteBuffer.cpp:497
size_t write(ByteBuffer &byteBuffer) override
Write myself out into a byte buffer.This write is relative–i.e., it uses the current position of the ...
Definition: BlockHeaderV2.h:456
bool hasFirstEvent() override
Does this block/record contain the &quot;first event&quot; (first event to be written to each file split)...
Definition: BlockHeaderV2.h:326
CompressionType
Enum of supported data compression types.
Definition: Compressor.h:65
Numerical values associated with endian byte order.
Definition: ByteOrder.h:53
BlockHeaderV2()=default
Constructor initializes all fields to default values.
ByteBuffer & putInt(uint32_t val)
Relative put method for writing an int value.
Definition: ByteBuffer.cpp:1570
void setHeaderLength(uint32_t len)
Set the block header length, in ints.
Definition: BlockHeaderV2.h:317
void setSize(uint32_t sz)
Set the size of the block (physical record).
Definition: BlockHeaderV2.h:216
Exception class for Evio software package.
Definition: EvioException.h:29
Definition: Compressor.h:66
static const uint32_t MAX_BLOCK_SIZE
The maximum block size in 32 bit ints in this implementation of evio.
Definition: BlockHeaderV2.h:83
size_t firstEventStartingPosition() override
Determines where the start of the first event in this block (record) is located (bytes).This assumes the start position has been maintained by the object performing the buffer read.where the start of the first event in this block (record) is located (bytes). In evio format version 2, returns 0 if start is 0, signaling that this entire record is part of a logical record that spans at least three physical records.
Definition: BlockHeaderV2.h:432
size_t nextBufferStartingPosition() override
Determines where the start of the next block (record) header in some buffer is located (bytes)...
Definition: BlockHeaderV2.h:428
uint32_t getVersion() override
Get the evio version of the block (record) header.evio version of the block (record) header...
Definition: BlockHeaderV2.h:334
void setMagicNumber(uint32_t magicNum)
Sets the value of magicNumber.
Definition: BlockHeaderV2.h:376
size_t getBufferStartingPosition() override
Get the starting position in the buffer (bytes) from which this header was read–if that happened...
Definition: BlockHeaderV2.h:420
void setNumber(uint32_t num)
Set the block number for this block (physical record).
Definition: BlockHeaderV2.h:296
uint32_t getSize() override
Get the size of the block (record) in 32 bit words.size of the block (record) in 32 bit words...
Definition: BlockHeaderV2.h:208
size_t bytesRemaining(size_t position) override
Gives the bytes remaining in this block (record) given a buffer position.The position is an absolute ...
Definition: BlockHeaderV2.h:441
BlockHeaderV2(uint32_t sz, uint32_t num)
Creates a BlockHeader for evio versions 1-3 format.
Definition: BlockHeaderV2.h:156
uint32_t getStart() const
Get the starting position of the block (physical record.).
Definition: BlockHeaderV2.h:234
void setStart(uint32_t strt)
Set the starting position of the block (physical record.).
Definition: BlockHeaderV2.h:249
size_t getBufferEndingPosition() override
Get the position in the buffer (bytes) of this block&#39;s last data word. position in the buffer (bytes...
Definition: BlockHeaderV2.h:416
uint32_t getEnd() const
Get the ending position of the block (physical record.) This is the number of valid words (header + d...
Definition: BlockHeaderV2.h:266
uint32_t getEventType() override
Get the type of events in block/record (see values of DataType.This is not supported by versions 1-3 ...
Definition: BlockHeaderV2.h:330
ByteOrder & getByteOrder() override
Get the byte order of the data being read.byte order of the data being read.
Definition: BlockHeaderV2.h:386
BlockHeaderV2(std::shared_ptr< BlockHeaderV2 > &blkHeader)
This copy constructor creates an evio version 1-3 BlockHeader from another object of this class...
Definition: BlockHeaderV2.h:168
uint32_t getMagicNumber() override
Get the magic number the block (record) header which should be 0xc0da0100.magic number in the block (...
Definition: BlockHeaderV2.h:363
Compressor::CompressionType getCompressionType() override
Get the type of data compression used.type of data compression used.
Definition: BlockHeaderV2.h:204
uint32_t getSourceId() override
Get the source ID number if in CODA online context and data is coming from ROC.source ID number if in...
Definition: BlockHeaderV2.h:345
uint32_t getReserved1() const
Get the first reserved word in the block (physical record) header.
Definition: BlockHeaderV2.h:352
This holds an evio block header, also known as a physical record header.
Definition: BlockHeaderV2.h:74
static const ByteOrder ENDIAN_LOCAL
Local host&#39;s byte order.
Definition: ByteOrder.h:61
uint32_t getHeaderLength() const
Get the block header length in ints.
Definition: BlockHeaderV2.h:303
static const uint32_t MAGIC_NUMBER
The magic number, should be the value of magicNumber.
Definition: IBlockHeader.h:42
bool isCompressed() override
Is this the data in this block compressed?true if the data in this block is compressed, else false.
Definition: BlockHeaderV2.h:200