MySQL 8.3.0
Source Code Documentation
basic_ostream.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef BASIC_OSTREAM_INCLUDED
24#define BASIC_OSTREAM_INCLUDED
25#include "my_byteorder.h"
26#include "my_inttypes.h"
27#include "my_sys.h"
30#include "sql_string.h"
31
32/**
33 The abstract class for basic output streams which provides write
34 operation.
35*/
37 public:
38 /**
39 Write some bytes into the output stream.
40 When all data is written into the stream successfully, then it return
41 false. Otherwise, true is returned. It will never returns false when
42 partial data is written into the stream.
43
44 @param[in] buffer Data to be written
45 @param[in] length Length of the data
46 @retval false Success.
47 @retval true Error.
48 */
49 virtual bool write(const unsigned char *buffer, my_off_t length) = 0;
50 virtual ~Basic_ostream() = default;
51};
52
53/**
54 Truncatable_ostream abstract class provides seek() and truncate() interfaces
55 to all truncatable output streams.
56*/
58 public:
59 /**
60 Truncate some data at the end of the output stream.
61
62 @param[in] offset Where the output stream will be truncated to.
63 @retval false Success
64 @retval true Error
65 */
66 virtual bool truncate(my_off_t offset) = 0;
67 /**
68 Put the write position to a given offset. The offset counts from the
69 beginning of the file.
70
71 @param[in] offset Where the write position will be
72 @retval false Success
73 @retval true Error
74 */
75 virtual bool seek(my_off_t offset) = 0;
76 /**
77 Flush data.
78
79 @retval false Success.
80 @retval true Error.
81 */
82 virtual bool flush() = 0;
83 /**
84 Sync.
85
86 @retval false Success
87 @retval true Error
88 */
89 virtual bool sync() = 0;
90
91 ~Truncatable_ostream() override = default;
92};
93
94/**
95 An output stream based on IO_CACHE class.
96*/
98 public:
102 ~IO_CACHE_ostream() override;
103
104 /**
105 Open the output stream. It opens related file and initialize IO_CACHE.
106
107 @param[in] log_file_key The PSI_file_key for this stream
108 @param[in] file_name The file will be opened
109 @param[in] flags The flags used by IO_CACHE.
110 @retval false Success
111 @retval true Error
112 */
113 bool open(
115 PSI_file_key log_file_key,
116#endif
117 const char *file_name, myf flags);
118 /**
119 Closes the stream. It deinitializes IO_CACHE and close the file
120 it opened.
121
122 @retval false Success
123 @retval true Error
124 */
125 bool close();
126
127 bool write(const unsigned char *buffer, my_off_t length) override;
128 bool seek(my_off_t offset) override;
129 bool truncate(my_off_t offset) override;
130
131 /**
132 Flush data to IO_CACHE's file if there is any data in IO_CACHE's buffer.
133
134 @retval false Success
135 @retval true Error
136 */
137 bool flush() override;
138
139 /**
140 Syncs the file to disk. It doesn't check and flush any remaining data still
141 left in IO_CACHE's buffer. So a call to flush() is necessary in order to
142 persist all data including the data in buffer.
143
144 @retval false Success
145 @retval true Error
146 */
147 bool sync() override;
148
149 private:
151};
152
153/**
154 A basic output stream based on StringBuffer class. It has a stack buffer of
155 size BUFFER_SIZE. It will allocate memory to create a heap buffer if
156 data exceeds the size of heap buffer.
157 */
158template <int BUFFER_SIZE>
160 public StringBuffer<BUFFER_SIZE> {
161 public:
165
166 bool write(const unsigned char *buffer, my_off_t length) override {
168 reinterpret_cast<const char *>(buffer), length);
169 }
170};
171
173 private:
175 using Compressor_ptr_t = std::shared_ptr<Compressor_t>;
181
182 public:
184 Managed_buffer_sequence_t &managed_buffer_sequence)
185 : m_compressor(std::move(compressor)),
186 m_managed_buffer_sequence(managed_buffer_sequence) {}
187 ~Compressed_ostream() override = default;
191 /// Compress the given bytes into the buffer sequence.
192 ///
193 /// @note This will consume the input, but may not produce all
194 /// output; it keeps the compression frame open so that the
195 /// compressor can make use of patterns across different invocations
196 /// of the function. The caller has to call Compressor::finish to
197 /// end the frame.
198 ///
199 /// @param buffer The input buffer
200 /// @param length The size of the input buffer
201 /// @retval false Success
202 /// @retval true Error
203 [[NODISCARD]] bool write(const unsigned char *buffer,
204 my_off_t length) override;
205 Status_t get_status() const;
206};
207
208#endif // BASIC_OSTREAM_INCLUDED
The abstract class for basic output streams which provides write operation.
Definition: basic_ostream.h:36
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:172
Managed_buffer_sequence_t & m_managed_buffer_sequence
Definition: basic_ostream.h:179
Compressor_ptr_t m_compressor
Definition: basic_ostream.h:178
Compressed_ostream(const Compressed_ostream &)=delete
Compressed_ostream(Compressor_ptr_t compressor, Managed_buffer_sequence_t &managed_buffer_sequence)
Definition: basic_ostream.h:183
Status_t m_status
Definition: basic_ostream.h:180
Compressed_ostream()=delete
std::shared_ptr< Compressor_t > Compressor_ptr_t
Definition: basic_ostream.h:175
bool write(const unsigned char *buffer, my_off_t length) override
Compress the given bytes into the buffer sequence.
Definition: basic_ostream.cc:91
Status_t get_status() const
Definition: basic_ostream.cc:98
Compressed_ostream & operator=(const Compressed_ostream &)=delete
~Compressed_ostream() override=default
An output stream based on IO_CACHE class.
Definition: basic_ostream.h:97
IO_CACHE m_io_cache
Definition: basic_ostream.h:150
bool close()
Closes the stream.
Definition: basic_ostream.cc:51
bool flush() override
Flush data to IO_CACHE's file if there is any data in IO_CACHE's buffer.
Definition: basic_ostream.cc:81
~IO_CACHE_ostream() override
Definition: basic_ostream.cc:31
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:71
bool seek(my_off_t offset) override
Put the write position to a given offset.
Definition: basic_ostream.cc:60
bool open(PSI_file_key log_file_key, const char *file_name, myf flags)
Open the output stream.
Definition: basic_ostream.cc:33
bool write(const unsigned char *buffer, my_off_t length) override
Write some bytes into the output stream.
Definition: basic_ostream.cc:65
bool sync() override
Syncs the file to disk.
Definition: basic_ostream.cc:86
IO_CACHE_ostream & operator=(const IO_CACHE_ostream &)=delete
A basic output stream based on StringBuffer class.
Definition: basic_ostream.h:160
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:166
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:658
bool append(const String &s)
Definition: sql_string.cc:418
size_t length() const
Definition: sql_string.h:240
Truncatable_ostream abstract class provides seek() and truncate() interfaces to all truncatable outpu...
Definition: basic_ostream.h:57
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:79
mysql::binlog::event::compression::buffer::Managed_buffer_sequence<> Managed_buffer_sequence_t
Definition: compressor.h:82
Owned, non-contiguous, growable memory buffer.
Definition: managed_buffer_sequence.h:114
unsigned int PSI_file_key
Instrumented file key.
Definition: psi_file_bits.h:47
static int flags[50]
Definition: hp_test1.cc:39
Functions for reading and storing in machine-independent format.
Some integer typedefs for easier portability.
int myf
Definition: my_inttypes.h:93
ulonglong my_off_t
Definition: my_inttypes.h:71
#define HAVE_PSI_INTERFACE
Definition: my_psi_config.h:38
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:75
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:93
Grow_status
Error statuses for classes that use Grow_calculator.
Definition: grow_status.h:37
mysql::binlog::event::compression::buffer::Grow_status Compress_status
Definition: compressor.h:37
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:417
Definition: varlen_sort.h:174
#define NODISCARD
The function attribute [[NODISCARD]] is a replacement for [[nodiscard]] to workaround a gcc bug.
Definition: nodiscard.h:46
Our own string classes, used pervasively throughout the executor.
Definition: my_sys.h:345