MySQL 8.4.0
Source Code Documentation
basic_ostream.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 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 BASIC_OSTREAM_INCLUDED
25#define BASIC_OSTREAM_INCLUDED
26#include "my_byteorder.h"
27#include "my_inttypes.h"
28#include "my_sys.h"
31#include "sql_string.h"
32
33/**
34 The abstract class for basic output streams which provides write
35 operation.
36*/
38 public:
39 /**
40 Write some bytes into the output stream.
41 When all data is written into the stream successfully, then it return
42 false. Otherwise, true is returned. It will never returns false when
43 partial data is written into the stream.
44
45 @param[in] buffer Data to be written
46 @param[in] length Length of the data
47 @retval false Success.
48 @retval true Error.
49 */
50 virtual bool write(const unsigned char *buffer, my_off_t length) = 0;
51 virtual ~Basic_ostream() = default;
52};
53
54/**
55 Truncatable_ostream abstract class provides seek() and truncate() interfaces
56 to all truncatable output streams.
57*/
59 public:
60 /**
61 Truncate some data at the end of the output stream.
62
63 @param[in] offset Where the output stream will be truncated to.
64 @retval false Success
65 @retval true Error
66 */
67 virtual bool truncate(my_off_t offset) = 0;
68 /**
69 Put the write position to a given offset. The offset counts from the
70 beginning of the file.
71
72 @param[in] offset Where the write position will be
73 @retval false Success
74 @retval true Error
75 */
76 virtual bool seek(my_off_t offset) = 0;
77 /**
78 Flush data.
79
80 @retval false Success.
81 @retval true Error.
82 */
83 virtual bool flush() = 0;
84 /**
85 Sync.
86
87 @retval false Success
88 @retval true Error
89 */
90 virtual bool sync() = 0;
91
92 ~Truncatable_ostream() override = default;
93};
94
95/**
96 An output stream based on IO_CACHE class.
97*/
99 public:
103 ~IO_CACHE_ostream() override;
104
105 /**
106 Open the output stream. It opens related file and initialize IO_CACHE.
107
108 @param[in] log_file_key The PSI_file_key for this stream
109 @param[in] file_name The file will be opened
110 @param[in] flags The flags used by IO_CACHE.
111 @retval false Success
112 @retval true Error
113 */
114 bool open(
116 PSI_file_key log_file_key,
117#endif
118 const char *file_name, myf flags);
119 /**
120 Closes the stream. It deinitializes IO_CACHE and close the file
121 it opened.
122
123 @retval false Success
124 @retval true Error
125 */
126 bool close();
127
128 bool write(const unsigned char *buffer, my_off_t length) override;
129 bool seek(my_off_t offset) override;
130 bool truncate(my_off_t offset) override;
131
132 /**
133 Flush data to IO_CACHE's file if there is any data in IO_CACHE's buffer.
134
135 @retval false Success
136 @retval true Error
137 */
138 bool flush() override;
139
140 /**
141 Syncs the file to disk. It doesn't check and flush any remaining data still
142 left in IO_CACHE's buffer. So a call to flush() is necessary in order to
143 persist all data including the data in buffer.
144
145 @retval false Success
146 @retval true Error
147 */
148 bool sync() override;
149
150 private:
152};
153
154/**
155 A basic output stream based on StringBuffer class. It has a stack buffer of
156 size BUFFER_SIZE. It will allocate memory to create a heap buffer if
157 data exceeds the size of heap buffer.
158 */
159template <int BUFFER_SIZE>
161 public StringBuffer<BUFFER_SIZE> {
162 public:
166
167 bool write(const unsigned char *buffer, my_off_t length) override {
169 reinterpret_cast<const char *>(buffer), length);
170 }
171};
172
174 private:
176 using Compressor_ptr_t = std::shared_ptr<Compressor_t>;
182
183 public:
185 Managed_buffer_sequence_t &managed_buffer_sequence)
186 : m_compressor(std::move(compressor)),
187 m_managed_buffer_sequence(managed_buffer_sequence) {}
188 ~Compressed_ostream() override = default;
192 /// Compress the given bytes into the buffer sequence.
193 ///
194 /// @note This will consume the input, but may not produce all
195 /// output; it keeps the compression frame open so that the
196 /// compressor can make use of patterns across different invocations
197 /// of the function. The caller has to call Compressor::finish to
198 /// end the frame.
199 ///
200 /// @param buffer The input buffer
201 /// @param length The size of the input buffer
202 /// @retval false Success
203 /// @retval true Error
204 [[NODISCARD]] bool write(const unsigned char *buffer,
205 my_off_t length) override;
206 Status_t get_status() const;
207};
208
209#endif // BASIC_OSTREAM_INCLUDED
The abstract class for basic output streams which provides write operation.
Definition: basic_ostream.h:37
virtual ~Basic_ostream()=default
virtual bool write(const unsigned char *buffer, my_off_t length)=0
Write some bytes into the output stream.
Definition: basic_ostream.h:173
Managed_buffer_sequence_t & m_managed_buffer_sequence
Definition: basic_ostream.h:180
Compressor_ptr_t m_compressor
Definition: basic_ostream.h:179
Compressed_ostream(const Compressed_ostream &)=delete
Compressed_ostream(Compressor_ptr_t compressor, Managed_buffer_sequence_t &managed_buffer_sequence)
Definition: basic_ostream.h:184
Status_t m_status
Definition: basic_ostream.h:181
Compressed_ostream()=delete
std::shared_ptr< Compressor_t > Compressor_ptr_t
Definition: basic_ostream.h:176
bool write(const unsigned char *buffer, my_off_t length) override
Compress the given bytes into the buffer sequence.
Definition: basic_ostream.cc:92
Status_t get_status() const
Definition: basic_ostream.cc:99
Compressed_ostream & operator=(const Compressed_ostream &)=delete
~Compressed_ostream() override=default
An output stream based on IO_CACHE class.
Definition: basic_ostream.h:98
IO_CACHE m_io_cache
Definition: basic_ostream.h:151
bool close()
Closes the stream.
Definition: basic_ostream.cc:52
bool flush() override
Flush data to IO_CACHE's file if there is any data in IO_CACHE's buffer.
Definition: basic_ostream.cc:82
~IO_CACHE_ostream() override
Definition: basic_ostream.cc:32
IO_CACHE_ostream(const IO_CACHE_ostream &)=delete
bool truncate(my_off_t offset) override
Truncate some data at the end of the output stream.
Definition: basic_ostream.cc:72
bool seek(my_off_t offset) override
Put the write position to a given offset.
Definition: basic_ostream.cc:61
bool open(PSI_file_key log_file_key, const char *file_name, myf flags)
Open the output stream.
Definition: basic_ostream.cc:34
bool write(const unsigned char *buffer, my_off_t length) override
Write some bytes into the output stream.
Definition: basic_ostream.cc:66
bool sync() override
Syncs the file to disk.
Definition: basic_ostream.cc:87
IO_CACHE_ostream & operator=(const IO_CACHE_ostream &)=delete
A basic output stream based on StringBuffer class.
Definition: basic_ostream.h:161
StringBuffer_ostream(const StringBuffer_ostream &)=delete
bool write(const unsigned char *buffer, my_off_t length) override
Write some bytes into the output stream.
Definition: basic_ostream.h:167
StringBuffer_ostream()=default
StringBuffer_ostream & operator=(const StringBuffer_ostream &)=delete
String class wrapper with a preallocated buffer of size buff_sz.
Definition: sql_string.h:681
bool append(const String &s)
Definition: sql_string.cc:419
size_t length() const
Definition: sql_string.h:241
Truncatable_ostream abstract class provides seek() and truncate() interfaces to all truncatable outpu...
Definition: basic_ostream.h:58
virtual bool flush()=0
Flush data.
virtual bool seek(my_off_t offset)=0
Put the write position to a given offset.
~Truncatable_ostream() override=default
virtual bool sync()=0
Sync.
virtual bool truncate(my_off_t offset)=0
Truncate some data at the end of the output stream.
Abstract base class for compressors.
Definition: compressor.h:80
mysql::binlog::event::compression::buffer::Managed_buffer_sequence<> Managed_buffer_sequence_t
Definition: compressor.h:83
Owned, non-contiguous, growable memory buffer.
Definition: managed_buffer_sequence.h:115
unsigned int PSI_file_key
Instrumented file key.
Definition: psi_file_bits.h:48
static int flags[50]
Definition: hp_test1.cc:40
Functions for reading and storing in machine-independent format.
Some integer typedefs for easier portability.
int myf
Definition: my_inttypes.h:94
ulonglong my_off_t
Definition: my_inttypes.h:72
#define HAVE_PSI_INTERFACE
Definition: my_psi_config.h:39
Common header for many mysys elements.
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
std::string file_name(Log_file_id file_id)
Provides name of the log file with the given file id, e.g.
Definition: log0pre_8_0_30.cc:94
Grow_status
Error statuses for classes that use Grow_calculator.
Definition: grow_status.h:38
mysql::binlog::event::compression::buffer::Grow_status Compress_status
Definition: compressor.h:38
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
Definition: gcs_xcom_synode.h:64
#define NODISCARD
The function attribute [[NODISCARD]] is a replacement for [[nodiscard]] to workaround a gcc bug.
Definition: nodiscard.h:47
Our own string classes, used pervasively throughout the executor.
Definition: my_sys.h:346