BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnFileReaderZStd.cxx
Go to the documentation of this file.
1// headers from this target
2#include "BmnFileReaderZStd.h"
3
4// std:: headers
5#include <cassert>
6#include <filesystem>
7// FairLogger headers
8#include <fairlogger/Logger.h>
9// the main Zstd header
10#ifdef BMNCOMPRESSION_WITH_ZSTD
11#include <zstd.h>
12#endif // BMNCOMPRESSION_WITH_ZSTD
13
14// 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
15// ================== BmnFileReaderZStd public methods ==================
16// 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
17
18// ========== BmnIFileReaderInterface ==========
19
20BmnFileReaderZStd::BmnFileReaderZStd([[maybe_unused]] const std::filesystem::path& aFilePath)
22 , fZstdStream(nullptr)
23 , fArchiveReadManager(*this)
24 , fNumberOfReadBytes(0ull)
26{
27#ifdef BMNCOMPRESSION_WITH_ZSTD
28 // Keeping it hidden so ZStd would be PRIVATE in CMake
29 static_assert(ZSTD_VERSION_MAJOR >= 1 && ZSTD_VERSION_MINOR > 2,
30 "You are trying to use an old version of ZStd - in that version `ZSTD_DStream` was not the same as "
31 "`ZSTD_DCtx_s`!");
32
33 if (aFilePath.empty())
34 return;
35 Open(aFilePath);
36#endif // BMNCOMPRESSION_WITH_ZSTD
37}
38
43
44void BmnFileReaderZStd::Open([[maybe_unused]] const std::filesystem::path& aFilePath)
45{
46#ifdef BMNCOMPRESSION_WITH_ZSTD
47 /*
48 While we actually don't need so many comments and checks here, we are still performing them just to ensure
49 one doesn't make an accidental mistake - it might be easier to catch it
50 */
51 if (fZstdStream) {
52 LOG(error) << "ZStd stream is already valid! The purpose of the BMN ZStd classes is to compress/decompress "
53 "huge files - if you however\n"
54 "really need to support compressing/decompressing many small files (frames), please let us know "
55 "and we will allow it along\n"
56 "with dictionaries (preferred for small files)\n";
57 return;
58 }
60 return;
61 fArchiveReadManager.Open(aFilePath, std::ios::in | std::ios::binary);
62 fZstdStream = ZSTD_createDStream();
63#else
64 throw std::runtime_error{"Trying to use ZSTD while BMN is not compiled with it!"};
65#endif // BMNCOMPRESSION_WITH_ZSTD
66}
67
69{
70#ifdef BMNCOMPRESSION_WITH_ZSTD
71 if (!fZstdStream)
72 return;
73 assert(fArchiveReadManager.IsOpen());
74 fLastReadReturnValue = ZSTD_freeDStream(fZstdStream);
75 if (ZSTD_isError(fLastReadReturnValue)) {
76 LOG(fatal) << "Failed to free ZStd stream - possible huge memory leak! Failure reason: "
77 << ZSTD_getErrorName(fLastReadReturnValue) << '\n';
78 std::terminate();
79 }
80 fZstdStream = nullptr;
81 fArchiveReadManager.Close();
82 fNumberOfReadBytes = 0ull;
84#endif // BMNCOMPRESSION_WITH_ZSTD
85}
86
87int64_t BmnFileReaderZStd::Read([[maybe_unused]] void* dataToFill, [[maybe_unused]] const int64_t sizeToFill)
88{
89#ifdef BMNCOMPRESSION_WITH_ZSTD
90 if (ZSTD_isError(fLastReadReturnValue))
91 return 0ll;
92 return fArchiveReadManager.Read(dataToFill, sizeToFill);
93#else
94 throw std::runtime_error{"Trying to use ZSTD while BMN is not compiled with it!"};
95#endif // BMNCOMPRESSION_WITH_ZSTD
96}
97
98int64_t BmnFileReaderZStd::Tell() const noexcept
99{
100 return (int64_t)(fNumberOfReadBytes + fArchiveReadManager.GetCurrentOutputBufferPosition()
101 - fArchiveReadManager.GetTotalOutputBufferFilledSize());
102}
103
104void BmnFileReaderZStd::Seek([[maybe_unused]] int64_t value)
105{
106#ifdef BMNCOMPRESSION_WITH_ZSTD
107 if (value < 0ll || value == Tell())
108 return;
109 if (ZSTD_isError(fLastReadReturnValue)) {
110 LOG(error) << "Before calling `BmnFileReaderZStd::Seek` ensure to clear bad flags!\n";
111 return;
112 }
113 if (!fZstdStream) {
114 LOG(error) << "Before calling `BmnFileReaderZStd::Seek` ensure to open the stream!\n";
115 return;
116 }
117 if (!fArchiveReadManager.IsOpen()) {
118 LOG(error) << "Reading file missing while calling `BmnFileReaderZStd::Seek`\n";
119 return;
120 }
121 if ((size_t)value < (fNumberOfReadBytes - fArchiveReadManager.GetTotalOutputBufferFilledSize())) {
122 if (ZSTD_isError(ZSTD_initDStream(fZstdStream))) {
123 LOG(error) << "Failed to reinitialize ZStd stream! Removing corrupted data...";
124 Close();
125 return;
126 }
127 fNumberOfReadBytes = 0ull;
128 fArchiveReadManager.Seek(0ll);
129 }
130 // in theory I could check IsOpen again for unexpected errors, but....
131 fArchiveReadManager.MoveToDecompressingLocation(value);
132#else
133 throw std::runtime_error{"Trying to use ZSTD while BMN is not compiled with it!"};
134#endif // BMNCOMPRESSION_WITH_ZSTD
135}
136
137bool BmnFileReaderZStd::IsGood() const noexcept
138{
139#ifdef BMNCOMPRESSION_WITH_ZSTD
140 return fZstdStream && !ZSTD_isError(fLastReadReturnValue) && fArchiveReadManager.IsGood();
141#else
142 return false;
143#endif // BMNCOMPRESSION_WITH_ZSTD
144}
145
146bool BmnFileReaderZStd::IsEOF() const noexcept
147{
148 return fArchiveReadManager.IsEOF();
149}
150
152{
153 // I don't like this method much
155 fArchiveReadManager.Clear();
156}
157
158bool BmnFileReaderZStd::IsOpen() const noexcept
159{
160 return fArchiveReadManager.IsOpen() && fZstdStream;
161}
162
164{
165 return BmnFileType::ZStd;
166}
167
168// ========== BmnIArchiveLib Interface ==========
169
170BmnFileStreamResults BmnFileReaderZStd::Uncompress([[maybe_unused]] const void* source,
171 [[maybe_unused]] const size_t sourceTotalSize,
172 [[maybe_unused]] size_t& numberOfReadBytes,
173 [[maybe_unused]] void* output,
174 [[maybe_unused]] const size_t outputTotalSize,
175 [[maybe_unused]] size_t& numberOfSetOutputBytes)
176{
177 if (!fZstdStream)
179#ifdef BMNCOMPRESSION_WITH_ZSTD
180 if (ZSTD_isError(fLastReadReturnValue))
182 ZSTD_outBuffer outBuffer{output, outputTotalSize, 0ull};
183 ZSTD_inBuffer inBuffer{source, sourceTotalSize, 0ull};
184 fLastReadReturnValue = ZSTD_decompressStream(fZstdStream, &outBuffer, &inBuffer);
185 if (ZSTD_isError(fLastReadReturnValue))
187 numberOfReadBytes = inBuffer.pos;
188 numberOfSetOutputBytes = outBuffer.pos;
189 fNumberOfReadBytes += outBuffer.pos;
190 if (fLastReadReturnValue == 0ull)
193#else
194 throw std::runtime_error{"Trying to use ZSTD while BMN is not compiled with it!"};
195#endif // BMNCOMPRESSION_WITH_ZSTD
196}
197
199{
200#ifdef BMNCOMPRESSION_WITH_ZSTD
201 std::string strToReturn{};
202 if (ZSTD_isError(fLastReadReturnValue))
203 strToReturn = std::string{ZSTD_getErrorName(fLastReadReturnValue)};
204 return strToReturn;
205#else
206 return std::string{"BMN NOT BUILT WITH ZSTD"};
207#endif // BMNCOMPRESSION_WITH_ZSTD
208}
209
211{
212 return fNumberOfReadBytes;
213}
214
216{
217#ifdef BMNCOMPRESSION_WITH_ZSTD
218 if (ZSTD_isError(fLastReadReturnValue))
219 return true;
220 return fLastReadReturnValue == 0;
221#else
222 return true;
223#endif // BMNCOMPRESSION_WITH_ZSTD
224}
225
227{
228#ifdef BMNCOMPRESSION_WITH_ZSTD
229 return !ZSTD_isError(fLastReadReturnValue);
230#else
231 return false;
232#endif // BMNCOMPRESSION_WITH_ZSTD
233}
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
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
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
virtual bool IsEOF() const noexcept override
virtual void Open(const std::filesystem::path &aFilePath) override
BmnIFileReader()=default
bool PerformStandardChecksBeforeOpening(const std::filesystem::path &aFilePath) const noexcept