MySQL 8.4.0
Source Code Documentation
ha_example.h
Go to the documentation of this file.
1/* Copyright (c) 2004, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/** @file ha_example.h
25
26 @brief
27 The ha_example engine is a stubbed storage engine for example purposes only;
28 it does nothing at this point. Its purpose is to provide a source
29 code illustration of how to begin writing new storage engines; see also
30 /storage/example/ha_example.cc.
31
32 @note
33 Please read ha_example.cc before reading this file.
34 Reminder: The example storage engine implements all methods that are
35 *required* to be implemented. For a full list of all methods that you can
36 implement, see handler.h.
37
38 @see
39 /sql/handler.h and /storage/example/ha_example.cc
40*/
41
42#include <sys/types.h>
43
44#include "my_base.h" /* ha_rows */
45#include "my_compiler.h"
46#include "my_inttypes.h"
47#include "sql/handler.h" /* handler */
48#include "thr_lock.h" /* THR_LOCK, THR_LOCK_DATA */
49
50/** @brief
51 Example_share is a class that will be shared among all open handlers.
52 This example implements the minimum of what you will probably need.
53*/
55 public:
59};
60
61/** @brief
62 Class definition for the storage engine
63*/
64class ha_example : public handler {
65 THR_LOCK_DATA lock; ///< MySQL lock
66 Example_share *share; ///< Shared lock info
67 Example_share *get_share(); ///< Get the share
68
69 public:
70 ha_example(handlerton *hton, TABLE_SHARE *table_arg);
71 ~ha_example() override = default;
72
73 /** @brief
74 The name that will be used for display purposes.
75 */
76 const char *table_type() const override { return "EXAMPLE"; }
77
78 /**
79 Replace key algorithm with one supported by SE, return the default key
80 algorithm for SE if explicit key algorithm was not provided.
81
82 @sa handler::adjust_index_algorithm().
83 */
85 return HA_KEY_ALG_HASH;
86 }
87 bool is_index_algorithm_supported(enum ha_key_alg key_alg) const override {
88 return key_alg == HA_KEY_ALG_HASH;
89 }
90
91 /** @brief
92 This is a list of flags that indicate what functionality the storage engine
93 implements. The current table flags are documented in handler.h
94 */
95 ulonglong table_flags() const override {
96 /*
97 We are saying that this engine is just statement capable to have
98 an engine that can only handle statement-based logging. This is
99 used in testing.
100 */
102 }
103
104 /** @brief
105 This is a bitmap of flags that indicates how the storage engine
106 implements indexes. The current index flags are documented in
107 handler.h. If you do not implement indexes, just return zero here.
108
109 @details
110 part is the key part to check. First key part is 0.
111 If all_parts is set, MySQL wants to know the flags for the combined
112 index, up to and including 'part'.
113 */
114 ulong index_flags(uint inx [[maybe_unused]], uint part [[maybe_unused]],
115 bool all_parts [[maybe_unused]]) const override {
116 return 0;
117 }
118
119 /** @brief
120 unireg.cc will call max_supported_record_length(), max_supported_keys(),
121 max_supported_key_parts(), uint max_supported_key_length()
122 to make sure that the storage engine can handle the data it is about to
123 send. Return *real* limits of your storage engine here; MySQL will do
124 min(your_limits, MySQL_limits) automatically.
125 */
126 uint max_supported_record_length() const override {
127 return HA_MAX_REC_LENGTH;
128 }
129
130 /** @brief
131 unireg.cc will call this to make sure that the storage engine can handle
132 the data it is about to send. Return *real* limits of your storage engine
133 here; MySQL will do min(your_limits, MySQL_limits) automatically.
134
135 @details
136 There is no need to implement ..._key_... methods if your engine doesn't
137 support indexes.
138 */
139 uint max_supported_keys() const override { return 0; }
140
141 /** @brief
142 unireg.cc will call this to make sure that the storage engine can handle
143 the data it is about to send. Return *real* limits of your storage engine
144 here; MySQL will do min(your_limits, MySQL_limits) automatically.
145
146 @details
147 There is no need to implement ..._key_... methods if your engine doesn't
148 support indexes.
149 */
150 uint max_supported_key_parts() const override { return 0; }
151
152 /** @brief
153 unireg.cc will call this to make sure that the storage engine can handle
154 the data it is about to send. Return *real* limits of your storage engine
155 here; MySQL will do min(your_limits, MySQL_limits) automatically.
156
157 @details
158 There is no need to implement ..._key_... methods if your engine doesn't
159 support indexes.
160 */
161 uint max_supported_key_length() const override { return 0; }
162
163 /** @brief
164 Called in test_quick_select to determine if indexes should be used.
165 */
166 double scan_time() override {
167 return (double)(stats.records + stats.deleted) / 20.0 + 10;
168 }
169
170 /** @brief
171 This method will never be called if you do not implement indexes.
172 */
173 double read_time(uint, uint, ha_rows rows) override {
174 return (double)rows / 20.0 + 1;
175 }
176
177 /*
178 Everything below are methods that we implement in ha_example.cc.
179
180 Most of these methods are not obligatory, skip them and
181 MySQL will treat them as not implemented
182 */
183 /** @brief
184 We implement this in ha_example.cc; it's a required method.
185 */
186 int open(const char *name, int mode, uint test_if_locked,
187 const dd::Table *table_def) override; // required
188
189 /** @brief
190 We implement this in ha_example.cc; it's a required method.
191 */
192 int close(void) override; // required
193
194 /** @brief
195 We implement this in ha_example.cc. It's not an obligatory method;
196 skip it and and MySQL will treat it as not implemented.
197 */
198 int write_row(uchar *buf) override;
199
200 /** @brief
201 We implement this in ha_example.cc. It's not an obligatory method;
202 skip it and and MySQL will treat it as not implemented.
203 */
204 int update_row(const uchar *old_data, uchar *new_data) override;
205
206 /** @brief
207 We implement this in ha_example.cc. It's not an obligatory method;
208 skip it and and MySQL will treat it as not implemented.
209 */
210 int delete_row(const uchar *buf) override;
211
212 /** @brief
213 We implement this in ha_example.cc. It's not an obligatory method;
214 skip it and and MySQL will treat it as not implemented.
215 */
216 int index_read_map(uchar *buf, const uchar *key, key_part_map keypart_map,
217 enum ha_rkey_function find_flag) override;
218
219 /** @brief
220 We implement this in ha_example.cc. It's not an obligatory method;
221 skip it and and MySQL will treat it as not implemented.
222 */
223 int index_next(uchar *buf) override;
224
225 /** @brief
226 We implement this in ha_example.cc. It's not an obligatory method;
227 skip it and and MySQL will treat it as not implemented.
228 */
229 int index_prev(uchar *buf) override;
230
231 /** @brief
232 We implement this in ha_example.cc. It's not an obligatory method;
233 skip it and and MySQL will treat it as not implemented.
234 */
235 int index_first(uchar *buf) override;
236
237 /** @brief
238 We implement this in ha_example.cc. It's not an obligatory method;
239 skip it and and MySQL will treat it as not implemented.
240 */
241 int index_last(uchar *buf) override;
242
243 /** @brief
244 Unlike index_init(), rnd_init() can be called two consecutive times
245 without rnd_end() in between (it only makes sense if scan=1). In this
246 case, the second call should prepare for the new table scan (e.g if
247 rnd_init() allocates the cursor, the second call should position the
248 cursor to the start of the table; no need to deallocate and allocate
249 it again. This is a required method.
250 */
251 int rnd_init(bool scan) override; // required
252 int rnd_end() override;
253 int rnd_next(uchar *buf) override; ///< required
254 int rnd_pos(uchar *buf, uchar *pos) override; ///< required
255 void position(const uchar *record) override; ///< required
256 int info(uint) override; ///< required
257 int extra(enum ha_extra_function operation) override;
258 int external_lock(THD *thd, int lock_type) override; ///< required
259 int delete_all_rows(void) override;
260 ha_rows records_in_range(uint inx, key_range *min_key,
261 key_range *max_key) override;
262 int delete_table(const char *from, const dd::Table *table_def) override;
263 int rename_table(const char *from, const char *to,
264 const dd::Table *from_table_def,
265 dd::Table *to_table_def) override;
266 int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info,
267 dd::Table *table_def) override; ///< required
268
270 THD *thd, THR_LOCK_DATA **to,
271 enum thr_lock_type lock_type) override; ///< required
272};
app_data_ptr new_data(u_int n, char *val, cons_type consensus)
Example_share is a class that will be shared among all open handlers.
Definition: ha_example.h:54
~Example_share() override
Definition: ha_example.h:58
THR_LOCK lock
Definition: ha_example.h:56
Example_share()
Definition: ha_example.cc:115
Base class to be used by handlers different shares.
Definition: handler.h:4114
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:47
Class definition for the storage engine.
Definition: ha_example.h:64
enum ha_key_alg get_default_index_algorithm() const override
Replace key algorithm with one supported by SE, return the default key algorithm for SE if explicit k...
Definition: ha_example.h:84
double scan_time() override
Called in test_quick_select to determine if indexes should be used.
Definition: ha_example.h:166
int rnd_init(bool scan) override
Unlike index_init(), rnd_init() can be called two consecutive times without rnd_end() in between (it ...
Definition: ha_example.cc:438
const char * table_type() const override
The name that will be used for display purposes.
Definition: ha_example.h:76
int delete_row(const uchar *buf) override
We implement this in ha_example.cc.
Definition: ha_example.cc:346
int close(void) override
We implement this in ha_example.cc; it's a required method.
Definition: ha_example.cc:252
Example_share * share
Shared lock info.
Definition: ha_example.h:66
bool is_index_algorithm_supported(enum ha_key_alg key_alg) const override
Check if SE supports specific key algorithm.
Definition: ha_example.h:87
int delete_table(const char *from, const dd::Table *table_def) override
Used to delete a table.
Definition: ha_example.cc:681
int write_row(uchar *buf) override
We implement this in ha_example.cc.
Definition: ha_example.cc:287
int index_next(uchar *buf) override
We implement this in ha_example.cc.
Definition: ha_example.cc:371
int external_lock(THD *thd, int lock_type) override
required
Definition: ha_example.cc:613
uint max_supported_record_length() const override
unireg.cc will call max_supported_record_length(), max_supported_keys(), max_supported_key_parts(),...
Definition: ha_example.h:126
~ha_example() override=default
int rnd_end() override
Definition: ha_example.cc:443
int index_first(uchar *buf) override
We implement this in ha_example.cc.
Definition: ha_example.cc:400
double read_time(uint, uint, ha_rows rows) override
This method will never be called if you do not implement indexes.
Definition: ha_example.h:173
ulonglong table_flags() const override
This is a list of flags that indicate what functionality the storage engine implements.
Definition: ha_example.h:95
Example_share * get_share()
Get the share.
Definition: ha_example.cc:145
int update_row(const uchar *old_data, uchar *new_data) override
We implement this in ha_example.cc.
Definition: ha_example.cc:321
THR_LOCK_DATA ** store_lock(THD *thd, THR_LOCK_DATA **to, enum thr_lock_type lock_type) override
required
Definition: ha_example.cc:655
ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key) override
Given a starting key and an ending key, estimate the number of rows that will exist between the two k...
Definition: ha_example.cc:720
int extra(enum ha_extra_function operation) override
extra() is called whenever the server wishes to send a hint to the storage engine.
Definition: ha_example.cc:566
ha_example(handlerton *hton, TABLE_SHARE *table_arg)
Definition: ha_example.cc:167
void position(const uchar *record) override
required
Definition: ha_example.cc:491
uint max_supported_key_length() const override
unireg.cc will call this to make sure that the storage engine can handle the data it is about to send...
Definition: ha_example.h:161
int index_prev(uchar *buf) override
We implement this in ha_example.cc.
Definition: ha_example.cc:383
int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info, dd::Table *table_def) override
required
Definition: ha_example.cc:750
uint max_supported_key_parts() const override
unireg.cc will call this to make sure that the storage engine can handle the data it is about to send...
Definition: ha_example.h:150
int open(const char *name, int mode, uint test_if_locked, const dd::Table *table_def) override
We implement this in ha_example.cc; it's a required method.
Definition: ha_example.cc:228
THR_LOCK_DATA lock
MySQL lock.
Definition: ha_example.h:65
int rename_table(const char *from, const char *to, const dd::Table *from_table_def, dd::Table *to_table_def) override
Renames a table from one name to another via an alter table call.
Definition: ha_example.cc:701
int rnd_next(uchar *buf) override
required
Definition: ha_example.cc:463
uint max_supported_keys() const override
unireg.cc will call this to make sure that the storage engine can handle the data it is about to send...
Definition: ha_example.h:139
int delete_all_rows(void) override
Used to delete all rows in a table, including cases of truncate and cases where the optimizer realize...
Definition: ha_example.cc:591
int index_read_map(uchar *buf, const uchar *key, key_part_map keypart_map, enum ha_rkey_function find_flag) override
We implement this in ha_example.cc.
Definition: ha_example.cc:358
ulong index_flags(uint inx, uint part, bool all_parts) const override
This is a bitmap of flags that indicates how the storage engine implements indexes.
Definition: ha_example.h:114
int rnd_pos(uchar *buf, uchar *pos) override
required
Definition: ha_example.cc:507
int index_last(uchar *buf) override
We implement this in ha_example.cc.
Definition: ha_example.cc:417
int info(uint) override
required
Definition: ha_example.cc:552
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4572
A table definition from the master.
Definition: rpl_utility.h:248
This file includes constants used by all storage engines.
ha_key_alg
Definition: my_base.h:98
@ HA_KEY_ALG_HASH
Definition: my_base.h:110
ha_rkey_function
Definition: my_base.h:78
ulong key_part_map
Definition: my_base.h:1008
my_off_t ha_rows
Definition: my_base.h:1141
ha_extra_function
Definition: my_base.h:185
Header for compiler-dependent features.
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
unsigned char uchar
Definition: my_inttypes.h:52
static int record
Definition: mysqltest.cc:195
Definition: buf0block_hint.cc:30
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
mode
Definition: file_handle.h:61
required string key
Definition: replication_asynchronous_connection_failover.proto:60
#define HA_MAX_REC_LENGTH
Definition: handler.h:625
#define HA_BINLOG_STMT_CAPABLE
Definition: handler.h:348
case opt name
Definition: sslopt-case.h:29
Struct to hold information about the table that should be created.
Definition: handler.h:3201
This structure is shared between different table objects.
Definition: table.h:700
Definition: table.h:1405
Definition: thr_lock.h:124
Definition: thr_lock.h:139
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2733
Definition: my_base.h:1125
Definition: mysqlslap.cc:240
thr_lock_type
Definition: thr_lock.h:51
void thr_lock_delete(THR_LOCK *lock)
Definition: thr_lock.cc:323