BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
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
7#include "BmnIFileReader.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
12struct 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 */
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;
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;
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
184ZSTD_DCtx_s* BmnFileReaderZStd::GetUnderlyingLibraryHandler() const noexcept
185{
186 return fZstdStream;
187}
188
190{
192}
193
194#endif // BmnFileReaderZStd_h
virtual void Seek(int64_t value) 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 bool IsGood() const noexcept override
size_t GetLastReadReturnValue() const noexcept
ZSTD_DCtx_s * GetUnderlyingLibraryHandler() const noexcept
virtual ~BmnFileReaderZStd() override
virtual void Close() noexcept override
virtual std::string GetLastUncompressErrorDescription() const override
virtual void ClearStateFlags() override
virtual size_t GetNumberOfDecompressedBytes() const noexcept override
virtual bool IsStreamInGoodState() const noexcept override
virtual int64_t Tell() const noexcept override
BmnFileReaderZStd(const std::filesystem::path &aFilePath="")
virtual bool HasReachedTheEndOfStream() const noexcept override
ZSTD_DCtx_s * fZstdStream
BmnFileReaderZStd & operator=(const BmnFileReaderZStd &)=delete
virtual int64_t Read(void *dataToFill, const int64_t sizeToFill) override
virtual BmnFileType GetFileType() const noexcept override
virtual bool IsOpen() const noexcept override
BmnReaderCacheManager< 32ull *1024ull, 64ull *1024ull > fArchiveReadManager
BmnFileReaderZStd(BmnFileReaderZStd &&) noexcept=delete
virtual bool IsEOF() const noexcept override
virtual void Open(const std::filesystem::path &aFilePath) override
BmnFileReaderZStd(const BmnFileReaderZStd &)=delete
BmnIFileReader()=default
BmnIArchiveLibRead()=default