25#include <vrs/Compressor.h>
26#include <vrs/DiskFile.h>
27#include <vrs/FileFormat.h>
28#include <vrs/IndexRecord.h>
29#include <vrs/NewChunkHandler.h>
30#include <vrs/Record.h>
31#include <vrs/Recordable.h>
32#include <vrs/VrsExport.h>
33#include <vrs/WriteFileHandler.h>
38struct RecordFileWriterTester;
51namespace RecordFileWriter_ {
52struct WriterThreadData;
53struct PurgeThreadData;
54struct CompressionThreadsData;
55struct RecordWriterData;
109 vector<Recordable*> getRecordables()
const;
115 void setCompressionThreadPoolSize(
size_t size = kMaxThreadPoolSizeForHW);
116 static constexpr size_t kMaxThreadPoolSizeForHW = UINT32_MAX;
131 int writeToFile(
const string& filePath);
138 void purgeOldRecords(
double maxTimestamp,
bool recycleBuffers =
true);
144 virtual int createFileAsync(
const string& filePath);
164 int createChunkedFile(
165 const string& filePath,
166 size_t maxChunkSizeMB = 0,
167 unique_ptr<NewChunkHandler>&& chunkHandler =
nullptr);
173 void setMaxChunkSizeMB(
size_t maxChunkSizeMB);
183 int preallocateIndex(unique_ptr<deque<IndexRecord::DiskRecordInfo>> preliminaryIndex);
189 int writeRecordsAsync(
double maxTimestamp);
196 int autoWriteRecordsAsync(
const function<
double()>& maxTimestampProvider,
double delay);
205 int autoPurgeRecords(
const function<
double()>& maxTimestampProvider,
double delay);
210 return file_->isOpened();
219 void trackBackgroundThreadQueueByteSize();
223 uint64_t getBackgroundThreadQueueByteSize();
227 int closeFileAsync();
231 virtual int waitForFileClosed();
240 void setTag(
const string& tagName,
const string& tagValue);
243 void addTags(
const map<string, string>& newTags);
256 int setWriteFileHandler(unique_ptr<WriteFileHandler> writeFileHandler);
260 void backgroundWriterThreadActivity();
263 void backgroundPurgeThreadActivity();
272 return this->record->getTimestamp() < rhs.record->
getTimestamp() ||
273 (this->record->getTimestamp() <= rhs.record->
getTimestamp() &&
274 (this->streamId < rhs.streamId ||
276 (this->streamId == rhs.streamId &&
277 this->record->getCreationOrder() < rhs.record->getCreationOrder())));
292 uint64_t collectOldRecords(
RecordBatch& batch,
double maxTimestamp);
293 bool autoCollectRecords(
bool checkTime);
294 static uint64_t addRecordBatchesToSortedRecords(
297 int createFileAsync(
const string& filePath,
bool splitHead);
298 int createFile(
const string& filePath,
bool splitHead);
300 int writeRecordsSingleThread(
SortedRecords& records,
int lastError);
301 int writeRecordsMultiThread(
310 uint32_t compressedSize);
311 int completeAndCloseFile();
313 void backgroundWriteCollectedRecord();
314 bool addRecordsReadyToWrite(
SortedRecords& inOutRecordsToWrite);
317 set<Recordable*> recordables_;
318 mutable std::mutex recordablesMutex_;
323 uint64_t maxChunkSize_{};
324 unique_ptr<NewChunkHandler> newChunkHandler_;
326 uint32_t lastRecordSize_{};
327 bool sortRecords_{
true};
328 bool skipFinalizeIndexRecords_ =
false;
329 unique_ptr<deque<IndexRecord::DiskRecordInfo>> preliminaryIndex_;
330 IndexRecord::Writer indexRecordWriter_;
331 map<string, string> fileTags_;
332 size_t compressionThreadPoolSize_{};
336 std::unique_ptr<std::atomic<uint64_t>> queueByteSize_;
343 friend struct ::vrs::test::RecordFileWriterTester;
Helper class to compress data using lz4 or zstd presets.
Definition Compressor.h:83
The class to create VRS files.
Definition RecordFileWriter.h:91
vector< unique_ptr< RecordBatch > > RecordBatches
Series of record batches collected.
Definition RecordFileWriter.h:286
const map< string, string > & getTags() const
Definition RecordFileWriter.h:246
vector< pair< StreamId, list< Record * > > > RecordBatch
Batch of records collected at one point in time, for each recordable.
Definition RecordFileWriter.h:284
RecordFileWriter_::PurgeThreadData * purgeThreadData_
when a purge thread is active
Definition RecordFileWriter.h:339
RecordFileWriter_::WriterThreadData * writerThreadData_
when a background thread is active
Definition RecordFileWriter.h:335
unique_ptr< WriteFileHandler > file_
data members valid while a file is being worked on
Definition RecordFileWriter.h:322
bool isWriting() const
Definition RecordFileWriter.h:209
deque< SortRecord > SortedRecords
List of records, sorted by time.
Definition RecordFileWriter.h:288
Essential VRS class holding a record's details and payload in memory during creation.
Definition Record.h:82
double getTimestamp() const
Get the record's timestamp.
Definition Record.h:136
Class to override to implement a record producing device, or virtual device.
Definition Recordable.h:51
VRS stream identifier class.
Definition StreamId.h:251
Definition Compressor.cpp:113
std::function< void(std::thread &, ThreadRole, int)> InitCreatedThreadCallback
Definition RecordFileWriter.h:63
ThreadRole
Thread types that are created with the RecordFileWriter interface.
Definition RecordFileWriter.h:59
Definition RecordFileWriter.h:267
bool operator<(const SortRecord &rhs) const
we are sorting records primarily by timestamp, but this order is a total order
Definition RecordFileWriter.h:271
Definition RecordFileWriter.cpp:135
Definition RecordFileWriter.cpp:216
Definition RecordFileWriter.cpp:930
Definition RecordFileWriter.cpp:168