MySQL 8.0.37
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 LIBBINLOGEVENTS_COMPRESSION_DECOMPRESSOR_H_
25#define LIBBINLOGEVENTS_COMPRESSION_DECOMPRESSOR_H_
26
27#include "base.h" // type
28#include "decompress_status.h" // Decompress_status
29#include "libbinlogevents/include/buffer/grow_constraint.h" // Grow_constraint
30#include "libbinlogevents/include/buffer/managed_buffer.h" // Managed_buffer
31#include "libbinlogevents/include/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;
63
64 private:
66
67 public:
68 Decompressor() = default;
69 Decompressor(const Decompressor &) = delete;
70 Decompressor(const Decompressor &&) = delete;
71 const Decompressor &operator=(const Decompressor &) = delete;
72 const Decompressor &operator=(const Decompressor &&) = delete;
73 virtual ~Decompressor() = default;
74
75 /// @return the compression type.
76 type get_type_code() const;
77
78 /// Reset the frame.
79 ///
80 /// This cancels the current frame and starts a new one.
81 void reset();
82
83 /// Submit data to decompress.
84 ///
85 /// @param input_data The buffer of input data that this decompressor
86 /// will read.
87 ///
88 /// @param input_size The size of the input buffer.
89 template <class Input_char_t>
90 void feed(const Input_char_t *input_data, Size_t input_size) {
91 feed_char_t(reinterpret_cast<const Char_t *>(input_data), input_size);
92 }
93
94 /// Decompress an exact, given number of bytes.
95 ///
96 /// @param out The output buffer. This function first grows the
97 /// write part of `out` to at least `output_size` bytes, then
98 /// decompresses into the write part, and then moves the
99 /// decompressed bytes from the beginning of the write part to the
100 /// end of the read part.
101 ///
102 /// @param output_size Number of bytes to decompress.
103 ///
104 /// @retval success Decompression succeeded. The requested bytes
105 /// are available in @c out.
106 ///
107 /// @retval eof There were no more bytes to decompress. The out
108 /// buffer has not been changed and the frame has not been reset.
109 ///
110 /// @retval truncated All input was consumed, but produced less
111 /// output than requested. The out buffer has been changed and the
112 /// frame has not been reset. The caller may resume decompression
113 /// after calling @c feed.
114 ///
115 /// @retval corrupted The compression library detected that the data
116 /// was corrupted. The frame has been reset.
117 ///
118 /// @retval out_of_memory The operation failed due to an out of
119 /// memory error. The frame has been reset.
120 ///
121 /// @retval exceeds_max_capacity The requested size exceeds the
122 /// maximium capacity configured for the Managed_buffer. The out
123 /// buffer has not been changed and the frame has not been reset.
124 /// The caller may resume decompression after increasing the
125 /// capacity, or resetting the buffer (perhaps after moving the data
126 /// elsewhere), or using a different output buffer, or similar.
128 Size_t output_size);
129
130 /// Decompress an exact, given number of bytes.
131 ///
132 /// @param out The output buffer.
133 ///
134 /// @param output_size The number of bytes to decompress.
135 ///
136 /// @returns a pair where the first component is a Decompress_status
137 /// value and the second component is the number of bytes that were
138 /// successfully stored in out. The Decompress_status has the same
139 /// possible values as for @c decompress(Managed_buffer_t,
140 /// output_size), except it cannot take the value
141 /// exceeds_max_capacity. The size will be equal to output_size if
142 /// the status is success; strictly between 0 and output_size if the
143 /// status is truncated; and 0 for the other cases.
144 [[NODISCARD]] std::pair<Decompress_status, Size_t> decompress(
145 Char_t *out, Size_t output_size);
146
147 /// Return a `Grow_constraint` that may be used with the
148 /// Managed_buffer storing the output, in order to optimize memory
149 /// usage for a particular compression algorithm.
151
152 private:
153 /// Implement @c get_type_code.
154 virtual type do_get_type_code() const = 0;
155
156 /// Implement @c do_reset.
157 virtual void do_reset() = 0;
158
159 void feed_char_t(const Char_t *input_data, Size_t input_size);
160
161 /// Implement @c feed.
162 virtual void do_feed(const Char_t *input_data, Size_t input_size) = 0;
163
164 /// Implement @c decompress.
165 ///
166 /// This differs from @c decompress in that it does not have to
167 /// reset the frame when returning out_of_memory or corrupted; the
168 /// caller does that.
169 [[NODISCARD]] virtual std::pair<Decompress_status, Size_t> do_decompress(
170 Char_t *out, Size_t output_size) = 0;
171
172 /// Implement @c get_grow_constraint_hint.
173 ///
174 /// In this base class, the function returns a default-constructed
175 /// Grow_constraint, i.e., one which does not limit the
176 /// Grow_calculator.
178};
179
180} // namespace binary_log::transaction::compression
181
182#endif // ifndef LIBBINLOGEVENTS_COMPRESSION_DECOMPRESSOR_H_
Abstract base class for decompressors.
Definition: decompressor.h:57
unsigned char Char_t
Definition: decompressor.h:59
void feed(const Input_char_t *input_data, Size_t input_size)
Submit data to decompress.
Definition: decompressor.h:90
void reset()
Reset the frame.
Definition: decompressor.cpp:30
const Decompressor & operator=(const Decompressor &)=delete
mysqlns::buffer::Managed_buffer< Char_t > Managed_buffer_t
Definition: decompressor.h:61
const Decompressor & operator=(const Decompressor &&)=delete
virtual void do_feed(const Char_t *input_data, Size_t input_size)=0
Implement feed.
virtual type do_get_type_code() const =0
Implement get_type_code.
mysqlns::buffer::Buffer_view< Char_t >::Size_t Size_t
Definition: decompressor.h:60
virtual std::pair< Decompress_status, Size_t > do_decompress(Char_t *out, Size_t output_size)=0
Implement decompress.
virtual void do_reset()=0
Implement do_reset.
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
mysqlns::buffer::Grow_constraint Grow_constraint_t
Definition: decompressor.h:62
Decompress_status decompress(Managed_buffer_t &out, Size_t output_size)
Decompress an exact, given number of bytes.
Definition: decompressor.cpp:40
virtual Grow_constraint_t do_get_grow_constraint_hint() const
Implement get_grow_constraint_hint.
Definition: decompressor.cpp:76
type get_type_code() const
Definition: decompressor.cpp:28
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:67
Owned, growable, contiguous memory buffer.
Definition: managed_buffer.h:111
Container class that provides a contiguous memory buffer to the caller, which the caller can request ...
Decompress_status
Definition: decompress_status.h:32
Grow_status
Error statuses for classes that use Grow_calculator.
Definition: grow_status.h:37
#define NODISCARD
The function attribute [[NODISCARD]] is a replacement for [[nodiscard]] to workaround a gcc bug.
Definition: nodiscard.h:47