evio  6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Writer.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_WRITER_H
12 #define EVIO_6_0_WRITER_H
13 
14 
15 #include <fstream>
16 #include <iostream>
17 #include <iomanip>
18 #include <fstream>
19 #include <vector>
20 #include <string>
21 #include <future>
22 #include <chrono>
23 #include <memory>
24 
25 
26 #include "FileHeader.h"
27 #include "ByteBuffer.h"
28 #include "ByteOrder.h"
29 #include "EvioNode.h"
30 #include "RecordOutput.h"
31 #include "RecordHeader.h"
32 #include "Compressor.h"
33 #include "Util.h"
34 #include "EvioException.h"
35 
36 
37 namespace evio {
38 
39 
47  class Writer {
48 
49  private:
50 
52  bool toFile = true;
53 
54  // If writing to file ...
55 
57  std::string fileName = "";
59  std::ofstream outFile;
61  FileHeader fileHeader;
63  std::future<void> future;
65  std::shared_ptr<RecordOutput> unusedRecord = nullptr;
66 
67  // If writing to buffer ...
68 
70  std::shared_ptr<ByteBuffer> buffer;
71 
72  // For both files & buffers
73 
75  std::shared_ptr<ByteBuffer> userHeaderBuffer = nullptr;
77  uint8_t* userHeader = nullptr;
79  uint32_t userHeaderLength = 0;
83  std::string dictionary;
85  std::shared_ptr<ByteBuffer> dictionaryFirstEventBuffer;
87  uint8_t* firstEvent = nullptr;
89  uint32_t firstEventLength = 0;
90 
94  std::shared_ptr<RecordOutput> outputRecord = nullptr;
96  std::shared_ptr<RecordOutput> beingWrittenRecord = nullptr;
98  std::vector<uint8_t> headerArray;
99 
102 
105  std::shared_ptr<std::vector<uint32_t>> recordLengths;
106 
108  size_t writerBytesWritten = 0;
110  uint32_t recordNumber = 1;
111 
113  bool addingTrailer = true;
115  bool addTrailerIndex = false;
117  bool closed = false;
119  bool opened = false;
121  bool firstRecordWritten = false;
123  bool haveDictionary = false;
125  bool haveFirstEvent = false;
127  bool haveUserHeader = false;
128 
129  public:
130 
131  // Writing to file
132 
133  Writer();
134 
135  explicit Writer(const ByteOrder & order,
136  uint32_t maxEventCount = 0,
137  uint32_t maxBufferSize = 0);
138 
139  Writer(const std::string & filename,
140  const ByteOrder & order,
141  uint32_t maxEventCount = 0,
142  uint32_t maxBufferSize = 0);
143 
144  explicit Writer(const HeaderType & hType,
145  const ByteOrder & order = ByteOrder::ENDIAN_LOCAL,
146  uint32_t maxEventCount = 0,
147  uint32_t maxBufferSize = 0,
148  const std::string & dictionary = std::string(""),
149  uint8_t* firstEvent = nullptr,
150  uint32_t firstEventLength = 0,
151  const Compressor::CompressionType & compressionType = Compressor::UNCOMPRESSED,
152  bool addTrailerIndex = false);
153 
154  // Writing to buffer
155 
156  explicit Writer(std::shared_ptr<ByteBuffer> & buf);
157  Writer(std::shared_ptr<ByteBuffer> & buf, uint8_t * userHdr, uint32_t len);
158  Writer(std::shared_ptr<ByteBuffer> & buf, uint32_t maxEventCount, uint32_t maxBufferSize,
159  const std::string & dictionary, uint8_t* firstEvent, uint32_t firstEventLength);
160 
161  ~Writer() = default;
162 
163 
165 
166  private:
167 
168  // Writer & operator=(Writer&& other) noexcept;
169  // Don't allow assignment
170  Writer & operator=(const Writer& other);
171 
172  std::shared_ptr<ByteBuffer> createDictionaryRecord();
173  void writeOutput();
174  void writeOutputToBuffer();
175 
176  static void staticWriteFunction(Writer *pWriter, const char* data, size_t len);
177 
178  public:
179 
180  const ByteOrder & getByteOrder() const;
181  std::shared_ptr<ByteBuffer> getBuffer();
183 // RecordHeader & getRecordHeader();
184 // RecordOutput & getRecord();
187 
188  bool addTrailer() const;
189  void addTrailer(bool add);
190  bool addTrailerWithIndex();
191  void addTrailerWithIndex(bool addTrailingIndex);
192 
193  void open(const std::string & filename);
194  void open(const std::string & filename, uint8_t* userHdr, uint32_t len);
195  void open(std::shared_ptr<ByteBuffer> & buf, uint8_t* userHdr, uint32_t len);
196 
197  static std::shared_ptr<ByteBuffer> createRecord(const std::string & dictionary,
198  uint8_t* firstEvent, uint32_t firstEventLen,
199  const ByteOrder & byteOrder,
200  FileHeader* fileHeader,
201  RecordHeader* recordHeader);
202 
203  std::shared_ptr<ByteBuffer> createHeader(uint8_t* userHdr, uint32_t userLen);
204  std::shared_ptr<ByteBuffer> createHeader(ByteBuffer & userHdr);
205  void createHeader(ByteBuffer & buf, uint8_t* userHdr, uint32_t userLen);
206  void createHeader(ByteBuffer & buf, ByteBuffer & userHdr);
207 
208  void writeRecord(RecordOutput & record);
209 
210  // Use internal RecordOutput to write individual events
211 
212  void addEvent(uint8_t* buffer, uint32_t length);
213  void addEvent(std::shared_ptr<ByteBuffer> & buffer);
214  void addEvent(ByteBuffer & buffer);
215  void addEvent(std::shared_ptr<EvioBank> & bank);
216  void addEvent(std::shared_ptr<EvioNode> & node);
217  void addEvent(EvioNode & node);
218 
219  void reset();
220  void close();
221 
222  private:
223 
224  void writeTrailer(bool writeIndex, uint32_t recordNum, uint64_t trailerPos);
225 
226  };
227 
228 }
229 
230 
231 #endif //EVIO_6_0_WRITER_H
This class is copied from one of the same name in the Java programming language.
Definition: ByteBuffer.h:42
FileHeader & getFileHeader()
Get the file header.
Definition: Writer.cpp:357
void addEvent(uint8_t *buffer, uint32_t length)
Add a byte array to the internal record.
Definition: Writer.cpp:1014
void writeRecord(RecordOutput &record)
Appends the record to the file/buffer.
Definition: Writer.cpp:953
CompressionType
Enum of supported data compression types.
Definition: Compressor.h:65
Numerical values associated with endian byte order.
Definition: ByteOrder.h:53
void setCompressionType(Compressor::CompressionType compression)
Convenience method that sets compression type for the file.
Definition: Writer.cpp:387
Compressor::CompressionType getCompressionType()
Get the internal record&#39;s header.
Definition: Writer.cpp:377
Definition: Compressor.h:66
void close()
Close opened file.
Definition: Writer.cpp:1247
Class which handles the creation and use of Evio &amp; HIPO Records.
Definition: RecordOutput.h:105
const ByteOrder & getByteOrder() const
Get the file&#39;s byte order.
Definition: Writer.cpp:350
bool addTrailerWithIndex()
Does this writer add a trailer with a record index to the end of the file? Or, if writing to a buffer...
Definition: Writer.cpp:418
std::shared_ptr< ByteBuffer > createHeader(uint8_t *userHdr, uint32_t userLen)
Create and return a buffer containing a general file header followed by the user header given in the ...
Definition: Writer.cpp:682
Class to write Evio-6.0/HIPO files.
Definition: Writer.h:47
This class is used to store relevant info about an evio container (bank, segment, or tag segment)...
Definition: EvioNode.h:41
~Writer()=default
void open(const std::string &filename)
Open a new file and write file header with no user header.
Definition: Writer.cpp:448
Writer()
Default constructor.
Definition: Writer.cpp:21
Definition: FileHeader.h:91
Definition: RecordHeader.h:182
static std::shared_ptr< ByteBuffer > createRecord(const std::string &dictionary, uint8_t *firstEvent, uint32_t firstEventLen, const ByteOrder &byteOrder, FileHeader *fileHeader, RecordHeader *recordHeader)
STATIC.
Definition: Writer.cpp:618
void reset()
Get this object ready for re-use.
Definition: Writer.cpp:1228
std::shared_ptr< ByteBuffer > getBuffer()
Get the buffer being written to.
Definition: Writer.cpp:343
bool addTrailer() const
Does this writer add a trailer to the end of the file/buffer?
Definition: Writer.cpp:399
static const ByteOrder ENDIAN_LOCAL
Local host&#39;s byte order.
Definition: ByteOrder.h:61
Numerical values associated with types of a file or record header.
Definition: HeaderType.h:32