-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSubTimeFrameFileWriter.h
More file actions
132 lines (105 loc) · 4.13 KB
/
SubTimeFrameFileWriter.h
File metadata and controls
132 lines (105 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
// Copyright 2019-2022 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \author Gvozden Nešković, Frankfurt Institute for Advanced Studies and Goethe University Frankfurt
#ifndef ALICEO2_SUBTIMEFRAME_FILE_WRITER_H_
#define ALICEO2_SUBTIMEFRAME_FILE_WRITER_H_
#include "SubTimeFrameDataModel.h"
#include "SubTimeFrameFile.h"
#include <Headers/DataHeader.h>
#include <type_traits>
#include <boost/filesystem.hpp>
#include <fstream>
#include <vector>
#include <optional>
namespace o2::DataDistribution
{
////////////////////////////////////////////////////////////////////////////////
/// SubTimeFrameFileWriter
////////////////////////////////////////////////////////////////////////////////
class SubTimeFrameFileWriter : public ISubTimeFrameConstVisitor
{
static const constexpr char* sSidecarFieldSep = " ";
static const constexpr char* sSidecarRecordSep = "\n";
public:
SubTimeFrameFileWriter() = delete;
SubTimeFrameFileWriter(const boost::filesystem::path& pFileName, bool pWriteInfo, std::optional<EosMetadata> pEosMetadata);
virtual ~SubTimeFrameFileWriter();
///
/// Writes a (Sub)TimeFrame
///
std::uint64_t write(const SubTimeFrame& pStf);
///
/// Tell current size of the file
///
std::uint64_t size() { return std::uint64_t(mFile.tellp()); }
///
/// Delete the (Sub)TimeFrame file on error
///
void remove();
///
/// Close the (Sub)TimeFrame file on error
///
void close();
private:
void visit(const SubTimeFrame& pStf, void*) override;
/// Writes a (Sub)TimeFrame
std::uint64_t _write(const SubTimeFrame& pStf);
//
// workaround for buffered operation (performance):
// - provide a new, larger, buffer
// - always write less than 1024B at a time (hard limit in libstdc++)
//
static constexpr std::streamsize sBuffSize = 1ul << 20; // 1 MiB
static constexpr std::streamsize sChunkSize = 512;
boost::filesystem::path mFileName;
std::ofstream mFile;
bool mRemoved = false;
bool mWriteInfo;
std::ofstream mInfoFile;
std::optional<EosMetadata> mEosMetadata;
boost::filesystem::path mMetaFileName;
std::unique_ptr<char[]> mFileBuf;
std::unique_ptr<char[]> mInfoFileBuf;
// helper to make sure the written blocks are buffered
template <
typename pointer,
typename std::enable_if<
std::is_pointer<pointer>::value && // pointers only
(std::is_void<std::remove_pointer_t<pointer>>::value || // void* or standard layout!
std::is_standard_layout<std::remove_pointer_t<pointer>>::value)>::type* = nullptr>
void buffered_write(const pointer p, std::streamsize pCount)
{
// make sure we're not doing a short write
assert((pCount % sizeof(std::conditional_t<std::is_void<std::remove_pointer_t<pointer>>::value,
char, std::remove_pointer_t<pointer>>) == 0) && "Performing short write?");
const char* lPtr = reinterpret_cast<const char*>(p);
// avoid the optimization if the write is large enough
if (pCount >= sBuffSize) {
mFile.write(lPtr, pCount);
} else {
// split the write to smaller chunks
while (pCount > 0) {
const auto lToWrite = std::min(pCount, sChunkSize);
assert(lToWrite > 0 && lToWrite <= sChunkSize && lToWrite <= pCount);
mFile.write(lPtr, lToWrite);
lPtr += lToWrite;
pCount -= lToWrite;
}
}
}
std::uint64_t getSizeInFile() const;
// vector of <headers, data> elements of a Stf to be written
std::vector<const SubTimeFrame::StfMessage*> mStfData;
SubTimeFrameFileDataIndex mStfDataIndex;
std::uint64_t mStfSize = std::uint64_t(0); // meta + index + data (and all headers)
};
} /* o2::DataDistribution */
#endif /* ALICEO2_SUBTIMEFRAME_FILE_WRITER_H_ */