-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.cc
More file actions
1039 lines (786 loc) · 26.4 KB
/
Copy pathhandler.cc
File metadata and controls
1039 lines (786 loc) · 26.4 KB
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) 2016, 2021, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2.0, as published by the
Free Software Foundation.
This program is also distributed with certain software (including but not
limited to OpenSSL) that is licensed under separate terms, as designated in a
particular file or component or in included license documentation. The authors
of MySQL hereby grant you an additional permission to link the program and
your derivative works with the separately licensed software that they have
included with MySQL.
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, version 2.0,
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 storage/temptable/src/handler.cc
TempTable public handler API implementation. */
#include "storage/temptable/include/temptable/handler.h"
#include "my_base.h"
#include "my_dbug.h"
#include "mysql/plugin.h"
#include "sql/mysqld.h"
#include "sql/sql_thd_internal_api.h"
#include "sql/system_variables.h"
#include "storage/temptable/include/temptable/row.h"
#include "storage/temptable/include/temptable/sharded_kv_store.h"
#include "storage/temptable/include/temptable/shared_block_pool.h"
namespace temptable {
/** Key-value store containing all tables for all existing connections.
* See `Sharded_key_value_store` documentation for more details.
* */
static Sharded_key_value_store<KV_STORE_SHARDS_COUNT> kv_store_shard;
/* Pool of shared-blocks, an external state to the custom `TempTable` memory
* allocator. See `Lock_free_shared_block_pool` documentation for more details.
* */
static Lock_free_shared_block_pool<SHARED_BLOCK_POOL_SIZE> shared_block_pool;
/** Small helper function which debug-prints the miscelaneous statistics which
* key-value store has collected.
* */
void kv_store_shards_debug_dump() { kv_store_shard.dbug_print(); }
/** Small helper function which releases the slot (and memory occupied by the
* Block) in shared-block pool.
* */
void shared_block_pool_release(THD *thd) {
shared_block_pool.try_release(thd_thread_id(thd));
}
#if defined(HAVE_WINNUMA)
/** Page size used in memory allocation. */
DWORD win_page_size;
#endif /* HAVE_WINNUMA */
#define DBUG_RET(result) return static_cast<int>(result)
Handler::Handler(handlerton *hton, TABLE_SHARE *table_share_arg)
: ::handler(hton, table_share_arg),
m_opened_table(),
m_shared_block(shared_block_pool.try_acquire(thd_thread_id(ha_thd()))),
m_rnd_iterator(),
m_rnd_iterator_is_positioned(),
m_index_cursor(),
m_index_read_number_of_cells(),
m_deleted_rows() {
handler::ref_length = sizeof(Storage::Element *);
// Overriding `handlerton::` with a function-pointer in `TempTable`, or
// any other plugin, is not always sufficient for server to actually invoke
// that hook. This is the behavior which is in contrast to what one would
// expect and this is not how most handlerton interfaces work. However, there
// is a subset of handlerton interfaces which require special care and in
// addition to overriding the function-pointer, they require initializing
// `handlerton::ha_data` with some data which is not a `nullptr`.
//
// `handlerton::close_connection()` is one of such interfaces, and being
// relied upon by the `TempTable` implementation, we have to fill in the
// `ha_data` with some existing data. In this particular case,
// `&shared_block_pool` is selected but from `TempTable` PoV this does not
// have any semantic value, e.g. it could very well have been something else.
//
// This is a bit confusing and unexpected, and hence this comment to make life
// easier for readers of this part of the code.
thd_set_ha_data(ha_thd(), hton, &shared_block_pool);
#if defined(HAVE_WINNUMA)
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
win_page_size = systemInfo.dwPageSize;
#endif /* HAVE_WINNUMA */
}
Handler::~Handler() {}
int Handler::create(const char *table_name, TABLE *mysql_table,
HA_CREATE_INFO *, dd::Table *) {
DBUG_TRACE;
assert(mysql_table != nullptr);
assert(mysql_table->s != nullptr);
assert(mysql_table->field != nullptr);
assert(table_name != nullptr);
bool all_columns_are_fixed_size = true;
for (uint i = 0; i < mysql_table->s->fields; ++i) {
Field *mysql_field = mysql_table->field[i];
assert(mysql_field != nullptr);
if (!is_field_type_fixed_size(*mysql_field)) {
all_columns_are_fixed_size = false;
break;
}
}
Result ret;
try {
DBUG_EXECUTE_IF("temptable_create_return_full",
throw Result::RECORD_FILE_FULL;);
DBUG_EXECUTE_IF("temptable_create_return_non_result_type_exception",
throw 42;);
auto &kv_store = kv_store_shard[thd_thread_id(ha_thd())];
const auto insert_result = kv_store.emplace(
std::piecewise_construct, std::forward_as_tuple(table_name),
std::forward_as_tuple(mysql_table, m_shared_block,
all_columns_are_fixed_size));
ret = insert_result.second ? Result::OK : Result::TABLE_EXIST;
} catch (Result ex) {
ret = ex;
} catch (...) {
ret = Result::OUT_OF_MEM;
}
DBUG_RET(ret);
}
int Handler::delete_table(const char *table_name, const dd::Table *) {
DBUG_TRACE;
assert(table_name != nullptr);
Result ret;
try {
auto &kv_store = kv_store_shard[thd_thread_id(ha_thd())];
auto table = kv_store.find(table_name);
if (table) {
if (m_opened_table != table) {
kv_store.erase(table_name);
ret = Result::OK;
} else {
/* Attempt to delete the currently opened table. */
ret = Result::UNSUPPORTED;
}
} else {
ret = Result::NO_SUCH_TABLE;
}
} catch (Result ex) {
ret = ex;
} catch (std::bad_alloc &) {
ret = Result::OUT_OF_MEM;
}
DBUG_PRINT("temptable_api", ("this=%p %s; return=%s", this, table_name,
result_to_string(ret)));
DBUG_RET(ret);
}
int Handler::open(const char *table_name, int, uint, const dd::Table *) {
DBUG_TRACE;
assert(m_opened_table == nullptr);
assert(table_name != nullptr);
assert(!m_rnd_iterator_is_positioned);
assert(!m_index_cursor.is_positioned());
assert(handler::active_index == MAX_KEY);
Result ret;
try {
auto &kv_store = kv_store_shard[thd_thread_id(ha_thd())];
m_opened_table = kv_store.find(table_name);
if (m_opened_table) {
ret = Result::OK;
opened_table_validate();
} else {
ret = Result::NO_SUCH_TABLE;
}
} catch (std::bad_alloc &) {
ret = Result::OUT_OF_MEM;
}
DBUG_PRINT("temptable_api", ("this=%p %s; return=%s", this, table_name,
result_to_string(ret)));
DBUG_RET(ret);
}
int Handler::close() {
DBUG_TRACE;
assert(m_opened_table != nullptr);
m_opened_table = nullptr;
handler::active_index = MAX_KEY;
m_rnd_iterator_is_positioned = false;
m_index_cursor.unposition();
/* Marked unused - temporary fix for GCC 8 bug 82728. */
const Result ret TEMPTABLE_UNUSED = Result::OK;
DBUG_PRINT("temptable_api",
("this=%p; return=%s", this, result_to_string(ret)));
DBUG_RET(ret);
}
int Handler::rnd_init(bool) {
DBUG_TRACE;
m_rnd_iterator_is_positioned = false;
/* Marked unused - temporary fix for GCC 8 bug 82728. */
const Result ret TEMPTABLE_UNUSED = Result::OK;
DBUG_PRINT("temptable_api",
("this=%p; return=%s", this, result_to_string(ret)));
DBUG_RET(ret);
}
int Handler::rnd_next(uchar *mysql_row) {
DBUG_TRACE;
opened_table_validate();
handler::ha_statistic_increment(&System_status_var::ha_read_rnd_next_count);
const Storage &rows = m_opened_table->rows();
Result ret;
if (!m_rnd_iterator_is_positioned) {
/* This is the first call to `rnd_next()` */
m_rnd_iterator = rows.begin();
if (m_rnd_iterator != rows.end()) {
m_rnd_iterator_is_positioned = true;
m_opened_table->row(m_rnd_iterator, mysql_row);
ret = Result::OK;
} else {
ret = Result::END_OF_FILE;
}
} else {
assert(m_rnd_iterator != rows.end());
Storage::Element *previous = *m_rnd_iterator;
++m_rnd_iterator;
if (m_rnd_iterator != rows.end()) {
m_opened_table->row(m_rnd_iterator, mysql_row);
ret = Result::OK;
} else {
/* Undo the ++ operation above. The expectation of the users of the
* API is that if we hit the end and then new rows are inserted and then
* `rnd_next()` is called again - that it will fetch the newly inserted
* rows. For example: let the table have 2 rows: "a" and "b", then:
* 1. `rnd_next()` moves to "b" and returns it to the caller
* 2. `rnd_next()` returns END_OF_FILE, but keeps the cursor at "b", it
* does not advance it past the end
* 3. possibly more calls to `rnd_next()`, they act as in 2.
* 4. another row is inserted: "c"
* 5. `rnd_next()` moves to "c" and returns it to the caller
* If we do not undo the ++ and let the cursor move past the last
* element then we will miss the first newly inserted row in the above
* scenario:
* 1. `rnd_next()` moves to "b" and returns it to the caller
* 2. `rnd_next()` moves after "b" and returns END_OF_FILE
* 3. two rows are inserted: "c" and "d" (the cursor now points to "c")
* 4. `rnd_next()` moves to "d" and returns it to the caller
* 5. "c" has been erroneously skipped */
m_rnd_iterator = previous;
ret = Result::END_OF_FILE;
}
}
DBUG_RET(ret);
}
int Handler::rnd_pos(uchar *mysql_row, uchar *position) {
DBUG_TRACE;
opened_table_validate();
handler::ha_statistic_increment(&System_status_var::ha_read_rnd_count);
Storage::Element *row = *reinterpret_cast<Storage::Element **>(position);
m_rnd_iterator = Storage::Iterator(&m_opened_table->rows(), row);
m_rnd_iterator_is_positioned = true;
m_opened_table->row(m_rnd_iterator, mysql_row);
/* Marked unused - temporary fix for GCC 8 bug 82728. */
const Result ret TEMPTABLE_UNUSED = Result::OK;
DBUG_RET(ret);
}
int Handler::rnd_end() {
DBUG_TRACE;
m_rnd_iterator_is_positioned = false;
/* Marked unused - temporary fix for GCC 8 bug 82728. */
const Result ret TEMPTABLE_UNUSED = Result::OK;
DBUG_PRINT("temptable_api",
("this=%p; return=%s", this, result_to_string(ret)));
DBUG_RET(ret);
}
int Handler::index_init(uint index_no, bool) {
DBUG_TRACE;
opened_table_validate();
Result ret;
if (index_no >= m_opened_table->number_of_indexes()) {
ret = Result::WRONG_INDEX;
} else {
handler::active_index = index_no;
ret = Result::OK;
}
DBUG_PRINT("temptable_api", ("this=%p index=%d; return=%s", this, index_no,
result_to_string(ret)));
DBUG_RET(ret);
}
int Handler::index_read(uchar *mysql_row, const uchar *mysql_search_cells,
uint mysql_search_cells_len_bytes,
ha_rkey_function find_flag) {
DBUG_TRACE;
opened_table_validate();
handler::ha_statistic_increment(&System_status_var::ha_read_key_count);
assert(handler::active_index < m_opened_table->number_of_indexes());
Result ret = Result::UNSUPPORTED;
try {
const Index &index = m_opened_table->index(handler::active_index);
Indexed_cells search_cells(mysql_search_cells, mysql_search_cells_len_bytes,
index);
switch (find_flag) {
case HA_READ_KEY_EXACT:
switch (index.lookup(search_cells, &m_index_cursor)) {
case Index::Lookup::FOUND:
ret = Result::OK;
break;
case Index::Lookup::NOT_FOUND_CURSOR_POSITIONED_ON_NEXT:
case Index::Lookup::NOT_FOUND_CURSOR_UNDEFINED:
ret = Result::KEY_NOT_FOUND;
break;
}
break;
case HA_READ_AFTER_KEY:
ret = Result::UNSUPPORTED;
break;
case HA_READ_KEY_OR_NEXT:
if (handler::table->s->key_info[handler::active_index].algorithm !=
HA_KEY_ALG_BTREE) {
ret = Result::UNSUPPORTED;
break;
}
switch (index.lookup(search_cells, &m_index_cursor)) {
case Index::Lookup::FOUND:
case Index::Lookup::NOT_FOUND_CURSOR_POSITIONED_ON_NEXT:
ret = Result::OK;
break;
case Index::Lookup::NOT_FOUND_CURSOR_UNDEFINED:
ret = Result::KEY_NOT_FOUND;
break;
}
break;
case HA_READ_PREFIX_LAST: {
if (handler::table->s->key_info[handler::active_index].algorithm !=
HA_KEY_ALG_BTREE) {
ret = Result::UNSUPPORTED;
break;
}
Cursor first_unused;
switch (index.lookup(search_cells, &first_unused, &m_index_cursor)) {
case Index::Lookup::FOUND:
/* m_index_cursor is positioned after the last matching element. */
--m_index_cursor;
ret = Result::OK;
break;
case Index::Lookup::NOT_FOUND_CURSOR_POSITIONED_ON_NEXT:
case Index::Lookup::NOT_FOUND_CURSOR_UNDEFINED:
ret = Result::KEY_NOT_FOUND;
break;
}
break;
}
case HA_READ_KEY_OR_PREV:
case HA_READ_BEFORE_KEY:
case HA_READ_PREFIX:
case HA_READ_PREFIX_LAST_OR_PREV:
case HA_READ_MBR_CONTAIN:
case HA_READ_MBR_INTERSECT:
case HA_READ_MBR_WITHIN:
case HA_READ_MBR_DISJOINT:
case HA_READ_MBR_EQUAL:
case HA_READ_INVALID:
ret = Result::UNSUPPORTED;
break;
}
if (ret == Result::OK) {
m_index_cursor.export_row_to_mysql(m_opened_table->columns(), mysql_row,
handler::table->s->rec_buff_length);
m_index_read_number_of_cells = search_cells.number_of_cells();
}
} catch (Result ex) {
ret = ex;
} catch (std::bad_alloc &) {
ret = Result::OUT_OF_MEM;
}
DBUG_RET(ret);
}
int Handler::index_next(uchar *mysql_row) {
DBUG_TRACE;
opened_table_validate();
handler::ha_statistic_increment(&System_status_var::ha_read_next_count);
const Result ret = index_next_conditional(mysql_row, NextCondition::NO);
DBUG_RET(ret);
}
int Handler::index_next_same(uchar *mysql_row, const uchar *, uint) {
DBUG_TRACE;
opened_table_validate();
handler::ha_statistic_increment(&System_status_var::ha_read_next_count);
const Result ret =
index_next_conditional(mysql_row, NextCondition::ONLY_IF_SAME);
DBUG_RET(ret);
}
Result Handler::index_next_conditional(uchar *mysql_row,
NextCondition condition) {
DBUG_TRACE;
opened_table_validate();
assert(m_index_cursor.is_positioned());
Result ret;
try {
const Index &index = m_opened_table->index(handler::active_index);
const Cursor &end = index.end();
if (m_index_cursor == end) {
ret = Result::END_OF_FILE;
} else {
Indexed_cells indexed_cells_previous = m_index_cursor.indexed_cells();
/* Lower the number of cells to what was given to `index_read()`. */
assert(m_index_read_number_of_cells <=
indexed_cells_previous.number_of_cells());
indexed_cells_previous.number_of_cells(m_index_read_number_of_cells);
++m_index_cursor;
if (m_index_cursor == end) {
ret = Result::END_OF_FILE;
} else {
bool ok = false;
switch (condition) {
case NextCondition::NO:
ok = true;
break;
case NextCondition::ONLY_IF_SAME: {
const Indexed_cells &indexed_cells_current =
m_index_cursor.indexed_cells();
const Indexed_cells_equal_to comparator{index};
/* indexed_cells_previous == indexed_cells_current */
ok = comparator(indexed_cells_previous, indexed_cells_current);
break;
}
}
if (ok) {
m_index_cursor.export_row_to_mysql(
m_opened_table->columns(), mysql_row,
handler::table->s->rec_buff_length);
ret = Result::OK;
} else {
ret = Result::END_OF_FILE;
}
}
}
if (ret != Result::OK) {
m_index_cursor.unposition();
}
} catch (Result ex) {
ret = ex;
} catch (std::bad_alloc &) {
ret = Result::OUT_OF_MEM;
}
return ret;
}
int Handler::index_read_last(uchar *mysql_row, const uchar *mysql_search_cells,
uint mysql_search_cells_len_bytes) {
DBUG_TRACE;
opened_table_validate();
const Result ret = static_cast<Result>(
index_read(mysql_row, mysql_search_cells, mysql_search_cells_len_bytes,
HA_READ_PREFIX_LAST));
DBUG_RET(ret);
}
int Handler::index_prev(uchar *mysql_row) {
DBUG_TRACE;
opened_table_validate();
assert(m_index_cursor.is_positioned());
handler::ha_statistic_increment(&System_status_var::ha_read_prev_count);
Result ret;
try {
const Cursor &begin = m_opened_table->index(handler::active_index).begin();
if (handler::table->s->key_info[handler::active_index].algorithm !=
HA_KEY_ALG_BTREE) {
ret = Result::UNSUPPORTED;
} else if (m_index_cursor == begin) {
ret = Result::END_OF_FILE;
} else {
--m_index_cursor;
m_index_cursor.export_row_to_mysql(m_opened_table->columns(), mysql_row,
handler::table->s->rec_buff_length);
ret = Result::OK;
}
} catch (Result ex) {
ret = ex;
} catch (std::bad_alloc &) {
ret = Result::OUT_OF_MEM;
}
DBUG_RET(ret);
}
int Handler::index_end() {
DBUG_TRACE;
handler::active_index = MAX_KEY;
m_index_cursor.unposition();
/* Marked unused - temporary fix for GCC 8 bug 82728. */
const Result ret TEMPTABLE_UNUSED = Result::OK;
DBUG_PRINT("temptable_api",
("this=%p; return=%s", this, result_to_string(ret)));
DBUG_RET(ret);
}
void Handler::position(const uchar *) {
DBUG_TRACE;
Storage::Element *row;
if (m_rnd_iterator_is_positioned) {
assert(!m_index_cursor.is_positioned());
row = *m_rnd_iterator;
} else {
assert(m_index_cursor.is_positioned());
row = m_index_cursor.row();
}
*reinterpret_cast<Storage::Element **>(handler::ref) = row;
DBUG_PRINT("temptable_api", ("this=%p; saved position=%p", this, row));
}
int Handler::write_row(uchar *mysql_row) {
DBUG_TRACE;
opened_table_validate();
handler::ha_statistic_increment(&System_status_var::ha_write_count);
const Result ret = m_opened_table->insert(mysql_row);
DBUG_RET(ret);
}
int Handler::update_row(const uchar *mysql_row_old, uchar *mysql_row_new) {
DBUG_TRACE;
opened_table_validate();
handler::ha_statistic_increment(&System_status_var::ha_update_count);
Storage::Element *target_row;
if (m_rnd_iterator_is_positioned) {
assert(!m_index_cursor.is_positioned());
target_row = *m_rnd_iterator;
} else {
assert(m_index_cursor.is_positioned());
target_row = m_index_cursor.row();
}
const Result ret =
m_opened_table->update(mysql_row_old, mysql_row_new, target_row);
DBUG_RET(ret);
}
int Handler::delete_row(const uchar *mysql_row) {
DBUG_TRACE;
opened_table_validate();
assert(m_rnd_iterator_is_positioned);
ha_statistic_increment(&System_status_var::ha_delete_count);
const Storage::Iterator victim_position = m_rnd_iterator;
/* Move `m_rnd_iterator` to the preceding position. */
if (m_rnd_iterator == m_opened_table->rows().begin()) {
/* Position before the first. */
m_rnd_iterator_is_positioned = false;
} else {
--m_rnd_iterator;
}
const Result ret = m_opened_table->remove(mysql_row, victim_position);
if (ret == Result::OK) {
++m_deleted_rows;
}
DBUG_RET(ret);
}
int Handler::truncate(dd::Table *) {
DBUG_TRACE;
opened_table_validate();
m_opened_table->truncate();
m_rnd_iterator_is_positioned = false;
m_index_cursor.unposition();
/* Marked unused - temporary fix for GCC 8 bug 82728. */
const Result ret TEMPTABLE_UNUSED = Result::OK;
DBUG_PRINT("temptable_api",
("this=%p; return=%s", this, result_to_string(ret)));
DBUG_RET(ret);
}
int Handler::delete_all_rows() {
DBUG_TRACE;
return truncate(nullptr);
}
int Handler::info(uint) {
DBUG_TRACE;
opened_table_validate();
stats.deleted = m_deleted_rows;
stats.records = m_opened_table->number_of_rows();
stats.table_in_mem_estimate = 1.0;
for (uint i = 0; i < table->s->keys; ++i) {
KEY *key = &table->key_info[i];
key->set_in_memory_estimate(1.0);
}
/* Marked unused - temporary fix for GCC 8 bug 82728. */
const Result ret TEMPTABLE_UNUSED = Result::OK;
DBUG_PRINT("temptable_api", ("this=%p out=(stats.records=%llu); return=%s",
this, stats.records, result_to_string(ret)));
DBUG_RET(ret);
}
longlong Handler::get_memory_buffer_size() const {
DBUG_TRACE;
DBUG_PRINT("temptable_api",
("this=%p; return=%lld", this, temptable_max_ram));
return temptable_max_ram;
}
const char *Handler::table_type() const {
DBUG_TRACE;
return "TempTable";
}
::handler::Table_flags Handler::table_flags() const {
DBUG_TRACE;
// clang-format off
const Table_flags flags =
HA_NO_TRANSACTIONS |
HA_CAN_GEOMETRY |
HA_FAST_KEY_READ |
HA_NULL_IN_KEY |
HA_CAN_INDEX_BLOBS |
HA_STATS_RECORDS_IS_EXACT |
HA_NO_AUTO_INCREMENT |
HA_COUNT_ROWS_INSTANT;
// clang-format on
DBUG_PRINT("temptable_api", ("this=%p; return=%lld", this, flags));
return flags;
}
ulong Handler::index_flags(uint index_no, uint, bool) const {
DBUG_TRACE;
ulong flags = 0;
switch (table_share->key_info[index_no].algorithm) {
case HA_KEY_ALG_BTREE:
// clang-format off
flags =
HA_READ_NEXT |
HA_READ_PREV |
HA_READ_ORDER |
HA_READ_RANGE |
HA_KEY_SCAN_NOT_ROR;
// clang-format on
break;
case HA_KEY_ALG_HASH:
// clang-format off
flags =
HA_READ_NEXT |
HA_ONLY_WHOLE_INDEX |
HA_KEY_SCAN_NOT_ROR;
// clang-format on
break;
case HA_KEY_ALG_SE_SPECIFIC:
case HA_KEY_ALG_RTREE:
case HA_KEY_ALG_FULLTEXT:
flags = 0;
break;
}
DBUG_PRINT("temptable_api", ("this=%p; return=%lu", this, flags));
return flags;
}
ha_key_alg Handler::get_default_index_algorithm() const {
DBUG_TRACE;
return HA_KEY_ALG_HASH;
}
bool Handler::is_index_algorithm_supported(ha_key_alg algorithm) const {
DBUG_TRACE;
return algorithm == HA_KEY_ALG_BTREE || algorithm == HA_KEY_ALG_HASH;
}
uint Handler::max_supported_key_length() const {
DBUG_TRACE;
const uint length = std::numeric_limits<uint>::max();
DBUG_PRINT("temptable_api", ("this=%p; return=%u", this, length));
return length;
}
uint Handler::max_supported_key_part_length(
HA_CREATE_INFO *create_info MY_ATTRIBUTE((unused))) const {
DBUG_TRACE;
const uint length = std::numeric_limits<uint>::max();
DBUG_PRINT("temptable_api", ("this=%p; return=%u", this, length));
return length;
}
#if 0
/* This is disabled in order to mimic ha_heap's implementation which relies on
* the method from the parent class which adds a magic +10. */
ha_rows Handler::estimate_rows_upper_bound() {
DBUG_TRACE;
assert(m_opened_table != nullptr);
const ha_rows n = m_opened_table->number_of_rows();
DBUG_PRINT("temptable_api", ("this=%p; return=%llu", this, n));
return n;
}
#endif
THR_LOCK_DATA **Handler::store_lock(THD *, THR_LOCK_DATA **, thr_lock_type) {
DBUG_TRACE;
return nullptr;
}
double Handler::scan_time() {
DBUG_TRACE;
/* Mimic ha_heap::scan_time() to avoid a storm of execution plan changes. */
const double t = (stats.records + stats.deleted) / 20.0 + 10;
DBUG_PRINT("temptable_api", ("this=%p; return=%.4lf", this, t));
return t;
}
double Handler::read_time(uint, uint, ha_rows rows) {
DBUG_TRACE;
/* Mimic ha_heap::read_time() to avoid a storm of execution plan changes. */
const double t = rows / 20.0 + 1;
DBUG_PRINT("temptable_api", ("this=%p; return=%.4lf", this, t));
return t;
}
int Handler::disable_indexes(uint mode) {
DBUG_TRACE;
assert(m_opened_table != nullptr);
Result ret;
if (mode == HA_KEY_SWITCH_ALL) {
ret = m_opened_table->disable_indexes();
} else {
ret = Result::WRONG_COMMAND;
}
DBUG_PRINT("temptable_api",
("this=%p; return=%s", this, result_to_string(ret)));
DBUG_RET(ret);
}
int Handler::enable_indexes(uint mode) {
DBUG_TRACE;
Result ret;
if (mode == HA_KEY_SWITCH_ALL) {
ret = m_opened_table->enable_indexes();
} else {
ret = Result::WRONG_COMMAND;
}
DBUG_PRINT("temptable_api",
("this=%p; return=%s", this, result_to_string(ret)));
DBUG_RET(ret);
}
/* Not implemented methods. */
int Handler::external_lock(THD *, int) {
DBUG_TRACE;
DBUG_ABORT();
return 0;
}
void Handler::unlock_row() { DBUG_TRACE; }
handler *Handler::clone(const char *, MEM_ROOT *) {
DBUG_TRACE;
DBUG_ABORT();
return nullptr;
}
int Handler::index_first(uchar *) {
DBUG_TRACE;
DBUG_ABORT();
return 0;
}
int Handler::index_last(uchar *) {
DBUG_TRACE;
DBUG_ABORT();
return 0;
}
int Handler::analyze(THD *, HA_CHECK_OPT *) {
DBUG_TRACE;
DBUG_ABORT();
return 0;
}
int Handler::optimize(THD *, HA_CHECK_OPT *) {
DBUG_TRACE;
DBUG_ABORT();
return 0;
}
int Handler::check(THD *, HA_CHECK_OPT *) {
DBUG_TRACE;
DBUG_ABORT();
return 0;
}
int Handler::start_stmt(THD *, thr_lock_type) {
DBUG_TRACE;
DBUG_ABORT();
return 0;
}
int Handler::reset() {
DBUG_TRACE;
DBUG_ABORT();
return 0;
}
int Handler::records(ha_rows *) {
DBUG_TRACE;
DBUG_ABORT();
return 0;
}
void Handler::update_create_info(HA_CREATE_INFO *) {
DBUG_TRACE;