DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
BufferedFileReader.hpp
Go to the documentation of this file.
1
9#ifndef DATAHANDLINGLIBS_INCLUDE_DATAHANDLINGLIBS_UTILS_BUFFEREDFILEREADER_HPP_
10#define DATAHANDLINGLIBS_INCLUDE_DATAHANDLINGLIBS_UTILS_BUFFEREDFILEREADER_HPP_
11
14
15#include "logging/Logging.hpp"
16
17#include <boost/align/aligned_allocator.hpp>
18#include <boost/iostreams/device/file_descriptor.hpp>
19#include <boost/iostreams/filter/lzma.hpp>
20#include <boost/iostreams/filter/zlib.hpp>
21#include <boost/iostreams/filter/zstd.hpp>
22#include <boost/iostreams/filtering_stream.hpp>
23#include <boost/iostreams/stream.hpp>
24#include <boost/iostreams/stream_buffer.hpp>
25
26#include <fcntl.h>
27#include <fstream>
28#include <iostream>
29#include <limits>
30#include <string>
31#include <unistd.h>
32
34
35namespace dunedaq {
36namespace datahandlinglibs {
42template<class ReadoutType, size_t Alignment = 4096>
44{
45 using io_source_t = boost::iostreams::file_descriptor_source;
46 using aligned_allocator_t = boost::alignment::aligned_allocator<io_source_t::char_type, Alignment>;
48 boost::iostreams::filtering_stream<boost::iostreams::input, char, std::char_traits<char>, aligned_allocator_t>;
49
50public:
59 BufferedFileReader(std::string filename, size_t buffer_size, std::string compression_algorithm = "None")
60 {
61 open(filename, buffer_size, compression_algorithm);
62 }
63
68
73
82 void open(std::string filename, size_t buffer_size, std::string compression_algorithm = "None")
83 {
85 m_buffer_size = buffer_size;
86 m_compression_algorithm = compression_algorithm;
87
88 int fd = ::open(m_filename.c_str(), O_RDONLY);
89 if (fd == -1) {
90 throw BufferedReaderWriterCannotOpenFile(ERS_HERE, m_filename);
91 }
92
93 io_source_t io_source(fd, boost::iostreams::file_descriptor_flags::close_handle);
94 if (m_compression_algorithm == "zstd") {
95 TLOG_DEBUG(TLVL_WORK_STEPS) << "Using zstd compression" << std::endl;
96 m_input_stream.push(boost::iostreams::zstd_decompressor());
97 } else if (m_compression_algorithm == "lzma") {
98 TLOG_DEBUG(TLVL_WORK_STEPS) << "Using lzma compression" << std::endl;
99 m_input_stream.push(boost::iostreams::lzma_decompressor());
100 } else if (m_compression_algorithm == "zlib") {
101 TLOG_DEBUG(TLVL_WORK_STEPS) << "Using zlib compression" << std::endl;
102 m_input_stream.push(boost::iostreams::zlib_decompressor());
103 } else if (m_compression_algorithm == "None") {
104 TLOG_DEBUG(TLVL_WORK_STEPS) << "Running without compression" << std::endl;
105 } else {
107 "Non-recognized compression algorithm: " + m_compression_algorithm);
108 }
109
110 m_input_stream.push(io_source, m_buffer_size);
111 m_is_open = true;
112 }
113
120 bool is_open() const { return m_is_open; }
121
127 bool read(ReadoutType& element)
128 {
129 if (!m_is_open)
130 return false;
131 m_input_stream.read(reinterpret_cast<char*>(&element), sizeof(element)); // NOLINT
132 return (m_input_stream.gcount() == sizeof(element));
133 }
134
138 void close()
139 {
140 m_input_stream.reset();
141 m_is_open = false;
142 }
143
144private:
145 // Config parameters
146 std::string m_filename;
149
150 // Internals
152 bool m_is_open = false;
153};
154
155} // namespace datahandlinglibs
156} // namespace dunedaq
157
158#endif // DATAHANDLINGLIBS_INCLUDE_DATAHANDLINGLIBS_UTILS_BUFFEREDFILEREADER_HPP_
#define ERS_HERE
BufferedFileReader & operator=(BufferedFileReader &&)=delete
BufferedFileReader is not move-assignable.
void open(std::string filename, size_t buffer_size, std::string compression_algorithm="None")
boost::iostreams::file_descriptor_source io_source_t
BufferedFileReader & operator=(const BufferedFileReader &)=delete
BufferedFileReader is not copy-assginable.
BufferedFileReader(const BufferedFileReader &)=delete
BufferedFileReader is not copy-constructible.
boost::iostreams::filtering_stream< boost::iostreams::input, char, std::char_traits< char >, aligned_allocator_t > filtering_istream_t
BufferedFileReader(std::string filename, size_t buffer_size, std::string compression_algorithm="None")
boost::alignment::aligned_allocator< io_source_t::char_type, Alignment > aligned_allocator_t
BufferedFileReader(BufferedFileReader &&)=delete
BufferedFileReader is not move-constructible.
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
Including Qt Headers.
SourceID[" << sourceid << "] Command daqdataformats::SourceID Readout Initialization std::string initerror BufferedReaderWriterConfigurationError