diff --git a/components/core/src/clp/streaming_compression/Compressor.hpp b/components/core/src/clp/streaming_compression/Compressor.hpp index ee7846aee..602a16afc 100644 --- a/components/core/src/clp/streaming_compression/Compressor.hpp +++ b/components/core/src/clp/streaming_compression/Compressor.hpp @@ -22,7 +22,7 @@ class Compressor : public WriterInterface { public: // Constructors OperationFailed(ErrorCode error_code, char const* const filename, int line_number) - : TraceableException(error_code, filename, line_number) {} + : TraceableException{error_code, filename, line_number} {} // Methods [[nodiscard]] auto what() const noexcept -> char const* override { @@ -31,7 +31,7 @@ class Compressor : public WriterInterface { }; // Constructor - explicit Compressor(CompressorType type) : m_type(type) {} + explicit Compressor(CompressorType type) : m_type{type} {} // Destructor virtual ~Compressor() = default; @@ -41,7 +41,7 @@ class Compressor : public WriterInterface { auto operator=(Compressor const&) -> Compressor& = delete; Compressor(Compressor&&) noexcept = default; - auto operator=(Compressor&&) -> Compressor& = default; + auto operator=(Compressor&&) noexcept -> Compressor& = default; // Methods implementing the WriterInterface /** @@ -68,17 +68,23 @@ class Compressor : public WriterInterface { */ virtual auto close() -> void = 0; + /** + * Initializes the compression stream + * @param file_writer + */ + virtual auto open(FileWriter& file_writer) -> void { open(file_writer, 0); } + /** * Initializes the compression stream with the given compression level * @param file_writer * @param compression_level */ - virtual auto open(FileWriter& file_writer, [[maybe_unused]] int compression_level = 0) -> void - = 0; + virtual auto open(FileWriter& file_writer, [[maybe_unused]] int const compression_level) -> void + = 0; private: // Variables - CompressorType m_type{}; + CompressorType m_type; }; } // namespace clp::streaming_compression diff --git a/components/core/src/clp/streaming_compression/passthrough/Compressor.cpp b/components/core/src/clp/streaming_compression/passthrough/Compressor.cpp index 112bf7d39..5f1914a3b 100644 --- a/components/core/src/clp/streaming_compression/passthrough/Compressor.cpp +++ b/components/core/src/clp/streaming_compression/passthrough/Compressor.cpp @@ -42,7 +42,8 @@ auto Compressor::close() -> void { m_compressed_stream_file_writer = nullptr; } -auto Compressor::open(FileWriter& file_writer, [[maybe_unused]] int compression_level) -> void { +auto Compressor::open(FileWriter& file_writer, [[maybe_unused]] int const compression_level) + -> void { m_compressed_stream_file_writer = &file_writer; } } // namespace clp::streaming_compression::passthrough diff --git a/components/core/src/clp/streaming_compression/passthrough/Compressor.hpp b/components/core/src/clp/streaming_compression/passthrough/Compressor.hpp index a24024666..8674e8937 100644 --- a/components/core/src/clp/streaming_compression/passthrough/Compressor.hpp +++ b/components/core/src/clp/streaming_compression/passthrough/Compressor.hpp @@ -20,7 +20,7 @@ class Compressor : public ::clp::streaming_compression::Compressor { public: // Constructors OperationFailed(ErrorCode error_code, char const* const filename, int line_number) - : TraceableException(error_code, filename, line_number) {} + : TraceableException{error_code, filename, line_number} {} // Methods [[nodiscard]] auto what() const noexcept -> char const* override { @@ -39,7 +39,7 @@ class Compressor : public ::clp::streaming_compression::Compressor { auto operator=(Compressor const&) -> Compressor& = delete; Compressor(Compressor&&) noexcept = default; - auto operator=(Compressor&&) -> Compressor& = default; + auto operator=(Compressor&&) noexcept -> Compressor& = default; // Methods implementing the WriterInterface /** @@ -73,7 +73,8 @@ class Compressor : public ::clp::streaming_compression::Compressor { * @param file_writer * @param compression_level */ - auto open(FileWriter& file_writer, [[maybe_unused]] int compression_level = 0) -> void override; + auto + open(FileWriter& file_writer, [[maybe_unused]] int const compression_level) -> void override; private: // Variables diff --git a/components/core/src/clp/streaming_compression/zstd/Compressor.cpp b/components/core/src/clp/streaming_compression/zstd/Compressor.cpp index 8adc47aae..c75177201 100644 --- a/components/core/src/clp/streaming_compression/zstd/Compressor.cpp +++ b/components/core/src/clp/streaming_compression/zstd/Compressor.cpp @@ -15,11 +15,7 @@ namespace clp::streaming_compression::zstd { Compressor::Compressor() : ::clp::streaming_compression::Compressor{CompressorType::ZSTD}, - m_compressed_stream_block{}, - m_compressed_stream_file_writer{nullptr}, - m_compression_stream_contains_data{false}, - m_compression_stream{ZSTD_createCStream()}, - m_uncompressed_stream_pos{0} { + m_compression_stream{ZSTD_createCStream()} { if (nullptr == m_compression_stream) { SPDLOG_ERROR("streaming_compression::zstd::Compressor: ZSTD_createCStream() error"); throw OperationFailed(ErrorCode_Failure, __FILENAME__, __LINE__); @@ -37,7 +33,7 @@ auto Compressor::open(FileWriter& file_writer, int const compression_level) -> v // Setup compressed stream parameters auto const compressed_stream_block_size{ZSTD_CStreamOutSize()}; - m_compressed_stream_block_buffer.reserve(compressed_stream_block_size); + m_compressed_stream_block_buffer.resize(compressed_stream_block_size); m_compressed_stream_block.dst = m_compressed_stream_block_buffer.data(); m_compressed_stream_block.size = compressed_stream_block_size; @@ -147,7 +143,7 @@ auto Compressor::flush_without_ending_frame() -> void { while (true) { m_compressed_stream_block.pos = 0; - auto flush_result{ZSTD_flushStream(m_compression_stream, &m_compressed_stream_block)}; + auto const flush_result{ZSTD_flushStream(m_compression_stream, &m_compressed_stream_block)}; if (zstd_is_error(flush_result)) { SPDLOG_ERROR( "streaming_compression::zstd::Compressor: ZSTD_compressStream2() error: {}", diff --git a/components/core/src/clp/streaming_compression/zstd/Compressor.hpp b/components/core/src/clp/streaming_compression/zstd/Compressor.hpp index de41709bc..0341c8bbf 100644 --- a/components/core/src/clp/streaming_compression/zstd/Compressor.hpp +++ b/components/core/src/clp/streaming_compression/zstd/Compressor.hpp @@ -20,11 +20,12 @@ class Compressor : public ::clp::streaming_compression::Compressor { public: // Constructors OperationFailed(ErrorCode error_code, char const* const filename, int line_number) - : TraceableException(error_code, filename, line_number) {} + : TraceableException{error_code, filename, line_number} {} // Methods [[nodiscard]] auto what() const noexcept -> char const* override { - return "streaming_compression::zstd::Compressor operation failed"; + return "streaming_compression::zstd::Compressor " + "operation failed"; } }; @@ -39,7 +40,7 @@ class Compressor : public ::clp::streaming_compression::Compressor { auto operator=(Compressor const&) -> Compressor& = delete; Compressor(Compressor&&) noexcept = default; - auto operator=(Compressor&&) -> Compressor& = default; + auto operator=(Compressor&&) noexcept -> Compressor& = default; // Methods implementing the WriterInterface /** @@ -48,6 +49,7 @@ class Compressor : public ::clp::streaming_compression::Compressor { * @param data_length */ auto write(char const* data, size_t data_length) -> void override; + /** * Writes any internally buffered data to file and ends the current frame */ @@ -67,13 +69,20 @@ class Compressor : public ::clp::streaming_compression::Compressor { */ auto close() -> void override; + /** + * Initializes the compression stream with the default compression level + * @param file_writer + */ + auto open(FileWriter& file_writer) -> void override { + this->open(file_writer, cDefaultCompressionLevel); + } + /** * Initializes the compression stream with the given compression level * @param file_writer * @param compression_level */ - auto open(FileWriter& file_writer, int compression_level = cDefaultCompressionLevel) - -> void override; + auto open(FileWriter& file_writer, int const compression_level) -> void override; /** * Flushes the stream without ending the current frame @@ -82,16 +91,16 @@ class Compressor : public ::clp::streaming_compression::Compressor { private: // Variables - FileWriter* m_compressed_stream_file_writer; + FileWriter* m_compressed_stream_file_writer{nullptr}; // Compressed stream variables ZSTD_CStream* m_compression_stream; - bool m_compression_stream_contains_data; + bool m_compression_stream_contains_data{false}; - ZSTD_outBuffer m_compressed_stream_block; + ZSTD_outBuffer m_compressed_stream_block{}; std::vector m_compressed_stream_block_buffer; - size_t m_uncompressed_stream_pos; + size_t m_uncompressed_stream_pos{0}; /** * Tells if a `size_t` ZStd function result is an error code and is not `ZSTD_error_no_error` diff --git a/components/core/tests/test-StreamingCompression.cpp b/components/core/tests/test-StreamingCompression.cpp index 2849f20a9..63d0fb4d8 100644 --- a/components/core/tests/test-StreamingCompression.cpp +++ b/components/core/tests/test-StreamingCompression.cpp @@ -59,13 +59,13 @@ TEST_CASE("StreamingCompression", "[StreamingCompression]") { // Initialize buffers std::vector uncompressed_buffer{}; - uncompressed_buffer.reserve(cUncompressedDataSize); + uncompressed_buffer.resize(cUncompressedDataSize); for (size_t i{0}; i < cUncompressedDataSize; ++i) { - uncompressed_buffer.push_back((char)('a' + (i % cUncompressedDataPatternPeriod))); + uncompressed_buffer.at(i) = ((char)('a' + (i % cUncompressedDataPatternPeriod))); } std::vector decompressed_buffer{}; - decompressed_buffer.reserve(cUncompressedDataSize); + decompressed_buffer.resize(cUncompressedDataSize); // Compress FileWriter file_writer; @@ -83,7 +83,7 @@ TEST_CASE("StreamingCompression", "[StreamingCompression]") { decompressor->open(compressed_file_view.data(), compressed_file_view.size()); size_t uncompressed_bytes{0}; - for (auto chunk_size : compression_chunk_sizes) { + for (auto const chunk_size : compression_chunk_sizes) { memset(decompressed_buffer.data(), 0, cUncompressedDataSize); REQUIRE( (ErrorCode_Success