BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnFileReaderZLibNG.h
Go to the documentation of this file.
1#pragma once
2#ifndef BmnFileReaderZLibNG_h
3#define BmnFileReaderZLibNG_h
4// headers from this target
6#include "BmnIFileReader.h"
8
9struct zng_stream_s;
10
11/*
12 * A class responsible for reading files compressed with ZLib-ng (Deflate algorithm, a.k.a. ZLib new generation)
13 * or GZip (internally, it uses ZLib which is also based on the Deflate algorithm). In our tests, we have observed
14 * that ZLib-ng achieves comparable compression ratios with ZLib (on higher compression level, while -1 is BAD)
15 * while compression/decompression is significantly faster. In fact, decompression can even be faster than ZStd
16 * compression! Thus, if ZLib-ng is built along with BMN, we will use this class by default for Deflate-compressed
17 * files, unless specified otherwise by a user.
18 * It's advisable to allocate this class on the heap as internally it uses big std::array
19 * @warning Using this class directly can result in throws if the ZLib-ng library is not found during compilation.
20 * Therefore, it's always advisable to obtain appropriate read objects through
21 * `BmnCompressionCreateNewSuitableReaderForAFile`
22 */
24 : public BmnIFileReader
25 , public BmnIArchiveLibRead
26{
27 public:
28 /*
29 * A standard constructor that sets a path to the input binary (Deflate-compressed) file. If provided, the file will
30 * be opened in the constructor
31 * @param aFilePath A path to the input binary (Deflate-compressed) file. If provided, the file will be opened in
32 * the constructor
33 * @warning If using relative paths, be very careful about currently-working directory as it might
34 * be something unexpected based on the directory that initalized an application
35 */
36 explicit BmnFileReaderZLibNG(const std::filesystem::path& aFilePath = "");
37
38 // Closes the stream while destroying the instance
39 virtual ~BmnFileReaderZLibNG() override;
43 BmnFileReaderZLibNG& operator=(BmnFileReaderZLibNG&&) noexcept = delete;
44
45 // ========== BmnIFileReaderInterface ==========
46
47 /*
48 * A method that opens a binary ZLibNG-compressed file for reading
49 * @param aFilePath A path to the file that should be opened
50 * @warning Be very carful with "the currently-working directory" as it may not be the same as the executable path!
51 */
52 virtual void Open(const std::filesystem::path& aFilePath) override;
53
54 /*
55 * Closes the stream and resets all internal data. After calling `Close`, it's safe to reopen using the same
56 * instance of this class
57 */
58 virtual void Close() noexcept override;
59
60 /*
61 * Reads and decompresses the data while still managing caching for efficiency.
62 * @param dataToFill A buffer that will be filled with uncompressed data
63 * @param sizeToFill A number of bytes that will be filled in `dataToFill`
64 * @return A number of bytes that were filled in `dataToFill`. This value should almost always be the same as
65 * `sizeToFill` except when the end of the stream is reached (only if the user asked for more data than
66 * possible)
67 */
68 virtual int64_t Read(void* dataToFill, const int64_t sizeToFill) override;
69
70 /*
71 * A method that tells us what's the next byte 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 * Check 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::ZLib_ng
111 */
112 [[nodiscard]] virtual BmnFileType GetFileType() const noexcept override;
113
114 // ========== BmnIArchiveLib Interface ==========
115
116 /*
117 * A method that is called to uncompress Deflate data with ZLib-ng
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. If one has reached the EOF, it's ok to
120 * pass a 0 here
121 * @param numberOfReadBytes A number of bytes that were consumed by ZLib-ng while decompressing. This value will be
122 * set.
123 * @param output A pointer to a buffer that will be filled with uncompressed data
124 * @param outputTotalSize A number of bytes that may be filled in the output buffer
125 * @param numberOfSetOutputBytes A number of bytes that were set while decompressing. This value will be set.
126 * @return There are a few options:
127 * 1. `BmnFileStreamResults::FileNotOpen` if the stream is not open
128 * 2. `BmnFileStreamResults::TryingToReadBadStream` if the stream previously hit an error while it wasn't
129 * cleared later
130 * 3. `BmnFileStreamResults::UnexpectedFail` ZLib-ng failed while decompressing
131 * 4. `BmnFileStreamResults::ReachedEOF` ZLib-ng reached the end of frame (in our case file)
132 * 5. `BmnFileStreamResults::Success` ZLib-ng successfully uncompressed the package
133 */
134 virtual BmnFileStreamResults Uncompress(const void* source,
135 const size_t sourceTotalSize,
136 size_t& numberOfReadBytes,
137 void* output,
138 const size_t outputTotalSize,
139 size_t& numberOfSetOutputBytes) override;
140
141 /*
142 * If an error occurred while decompressing, this method can be called to obtain a description of the error
143 * @return A description of an error that occurred while decompressing, or an empty string if no such error happened
144 * @throw std::bad_alloc if an allocation error occurs
145 */
146 [[nodiscard]] virtual std::string GetLastUncompressErrorDescription() const override;
147
148 /*
149 * A method that tells us how many uncompressed bytes were read from an input file
150 * @return A number of uncompressed bytes that passed through the ZLib-ng
151 */
152 [[nodiscard]] virtual size_t GetNumberOfDecompressedBytes() const noexcept override;
153
154 /*
155 * A method that can be used to check if the reading stream has ended
156 * @return 'true' if we have reached EOF, 'false' otherwise
157 */
158 [[nodiscard]] virtual bool HasReachedTheEndOfStream() const noexcept override;
159
160 /*
161 * A method that checks whether the last read was good or not. If not, the stream is not good anymore
162 * @return 'true' if the stream is in a good state, 'false' otherwise
163 */
164 [[nodiscard]] virtual bool IsStreamInGoodState() const noexcept override;
165
166 // ========== BmnFileReaderZLibNG inlines ==========
167
168 [[nodiscard]] inline zng_stream_s* GetUnderlyingLibraryStream() const noexcept;
169 // Get number of read bytes via `GetNumberOfDecompressedBytes`
170 // No need to expose `fArchiveReadManager`
171
172 private:
173 zng_stream_s* fZlibNgStream;
174 BmnReaderCacheManager<32ull * 1024ull, 64ull * 1024ull> fArchiveReadManager;
177 // 4 padding bytes
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 o
181// ================== BmnFileReaderZLibNG 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 o
183
184[[nodiscard]] inline zng_stream_s* BmnFileReaderZLibNG::GetUnderlyingLibraryStream() const noexcept
185{
186 return fZlibNgStream;
187}
188
189#endif // BmnFileReaderZLibNG_h
virtual int64_t Read(void *dataToFill, const int64_t sizeToFill) override
virtual bool IsStreamInGoodState() const noexcept override
zng_stream_s * GetUnderlyingLibraryStream() const noexcept
virtual void Close() noexcept override
virtual bool IsGood() const noexcept override
virtual int64_t Tell() const noexcept override
BmnFileReaderZLibNG(const BmnFileReaderZLibNG &)=delete
virtual BmnFileType GetFileType() const noexcept override
virtual std::string GetLastUncompressErrorDescription() const override
virtual bool HasReachedTheEndOfStream() const noexcept override
virtual BmnFileStreamResults Uncompress(const void *source, const size_t sourceTotalSize, size_t &numberOfReadBytes, void *output, const size_t outputTotalSize, size_t &numberOfSetOutputBytes) override
virtual void ClearStateFlags() override
virtual ~BmnFileReaderZLibNG() override
virtual bool IsOpen() const noexcept override
BmnFileReaderZLibNG & operator=(const BmnFileReaderZLibNG &)=delete
virtual size_t GetNumberOfDecompressedBytes() const noexcept override
BmnFileReaderZLibNG(BmnFileReaderZLibNG &&) noexcept=delete
virtual void Open(const std::filesystem::path &aFilePath) override
BmnFileReaderZLibNG(const std::filesystem::path &aFilePath="")
virtual bool IsEOF() const noexcept override
BmnReaderCacheManager< 32ull *1024ull, 64ull *1024ull > fArchiveReadManager
virtual void Seek(int64_t value) override
BmnIFileReader()=default
BmnIArchiveLibRead()=default