| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/ddl0impl-file-reader.h |
| 29 |
For scanning the temporary file. |
| 30 |
Created 2020-11-01 by Sunny Bains. */ |
| 31 |
|
| 32 |
#ifndef ddl0impl_file_reader_h |
| 33 |
#define ddl0impl_file_reader_h |
| 34 |
|
| 35 |
#include "ddl0impl-buffer.h" |
| 36 |
|
| 37 |
namespace ddl { |
| 38 |
|
| 39 |
// Forward declaration |
| 40 |
struct File_cursor; |
| 41 |
|
| 42 |
/** Represents the chunk in bytes : first element represents the beginning |
| 43 |
offset of the chunk and, second element represents the length of the chunk. */ |
| 44 |
using Range = std::pair<os_offset_t, os_offset_t>; |
| 45 |
|
| 46 |
/** Read rows from the temporary file. Rows could be read in the from of chunks |
| 47 |
or ranges. */ |
| 48 |
struct File_reader : private ut::Non_copyable { |
| 49 |
/** Constructor. |
| 50 |
@param[in] file Opened file. |
| 51 |
@param[in,out] index Index that the rows belong to. |
| 52 |
@param[in] buffer_size Size of file buffer for reading. |
| 53 |
@param[in] range Offsets of the chunk to read */ |
| 54 |
File_reader(const Unique_os_file_descriptor &file, dict_index_t *index, |
| 55 |
size_t buffer_size, const Range &range) noexcept |
| 56 |
: m_index(index), |
| 57 |
m_file(file), |
| 58 |
m_range(range), |
| 59 |
m_buffer_size(buffer_size) { |
| 60 |
ut_a(range.first < range.second); |
| 61 |
ut_a(m_buffer_size > 0); |
| 62 |
ut_a(m_index != nullptr); |
| 63 |
ut_a(m_file.is_open()); |
| 64 |
} |
| 65 |
|
| 66 |
/** Destructor. */ |
| 67 |
~File_reader() noexcept { |
| 68 |
if (m_aux_buf != nullptr) { |
| 69 |
ut::delete_arr(m_aux_buf); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** Prepare the file for reading. |
| 74 |
@return DB_SUCCESS or error code. */ |
| 75 |
[[nodiscard]] dberr_t prepare() noexcept; |
| 76 |
|
| 77 |
/** The current row as a tuple. Note: the tuple only does a shallow copy. |
| 78 |
@param[in,out] builder Index builder instance. |
| 79 |
@param[in,out] heap Heap to use for allocation. |
| 80 |
@param[out] dtuple Row converted to a tuple. |
| 81 |
@return DB_SUCCESS or error code. */ |
| 82 |
[[nodiscard]] dberr_t get_tuple(Builder *builder, mem_heap_t *heap, |
| 83 |
dtuple_t *&dtuple) noexcept; |
| 84 |
|
| 85 |
/** Set the start and end offsets to read from, to avoid the possibility of |
| 86 |
overlapping reads from the other ranges in subsequent reads. Seek to the |
| 87 |
start offset, read the page and, position to the first record on the page |
| 88 |
@param[in] range Start and end offsets of the range to read from. |
| 89 |
@return DB_SUCCESS or error code. */ |
| 90 |
dberr_t read(const Range &range) noexcept; |
| 91 |
|
| 92 |
/** @return true if the range first == second. */ |
| 93 |
[[nodiscard]] bool end_of_range() const noexcept { |
| 94 |
return m_range.first == m_range.second; |
| 95 |
} |
| 96 |
|
| 97 |
/** @return the number of rows read from the file. */ |
| 98 |
[[nodiscard]] uint64_t get_n_rows_read() const noexcept { |
| 99 |
return m_n_rows_read; |
| 100 |
} |
| 101 |
|
| 102 |
private: |
| 103 |
/** Read the page in the file buffer from the start offset, and reset the |
| 104 |
cursor to the beginning of the file buffer |
| 105 |
@return DB_SUCCESS or error code. */ |
| 106 |
[[nodiscard]] dberr_t seek() noexcept; |
| 107 |
|
| 108 |
/** Advance page number to the next and read in. |
| 109 |
@return DB_SUCCESS or error code. */ |
| 110 |
[[nodiscard]] dberr_t read_next() noexcept; |
| 111 |
|
| 112 |
/** Advance the "cursor". |
| 113 |
@return DB_SUCCESS or error code. */ |
| 114 |
[[nodiscard]] dberr_t next() noexcept; |
| 115 |
|
| 116 |
public: |
| 117 |
using Offsets = std::vector<ulint, ut::allocator<ulint>>; |
| 118 |
|
| 119 |
/** Index that the records belong to. */ |
| 120 |
dict_index_t *m_index{}; |
| 121 |
|
| 122 |
/** Pointer to current row. */ |
| 123 |
const mrec_t *m_mrec{}; |
| 124 |
|
| 125 |
/** Columns offsets. */ |
| 126 |
Offsets m_field_offsets{}; |
| 127 |
|
| 128 |
/** File handle to read from. */ |
| 129 |
const Unique_os_file_descriptor &m_file; |
| 130 |
|
| 131 |
private: |
| 132 |
using Bounds = std::pair<const byte *, const byte *>; |
| 133 |
|
| 134 |
/* Coordinates of the chunk that this file reader can read the rows from */ |
| 135 |
Range m_range{}; |
| 136 |
|
| 137 |
/** Pointer current offset within file buffer. */ |
| 138 |
const byte *m_ptr{}; |
| 139 |
|
| 140 |
/** File buffer bounds. */ |
| 141 |
Bounds m_bounds{}; |
| 142 |
|
| 143 |
/** Auxiliary buffer for records that span across pages. */ |
| 144 |
byte *m_aux_buf{}; |
| 145 |
|
| 146 |
/** IO buffer size in bytes. */ |
| 147 |
size_t m_buffer_size{}; |
| 148 |
|
| 149 |
/** Aligned IO buffer. */ |
| 150 |
ut::unique_ptr_aligned<byte[]> m_aligned_buffer{}; |
| 151 |
|
| 152 |
/** File buffer for reading. */ |
| 153 |
IO_buffer m_io_buffer{}; |
| 154 |
|
| 155 |
/** Number of rows read from the file. */ |
| 156 |
uint64_t m_n_rows_read{}; |
| 157 |
|
| 158 |
friend File_cursor; |
| 159 |
}; |
| 160 |
|
| 161 |
} // namespace ddl |
| 162 |
|
| 163 |
#endif /* ddl0impl_file_reader_h */ |