From 740bc1c1216f999c881dffd49564eeabcf1d4bbd Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 2 Dec 2024 10:20:12 -0500 Subject: [PATCH] Address review concern --- .../streaming_compression/lzma/Compressor.cpp | 26 +++++++++++++------ .../streaming_compression/lzma/Compressor.hpp | 15 +++-------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/components/core/src/clp/streaming_compression/lzma/Compressor.cpp b/components/core/src/clp/streaming_compression/lzma/Compressor.cpp index 6092207d6..a1d5dfaa2 100644 --- a/components/core/src/clp/streaming_compression/lzma/Compressor.cpp +++ b/components/core/src/clp/streaming_compression/lzma/Compressor.cpp @@ -14,13 +14,21 @@ #include "../../type_utils.hpp" #include "Constants.hpp" -namespace clp::streaming_compression::lzma { -auto Compressor::init_lzma_encoder(lzma_stream* strm, int compression_level, size_t dict_size) - -> void { +namespace { +using clp::streaming_compression::lzma::Compressor; + +/** + * Initialize the Lzma compression stream + * @param strm A pre-allocated `lzma_stream` object + * @param compression_level + * @param dict_size Dictionary size that indicates how many bytes of the + * recently processed uncompressed data is kept in memory + */ +auto init_lzma_encoder(lzma_stream* strm, int compression_level, size_t dict_size) -> void { lzma_options_lzma options; if (0 != lzma_lzma_preset(&options, compression_level)) { SPDLOG_ERROR("Failed to initialize LZMA options' compression level."); - throw OperationFailed(ErrorCode_BadParam, __FILENAME__, __LINE__); + throw Compressor::OperationFailed(clp::ErrorCode_BadParam, __FILENAME__, __LINE__); } options.dict_size = dict_size; std::array filters{{ @@ -64,9 +72,11 @@ auto Compressor::init_lzma_encoder(lzma_stream* strm, int compression_level, siz } SPDLOG_ERROR("Error initializing the encoder: {} (error code {})", msg, static_cast(rc)); - throw OperationFailed(ErrorCode_BadParam, __FILENAME__, __LINE__); + throw Compressor::OperationFailed(clp::ErrorCode_BadParam, __FILENAME__, __LINE__); } +} // namespace +namespace clp::streaming_compression::lzma { auto Compressor::open(FileWriter& file_writer, int compression_level) -> void { if (nullptr != m_compressed_stream_file_writer) { throw OperationFailed(ErrorCode_NotReady, __FILENAME__, __LINE__); @@ -186,17 +196,17 @@ auto Compressor::run_lzma(lzma_action action) -> void { // Write output buffer to file if it's full if (0 == m_compression_stream.avail_out) { - pipe_data(); + flush_stream_output_block_buffer(); } } // Write remaining compressed data if (m_compression_stream.avail_out < cCompressedStreamBlockBufferSize) { - pipe_data(); + flush_stream_output_block_buffer(); } } -auto Compressor::pipe_data() -> void { +auto Compressor::flush_stream_output_block_buffer() -> void { m_compressed_stream_file_writer->write( clp::size_checked_pointer_cast(m_compressed_stream_block_buffer.data()), cCompressedStreamBlockBufferSize - m_compression_stream.avail_out diff --git a/components/core/src/clp/streaming_compression/lzma/Compressor.hpp b/components/core/src/clp/streaming_compression/lzma/Compressor.hpp index 5b1adb404..4afdce36a 100644 --- a/components/core/src/clp/streaming_compression/lzma/Compressor.hpp +++ b/components/core/src/clp/streaming_compression/lzma/Compressor.hpp @@ -86,15 +86,6 @@ class Compressor : public ::clp::streaming_compression::Compressor { auto open(FileWriter& file_writer, int compression_level) -> void; private: - /** - * Initialize the Lzma compression stream - * @param strm A pre-allocated `lzma_stream` object - * @param compression_level - * @param dict_size Dictionary size that indicates how many bytes of the - * recently processed uncompressed data is kept in memory - */ - static auto - init_lzma_encoder(lzma_stream* strm, int compression_level, size_t dict_size) -> void; static constexpr size_t cCompressedStreamBlockBufferSize{4096}; // 4KiB /** @@ -108,10 +99,10 @@ class Compressor : public ::clp::streaming_compression::Compressor { auto run_lzma(lzma_action action) -> void; /** - * Pipes the current compressed data in the lzma buffer to the output file - * and reset the compression buffer to receive new data. + * Flushes the current compressed data in the lzma output buffer to the + * output file handler. Reset the compression buffer to receive new data. */ - auto pipe_data() -> void; + auto flush_stream_output_block_buffer() -> void; // Variables FileWriter* m_compressed_stream_file_writer{nullptr};