BmnRoot
26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
BmnFileReaderZStd.h
Go to the documentation of this file.
1
#pragma once
2
#ifndef BmnFileReaderZStd_h
3
#define BmnFileReaderZStd_h
4
5
// headers from this target
6
#include "
BmnIArchiveLibRead.h
"
7
#include "
BmnIFileReader.h
"
8
#include "
BmnReaderCacheManager.h
"
9
10
// keeping it only as a declaration so one would be able to include the header even if <zstd.h> is not in the include
11
// list
12
struct
ZSTD_DCtx_s;
13
14
/*
15
* A class responsible for reading compressed ZSTD files. The primary goal of this class is to read
16
* large compressed files (possibly a few GB) such as those for raw data. If smaller files are used,
17
* there might be better ways to compress/uncompress data (e.g., with dictionaries), but such cases
18
* were not considered while preparing this class - if needed, add a new one that handles such cases
19
* or upgrade this.
20
* It's advisable to allocate this class on the heap as internally it uses big std::array
21
* @warning Using this class directly can result in throws if the zstd library is not found during compilation.
22
* Therefore, it's always advisable to obtain appropriate read objects through
23
* `BmnCompressionCreateNewSuitableReaderForAFile`
24
*/
25
class
BmnFileReaderZStd
26
:
public
BmnIFileReader
27
,
public
BmnIArchiveLibRead
28
{
29
public
:
30
/*
31
* A standard constructor that sets a path to the input binary (ZSTD) file. If provided, the file will be opened in
32
* the constructor
33
* @param aFilePath A path to the input binary (ZSTD) file. If provided, the file will be opened in the constructor
34
* @warning If using relative paths, be very careful about currently-working directory as it might
35
* be something unexpected based on the directory that initalized an application
36
*/
37
explicit
BmnFileReaderZStd
(
const
std::filesystem::path& aFilePath =
""
);
38
39
// Closes the stream while destroying the instance
40
virtual
~BmnFileReaderZStd
()
override
;
41
BmnFileReaderZStd
(
const
BmnFileReaderZStd
&) =
delete
;
42
BmnFileReaderZStd
&
operator=
(
const
BmnFileReaderZStd
&) =
delete
;
43
BmnFileReaderZStd
(
BmnFileReaderZStd
&&) noexcept = delete;
44
BmnFileReaderZStd
& operator=(
BmnFileReaderZStd
&&) noexcept = delete;
45
46
// ========== BmnIFileReaderInterface ==========
47
48
/*
49
* A method that opens a binary ZStd-compressed file for reading
50
* @param aFilePath A path to the file that should be opened
51
* @warning Be very carful with "the currently-working directory" as it may not be the same as the executable path!
52
*/
53
virtual
void
Open
(const std::filesystem::path& aFilePath) override;
54
55
/*
56
* Closes the stream and resets all internal data. After calling `Close`, it's safe to reopen using the same
57
* instance of this class
58
*/
59
virtual
void
Close
() noexcept override;
60
61
/*
62
* Reads and decompresses the data while still managing caching for efficiency.
63
* @param dataToFill A buffer that will be filled with uncompressed data
64
* @param sizeToFill A number of bytes that will be filled in `dataToFill`
65
* @return A number of bytes that were filled in `dataToFill`. This value should almost always be the same as
66
* `sizeToFill` except when the end of the stream is reached (only if the user asked for more data than possible)
67
*/
68
virtual int64_t
Read
(
void
* dataToFill, const int64_t sizeToFill) override;
69
70
/*
71
* A method that tells us what's next to read
72
* @return A position of the next byte that should be read
73
*/
74
[[nodiscard]] virtual int64_t
Tell
() const noexcept override;
75
76
/*
77
* A method that allows us to go to another place in the file (uncompressed data) and read from that place.
78
* This method is NOT EFFICIENT as it still needs to uncompress data to reach the needed place!
79
* @param value A position in uncompressed data to which one wants to move before the next read
80
*/
81
virtual
void
Seek
(int64_t value) override;
82
83
/*
84
* Checks whether the reading stream is good
85
* @return 'true' if no decompression error has occurred and the input-file stream is good, or 'false' otherwise
86
*/
87
[[nodiscard]] virtual
bool
IsGood
() const noexcept override;
88
89
/*
90
* Check if we have reached the file. This will tell us if we have reached EOF in the uncompressed data, not
91
* in the file itself. That is, the file might be at EOF, while this method returns will return true as cache
92
* still has some data
93
* @return 'true' if both file and internal cache are completely read, 'false' otherwise
94
*/
95
[[nodiscard]] virtual
bool
IsEOF
() const noexcept override;
96
97
/*
98
* A method that clears bad flags after an error has occurred. Just calling this error will most likely
99
* not solve the problem, and you will get an error again in the next read. That is, before calling `Read`,
100
* remove the previous error source
101
*/
102
virtual
void
ClearStateFlags
() override;
103
104
/*
105
* @return 'true' if the input stream is open, or 'false' if not
106
*/
107
[[nodiscard]] virtual
bool
IsOpen
() const noexcept override;
108
109
/*
110
* @return BmnFileType::ZStd
111
*/
112
[[nodiscard]] virtual
BmnFileType
GetFileType
() const noexcept override;
113
114
// ========== BmnIArchiveLib Interface ==========
115
116
/*
117
* A method that is called to uncompress ZSTD data
118
* @param source A pointer to a buffer with the subsequent compressed data that should be uncompressed
119
* @param sourceTotalSize A number of bytes available in the source buffer
120
* @param numberOfReadBytes A number of bytes that were consumed by ZSTD while compressing. This value will be set.
121
* @param output A pointer to a buffer that will be filled with uncompressed data
122
* @param outputTotalSize A number of bytes that may be filled in the output buffer
123
* @param numberOfSetOutputBytes A number of bytes that were set while decompressing. This value will be set.
124
* @return There are a few options:
125
* 1. `BmnFileStreamResults::FileNotOpen` if the stream is not open
126
* 2. `BmnFileStreamResults::TryingToReadBadStream` if the stream previously hit an error while it wasn't
127
* cleared later
128
* 3. `BmnFileStreamResults::UnexpectedFail` ZSTD failed while decompressing
129
* 4. `BmnFileStreamResults::ReachedEOF` ZSTD reached the end of frame (in our case file)
130
* 5. `BmnFileStreamResults::Success` ZSTD successfully uncompressed the package
131
*/
132
virtual
BmnFileStreamResults
Uncompress
(const
void
* source,
133
const
size_t
sourceTotalSize,
134
size_t
& numberOfReadBytes,
135
void
* output,
136
const
size_t
outputTotalSize,
137
size_t
& numberOfSetOutputBytes) override;
138
139
/*
140
* If an error occurred while decompressing, this method can be called to obtain a description of the error
141
* @return A description of an error that occurred while decompressing, or an empty string if no such error happened
142
* @throw std::bad_alloc if an allocation error occurs
143
*/
144
[[nodiscard]] virtual std::
string
GetLastUncompressErrorDescription
() const override;
145
146
/*
147
* A method that tells us how many uncompressed bytes were read from an input file
148
* @return A number of uncompressed bytes that passed through the ZSTD
149
*/
150
[[nodiscard]] virtual
size_t
GetNumberOfDecompressedBytes
() const noexcept override;
151
152
/*
153
* A method that checks whether there's something left in ZSTD cache, i.e., if we should
154
* continue reading even though we have reached the end of the stream. If stream is in a
155
* bad state, it will also return as EOF
156
* @return 'true' if the underlying stream has a bad state or empty internal cache, or 'false' otherwise
157
*/
158
[[nodiscard]] virtual
bool
HasReachedTheEndOfStream
() const noexcept override;
159
160
/*
161
* A method that tells us whether an underlying ZSTD stream reported an error or not
162
* @return 'true' if previous read calls didn't report an error, otherwise 'false'
163
*/
164
[[nodiscard]] virtual
bool
IsStreamInGoodState
() const noexcept override;
165
166
// ========== BmnFileReaderZStd inlines ==========
167
168
[[nodiscard]] inline ZSTD_DCtx_s*
GetUnderlyingLibraryHandler
() const noexcept;
169
[[nodiscard]] inline
size_t
GetLastReadReturnValue
() const noexcept;
170
// Get number of read bytes via `GetNumberOfDecompressedBytes`
171
// No need to expose `fArchiveReadManager`
172
173
private:
174
ZSTD_DCtx_s*
fZstdStream
;
175
BmnReaderCacheManager
<32ull * 1024ull, 64ull * 1024ull>
fArchiveReadManager
;
176
size_t
fNumberOfReadBytes
;
177
size_t
fLastReadReturnValue
;
// possibly carries information about an error that occurred
178
};
179
180
// o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
181
// ================== BmnFileReaderZStd inline methods ==================
182
// o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
183
184
ZSTD_DCtx_s*
BmnFileReaderZStd
::
GetUnderlyingLibraryHandler
() const noexcept
185
{
186
return
fZstdStream
;
187
}
188
189
size_t
BmnFileReaderZStd::GetLastReadReturnValue
() const noexcept
190
{
191
return
fLastReadReturnValue
;
192
}
193
194
#endif
// BmnFileReaderZStd_h
BmnFileStreamResults
BmnFileStreamResults
Definition
BmnFileProcessingFlags.h:74
BmnFileType
BmnFileType
Definition
BmnFileProcessingFlags.h:16
BmnIArchiveLibRead.h
BmnIFileReader.h
BmnReaderCacheManager.h
BmnFileReaderZStd::Seek
virtual void Seek(int64_t value) override
Definition
BmnFileReaderZStd.cxx:104
BmnFileReaderZStd::Uncompress
virtual BmnFileStreamResults Uncompress(const void *source, const size_t sourceTotalSize, size_t &numberOfReadBytes, void *output, const size_t outputTotalSize, size_t &numberOfSetOutputBytes) override
Definition
BmnFileReaderZStd.cxx:170
BmnFileReaderZStd::IsGood
virtual bool IsGood() const noexcept override
Definition
BmnFileReaderZStd.cxx:137
BmnFileReaderZStd::GetLastReadReturnValue
size_t GetLastReadReturnValue() const noexcept
Definition
BmnFileReaderZStd.h:189
BmnFileReaderZStd::GetUnderlyingLibraryHandler
ZSTD_DCtx_s * GetUnderlyingLibraryHandler() const noexcept
Definition
BmnFileReaderZStd.h:184
BmnFileReaderZStd::~BmnFileReaderZStd
virtual ~BmnFileReaderZStd() override
Definition
BmnFileReaderZStd.cxx:39
BmnFileReaderZStd::Close
virtual void Close() noexcept override
Definition
BmnFileReaderZStd.cxx:68
BmnFileReaderZStd::GetLastUncompressErrorDescription
virtual std::string GetLastUncompressErrorDescription() const override
Definition
BmnFileReaderZStd.cxx:198
BmnFileReaderZStd::ClearStateFlags
virtual void ClearStateFlags() override
Definition
BmnFileReaderZStd.cxx:151
BmnFileReaderZStd::GetNumberOfDecompressedBytes
virtual size_t GetNumberOfDecompressedBytes() const noexcept override
Definition
BmnFileReaderZStd.cxx:210
BmnFileReaderZStd::fNumberOfReadBytes
size_t fNumberOfReadBytes
Definition
BmnFileReaderZStd.h:176
BmnFileReaderZStd::IsStreamInGoodState
virtual bool IsStreamInGoodState() const noexcept override
Definition
BmnFileReaderZStd.cxx:226
BmnFileReaderZStd::Tell
virtual int64_t Tell() const noexcept override
Definition
BmnFileReaderZStd.cxx:98
BmnFileReaderZStd::BmnFileReaderZStd
BmnFileReaderZStd(const std::filesystem::path &aFilePath="")
Definition
BmnFileReaderZStd.cxx:20
BmnFileReaderZStd::HasReachedTheEndOfStream
virtual bool HasReachedTheEndOfStream() const noexcept override
Definition
BmnFileReaderZStd.cxx:215
BmnFileReaderZStd::fZstdStream
ZSTD_DCtx_s * fZstdStream
Definition
BmnFileReaderZStd.h:174
BmnFileReaderZStd::fLastReadReturnValue
size_t fLastReadReturnValue
Definition
BmnFileReaderZStd.h:177
BmnFileReaderZStd::operator=
BmnFileReaderZStd & operator=(const BmnFileReaderZStd &)=delete
BmnFileReaderZStd::Read
virtual int64_t Read(void *dataToFill, const int64_t sizeToFill) override
Definition
BmnFileReaderZStd.cxx:87
BmnFileReaderZStd::GetFileType
virtual BmnFileType GetFileType() const noexcept override
Definition
BmnFileReaderZStd.cxx:163
BmnFileReaderZStd::IsOpen
virtual bool IsOpen() const noexcept override
Definition
BmnFileReaderZStd.cxx:158
BmnFileReaderZStd::fArchiveReadManager
BmnReaderCacheManager< 32ull *1024ull, 64ull *1024ull > fArchiveReadManager
Definition
BmnFileReaderZStd.h:175
BmnFileReaderZStd::BmnFileReaderZStd
BmnFileReaderZStd(BmnFileReaderZStd &&) noexcept=delete
BmnFileReaderZStd::IsEOF
virtual bool IsEOF() const noexcept override
Definition
BmnFileReaderZStd.cxx:146
BmnFileReaderZStd::Open
virtual void Open(const std::filesystem::path &aFilePath) override
Definition
BmnFileReaderZStd.cxx:44
BmnFileReaderZStd::BmnFileReaderZStd
BmnFileReaderZStd(const BmnFileReaderZStd &)=delete
BmnIFileReader::BmnIFileReader
BmnIFileReader()=default
BmnReaderCacheManager
Definition
BmnReaderCacheManager.h:27
BmnIArchiveLibRead::BmnIArchiveLibRead
BmnIArchiveLibRead()=default
compression
include
BmnFileReaderZStd.h
Generated on
for BmnRoot by
1.17.0