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