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