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