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