Skip to content

Commit

Permalink
Address review concern
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill-hbrhbr committed Dec 2, 2024
1 parent 26b0663 commit 740bc1c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
26 changes: 18 additions & 8 deletions components/core/src/clp/streaming_compression/lzma/Compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<lzma_filter, 2> filters{{
Expand Down Expand Up @@ -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<int>(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__);
Expand Down Expand Up @@ -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<char>(m_compressed_stream_block_buffer.data()),
cCompressedStreamBlockBufferSize - m_compression_stream.avail_out
Expand Down
15 changes: 3 additions & 12 deletions components/core/src/clp/streaming_compression/lzma/Compressor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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};
Expand Down

0 comments on commit 740bc1c

Please sign in to comment.