-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSubTimeFrameFileReader.h
More file actions
154 lines (123 loc) · 4.3 KB
/
Copy pathSubTimeFrameFileReader.h
File metadata and controls
154 lines (123 loc) · 4.3 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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_READER_H_
#define ALICEO2_SUBTIMEFRAME_FILE_READER_H_
#include "SubTimeFrameDataModel.h"
#include <Headers/DataHeader.h>
#include <Headers/Stack.h>
#include <boost/filesystem.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <fstream>
#include <vector>
namespace o2::DataDistribution
{
class SubTimeFrameFileBuilder;
////////////////////////////////////////////////////////////////////////////////
/// SubTimeFrameFileReader
////////////////////////////////////////////////////////////////////////////////
class SubTimeFrameFileReader : public ISubTimeFrameVisitor
{
public:
SubTimeFrameFileReader() = delete;
SubTimeFrameFileReader(boost::filesystem::path& pFileName);
~SubTimeFrameFileReader();
///
/// Read a single TF from the file
///
std::unique_ptr<SubTimeFrame> read(SubTimeFrameFileBuilder &pFileBuilder);
///
/// Tell the current position of the file
///
inline
std::uint64_t position() const { return mFileMapOffset; }
///
/// Set the current position of the file
///
inline
void set_position(std::uint64_t pPos)
{
const std::uint64_t lPos = std::min(pPos, mFileSize);
assert(pPos == lPos);
mFileMapOffset = lPos;
}
///
/// Is the stream position at EOF
///
inline
bool eof() const { return mFileMapOffset == mFileSize; }
///
/// Tell the size of the file
///
inline
std::uint64_t size() const { return mFileSize; }
private:
void visit(SubTimeFrame& pStf, void*) override;
std::string mFileName;
boost::iostreams::mapped_file_source mFileMap;
std::uint64_t mFileMapOffset = 0;
std::uint64_t mFileSize = 0;
// helper to make sure written chunks are buffered, only allow pointers
template <typename pointer,
typename = std::enable_if_t<std::is_pointer<pointer>::value>>
bool read_advance(pointer pPtr, std::uint64_t pLen)
{
if (pLen == 0) {
return true;
}
if (!mFileMap.is_open()) {
return false;
}
assert(mFileMapOffset <= mFileSize);
const std::uint64_t lToRead = std::min(pLen, mFileSize - mFileMapOffset);
if (lToRead != pLen) {
EDDLOG("FileReader: request to read beyond the file end. pos={} size={} len={}",
mFileMapOffset, mFileSize, pLen);
EDDLOG("Closing the file {}. The read data is invalid.", mFileName);
mFileMap.close(); mFileMapOffset = 0; mFileSize = 0;
return false;
}
std::memcpy(reinterpret_cast<char*>(pPtr), mFileMap.data() + mFileMapOffset, lToRead);
mFileMapOffset += lToRead;
return true;
}
// return the pointer
unsigned char* peek() const
{
return const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(mFileMap.data() + mFileMapOffset));
}
inline
bool ignore_nbytes(const std::size_t pLen)
{
if (pLen == 0) {
return true;
}
const std::size_t lToIgnore = std::min(pLen, std::size_t(mFileSize - mFileMapOffset));
if (pLen != lToIgnore) {
EDDLOG("FileReader: request to ignore bytes beyond the file end. pos={} size={} len={}",
mFileMapOffset, mFileSize, pLen);
EDDLOG("Closing the file {}. The read data is invalid.", mFileName);
mFileMap.close(); mFileMapOffset = 0; mFileSize = 0;
return false;
}
mFileMapOffset += lToIgnore;
assert(mFileMapOffset <= mFileSize);
return true;
}
std::size_t getHeaderStackSize();
o2::header::Stack getHeaderStack(std::size_t &pOrigsize);
// vector of <hdr, fmqMsg> elements of a tf read from the file
std::vector<SubTimeFrame::StfData> mStfData;
// flags for upgrading DataHeader versions
static std::uint64_t sStfId;
};
} /* o2::DataDistribution */
#endif /* ALICEO2_SUBTIMEFRAME_FILE_READER_H_ */