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