MySQL 9.1.0
Source Code Documentation
cursor.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify it under
4the terms of the GNU General Public License, version 2.0, as published by the
5Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
18for more details.
19
20You should have received a copy of the GNU General Public License along with
21this program; if not, write to the Free Software Foundation, Inc.,
2251 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/** @file storage/temptable/include/temptable/cursor.h
25TempTable index cursor. */
26
27#ifndef TEMPTABLE_CURSOR_H
28#define TEMPTABLE_CURSOR_H
29
30#include <assert.h>
31
37
38namespace temptable {
39
40/** A cursor for iterating over an `Index`. */
41class Cursor {
42 public:
43 /** Constructor. */
44 Cursor();
45
46 /** Constructor from `Hash_duplicates` iterator. */
47 explicit Cursor(
48 /** [in] Iterator for cursor initial position. */
49 const Hash_duplicates_container::const_iterator &iterator);
50
51 /** Constructor from `Tree` iterator. */
52 explicit Cursor(
53 /** [in] Iterator for cursor initial position. */
54 const Tree_container::const_iterator &iterator);
55
56 Cursor(const Cursor &) = default;
57 Cursor(Cursor &&) noexcept = default;
58
59 /** Check if the cursor is positioned.
60 * @return true if positioned */
61 bool is_positioned() const;
62
63 /** Unposition the cursor. */
64 void unposition();
65
66 /** Get the indexed cells of the current cursor position.
67 * @return indexed cells */
68 const Indexed_cells &indexed_cells() const;
69
70 /** Get a pointer to the row of the current cursor position.
71 * @return a pointer to a row */
72 Storage::Element *row() const;
73
74 /** Export the row that is pointed to by this cursor in mysql write_row()
75 * format. */
77 /** [in] Metadata for the columns that constitute the row. */
78 const Columns &columns,
79 /** [out] Destination buffer to write the row to. */
80 unsigned char *mysql_row,
81 /** [in] Presumed length of the mysql row in bytes. */
82 size_t mysql_row_length) const;
83
84 /** Get the underlying hash iterator. The cursor must be on a hash index.
85 * @return iterator */
86 const Hash_duplicates_container::const_iterator &hash_iterator() const;
87
88 /** Get the underlying tree iterator. The cursor must be on a tree index.
89 * @return iterator */
90 const Tree_container::const_iterator &tree_iterator() const;
91
92 /** Copy-assign from another cursor.
93 * @return *this */
94 Cursor &operator=(
95 /** [in] Source cursor to assign from. */
96 const Cursor &rhs);
97
98 /** Advance the cursor forward.
99 * @return *this */
100 Cursor &operator++();
101
102 /** Recede the cursor backwards.
103 * @return *this */
104 Cursor &operator--();
105
106 /** Check if equal to another cursor.
107 * @return true if equal */
108 bool operator==(
109 /** [in] Cursor to compare with. */
110 const Cursor &other) const;
111
112 /** Check if not equal to another cursor.
113 * @return true if not equal */
114 bool operator!=(
115 /** [in] Cursor to compare with. */
116 const Cursor &other) const;
117
118 private:
119 /** Type of the index the cursor iterates over. */
120 enum class Type : uint8_t {
121 /** Hash index. */
122 HASH,
123 /** Tree index. */
124 TREE,
125 };
126
127 /** Type of the index the cursor iterates over. */
129
130 /** Indicate whether the cursor is positioned. */
132
133 /** Iterator that is used if `m_type == Type::HASH`. */
134 Hash_duplicates_container::const_iterator m_hash_iterator;
135
136 /** Iterator that is used if `m_type == Type::TREE`. */
137 Tree_container::const_iterator m_tree_iterator;
138};
139
140/* Implementation of inlined methods. */
141
142inline Cursor::Cursor() : m_is_positioned(false) {}
143
144inline Cursor::Cursor(const Hash_duplicates_container::const_iterator &iterator)
145 : m_type(Type::HASH), m_is_positioned(true), m_hash_iterator(iterator) {}
146
147inline Cursor::Cursor(const Tree_container::const_iterator &iterator)
148 : m_type(Type::TREE), m_is_positioned(true), m_tree_iterator(iterator) {}
149
150inline bool Cursor::is_positioned() const { return m_is_positioned; }
151
152inline void Cursor::unposition() { m_is_positioned = false; }
153
155 assert(m_is_positioned);
156
157 if (m_type == Type::HASH) {
158 return *m_hash_iterator;
159 }
160
161 assert(m_type == Type::TREE);
162 return *m_tree_iterator;
163}
164
166 assert(m_is_positioned);
167
168 if (m_type == Type::HASH) {
169 return m_hash_iterator->row();
170 }
171
172 assert(m_type == Type::TREE);
173 return m_tree_iterator->row();
174}
175
176inline void Cursor::export_row_to_mysql(const Columns &columns,
177 unsigned char *mysql_row,
178 size_t mysql_row_length) const {
179 assert(m_is_positioned);
180
181 if (m_type == Type::HASH) {
182 return m_hash_iterator->export_row_to_mysql(columns, mysql_row,
183 mysql_row_length);
184 }
185
186 assert(m_type == Type::TREE);
187 return m_tree_iterator->export_row_to_mysql(columns, mysql_row,
188 mysql_row_length);
189}
190
191inline const Hash_duplicates_container::const_iterator &Cursor::hash_iterator()
192 const {
193 assert(m_type == Type::HASH);
194 return m_hash_iterator;
195}
196
197inline const Tree_container::const_iterator &Cursor::tree_iterator() const {
198 assert(m_type == Type::TREE);
199 return m_tree_iterator;
200}
201
202inline Cursor &Cursor::operator=(const Cursor &rhs) {
204
205 m_type = rhs.m_type;
206
207 if (m_is_positioned) {
208 if (m_type == Type::HASH) {
210 } else {
211 assert(m_type == Type::TREE);
213 }
214 }
215
216 return *this;
217}
218
220 assert(m_is_positioned);
221
222 if (m_type == Type::HASH) {
224 } else {
225 assert(m_type == Type::TREE);
227 }
228
229 return *this;
230}
231
233 assert(m_is_positioned);
234
235 if (m_type == Type::HASH) {
236 /* We don't support decrement on a hash and it shouldn't be called. */
237 my_abort();
238 } else {
239 assert(m_type == Type::TREE);
241 }
242
243 return *this;
244}
245
246inline bool Cursor::operator==(const Cursor &other) const {
247 assert(m_is_positioned);
248
249 if (m_type == Type::HASH) {
250 return m_hash_iterator == other.m_hash_iterator;
251 }
252
253 assert(m_type == Type::TREE);
254 return m_tree_iterator == other.m_tree_iterator;
255}
256
257inline bool Cursor::operator!=(const Cursor &other) const {
258 return !(*this == other);
259}
260
261} /* namespace temptable */
262
263#endif /* TEMPTABLE_CURSOR_H */
A cursor for iterating over an Index.
Definition: cursor.h:41
Cursor()
Constructor.
Definition: cursor.h:142
Cursor & operator--()
Recede the cursor backwards.
Definition: cursor.h:232
bool m_is_positioned
Indicate whether the cursor is positioned.
Definition: cursor.h:131
bool operator==(const Cursor &other) const
Check if equal to another cursor.
Definition: cursor.h:246
void unposition()
Unposition the cursor.
Definition: cursor.h:152
Tree_container::const_iterator m_tree_iterator
Iterator that is used if m_type == Type::TREE.
Definition: cursor.h:137
Type
Type of the index the cursor iterates over.
Definition: cursor.h:120
const Hash_duplicates_container::const_iterator & hash_iterator() const
Get the underlying hash iterator.
Definition: cursor.h:191
Cursor & operator++()
Advance the cursor forward.
Definition: cursor.h:219
void export_row_to_mysql(const Columns &columns, unsigned char *mysql_row, size_t mysql_row_length) const
Export the row that is pointed to by this cursor in mysql write_row() format.
Definition: cursor.h:176
Type m_type
Type of the index the cursor iterates over.
Definition: cursor.h:128
Storage::Element * row() const
Get a pointer to the row of the current cursor position.
Definition: cursor.h:165
const Indexed_cells & indexed_cells() const
Get the indexed cells of the current cursor position.
Definition: cursor.h:154
Cursor & operator=(const Cursor &rhs)
Copy-assign from another cursor.
Definition: cursor.h:202
Cursor(const Cursor &)=default
bool is_positioned() const
Check if the cursor is positioned.
Definition: cursor.h:150
bool operator!=(const Cursor &other) const
Check if not equal to another cursor.
Definition: cursor.h:257
Hash_duplicates_container::const_iterator m_hash_iterator
Iterator that is used if m_type == Type::HASH.
Definition: cursor.h:134
const Tree_container::const_iterator & tree_iterator() const
Get the underlying tree iterator.
Definition: cursor.h:197
Cursor(Cursor &&) noexcept=default
Indexed cells represent one or more cells that are covered by an index.
Definition: indexed_cells.h:46
Storage container.
Definition: storage.h:43
void Element
Type used for elements.
Definition: storage.h:46
TempTable index containers declarations.
TempTable Indexed Cells declaration.
void my_abort()
Calls our own implementation of abort, if specified, or std's abort().
Definition: my_init.cc:261
Definition: allocator.h:48
std::multiset< Indexed_cells, Indexed_cells_less, Allocator< Indexed_cells > > Tree_container
The container used by tree unique and non-unique indexes.
Definition: containers.h:42
std::unordered_multiset< Indexed_cells, Indexed_cells_hash, Indexed_cells_equal_to, Allocator< Indexed_cells > > Hash_duplicates_container
The container used by hash non-unique indexes.
Definition: containers.h:48
std::vector< Column, Allocator< Column > > Columns
A type that designates all the columns of a table.
Definition: column.h:227
TempTable Row declarations.
TempTable Column declaration.
TempTable Storage.
Definition: my_tree.h:68