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