MySQL 9.4.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
decompressor.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_DECOMPRESSOR_H
25#define MYSQL_BINLOG_EVENT_COMPRESSION_DECOMPRESSOR_H
26
28#include "mysql/binlog/event/compression/decompress_status.h" // Decompress_status
29#include "mysql/containers/buffers/grow_constraint.h" // Grow_constraint
30#include "mysql/containers/buffers/managed_buffer.h" // Managed_buffer
31
33
34/// Abstract base class for decompressors.
35///
36/// Each subclass normally corresponds to a compression
37/// algorithm, and maintains the algorithm-specific state for it.
38///
39/// An instance of this class can be reused to decompress several
40/// different *frames*. Each frame input may be split into multiple
41/// pieces. @see Compressor.
42///
43/// To decompress one or more frames in one piece, use this API as
44/// follows:
45/// 1. Call @c feed to provide all input
46/// 2. Repeatedly call @c decompress, each time producing as much
47/// output as you need.
48///
49/// To decompress one or more frames in multiple pieces, use this API
50/// as follows:
51/// 1. Repeatedly call @c decompress, each time producing as much
52/// output as you need. Whenever it returns eof or truncated, call
53/// @c feed to provide more input, and try again.
54///
55/// This class uses a @c buffer::Managed_buffer to store output.
57 public:
58 using Char_t = unsigned char;
62
63 private:
65
66 public:
67 Decompressor() = default;
68 Decompressor(const Decompressor &) = delete;
69 Decompressor(const Decompressor &&) = delete;
70 const Decompressor &operator=(const Decompressor &) = delete;
71 const Decompressor &operator=(const Decompressor &&) = delete;
72 virtual ~Decompressor() = default;
73
74 /// @return the compression type.
75 type get_type_code() const;
76
77 /// Reset the frame.
78 ///
79 /// This cancels the current frame and starts a new one.
80 void reset();
81
82 /// Submit data to decompress.
83 ///
84 /// @param input_data The buffer of input data that this decompressor
85 /// will read.
86 ///
87 /// @param input_size The size of the input buffer.
88 template <class Input_char_t>
89 void feed(const Input_char_t *input_data, Size_t input_size) {
90 feed_char_t(reinterpret_cast<const Char_t *>(input_data), input_size);
91 }
92
93 /// Decompress an exact, given number of bytes.
94 ///
95 /// @param out The output buffer. This function first grows the
96 /// write part of `out` to at least `output_size` bytes, then
97 /// decompresses into the write part, and then moves the
98 /// decompressed bytes from the beginning of the write part to the
99 /// end of the read part.
100 ///
101 /// @param output_size Number of bytes to decompress.
102 ///
103 /// @retval success Decompression succeeded. The requested bytes
104 /// are available in @c out.
105 ///
106 /// @retval eof There were no more bytes to decompress. The out
107 /// buffer has not been changed and the frame has not been reset.
108 ///
109 /// @retval truncated All input was consumed, but produced less
110 /// output than requested. The out buffer has been changed and the
111 /// frame has not been reset. The caller may resume decompression
112 /// after calling @c feed.
113 ///
114 /// @retval corrupted The compression library detected that the data
115 /// was corrupted. The frame has been reset.
116 ///
117 /// @retval out_of_memory The operation failed due to an out of
118 /// memory error. The frame has been reset.
119 ///
120 /// @retval exceeds_max_capacity The requested size exceeds the
121 /// maximium capacity configured for the Managed_buffer. The out
122 /// buffer has not been changed and the frame has not been reset.
123 /// The caller may resume decompression after increasing the
124 /// capacity, or resetting the buffer (perhaps after moving the data
125 /// elsewhere), or using a different output buffer, or similar.
127 Size_t output_size);
128
129 /// Decompress an exact, given number of bytes.
130 ///
131 /// @param out The output buffer.
132 ///
133 /// @param output_size The number of bytes to decompress.
134 ///
135 /// @returns a pair where the first component is a Decompress_status
136 /// value and the second component is the number of bytes that were
137 /// successfully stored in out. The Decompress_status has the same
138 /// possible values as for @c decompress(Managed_buffer_t,
139 /// output_size), except it cannot take the value
140 /// exceeds_max_capacity. The size will be equal to output_size if
141 /// the status is success; strictly between 0 and output_size if the
142 /// status is truncated; and 0 for the other cases.
143 [[nodiscard]] std::pair<Decompress_status, Size_t> decompress(
144 Char_t *out, Size_t output_size);
145
146 /// Return a `Grow_constraint` that may be used with the
147 /// Managed_buffer storing the output, in order to optimize memory
148 /// usage for a particular compression algorithm.
150
151 private:
152 /// Implement @c get_type_code.
153 virtual type do_get_type_code() const = 0;
154
155 /// Implement @c do_reset.
156 virtual void do_reset() = 0;
157
158 void feed_char_t(const Char_t *input_data, Size_t input_size);
159
160 /// Implement @c feed.
161 virtual void do_feed(const Char_t *input_data, Size_t input_size) = 0;
162
163 /// Implement @c decompress.
164 ///
165 /// This differs from @c decompress in that it does not have to
166 /// reset the frame when returning out_of_memory or corrupted; the
167 /// caller does that.
168 [[nodiscard]] virtual std::pair<Decompress_status, Size_t> do_decompress(
169 Char_t *out, Size_t output_size) = 0;
170
171 /// Implement @c get_grow_constraint_hint.
172 ///
173 /// In this base class, the function returns a default-constructed
174 /// Grow_constraint, i.e., one which does not limit the
175 /// Grow_calculator.
177};
178
179} // namespace mysql::binlog::event::compression
180
181#endif // ifndef MYSQL_BINLOG_EVENT_COMPRESSION_DECOMPRESSOR_H
Abstract base class for decompressors.
Definition: decompressor.h:56
virtual std::pair< Decompress_status, Size_t > do_decompress(Char_t *out, Size_t output_size)=0
Implement decompress.
mysql::containers::buffers::Managed_buffer< Char_t > Managed_buffer_t
Definition: decompressor.h:60
mysql::containers::buffers::Buffer_view< Char_t >::Size_t Size_t
Definition: decompressor.h:59
void feed(const Input_char_t *input_data, Size_t input_size)
Submit data to decompress.
Definition: decompressor.h:89
Grow_constraint_t get_grow_constraint_hint() const
Return a Grow_constraint that may be used with the Managed_buffer storing the output,...
Definition: decompressor.cpp:72
Decompressor(const Decompressor &&)=delete
void reset()
Reset the frame.
Definition: decompressor.cpp:30
virtual type do_get_type_code() const =0
Implement get_type_code.
Decompressor(const Decompressor &)=delete
virtual Grow_constraint_t do_get_grow_constraint_hint() const
Implement get_grow_constraint_hint.
Definition: decompressor.cpp:76
const Decompressor & operator=(const Decompressor &&)=delete
virtual void do_reset()=0
Implement do_reset.
unsigned char Char_t
Definition: decompressor.h:58
const Decompressor & operator=(const Decompressor &)=delete
mysql::containers::buffers::Grow_constraint Grow_constraint_t
Definition: decompressor.h:61
type get_type_code() const
Definition: decompressor.cpp:28
virtual void do_feed(const Char_t *input_data, Size_t input_size)=0
Implement feed.
Decompress_status decompress(Managed_buffer_t &out, Size_t output_size)
Decompress an exact, given number of bytes.
Definition: decompressor.cpp:40
void feed_char_t(const Char_t *input_data, Size_t input_size)
Definition: decompressor.cpp:35
std::size_t Size_t
The 'size' type.
Definition: buffer_view.h:55
Description of a heuristic to determine how much memory to allocate.
Definition: grow_constraint.h:64
Owned, growable, contiguous memory buffer.
Definition: managed_buffer.h:107
Container class that provides a contiguous memory buffer to the caller, which the caller can request ...
Definition: base.cpp:27
Decompress_status
Definition: decompress_status.h:32
Grow_status
Error statuses for classes that use Grow_calculator.
Definition: grow_status.h:38