MySQL 9.4.0
Source Code Documentation
zstd_comp.h
Go to the documentation of this file.
1/* Copyright (c) 2019, 2025, 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 MYSQL_BINLOG_EVENT_COMPRESSION_ZSTD_COMP_H
25#define MYSQL_BINLOG_EVENT_COMPRESSION_ZSTD_COMP_H
26
27#define ZSTD_STATIC_LINKING_ONLY 1
28#include <zstd.h>
29
30#include "mysql/allocators/memory_resource.h" // Memory_resource
33
34struct ZSTD_outBuffer_s;
35
37
38/// Compressor class that uses the ZSTD library.
39class Zstd_comp : public Compressor {
40 public:
41 using typename Compressor::Char_t;
43 using typename Compressor::Size_t;
46 static constexpr type type_code = ZSTD;
47
48 /// The default compression level for this compressor.
50
51 Zstd_comp(const Memory_resource_t &memory_resource = Memory_resource_t());
52
53 ~Zstd_comp() override;
54
55 Zstd_comp(const Zstd_comp &) = delete;
56 Zstd_comp(const Zstd_comp &&) = delete;
57 Zstd_comp &operator=(const Zstd_comp &rhs) = delete;
58 Zstd_comp &operator=(const Zstd_comp &&rhs) = delete;
59
60 /// Set the compression level for this compressor.
61 ///
62 /// This function may be invoked at any time, but will only take
63 /// effect the next time a new frame starts, i.e., at the first call
64 /// to @c feed after the frame has been reset.
65 ///
66 /// @param compression_level the new compression level.
67 void set_compression_level(Compression_level_t compression_level);
68
69 private:
70 /// @return ZSTD
71 type do_get_type_code() const override;
72
73 /// @copydoc Compressor::do_reset
74 void do_reset() override;
75
76 /// @copydoc Compressor::do_feed
77 void do_feed(const Char_t *input_data, Size_t input_size) override;
78
79 /// @copydoc Compressor::do_compress
80 [[nodiscard]] Compress_status do_compress(
81 Managed_buffer_sequence_t &out) override;
82
83 /// @copydoc Compressor::do_finish
84 [[nodiscard]] Compress_status do_finish(
85 Managed_buffer_sequence_t &out) override;
86
87 /// @copydoc Compressor::do_get_grow_constraint_hint
88 [[nodiscard]] Grow_constraint_t do_get_grow_constraint_hint() const override;
89
90 /// Deallocate the ZSTD compression context.
91 void destroy();
92
93 /// Reset just the ZSTD compressor state, not other state.
94 void reset_compressor();
95
96 /// Make the ZSTD buffer point to the next available buffer;
97 /// allocate one if necessary.
98 ///
99 /// @param[in,out] managed_buffer_sequence owns the data and
100 /// manages the growth.
101 ///
102 /// @param[out] obuf ZSTD buffer that will be altered to point to
103 /// the next available byte in buffer_sequence.
104 ///
105 /// @retval success managed_buffer_sequence was not full, or its
106 /// capacity has been incremented successfully. obuf has been set
107 /// to point to next available byte.
108 ///
109 /// @retval out_of_memory buffer_sequence was full and an out of
110 /// memory error occurred. buffer_sequence has not been modified.
111 ///
112 /// @retval exceeds_max_size buffer_sequence was full and at its max
113 /// capacity. buffer_sequence has not been modified.
114 [[nodiscard]] Compress_status get_obuf(
115 Managed_buffer_sequence_t &managed_buffer_sequence,
116 ZSTD_outBuffer_s &obuf);
117
118 /// Account for having written to the output buffer.
119 ///
120 /// This moves the read/write boundary in the
121 /// Managed_buffer_sequence, and also increments
122 /// m_total_output_size.
123 ///
124 /// @param managed_buffer_sequence The buffer sequence that has
125 /// been written to.
126 ///
127 /// @param delta The number of bytes that have been written.
128 void move_position(Managed_buffer_sequence_t &managed_buffer_sequence,
129 Size_t delta);
130
131 /// The ZSTD compression context.
132 ZSTD_CStream *m_ctx{nullptr};
133
134 /// The input buffer.
135 ZSTD_inBuffer m_ibuf{nullptr, 0, 0};
136
137 /// Value used to indicate that no compression level has been specified.
139
140 /// True when @c compress has been called and neither @c finish nor
141 /// @c reset has yet been called.
142 bool m_started{false};
143
144 /// Compression level that was set in the @c m_ctx object.
147
148 /// Compression level that was given in @c set_compression_level
150
151 /// Instrumented memory allocator object
153
154 /// ZSTD memory allocator objects and functions
155 ZSTD_customMem m_zstd_custom_mem;
156 static void *zstd_mem_res_alloc(void *opaque, size_t size);
157 static void zstd_mem_res_free(void *opaque, void *address);
158};
159
160} // namespace mysql::binlog::event::compression
161
162#endif // MYSQL_BINLOG_EVENT_COMPRESSION_ZSTD_COMP_H
Container class that provides a sequence of buffers to the caller.
Polymorphism-free memory resource class with custom allocator and deallocator functions.
Definition: memory_resource.h:88
Abstract base class for compressors.
Definition: compressor.h:79
Managed_buffer_sequence_t::Size_t Size_t
Definition: compressor.h:84
Managed_buffer_sequence_t::Char_t Char_t
Definition: compressor.h:83
mysql::containers::buffers::Managed_buffer_sequence<> Managed_buffer_sequence_t
Definition: compressor.h:82
Compressor class that uses the ZSTD library.
Definition: zstd_comp.h:39
ZSTD_inBuffer m_ibuf
The input buffer.
Definition: zstd_comp.h:135
void reset_compressor()
Reset just the ZSTD compressor state, not other state.
Definition: zstd_comp.cpp:67
Compress_status do_compress(Managed_buffer_sequence_t &out) override
Implement compress.
Definition: zstd_comp.cpp:94
bool m_started
True when compress has been called and neither finish nor reset has yet been called.
Definition: zstd_comp.h:142
static constexpr Compression_level_t default_compression_level
The default compression level for this compressor.
Definition: zstd_comp.h:49
mysql::allocators::Memory_resource Memory_resource_t
Definition: zstd_comp.h:44
void move_position(Managed_buffer_sequence_t &managed_buffer_sequence, Size_t delta)
Account for having written to the output buffer.
Definition: zstd_comp.cpp:189
static constexpr Compression_level_t uninitialized_compression_level
Value used to indicate that no compression level has been specified.
Definition: zstd_comp.h:138
Zstd_comp & operator=(const Zstd_comp &&rhs)=delete
~Zstd_comp() override
Definition: zstd_comp.cpp:37
Compress_status do_finish(Managed_buffer_sequence_t &out) override
Implement finish.
Definition: zstd_comp.cpp:161
ZSTD_customMem m_zstd_custom_mem
ZSTD memory allocator objects and functions.
Definition: zstd_comp.h:155
Zstd_comp & operator=(const Zstd_comp &rhs)=delete
type do_get_type_code() const override
Definition: zstd_comp.cpp:47
static void * zstd_mem_res_alloc(void *opaque, size_t size)
Definition: zstd_comp.cpp:225
void set_compression_level(Compression_level_t compression_level)
Set the compression level for this compressor.
Definition: zstd_comp.cpp:49
Compress_status get_obuf(Managed_buffer_sequence_t &managed_buffer_sequence, ZSTD_outBuffer_s &obuf)
Make the ZSTD buffer point to the next available buffer; allocate one if necessary.
Definition: zstd_comp.cpp:194
void do_feed(const Char_t *input_data, Size_t input_size) override
Implement feed.
Definition: zstd_comp.cpp:81
void do_reset() override
Implement reset.
Definition: zstd_comp.cpp:53
Compression_level_t m_next_compression_level
Compression level that was given in set_compression_level.
Definition: zstd_comp.h:149
Compression_level_t m_current_compression_level
Compression level that was set in the m_ctx object.
Definition: zstd_comp.h:145
static constexpr type type_code
Definition: zstd_comp.h:46
int Compression_level_t
Definition: zstd_comp.h:45
Zstd_comp(const Memory_resource_t &memory_resource=Memory_resource_t())
Definition: zstd_comp.cpp:32
static void zstd_mem_res_free(void *opaque, void *address)
Definition: zstd_comp.cpp:230
Memory_resource_t m_memory_resource
Instrumented memory allocator object.
Definition: zstd_comp.h:152
ZSTD_CStream * m_ctx
The ZSTD compression context.
Definition: zstd_comp.h:132
void destroy()
Deallocate the ZSTD compression context.
Definition: zstd_comp.cpp:39
Grow_constraint_t do_get_grow_constraint_hint() const override
Implement get_grow_constraint_hint.
Definition: zstd_comp.cpp:217
Description of a heuristic to determine how much memory to allocate.
Definition: grow_constraint.h:64
Owned, non-contiguous, growable memory buffer.
Definition: managed_buffer_sequence.h:114
Class that wraps resources in a polymorphic manner.
Definition: base.cpp:27
@ ZSTD
Definition: base.h:42
Grow_status
Error statuses for classes that use Grow_calculator.
Definition: grow_status.h:38
size_t size(const char *const c)
Definition: base64.h:46