This repository was archived by the owner on Dec 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathha_example.cc
1515 lines (1263 loc) · 44.4 KB
/
ha_example.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file ha_example.cc
@brief
The ha_example engine is a stubbed storage engine for example purposes only;
it does nothing at this point. Its purpose is to provide a source
code illustration of how to begin writing new storage engines; see also
/storage/example/ha_example.h.
@details
ha_example will let you create/open/delete tables, but
nothing further (for example, indexes are not supported nor can data
be stored in the table). Use this example as a template for
implementing the same functionality in your own storage engine. You
can enable the example storage engine in your build by doing the
following during your build process:<br> ./configure
--with-example-storage-engine
Once this is done, MySQL will let you create tables with:<br>
CREATE TABLE <table name> (...) ENGINE=EXAMPLE;
The example storage engine is set up to use table locks. It
implements an example "SHARE" that is inserted into a hash by table
name. You can use this to store information of state that any
example handler object will be able to see when it is using that
table.
Please read the object definition in ha_example.h before reading the rest
of this file.
@note
When you create an EXAMPLE table, the MySQL Server creates a table .frm
(format) file in the database directory, using the table name as the file
name as is customary with MySQL. No other files are created. To get an idea
of what occurs, here is an example select that would do a scan of an entire
table:
@code
ha_example::store_lock
ha_example::external_lock
ha_example::info
ha_example::rnd_init
ha_example::extra
ENUM HA_EXTRA_CACHE Cache record in HA_rrnd()
ha_example::rnd_next
ha_example::rnd_next
ha_example::rnd_next
ha_example::rnd_next
ha_example::rnd_next
ha_example::rnd_next
ha_example::rnd_next
ha_example::rnd_next
ha_example::rnd_next
ha_example::extra
ENUM HA_EXTRA_NO_CACHE End caching of records (def)
ha_example::external_lock
ha_example::extra
ENUM HA_EXTRA_RESET Reset database to after open
@endcode
Here you see that the example storage engine has 9 rows called before
rnd_next signals that it has reached the end of its data. Also note that
the table in question was already opened; had it not been open, a call to
ha_example::open() would also have been necessary. Calls to
ha_example::extra() are hints as to what will be occuring to the request.
A Longer Example can be found called the "Skeleton Engine" which can be
found on TangentOrg. It has both an engine and a full build environment
for building a pluggable storage engine.
Happy coding!<br>
-Brian
*/
#include "sql_class.h" // MYSQL_HANDLERTON_INTERFACE_VERSION
#include "ha_example.h"
#include "probes_mysql.h"
#include "sql_plugin.h"
static handler *example_create_handler(handlerton *hton,
TABLE_SHARE *table,
MEM_ROOT *mem_root);
handlerton *example_hton;
/* Interface to mysqld, to check system tables supported by SE */
static const char* example_system_database();
static bool example_is_supported_system_table(const char *db,
const char *table_name,
bool is_sql_layer_system_table);
Example_share::Example_share()
{
thr_lock_init(&lock);
}
static int example_init_func(void *p)
{
DBUG_ENTER("example_init_func");
example_hton = (handlerton *)p;
example_hton->state = SHOW_OPTION_YES;
example_hton->create = example_create_handler;
example_hton->flags = HTON_CAN_RECREATE;
example_hton->system_database = example_system_database;
example_hton->is_supported_system_table = example_is_supported_system_table;
DBUG_RETURN(0);
}
/**
@brief
Example of simple lock controls. The "share" it creates is a
structure we will pass to each example handler. Do you have to have
one of these? Well, you have pieces that are used for locking, and
they are needed to function.
*/
Example_share *ha_example::get_share()
{
Example_share *tmp_share;
DBUG_ENTER("ha_example::get_share()");
lock_shared_ha_data();
if (!(tmp_share = static_cast<Example_share*>(get_ha_share_ptr())))
{
tmp_share = new Example_share;
if (!tmp_share)
goto err;
set_ha_share_ptr(static_cast<Handler_share*>(tmp_share));
}
err:
unlock_shared_ha_data();
DBUG_RETURN(tmp_share);
}
static handler* example_create_handler(handlerton *hton,
TABLE_SHARE *table,
MEM_ROOT *mem_root)
{
return new (mem_root) ha_example(hton, table);
}
ha_example::ha_example(handlerton *hton, TABLE_SHARE *table_arg)
:handler(hton, table_arg)
{}
/**
@brief
If frm_error() is called then we will use this to determine
the file extensions that exist for the storage engine. This is also
used by the default rename_table and delete_table method in
handler.cc.
For engines that have two file name extentions (separate meta/index file
and data file), the order of elements is relevant. First element of engine
file name extentions array should be meta/index file extention. Second
element - data file extention. This order is assumed by
prepare_for_repair() when REPAIR TABLE ... USE_FRM is issued.
@see
rename_table method in handler.cc and
delete_table method in handler.cc
*/
static const char *ha_example_exts[] = {
".db",NullS
};
const char **ha_example::bas_ext() const
{
return ha_example_exts;
}
static int max(int a, int b) { return a > b ? a : b; }
static int primary_key_compare(const void *a, const void *b, void *p) {
TABLE_SHARE *share = (TABLE_SHARE *)p;
KEY *key_info = &share->key_info[0];
uchar *ap = (uchar *)a, *bp = (uchar *)b;
for (int i = 0; i < key_info->user_defined_key_parts; i++) {
KEY_PART_INFO *key_part = &key_info->key_part[i];
int off = 0;
if (key_part->null_bit) {
off++;
}
int cmp = key_part->field->key_cmp(ap + off, bp + off);
if (cmp)
return cmp;
ap += key_part->store_length;
bp += key_part->store_length;
}
return 0;
}
static int second_key_compare_prefix(const void *a, const void *b, void *p) {
int key_num = *(u8 *)a & 0x7f;
int key_numb = *(u8 *)b & 0x7f;
if (key_num != key_numb)
return key_num - key_numb;
uchar *ap = (uchar *)a + 1, *bp = (uchar *)b + 1;
TABLE_SHARE *share = (TABLE_SHARE *)p;
KEY *key_info = &share->key_info[key_num];
for (int i = 0; i < key_info->user_defined_key_parts; i++) {
KEY_PART_INFO *key_part = &key_info->key_part[i];
int off = 0;
if (key_part->null_bit) {
off++;
}
int cmp = key_part->field->key_cmp(ap + off, bp + off);
if (cmp)
return cmp;
ap += key_part->store_length;
bp += key_part->store_length;
}
return 0;
}
// TODO: prefix_retval
static int second_key_compare(const void *a, const void *b, void *p) {
int key_num = *(u8 *)a & 0x3f;
int key_numb = *(u8 *)b & 0x3f;
if (key_num != key_numb)
return key_num - key_numb;
uchar *ap = (uchar *)a + 1, *bp = (uchar *)b + 1;
int first_flag = *(u8*)b & 0x80;
int last_flag = *(u8*)b & 0x40;
assert((*(u8*)a & 0x80) == 0);
assert((first_flag&last_flag) == 0);
TABLE_SHARE *share = (TABLE_SHARE *)p;
KEY *key_info = &share->key_info[key_num];
for (int i = 0; i < key_info->user_defined_key_parts; i++) {
KEY_PART_INFO *key_part = &key_info->key_part[i];
int off = 0;
if (key_part->null_bit) {
off++;
}
int cmp = key_part->field->key_cmp(ap + off, bp + off);
if (cmp)
return cmp;
ap += key_part->store_length;
bp += key_part->store_length;
}
if (last_flag) return -1;
else if (first_flag) return 1;
return primary_key_compare(ap, bp, p);
}
/*
* @brief
* return the length of the key
* primary key: primary key only
* primary key length: primary_key_length
* normal key: 1B + key + primary key
* normal key length: 1 + this_key_length + primary_key_length + padding.
* padding = max_second_key_length - this_key_length
*
*/
void key_copy(uchar *to_key, const uchar *from_record, KEY *key_info) {
uint length;
KEY_PART_INFO *key_part;
uint key_length = key_info->key_length;
for (key_part = key_info->key_part; (int)key_length > 0; key_part++)
{
if (key_part->null_bit)
{
*to_key++ = MY_TEST(from_record[key_part->null_offset] &
key_part->null_bit);
key_length--;
}
if (key_part->key_part_flag & HA_BLOB_PART ||
key_part->key_part_flag & HA_VAR_LENGTH_PART)
{
key_length -= HA_KEY_BLOB_LENGTH;
length = key_length < key_part->length ? key_length : key_part->length;
memcpy(to_key, from_record + key_part->offset, length);
to_key += HA_KEY_BLOB_LENGTH;
}
else
{
length = key_length < key_part->length ? key_length : key_part->length;
memcpy(to_key, from_record + key_part->offset, length);
}
to_key += length;
key_length -= length;
}
}
void ha_example::pack_key(uchar *key, int key_num, const uchar *record) {
memset(key, 0, primary_key_length + max_second_key_length + 1);
if (key_num != 0) {
*key++ = key_num;
KEY *k = &table->key_info[key_num];
key_copy(key, record, k);
//key_copy(key, const_cast<uchar *>(record), k, 0);
key += table->key_info[key_num].key_length;
}
// primary key
//key_copy(key, const_cast<uchar *>(record), &table->key_info[0], 0);
key_copy(key, record, &table->key_info[0]);
}
int ha_example::pack_row(u8 *buf) {
uint max_len = table->s->reclength;
for (int i = 0; i < table->s->blob_fields; i++) {
Field_blob *blob = (Field_blob *)table->field[table->s->blob_field[i]];
max_len += blob->get_length();
}
if (buf_size < max_len) {
buf_size = max_len;
if (record_buffer != NULL)
free(record_buffer); // TODO check NULL
record_buffer = (u8 *)malloc(buf_size);
}
if (record_buffer == NULL) {
return -1;
}
u8 *to = record_buffer;
memcpy(to, buf, table->s->null_bytes);
to += table->s->null_bytes;
for (Field **field = table->field; *field; field++) {
if (!(*field)->is_null()) {
uint off = (*field)->offset(buf);
to = (*field)->pack(to, buf + off);
}
}
return to - record_buffer;
}
int ha_example::unpack_row(const void *from, u8 *to) {
memcpy(to, from, table->s->null_bytes);
u8 *p = (u8 *)from + table->s->null_bytes;
for (Field **field = table->field; *field; field++) {
if (!(*field)->is_null()) {
const uchar *q =
(*field)->unpack(to + (*field)->offset(to), p);
p += q - p;
}
}
return 0;
}
int ha_example::get_cur_row(int key, u8 *buf) {
if (key == 0) {
const void *p = primary_it.getValue();
if (p == NULL)
return HA_ERR_INTERNAL_ERROR;
unpack_row(p, buf);
}
else {
int key_len = table->key_info[key].key_length;
u8*sec_key = (u8*)sec_it.getKey();
if (key_read) {
key_restore(buf, sec_key + 1, table->key_info + key, key_len);
key_restore(buf, sec_key + 1 + key_len, table->key_info, primary_key_length);
return 0;
}
return rnd_pos(buf, sec_key + 1 + key_len);
}
return 0;
}
/*
Following handler function provides access to
system database specific to SE. This interface
is optional, so every SE need not implement it.
*/
const char* ha_example_system_database = NULL;
const char* example_system_database()
{
return ha_example_system_database;
}
/*
List of all system tables specific to the SE.
Array element would look like below,
{ "<database_name>", "<system table name>" },
The last element MUST be,
{ (const char*)NULL, (const char*)NULL }
This array is optional, so every SE need not implement it.
*/
static st_handler_tablename ha_example_system_tables[] = {
{(const char*)NULL, (const char*)NULL}
};
/**
@brief Check if the given db.tablename is a system table for this SE.
@param db Database name to check.
@param table_name table name to check.
@param is_sql_layer_system_table if the supplied db.table_name is a SQL
layer system table.
@return
@retval TRUE Given db.table_name is supported system table.
@retval FALSE Given db.table_name is not a supported system table.
*/
static bool example_is_supported_system_table(const char *db,
const char *table_name,
bool is_sql_layer_system_table)
{
st_handler_tablename *systab;
// Does this SE support "ALL" SQL layer system tables ?
if (is_sql_layer_system_table)
return false;
// Check if this is SE layer system tables
systab = ha_example_system_tables;
while (systab && systab->db)
{
if (systab->db == db &&
strcmp(systab->tablename, table_name) == 0)
return true;
systab++;
}
return false;
}
/**
@brief
Used for opening tables. The name will be the name of the file.
@details
A table is opened when it needs to be opened; e.g. when a request comes in
for a SELECT on the table (tables are not open and closed for each request,
they are cached).
Called from handler.cc by handler::ha_open(). The server opens all tables by
calling ha_open() which then calls the handler specific open().
@see
handler::ha_open() in handler.cc
*/
int ha_example::open(const char *name, int mode, uint test_if_locked)
{
DBUG_ENTER("ha_example::open");
if (!(share = get_share()))
DBUG_RETURN(1);
thr_lock_data_init(&share->lock, &lock, NULL);
// key length
primary_key_length = table->key_info[0].key_length;
max_second_key_length = 0;
for (int i = 1; i < table->s->keys; i++) {
if (table->key_info[i].key_length > max_second_key_length)
max_second_key_length = table->key_info[i].key_length;
}
char name_buf[1000];
// record
fn_format(name_buf, name, "", ".db", MY_REPLACE_EXT | MY_UNPACK_FILENAME);
p_btree = new BTree(primary_key_length, primary_key_compare, table->s);
if (!p_btree->open(name_buf)) {
delete p_btree;
p_btree = NULL;
DBUG_RETURN(HA_ERR_NO_SUCH_TABLE);
}
primary_it.setBTree(p_btree);
// second key
if (table->s->keys > 1) {
s_btree = new BTree(primary_key_length + max_second_key_length + 1,
second_key_compare, table->s, 0);
fn_format(name_buf, name, "", "_sec.db",
MY_REPLACE_EXT | MY_UNPACK_FILENAME);
if (!s_btree->open(name_buf)) {
delete p_btree;
delete s_btree;
p_btree = NULL;
s_btree = NULL;
DBUG_RETURN(HA_ERR_NO_SUCH_TABLE);
}
sec_it.setBTree(s_btree);
}
key_buffer = (u8 *)malloc(primary_key_length + max_second_key_length + 1);
DBUG_RETURN(0);
}
/**
@brief
Closes a table.
@details
Called from sql_base.cc, sql_select.cc, and table.cc. In sql_select.cc it is
only used to close up temporary tables or during the process where a
temporary table is converted over to being a myisam table.
For sql_base.cc look at close_data_tables().
@see
sql_base.cc, sql_select.cc and table.cc
*/
int ha_example::close(void)
{
DBUG_ENTER("ha_example::close");
if (p_btree != NULL)
delete p_btree;
if (s_btree != NULL)
delete s_btree;
DBUG_RETURN(0);
}
/**
@brief
write_row() inserts a row. No extra() hint is given currently if a bulk load
is happening. buf() is a byte array of data. You can use the field
information to extract the data from the native byte array type.
@details
Example of this would be:
@code
for (Field **field=table->field ; *field ; field++)
{
...
}
@endcode
See ha_tina.cc for an example of extracting all of the data as strings.
ha_berekly.cc has an example of how to store it intact by "packing" it
for ha_berkeley's own native storage type.
See the note for update_row() on auto_increments. This case also applies to
write_row().
Called from item_sum.cc, item_sum.cc, sql_acl.cc, sql_insert.cc,
sql_insert.cc, sql_select.cc, sql_table.cc, sql_udf.cc, and sql_update.cc.
@see
item_sum.cc, item_sum.cc, sql_acl.cc, sql_insert.cc,
sql_insert.cc, sql_select.cc, sql_table.cc, sql_udf.cc and sql_update.cc
*/
int ha_example::write_row(uchar *buf)
{
DBUG_ENTER("ha_example::write_row");
// pack row
// blob fields are kept outside of the row buffer
if (table->next_number_field && buf == table->record[0]) {
int error;
if (error = update_auto_increment())
DBUG_RETURN(error);
}
// row
int row_len = pack_row(buf);
if (row_len == -1)
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
// primary key
pack_key(ref, 0, buf);
int res = p_btree->put(ref, record_buffer, row_len);
if (res == -1)
DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
else if (res == 0) { // duplicate key
DBUG_RETURN(HA_ERR_FOUND_DUPP_KEY);
}
for (int idx = 1; idx < table->s->keys; idx++) {
pack_key(key_buffer, idx, buf);
if (s_btree->put(key_buffer, NULL, 0) != 1)
DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
}
/*p_btree->save();
s_btree->save();*/
DBUG_RETURN(0);
}
/**
@brief
Yes, update_row() does what you expect, it updates a row. old_data will have
the previous row record in it, while new_data will have the newest data in it.
Keep in mind that the server can do updates based on ordering if an ORDER BY
clause was used. Consecutive ordering is not guaranteed.
@details
Currently new_data will not have an updated auto_increament record. You can
do this for example by doing:
@code
if (table->next_number_field && record == table->record[0])
update_auto_increment();
@endcode
Called from sql_select.cc, sql_acl.cc, sql_update.cc, and sql_insert.cc.
@see
sql_select.cc, sql_acl.cc, sql_update.cc and sql_insert.cc
*/
int ha_example::update_row(const uchar *old_data, uchar *new_data)
{
DBUG_ENTER("ha_example::update_row");
if (table->next_number_field && new_data == table->record[0])
update_auto_increment();
int rc = delete_row(old_data);
if (rc != 0)
DBUG_RETURN(rc);
DBUG_RETURN(write_row(new_data));
}
/**
@brief
This will delete a row. buf will contain a copy of the row to be deleted.
The server will call this right after the current row has been called (from
either a previous rnd_nexT() or index call).
@details
If you keep a pointer to the last row or can access a primary key it will
make doing the deletion quite a bit easier. Keep in mind that the server does
not guarantee consecutive deletions. ORDER BY clauses can be used.
Called in sql_acl.cc and sql_udf.cc to manage internal table
information. Called in sql_delete.cc, sql_insert.cc, and
sql_select.cc. In sql_select it is used for removing duplicates
while in insert it is used for REPLACE calls.
@see
sql_acl.cc, sql_udf.cc, sql_delete.cc, sql_insert.cc and sql_select.cc
*/
int ha_example::delete_row(const uchar *buf)
{
DBUG_ENTER("ha_example::delete_row");
pack_key(ref, 0, buf);
BTree::Iterator it = p_btree->iterator();
if (!it.locate(ref, BTree::EXACT_KEY)) {
DBUG_RETURN(HA_ERR_KEY_NOT_FOUND);
}
it.remove();
it.setBTree(s_btree);
for (int idx = 1; idx < table->s->keys; idx++) {
pack_key(key_buffer, idx, buf);
if (!it.locate(key_buffer, BTree::EXACT_KEY)) {
DBUG_RETURN(HA_ERR_KEY_NOT_FOUND);
//exit(-1);
}
it.remove();
}
/*p_btree->save();
s_btree->save();*/
DBUG_RETURN(0);
}
/**
@brief
Positions an index cursor to the index specified in the handle. Fetches the
row if available. If the key value is null, begin at the first key of the
index.
*/
//int ha_example::index_read_map(uchar *buf, const uchar *key,
// key_part_map keypart_map __attribute__((unused)),
// enum ha_rkey_function find_flag
// __attribute__((unused)))
//{
// int rc;
// DBUG_ENTER("ha_example::index_read");
// MYSQL_INDEX_READ_ROW_START(table_share->db.str, table_share->table_name.str);
// rc= HA_ERR_WRONG_COMMAND;
// MYSQL_INDEX_READ_ROW_DONE(rc);
// DBUG_RETURN(rc);
//}
/**
@brief
Used to read forward through the index.
*/
int ha_example::index_read(uchar * buf, const uchar * key, uint key_len, ha_rkey_function find_flag)
{
DBUG_ENTER("ha_example::index_read");
table->status = STATUS_NOT_FOUND;
MYSQL_INDEX_READ_ROW_START(table_share->db.str, table_share->table_name.str);
int rc = 0;
if (active_index == 0) {
memcpy(key_buffer, key, key_len);
}
else {
key_buffer[0] = active_index;
memcpy(key_buffer + 1, key, key_len);
}
switch (find_flag) {
case HA_READ_KEY_EXACT:
if (active_index != 0) {
key_buffer[0] |= 0x80;
if (!sec_it.locate(key_buffer, BTree::KEY_OR_NEXT)) {
rc = HA_ERR_KEY_NOT_FOUND;
break;
}
if (second_key_compare_prefix(sec_it.getKey(), key_buffer, table->s)) {
rc = HA_ERR_KEY_NOT_FOUND;
}
break;
}
if (!primary_it.locate(key_buffer, BTree::EXACT_KEY))
rc = HA_ERR_KEY_NOT_FOUND;
break;
case HA_READ_KEY_OR_NEXT:
if (active_index != 0) {
key_buffer[0] |= 0x80;
if (!sec_it.locate(key_buffer, BTree::KEY_OR_NEXT)) {
rc = HA_ERR_KEY_NOT_FOUND;
}
break;
}
if (!primary_it.locate(key_buffer, BTree::KEY_OR_NEXT))
rc = HA_ERR_KEY_NOT_FOUND;
break;
case HA_READ_KEY_OR_PREV:
// TODO: key
if (active_index != 0) {
key_buffer[0] |= 0x80;
if (!sec_it.locate(key_buffer, BTree::KEY_OR_PREV)) {
rc = HA_ERR_KEY_NOT_FOUND;
}
break;
}
if (!primary_it.locate(key_buffer, BTree::KEY_OR_PREV))
rc = HA_ERR_KEY_NOT_FOUND;
break;
case HA_READ_AFTER_KEY:
// TODO: key
if (active_index != 0) {
key_buffer[0] |= 0x80;
if (!sec_it.locate(key_buffer, BTree::AFTER_KEY)) {
rc = HA_ERR_KEY_NOT_FOUND;
}
break;
}
if (!primary_it.locate(key_buffer, BTree::AFTER_KEY))
rc = HA_ERR_KEY_NOT_FOUND;
break;
case HA_READ_BEFORE_KEY:
if (active_index != 0) {
key_buffer[0] |= 0x80;
if (!sec_it.locate(key_buffer, BTree::BEFROE_KEY)) {
rc = HA_ERR_KEY_NOT_FOUND;
}
break;
}
if (!primary_it.locate(key_buffer, BTree::BEFROE_KEY))
rc = HA_ERR_KEY_NOT_FOUND;
break;
case HA_READ_PREFIX_LAST_OR_PREV:
if (active_index != 0) {
key_buffer[0] |= 0x40;
if (!sec_it.locate(key_buffer, BTree::KEY_OR_PREV)) {
rc = HA_ERR_KEY_NOT_FOUND;
}
break;
}
if (!primary_it.locate(key_buffer, BTree::KEY_OR_PREV))
rc = HA_ERR_KEY_NOT_FOUND;
break;
default:
rc = HA_ERR_WRONG_COMMAND;
break;
}
if (rc != 0)
DBUG_RETURN(rc);
rc = get_cur_row(active_index, buf);
table->status = 0;
MYSQL_INDEX_READ_ROW_DONE(rc);
DBUG_RETURN(rc);
}
int ha_example::index_next(uchar *buf)
{
int rc;
DBUG_ENTER("ha_example::index_next");
table->status = STATUS_NOT_FOUND;
MYSQL_INDEX_READ_ROW_START(table_share->db.str, table_share->table_name.str);
rc = 0;
if (active_index != 0) {
if (!sec_it.next())
rc = HA_ERR_END_OF_FILE;
}
else {
if (!primary_it.next())
rc = HA_ERR_END_OF_FILE;
}
if (rc == 0)
rc = get_cur_row(active_index, buf);
table->status = 0;
MYSQL_INDEX_READ_ROW_DONE(rc);
DBUG_RETURN(rc);
}
/**
@brief
Used to read backwards through the index.
*/
int ha_example::index_prev(uchar *buf)
{
int rc;
DBUG_ENTER("ha_example::index_prev");
table->status = STATUS_NOT_FOUND;
MYSQL_INDEX_READ_ROW_START(table_share->db.str, table_share->table_name.str);
rc = 0;
if (active_index != 0) {
if (!sec_it.prev())
rc = HA_ERR_END_OF_FILE;
}
else {
if (!primary_it.prev())
rc = HA_ERR_END_OF_FILE;
}
if (rc == 0)
rc = get_cur_row(active_index, buf);
table->status = 0;
MYSQL_INDEX_READ_ROW_DONE(rc);
DBUG_RETURN(rc);
}
/**
@brief
index_first() asks for the first key in the index.
@details
Called from opt_range.cc, opt_sum.cc, sql_handler.cc, and sql_select.cc.
@see
opt_range.cc, opt_sum.cc, sql_handler.cc and sql_select.cc
*/
int ha_example::index_first(uchar *buf)
{
DBUG_ENTER("ha_example::index_first");
table->status = STATUS_NOT_FOUND;
int rc = 0;
if (active_index != 0) {
if (!sec_it.first())
rc = HA_ERR_KEY_NOT_FOUND;
}
else {
if (!primary_it.first())
rc = HA_ERR_KEY_NOT_FOUND;
}
if (rc == 0)
rc = get_cur_row(active_index, buf);
table->status = 0;
DBUG_RETURN(rc);
}
/**
@brief
index_last() asks for the last key in the index.
@details
Called from opt_range.cc, opt_sum.cc, sql_handler.cc, and sql_select.cc.
@see
opt_range.cc, opt_sum.cc, sql_handler.cc and sql_select.cc
*/
int ha_example::index_last(uchar *buf)
{
DBUG_ENTER("ha_example::index_last");
table->status = STATUS_NOT_FOUND;
int rc = 0;
if (active_index != 0) {
if (!sec_it.last())
rc = HA_ERR_KEY_NOT_FOUND;
}
else {
if (!primary_it.last())
rc = HA_ERR_KEY_NOT_FOUND;
}
if (rc == 0)
rc = get_cur_row(active_index, buf);
table->status = 0;
DBUG_RETURN(rc);
}
/**
@brief
rnd_init() is called when the system wants the storage engine to do a table
scan. See the example in the introduction at the top of this file to see when
rnd_init() is called.
@details
Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc,
and sql_update.cc.
@see
filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc and sql_update.cc
*/
int ha_example::rnd_init(bool scan)
{
active_index = 0;
first_row = true;
return 0;
}
int ha_example::rnd_end()
{
DBUG_ENTER("ha_example::rnd_end");
first_row = false;
DBUG_RETURN(0);
}
/**
@brief
This is called for each row of the table scan. When you run out of records
you should return HA_ERR_END_OF_FILE. Fill buff up with the row information.
The Field structure for the table is the key to getting data into buf
in a manner that will allow the server to understand it.
@details
Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc,
and sql_update.cc.
@see
filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc and sql_update.cc
*/