MySQL 8.0.39
Source Code Documentation
basic_istream.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_ISTREAM_INCLUDED
25#define BASIC_ISTREAM_INCLUDED
26#include "my_io.h"
27#include "my_sys.h"
28
29/**
30 The abstract class for basic byte input streams which provides read
31 operations.
32*/
34 public:
35 /**
36 Read some bytes from the input stream. It should read exact 'length' bytes
37 unless error happens or it reaches the end of the stream. It should block
38 when reaching the end of a pipe that is not closed.
39
40 @param[out] buffer Where data will be put in.
41 @param[in] length The number of bytes that you want to read. length should
42 not be larger than max long.
43 @return Return values fall into three cases:
44 @retval 'length' Read 'length' bytes successfully
45 @retval >=0 Reach EOF, return the number of bytes actually read.
46 It is between 0 and length-1.
47 @retval -1 Error.
48 */
49 virtual ssize_t read(unsigned char *buffer, size_t length) = 0;
50
51 virtual ~Basic_istream() = default;
52};
53
54/**
55 The abstract class for seekable input streams which have fixed length
56 and provide seek operation.
57*/
59 public:
60 /**
61 Puts the read position to a given offset. The offset counts from the
62 beginning of the stream. In case an implementing class transforms the data
63 in a way that does not preserve positions, the offset here will be relative
64 to the bytes that are read out from the stream, not relative to the bytes
65 in lower layer storage.
66
67 it is allowed for a subclass to return success even if the position is
68 greater than the size of the file. Error may be returned by the next
69 read for this case. Users should call length() if they need to check that
70 the position is within bounds.
71
72 @param[in] offset Where the read position will be.
73 @retval false Success
74 @retval true Error
75 */
76 virtual bool seek(my_off_t offset) = 0;
77 /**
78 The total length of the stream.
79 */
80 virtual my_off_t length() = 0;
81 ~Basic_seekable_istream() override = default;
82};
83
84/**
85 A file input stream based on IO_CACHE class. It can be used to open a file
86 and provide a Basic_seekable_istream based on the file.
87*/
89 public:
93 ~IO_CACHE_istream() override;
94
95 /**
96 Open the stream. It opens related file and initializes IO_CACHE.
97
98 @param[in] log_file_key The PSI_file_key for this stream
99 @param[in] log_cache_key The PSI_file_key for the IO_CACHE
100 @param[in] file_name The file to be opened
101 @param[in] flags The flags used by IO_CACHE.
102 @param[in] cache_size Cache size of the IO_CACHE.
103 @retval false Success
104 @retval true Error
105 */
106 bool open(
108 PSI_file_key log_file_key, PSI_file_key log_cache_key,
109#endif
110 const char *file_name, myf flags, size_t cache_size = IO_SIZE * 2);
111 /**
112 Closes the stream. It deinitializes IO_CACHE and closes the file it opened.
113 */
114 void close();
115
116 ssize_t read(unsigned char *buffer, size_t length) override;
117 bool seek(my_off_t bytes) override;
118
119 /**
120 Get the length of the file.
121 */
122 my_off_t length() override;
123
124 private:
126};
127
128/**
129 A stdin input stream based on IO_CACHE. It provides a Basic_istream based on
130 stdin.
131*/
133 public:
135 Stdin_istream(const Stdin_istream &) = delete;
137 ~Stdin_istream() override;
138
139 /**
140 Opens the stdin stream. It initializes the IO_CACHE with stdin.
141 @param[out] errmsg An error message is returned if any error happens.
142 */
143 bool open(std::string *errmsg);
144 /**
145 Closes the stream. It deinitializes IO_CACHE.
146 */
147 void close();
148
149 ssize_t read(unsigned char *buffer, size_t length) override;
150 /**
151 Skip bytes data from the stdin stream.
152 @param[in] bytes How many bytes should be skipped
153 */
154 bool skip(my_off_t bytes);
155
156 private:
158};
159
160#endif // BASIC_ISTREAM_INCLUDED
The abstract class for basic byte input streams which provides read operations.
Definition: basic_istream.h:33
virtual ssize_t read(unsigned char *buffer, size_t length)=0
Read some bytes from the input stream.
virtual ~Basic_istream()=default
The abstract class for seekable input streams which have fixed length and provide seek operation.
Definition: basic_istream.h:58
~Basic_seekable_istream() override=default
virtual bool seek(my_off_t offset)=0
Puts the read position to a given offset.
virtual my_off_t length()=0
The total length of the stream.
A file input stream based on IO_CACHE class.
Definition: basic_istream.h:88
IO_CACHE_istream(const IO_CACHE_istream &)=delete
ssize_t read(unsigned char *buffer, size_t length) override
Read some bytes from the input stream.
Definition: basic_istream.cc:67
void close()
Closes the stream.
Definition: basic_istream.cc:58
bool open(PSI_file_key log_file_key, PSI_file_key log_cache_key, const char *file_name, myf flags, size_t cache_size=IO_SIZE *2)
Open the stream.
Definition: basic_istream.cc:34
IO_CACHE_istream & operator=(const IO_CACHE_istream &)=delete
~IO_CACHE_istream() override
Definition: basic_istream.cc:32
my_off_t length() override
Get the length of the file.
Definition: basic_istream.cc:65
bool seek(my_off_t bytes) override
Puts the read position to a given offset.
Definition: basic_istream.cc:75
IO_CACHE m_io_cache
Definition: basic_istream.h:125
A stdin input stream based on IO_CACHE.
Definition: basic_istream.h:132
Stdin_istream(const Stdin_istream &)=delete
bool skip(my_off_t bytes)
Skip bytes data from the stdin stream.
Definition: basic_istream.cc:119
ssize_t read(unsigned char *buffer, size_t length) override
Read some bytes from the input stream.
Definition: basic_istream.cc:114
bool open(std::string *errmsg)
Opens the stdin stream.
Definition: basic_istream.cc:87
IO_CACHE m_io_cache
Definition: basic_istream.h:157
Stdin_istream & operator=(const Stdin_istream &)=delete
~Stdin_istream() override
Definition: basic_istream.cc:85
void close()
Closes the stream.
Definition: basic_istream.cc:112
unsigned int PSI_file_key
Instrumented file key.
Definition: psi_file_bits.h:48
static int flags[50]
Definition: hp_test1.cc:40
int myf
Definition: my_inttypes.h:94
ulonglong my_off_t
Definition: my_inttypes.h:72
Common #defines and includes for file and socket I/O.
constexpr const size_t IO_SIZE
Definition: my_io.h:159
#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
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:420
Definition: my_sys.h:341
static uint64_t cache_size
Definition: xcom_cache.cc:362