MySQL 9.1.0
Source Code Documentation
record_buffer.h
Go to the documentation of this file.
1#ifndef RECORD_BUFFER_INCLUDED
2#define RECORD_BUFFER_INCLUDED
3
4/*
5 Copyright (c) 2016, 2024, Oracle and/or its affiliates.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License, version 2.0,
9 as published by the Free Software Foundation.
10
11 This program is designed to work with certain software (including
12 but not limited to OpenSSL) that is licensed under separate terms,
13 as designated in a particular file or component or in included license
14 documentation. The authors of MySQL hereby grant you an additional
15 permission to link the program and your derivative works with the
16 separately licensed software that they have either included with
17 the program or referenced in the documentation.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License, version 2.0, for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27*/
28
29#include <assert.h>
30#include "my_base.h" // ha_rows
31
32/**
33 This class represents a buffer that can be used for multi-row reads. It is
34 allocated by the executor and given to the storage engine through the
35 handler, using handler::ha_set_record_buffer(), so that the storage engine
36 can fill the buffer with multiple rows in a single read call.
37
38 For now, the record buffer is only used internally by the storage engine as
39 a prefetch cache. The storage engine fills the record buffer when the
40 executor requests the first record, but it returns a single record only to
41 the executor. If the executor wants to access the records in the buffer, it
42 has to call a handler function such as handler::ha_index_next() or
43 handler::ha_rnd_next(). Then the storage engine will copy the next record
44 from the record buffer to the memory area specified in the arguments of the
45 handler function, typically TABLE::record[0].
46*/
48 /// The maximum number of records that can be stored in the buffer.
50 /// The number of bytes available for each record.
52 /// The number of records currently stored in the buffer.
54 /// The @c uchar buffer that holds the records.
56 /// Tells if end-of-range was found while filling the buffer.
57 bool m_out_of_range = false;
58
59 public:
60 /**
61 Create a new record buffer with the specified size.
62
63 @param records the number of records that can be stored in the buffer
64 @param record_size the size of each record
65 @param buffer the @c uchar buffer that will hold the records (its
66 size should be at least
67 `Record_buffer::buffer_size(records, record_size)`)
68 */
71
72 /**
73 This function calculates how big the @c uchar buffer provided to
74 Record_buffer's constructor must be, given a number of records and
75 the record size.
76
77 @param records the maximum number of records in the buffer
78 @param record_size the size of each record
79 @return the total number of bytes needed for all the records
80 */
81 static constexpr size_t buffer_size(ha_rows records, size_t record_size) {
82 return static_cast<size_t>(records * record_size);
83 }
84
85 /**
86 Get the number of records that can be stored in the buffer.
87 @return the maximum number of records in the buffer
88 */
89 ha_rows max_records() const { return m_max_records; }
90
91 /**
92 Get the amount of space allocated for each record in the buffer.
93 @return the record size
94 */
95 size_t record_size() const { return m_record_size; }
96
97 /**
98 Get the number of records currently stored in the buffer.
99 @return the number of records stored in the buffer
100 */
101 ha_rows records() const { return m_count; }
102
103 /**
104 Get the buffer that holds the record on position @a pos.
105 @param pos the record number (must be smaller than records())
106 @return the @c uchar buffer that holds the specified record
107 */
108 uchar *record(ha_rows pos) const {
109 assert(pos < max_records());
110 return m_buffer + m_record_size * pos;
111 }
112
113 /**
114 Add a new record at the end of the buffer.
115 @return the @c uchar buffer of the added record
116 */
118 assert(m_count < max_records());
119 return record(m_count++);
120 }
121
122 /**
123 Remove the record that was last added to the buffer.
124 */
125 void remove_last() {
126 assert(m_count > 0);
127 --m_count;
128 }
129
130 /**
131 Clear the buffer. Remove all the records. The end-of-range flag is
132 preserved.
133 */
134 void clear() { m_count = 0; }
135
136 /**
137 Reset the buffer. Remove all records and clear the end-of-range flag.
138 */
139 void reset() {
140 m_count = 0;
141 set_out_of_range(false);
142 }
143
144 /**
145 Set whether the end of the range was reached while filling the buffer.
146 @param val true if end of range was reached, false if still within range
147 */
148 void set_out_of_range(bool val) { m_out_of_range = val; }
149
150 /**
151 Check if the end of the range was reached while filling the buffer.
152 @retval true if the end range was reached
153 @retval false if the scan is still within the range
154 */
155 bool is_out_of_range() const { return m_out_of_range; }
156};
157
158#endif /* RECORD_BUFFER_INCLUDED */
This class represents a buffer that can be used for multi-row reads.
Definition: record_buffer.h:47
ha_rows m_count
The number of records currently stored in the buffer.
Definition: record_buffer.h:53
void clear()
Clear the buffer.
Definition: record_buffer.h:134
ha_rows m_max_records
The maximum number of records that can be stored in the buffer.
Definition: record_buffer.h:49
static constexpr size_t buffer_size(ha_rows records, size_t record_size)
This function calculates how big the uchar buffer provided to Record_buffer's constructor must be,...
Definition: record_buffer.h:81
uchar * record(ha_rows pos) const
Get the buffer that holds the record on position pos.
Definition: record_buffer.h:108
size_t record_size() const
Get the amount of space allocated for each record in the buffer.
Definition: record_buffer.h:95
void set_out_of_range(bool val)
Set whether the end of the range was reached while filling the buffer.
Definition: record_buffer.h:148
size_t m_record_size
The number of bytes available for each record.
Definition: record_buffer.h:51
ha_rows max_records() const
Get the number of records that can be stored in the buffer.
Definition: record_buffer.h:89
void reset()
Reset the buffer.
Definition: record_buffer.h:139
ha_rows records() const
Get the number of records currently stored in the buffer.
Definition: record_buffer.h:101
bool m_out_of_range
Tells if end-of-range was found while filling the buffer.
Definition: record_buffer.h:57
bool is_out_of_range() const
Check if the end of the range was reached while filling the buffer.
Definition: record_buffer.h:155
uchar * add_record()
Add a new record at the end of the buffer.
Definition: record_buffer.h:117
uchar * m_buffer
The uchar buffer that holds the records.
Definition: record_buffer.h:55
void remove_last()
Remove the record that was last added to the buffer.
Definition: record_buffer.h:125
Record_buffer(ha_rows records, size_t record_size, uchar *buffer)
Create a new record buffer with the specified size.
Definition: record_buffer.h:69
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1141
unsigned char uchar
Definition: my_inttypes.h:52
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418