| 1 |
/* Copyright (c) 2019, 2026, Oracle and/or its affiliates. |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License, version 2.0, |
| 5 |
as published by the Free Software Foundation. |
| 6 |
|
| 7 |
This program is designed to work with certain software (including |
| 8 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 9 |
as designated in a particular file or component or in included license |
| 10 |
documentation. The authors of MySQL hereby grant you an additional |
| 11 |
permission to link the program and your derivative works with the |
| 12 |
separately licensed software that they have either included with |
| 13 |
the program or referenced in the documentation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License, version 2.0, for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 |
|
| 24 |
#ifndef LIBBINLOGEVENTS_COMPRESSION_ZSTD_COMP_H_ |
| 25 |
#define LIBBINLOGEVENTS_COMPRESSION_ZSTD_COMP_H_ |
| 26 |
|
| 27 |
#include <zstd.h> |
| 28 |
|
| 29 |
#include "compressor.h" |
| 30 |
#include "libbinlogevents/include/buffer/buffer_sequence_view.h" |
| 31 |
#include "libbinlogevents/include/nodiscard.h" |
| 32 |
|
| 33 |
struct ZSTD_outBuffer_s; |
| 34 |
|
| 35 |
namespace binary_log::transaction::compression { |
| 36 |
|
| 37 |
/// Compressor class that uses the ZSTD library. |
| 38 |
class Zstd_comp : public Compressor { |
| 39 |
public: |
| 40 |
using typename Compressor::Char_t; |
| 41 |
using typename Compressor::Managed_buffer_sequence_t; |
| 42 |
using typename Compressor::Size_t; |
| 43 |
using Compression_level_t = int; |
| 44 |
static constexpr type type_code = ZSTD; |
| 45 |
|
| 46 |
/// The default compression level for this compressor. |
| 47 |
static constexpr Compression_level_t default_compression_level = 3; |
| 48 |
|
| 49 |
Zstd_comp() = default; |
| 50 |
|
| 51 |
~Zstd_comp() override; |
| 52 |
|
| 53 |
Zstd_comp(const Zstd_comp &) = delete; |
| 54 |
Zstd_comp(const Zstd_comp &&) = delete; |
| 55 |
Zstd_comp &operator=(const Zstd_comp &rhs) = delete; |
| 56 |
Zstd_comp &operator=(const Zstd_comp &&rhs) = delete; |
| 57 |
|
| 58 |
/// Set the compression level for this compressor. |
| 59 |
/// |
| 60 |
/// This function may be invoked at any time, but will only take |
| 61 |
/// effect the next time a new frame starts, i.e., at the first call |
| 62 |
/// to @c feed after the frame has been reset. |
| 63 |
/// |
| 64 |
/// @param compression_level the new compression level. |
| 65 |
void set_compression_level(Compression_level_t compression_level); |
| 66 |
|
| 67 |
private: |
| 68 |
/// @return ZSTD |
| 69 |
type do_get_type_code() const override; |
| 70 |
|
| 71 |
/// @copydoc Compressor::do_reset |
| 72 |
void do_reset() override; |
| 73 |
|
| 74 |
/// @copydoc Compressor::do_feed |
| 75 |
void do_feed(const Char_t *input_data, Size_t input_size) override; |
| 76 |
|
| 77 |
/// @copydoc Compressor::do_compress |
| 78 |
[[NODISCARD]] Compress_status do_compress( |
| 79 |
Managed_buffer_sequence_t &out) override; |
| 80 |
|
| 81 |
/// @copydoc Compressor::do_finish |
| 82 |
[[NODISCARD]] Compress_status do_finish( |
| 83 |
Managed_buffer_sequence_t &out) override; |
| 84 |
|
| 85 |
/// @copydoc Compressor::do_get_grow_constraint_hint |
| 86 |
[[NODISCARD]] Grow_constraint_t do_get_grow_constraint_hint() const override; |
| 87 |
|
| 88 |
/// Deallocate the ZSTD compression context. |
| 89 |
void destroy(); |
| 90 |
|
| 91 |
/// Reset just the ZSTD compressor state, not other state. |
| 92 |
void reset_compressor(); |
| 93 |
|
| 94 |
/// Make the ZSTD buffer point to the next available buffer; |
| 95 |
/// allocate one if necessary. |
| 96 |
/// |
| 97 |
/// @param[in,out] managed_buffer_sequence owns the data and |
| 98 |
/// manages the growth. |
| 99 |
/// |
| 100 |
/// @param[out] obuf ZSTD buffer that will be altered to point to |
| 101 |
/// the next available byte in buffer_sequence. |
| 102 |
/// |
| 103 |
/// @retval success managed_buffer_sequence was not full, or its |
| 104 |
/// capacity has been incremented successfully. obuf has been set |
| 105 |
/// to point to next available byte. |
| 106 |
/// |
| 107 |
/// @retval out_of_memory buffer_sequence was full and an out of |
| 108 |
/// memory error occurred. buffer_sequence has not been modified. |
| 109 |
/// |
| 110 |
/// @retval exceeds_max_size buffer_sequence was full and at its max |
| 111 |
/// capacity. buffer_sequence has not been modified. |
| 112 |
[[NODISCARD]] Compress_status get_obuf( |
| 113 |
Managed_buffer_sequence_t &managed_buffer_sequence, |
| 114 |
ZSTD_outBuffer_s &obuf); |
| 115 |
|
| 116 |
/// Account for having written to the output buffer. |
| 117 |
/// |
| 118 |
/// This moves the read/write boundary in the |
| 119 |
/// Managed_buffer_sequence, and also increments |
| 120 |
/// m_total_output_size. |
| 121 |
/// |
| 122 |
/// @param managed_buffer_sequence The buffer sequence that has |
| 123 |
/// been written to. |
| 124 |
/// |
| 125 |
/// @param delta The number of bytes that have been written. |
| 126 |
void move_position(Managed_buffer_sequence_t &managed_buffer_sequence, |
| 127 |
Size_t delta); |
| 128 |
|
| 129 |
/// The ZSTD compression context. |
| 130 |
ZSTD_CStream *m_ctx{nullptr}; |
| 131 |
|
| 132 |
/// The input buffer. |
| 133 |
ZSTD_inBuffer m_ibuf{nullptr, 0, 0}; |
| 134 |
|
| 135 |
/// Value used to indicate that no compression level has been specified. |
| 136 |
static constexpr Compression_level_t uninitialized_compression_level{0}; |
| 137 |
|
| 138 |
/// True when @c compress has been called and neither @c finish nor |
| 139 |
/// @c reset has yet been called. |
| 140 |
bool m_started{false}; |
| 141 |
|
| 142 |
/// Compression level that was set in the @c m_ctx object. |
| 143 |
Compression_level_t m_current_compression_level{ |
| 144 |
uninitialized_compression_level}; |
| 145 |
|
| 146 |
/// Compression level that was given in @c set_compression_level |
| 147 |
Compression_level_t m_next_compression_level{default_compression_level}; |
| 148 |
}; |
| 149 |
|
| 150 |
} // namespace binary_log::transaction::compression |
| 151 |
|
| 152 |
#endif // ifndef LIBBINLOGEVENTS_COMPRESSION_ZSTD_COMP_H_ |