Debian Codegraph @

libbinlogevents/src/compression/zstd_comp.cpp

raw
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 #include <compression/zstd_comp.h>
25 // #include <my_byteorder.h> // TODO: fix this include
26 #include <algorithm> // std::min
27 #include "scope_guard.h" // Scope_guard
28 #include "wrapper_functions.h" // BAPI_TRACE
29
30 namespace binary_log::transaction::compression {
31
32 Zstd_comp::~Zstd_comp() { destroy(); }
33
34 void Zstd_comp::destroy() {
35 BAPI_TRACE;
36
37 ZSTD_freeCStream(m_ctx); // Can't fail
38 m_ctx = nullptr;
39 m_current_compression_level = uninitialized_compression_level;
40 }
41
42 type Zstd_comp::do_get_type_code() const { return type_code; }
43
44 void Zstd_comp::set_compression_level(Compression_level_t compression_level) {
45 m_next_compression_level = compression_level;
46 }
47
48 void Zstd_comp::do_reset() {
49 BAPI_TRACE;
50
51 reset_compressor();
52
53 // Clear pointer to input data.
54 m_ibuf.src = nullptr;
55 m_ibuf.size = 0;
56 m_ibuf.pos = 0;
57
58 // Allow next call to do_compress to change compression level
59 m_started = false;
60 }
61
62 void Zstd_comp::reset_compressor() {
63 // If context is not allocated, defer initialization until do_compress.
64 if (m_ctx != nullptr) {
65 // Try to initialize compression context; if it fails, free and
66 // set it to nullptr.
67 auto init_status = ZSTD_initCStream(m_ctx, m_next_compression_level);
68 if (ZSTD_isError(init_status) != 0) {
69 BAPI_LOG("info", BAPI_VAR(ZSTD_getErrorName(init_status)));
70 destroy();
71 } else
72 m_current_compression_level = m_next_compression_level;
73 }
74 }
75
76 void Zstd_comp::do_feed(const Char_t *input_data, size_t input_size) {
77 BAPI_TRACE;
78
79 // Protect against two successive calls to `feed` without a call to
80 // `decompress` between them.
81 assert(m_ibuf.pos == m_ibuf.size);
82
83 // Store pointer to input data.
84 m_ibuf.src = static_cast<const void *>(input_data);
85 m_ibuf.size = input_size;
86 m_ibuf.pos = 0;
87 }
88
89 Compress_status Zstd_comp::do_compress(Managed_buffer_sequence_t &out) {
90 BAPI_TRACE;
91
92 // Create ZSTD compression context if not already created.
93 if (m_ctx == nullptr) {
94 m_ctx = ZSTD_createCStream();
95 if (m_ctx == nullptr) return Compress_status::out_of_memory;
96 }
97
98 // First invocation of this function for this frame.
99 if (!m_started) {
100 // Update the compression level if needed.
101 if (m_next_compression_level != m_current_compression_level) {
102 BAPI_LOG("info", "reset compressor to update compression level from "
103 << m_current_compression_level << " to "
104 << m_next_compression_level);
105 reset_compressor();
106
107 // reset_compressor may get OOM error, in which case m_ctx is
108 // nullptr and we have to return out_of_memory.
109 if (m_ctx == nullptr) return Compress_status::out_of_memory;
110 }
111 #if ZSTD_VERSION_NUMBER >= 10400
112 // If user has set a "pledged input size", pass that to Zstd,
113 // allowing it to optimize memory usage. The API is only
114 // available in ZSTD version 1.4.0 and higher.
115 auto pledged_input_size = get_pledged_input_size();
116 if (pledged_input_size != pledged_input_size_unset)
117 ZSTD_CCtx_setPledgedSrcSize(m_ctx, pledged_input_size);
118 #endif
119 }
120 m_started = true;
121
122 // Consume all input.
123 while (m_ibuf.pos < m_ibuf.size) {
124 // Make place for more output.
125 ZSTD_outBuffer obuf;
126 auto grow_status = get_obuf(out, obuf);
127 if (grow_status != Compress_status::success) return grow_status;
128
129 // Consume more input and possibly output some more output.
130 auto zstd_status = ZSTD_compressStream(m_ctx, &obuf, &m_ibuf);
131 BAPI_LOG("info", BAPI_VAR(zstd_status) << " " << BAPI_VAR(obuf.pos));
132 if (ZSTD_isError(zstd_status) != 0) {
133 BAPI_LOG("info", BAPI_VAR(ZSTD_getErrorName(zstd_status)));
134 return Compress_status::out_of_memory;
135 }
136
137 // Move the position in the Managed_buffer_sequence.
138 move_position(out, obuf.pos);
139 }
140
141 BAPI_LOG("info", BAPI_VAR(m_ibuf.pos) << " " << BAPI_VAR(m_ibuf.size));
142
143 return Compress_status::success;
144 }
145
146 Compress_status Zstd_comp::do_finish(Managed_buffer_sequence_t &out) {
147 BAPI_TRACE;
148
149 size_t zstd_status{0};
150 // Produce all output
151 do {
152 // Make place for more output.
153 ZSTD_outBuffer obuf;
154 auto grow_status = get_obuf(out, obuf);
155 if (grow_status != Compress_status::success) return grow_status;
156
157 // Request ZSTD to end the stream after the current input, and to
158 // flush as much as possible of internal buffers to the output.
159 zstd_status = ZSTD_endStream(m_ctx, &obuf);
160
161 BAPI_LOG("info", BAPI_VAR(zstd_status) << " " << BAPI_VAR(obuf.pos));
162 if (ZSTD_isError(zstd_status) != 0) {
163 BAPI_LOG("info", BAPI_VAR(ZSTD_getErrorName(zstd_status)));
164 return Compress_status::out_of_memory;
165 }
166 // Move the position mark in the Managed_buffer_sequence.
167 move_position(out, obuf.pos);
168 } while (zstd_status > 0);
169 BAPI_LOG("info", BAPI_VAR(m_ibuf.pos) << " " << BAPI_VAR(m_ibuf.size));
170
171 return Compress_status::success;
172 }
173
174 void Zstd_comp::move_position(
175 Managed_buffer_sequence_t &managed_buffer_sequence, Size_t delta) {
176 managed_buffer_sequence.increase_position(delta);
177 }
178
179 [[NODISCARD]] Compress_status Zstd_comp::get_obuf(
180 Managed_buffer_sequence_t &managed_buffer_sequence, ZSTD_outBuffer &obuf) {
181 BAPI_TRACE;
182 auto &write_part = managed_buffer_sequence.write_part();
183 if (write_part.size() == 0) {
184 // Reserve at least one byte. Why one? Because the only strict
185 // requirement on this function is to get more space, i.e., at
186 // least one byte. Then there is logic encoded in the
187 // Grow_calculator and in the parameters for the Grow_calculator
188 // which typically make it allocate more. Those are
189 // performance-improving heuristics and not strict requirements.
190 auto grow_status = managed_buffer_sequence.reserve_write_size(1);
191 if (grow_status != Compress_status::success) return grow_status;
192 }
193 // Write output
194 auto buffer_it = write_part.begin();
195 assert(buffer_it != write_part.end());
196 obuf.dst = buffer_it->data();
197 obuf.size = buffer_it->size();
198 obuf.pos = 0;
199 return Compress_status::success;
200 }
201
202 Compressor::Grow_constraint_t Zstd_comp::do_get_grow_constraint_hint() const {
203 Grow_constraint_t ret;
204 ret.set_grow_increment(ZSTD_CStreamOutSize());
205 if (get_pledged_input_size() != pledged_input_size_unset)
206 ret.set_max_size(ZSTD_compressBound(get_pledged_input_size()));
207 return ret;
208 }
209
210 } // namespace binary_log::transaction::compression