-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
dict0dict.cc
5043 lines (4170 loc) · 132 KB
/
dict0dict.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) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2012, Facebook Inc.
Copyright (c) 2013, 2021, MariaDB Corporation.
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 Street, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
/******************************************************************//**
@file dict/dict0dict.cc
Data dictionary system
Created 1/8/1996 Heikki Tuuri
***********************************************************************/
#include <my_config.h>
#include <string>
#include "ha_prototypes.h"
#include <mysqld.h>
#include <strfunc.h>
#include "dict0dict.h"
#include "fts0fts.h"
#include "fil0fil.h"
#include <algorithm>
#include "sql_class.h"
#include "sql_table.h"
#include <mysql/service_thd_mdl.h>
#include "btr0btr.h"
#include "btr0cur.h"
#include "btr0sea.h"
#include "buf0buf.h"
#include "data0type.h"
#include "dict0boot.h"
#include "dict0load.h"
#include "dict0crea.h"
#include "dict0mem.h"
#include "dict0stats.h"
#include "fts0fts.h"
#include "fts0types.h"
#include "lock0lock.h"
#include "mach0data.h"
#include "mem0mem.h"
#include "page0page.h"
#include "page0zip.h"
#include "pars0pars.h"
#include "pars0sym.h"
#include "que0que.h"
#include "rem0cmp.h"
#include "row0log.h"
#include "row0merge.h"
#include "row0mysql.h"
#include "row0upd.h"
#include "srv0mon.h"
#include "srv0start.h"
#include "trx0undo.h"
#include <vector>
#include <algorithm>
/** the dictionary system */
dict_sys_t dict_sys;
/** System table names; @see dict_system_id_t */
const span<const char> dict_sys_t::SYS_TABLE[]=
{
{C_STRING_WITH_LEN("SYS_TABLES")},{C_STRING_WITH_LEN("SYS_INDEXES")},
{C_STRING_WITH_LEN("SYS_COLUMNS")},{C_STRING_WITH_LEN("SYS_FIELDS")},
{C_STRING_WITH_LEN("SYS_FOREIGN")},{C_STRING_WITH_LEN("SYS_FOREIGN_COLS")},
{C_STRING_WITH_LEN("SYS_VIRTUAL")}
};
/** Diagnostic message for exceeding the mutex_lock_wait() timeout */
const char dict_sys_t::fatal_msg[]=
"innodb_fatal_semaphore_wait_threshold was exceeded for dict_sys.mutex. "
"Please refer to "
"https://mariadb.com/kb/en/how-to-produce-a-full-stack-trace-for-mysqld/";
/** Percentage of compression failures that are allowed in a single
round */
ulong zip_failure_threshold_pct = 5;
/** Maximum percentage of a page that can be allowed as a pad to avoid
compression failures */
ulong zip_pad_max = 50;
#define DICT_HEAP_SIZE 100 /*!< initial memory heap size when
creating a table or index object */
#define DICT_POOL_PER_TABLE_HASH 512 /*!< buffer pool max size per table
hash table fixed size in bytes */
#define DICT_POOL_PER_VARYING 4 /*!< buffer pool max size per data
dictionary varying size in bytes */
/** Identifies generated InnoDB foreign key names */
static char dict_ibfk[] = "_ibfk_";
/*******************************************************************//**
Tries to find column names for the index and sets the col field of the
index.
@param[in] index index
@param[in] add_v new virtual columns added along with an add index call
@return whether the column names were found */
static
bool
dict_index_find_cols(
dict_index_t* index,
const dict_add_v_col_t* add_v);
/*******************************************************************//**
Builds the internal dictionary cache representation for a clustered
index, containing also system fields not defined by the user.
@return own: the internal representation of the clustered index */
static
dict_index_t*
dict_index_build_internal_clust(
/*============================*/
dict_index_t* index); /*!< in: user representation of
a clustered index */
/*******************************************************************//**
Builds the internal dictionary cache representation for a non-clustered
index, containing also system fields not defined by the user.
@return own: the internal representation of the non-clustered index */
static
dict_index_t*
dict_index_build_internal_non_clust(
/*================================*/
dict_index_t* index); /*!< in: user representation of
a non-clustered index */
/**********************************************************************//**
Builds the internal dictionary cache representation for an FTS index.
@return own: the internal representation of the FTS index */
static
dict_index_t*
dict_index_build_internal_fts(
/*==========================*/
dict_index_t* index); /*!< in: user representation of an FTS index */
/**********************************************************************//**
Removes an index from the dictionary cache. */
static
void
dict_index_remove_from_cache_low(
/*=============================*/
dict_table_t* table, /*!< in/out: table */
dict_index_t* index, /*!< in, own: index */
ibool lru_evict); /*!< in: TRUE if page being evicted
to make room in the table LRU list */
#ifdef UNIV_DEBUG
/**********************************************************************//**
Validate the dictionary table LRU list.
@return TRUE if validate OK */
static
ibool
dict_lru_validate(void);
/*===================*/
#endif /* UNIV_DEBUG */
/* Stream for storing detailed information about the latest foreign key
and unique key errors. Only created if !srv_read_only_mode */
FILE* dict_foreign_err_file = NULL;
/* mutex protecting the foreign and unique error buffers */
mysql_mutex_t dict_foreign_err_mutex;
/********************************************************************//**
Checks if the database name in two table names is the same.
@return TRUE if same db name */
ibool
dict_tables_have_same_db(
/*=====================*/
const char* name1, /*!< in: table name in the form
dbname '/' tablename */
const char* name2) /*!< in: table name in the form
dbname '/' tablename */
{
for (; *name1 == *name2; name1++, name2++) {
if (*name1 == '/') {
return(TRUE);
}
ut_a(*name1); /* the names must contain '/' */
}
return(FALSE);
}
/********************************************************************//**
Return the end of table name where we have removed dbname and '/'.
@return table name */
const char*
dict_remove_db_name(
/*================*/
const char* name) /*!< in: table name in the form
dbname '/' tablename */
{
const char* s = strchr(name, '/');
ut_a(s);
return(s + 1);
}
/** Open a persistent table.
@param[in] table_id persistent table identifier
@param[in] ignore_err errors to ignore
@param[in] cached_only whether to skip loading
@return persistent table
@retval NULL if not found */
static dict_table_t* dict_table_open_on_id_low(
table_id_t table_id,
dict_err_ignore_t ignore_err,
bool cached_only)
{
dict_table_t* table = dict_sys.find_table(table_id);
if (!table && !cached_only) {
table = dict_load_table_on_id(table_id, ignore_err);
}
return table;
}
/**********************************************************************//**
Try to drop any indexes after an aborted index creation.
This can also be after a server kill during DROP INDEX. */
static
void
dict_table_try_drop_aborted(
/*========================*/
dict_table_t* table, /*!< in: table, or NULL if it
needs to be looked up again */
table_id_t table_id, /*!< in: table identifier */
uint32_t ref_count) /*!< in: expected table->n_ref_count */
{
trx_t* trx;
trx = trx_create();
trx->op_info = "try to drop any indexes after an aborted index creation";
row_mysql_lock_data_dictionary(trx);
trx->dict_operation = true;
if (table == NULL) {
table = dict_table_open_on_id_low(
table_id, DICT_ERR_IGNORE_FK_NOKEY, FALSE);
} else {
ut_ad(table->id == table_id);
}
if (table && table->get_ref_count() == ref_count && table->drop_aborted
&& !UT_LIST_GET_FIRST(table->locks)) {
/* Silence a debug assertion in row_merge_drop_indexes(). */
ut_d(table->acquire());
row_merge_drop_indexes(trx, table, true);
ut_d(table->release());
ut_ad(table->get_ref_count() == ref_count);
trx_commit_for_mysql(trx);
}
row_mysql_unlock_data_dictionary(trx);
trx->free();
}
/**********************************************************************//**
When opening a table,
try to drop any indexes after an aborted index creation.
Release the dict_sys.mutex. */
static
void
dict_table_try_drop_aborted_and_mutex_exit(
/*=======================================*/
dict_table_t* table, /*!< in: table (may be NULL) */
ibool try_drop) /*!< in: FALSE if should try to
drop indexes whose online creation
was aborted */
{
if (try_drop
&& table != NULL
&& table->drop_aborted
&& table->get_ref_count() == 1
&& dict_table_get_first_index(table)) {
/* Attempt to drop the indexes whose online creation
was aborted. */
table_id_t table_id = table->id;
dict_sys.mutex_unlock();
dict_table_try_drop_aborted(table, table_id, 1);
} else {
dict_sys.mutex_unlock();
}
}
/** Decrements the count of open handles of a table.
@param[in,out] table table
@param[in] dict_locked data dictionary locked
@param[in] try_drop try to drop any orphan indexes after
an aborted online index creation
@param[in] thd thread to release MDL
@param[in] mdl metadata lock or NULL if the thread
is a foreground one. */
void
dict_table_close(
dict_table_t* table,
bool dict_locked,
bool try_drop,
THD* thd,
MDL_ticket* mdl)
{
if (!dict_locked) {
dict_sys.mutex_lock();
}
dict_sys.assert_locked();
ut_a(table->get_ref_count() > 0);
const bool last_handle = table->release();
/* Force persistent stats re-read upon next open of the table
so that FLUSH TABLE can be used to forcibly fetch stats from disk
if they have been manually modified. We reset table->stat_initialized
only if table reference count is 0 because we do not want too frequent
stats re-reads (e.g. in other cases than FLUSH TABLE). */
if (last_handle && strchr(table->name.m_name, '/') != NULL
&& dict_stats_is_persistent_enabled(table)) {
table->stats_mutex_lock();
dict_stats_deinit(table);
table->stats_mutex_unlock();
}
ut_ad(dict_lru_validate());
ut_ad(dict_sys.find(table));
if (!dict_locked) {
table_id_t table_id = table->id;
const bool drop_aborted = last_handle && try_drop
&& table->drop_aborted
&& dict_table_get_first_index(table);
dict_sys.mutex_unlock();
/* dict_table_try_drop_aborted() can generate undo logs.
So it should be avoided after shutdown of background
threads */
if (drop_aborted && !srv_undo_sources) {
dict_table_try_drop_aborted(NULL, table_id, 0);
}
}
if (!thd || !mdl) {
} else if (MDL_context *mdl_context= static_cast<MDL_context*>(
thd_mdl_context(thd))) {
mdl_context->release_lock(mdl);
}
}
/** Check if the table has a given (non_virtual) column.
@param[in] table table object
@param[in] col_name column name
@param[in] col_nr column number guessed, 0 as default
@return column number if the table has the specified column,
otherwise table->n_def */
ulint
dict_table_has_column(
const dict_table_t* table,
const char* col_name,
ulint col_nr)
{
ulint col_max = table->n_def;
ut_ad(table);
ut_ad(col_name);
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
if (col_nr < col_max
&& innobase_strcasecmp(
col_name, dict_table_get_col_name(table, col_nr)) == 0) {
return(col_nr);
}
/** The order of column may changed, check it with other columns */
for (ulint i = 0; i < col_max; i++) {
if (i != col_nr
&& innobase_strcasecmp(
col_name, dict_table_get_col_name(table, i)) == 0) {
return(i);
}
}
return(col_max);
}
/** Retrieve the column name.
@param[in] table the table of this column */
const char* dict_col_t::name(const dict_table_t& table) const
{
ut_ad(table.magic_n == DICT_TABLE_MAGIC_N);
size_t col_nr;
const char *s;
if (is_virtual()) {
col_nr = size_t(reinterpret_cast<const dict_v_col_t*>(this)
- table.v_cols);
ut_ad(col_nr < table.n_v_def);
s = table.v_col_names;
} else {
col_nr = size_t(this - table.cols);
ut_ad(col_nr < table.n_def);
s = table.col_names;
}
if (s) {
for (size_t i = 0; i < col_nr; i++) {
s += strlen(s) + 1;
}
}
return(s);
}
/** Returns a virtual column's name.
@param[in] table target table
@param[in] col_nr virtual column number (nth virtual column)
@return column name or NULL if column number out of range. */
const char*
dict_table_get_v_col_name(
const dict_table_t* table,
ulint col_nr)
{
const char* s;
ut_ad(table);
ut_ad(col_nr < table->n_v_def);
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
if (col_nr >= table->n_v_def) {
return(NULL);
}
s = table->v_col_names;
if (s != NULL) {
for (ulint i = 0; i < col_nr; i++) {
s += strlen(s) + 1;
}
}
return(s);
}
/** Search virtual column's position in InnoDB according to its position
in original table's position
@param[in] table target table
@param[in] col_nr column number (nth column in the MySQL table)
@return virtual column's position in InnoDB, ULINT_UNDEFINED if not find */
static
ulint
dict_table_get_v_col_pos_for_mysql(
const dict_table_t* table,
ulint col_nr)
{
ulint i;
ut_ad(table);
ut_ad(col_nr < static_cast<ulint>(table->n_t_def));
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
for (i = 0; i < table->n_v_def; i++) {
if (col_nr == dict_get_v_col_mysql_pos(
table->v_cols[i].m_col.ind)) {
break;
}
}
if (i == table->n_v_def) {
return(ULINT_UNDEFINED);
}
return(i);
}
/** Returns a virtual column's name according to its original
MySQL table position.
@param[in] table target table
@param[in] col_nr column number (nth column in the table)
@return column name. */
static
const char*
dict_table_get_v_col_name_mysql(
const dict_table_t* table,
ulint col_nr)
{
ulint i = dict_table_get_v_col_pos_for_mysql(table, col_nr);
if (i == ULINT_UNDEFINED) {
return(NULL);
}
return(dict_table_get_v_col_name(table, i));
}
/** Get nth virtual column according to its original MySQL table position
@param[in] table target table
@param[in] col_nr column number in MySQL Table definition
@return dict_v_col_t ptr */
dict_v_col_t*
dict_table_get_nth_v_col_mysql(
const dict_table_t* table,
ulint col_nr)
{
ulint i = dict_table_get_v_col_pos_for_mysql(table, col_nr);
if (i == ULINT_UNDEFINED) {
return(NULL);
}
return(dict_table_get_nth_v_col(table, i));
}
/** Get all the FTS indexes on a table.
@param[in] table table
@param[out] indexes all FTS indexes on this table
@return number of FTS indexes */
ulint
dict_table_get_all_fts_indexes(
const dict_table_t* table,
ib_vector_t* indexes)
{
dict_index_t* index;
ut_a(ib_vector_size(indexes) == 0);
for (index = dict_table_get_first_index(table);
index;
index = dict_table_get_next_index(index)) {
if (index->type == DICT_FTS) {
ib_vector_push(indexes, &index);
}
}
return(ib_vector_size(indexes));
}
/** Looks for column n in an index.
@param[in] index index
@param[in] n column number
@param[in] inc_prefix true=consider column prefixes too
@param[in] is_virtual true==virtual column
@param[out] prefix_col_pos col num if prefix
@return position in internal representation of the index;
ULINT_UNDEFINED if not contained */
ulint
dict_index_get_nth_col_or_prefix_pos(
const dict_index_t* index,
ulint n,
bool inc_prefix,
bool is_virtual,
ulint* prefix_col_pos)
{
const dict_field_t* field;
const dict_col_t* col;
ulint pos;
ulint n_fields;
ut_ad(index);
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
if (prefix_col_pos) {
*prefix_col_pos = ULINT_UNDEFINED;
}
if (is_virtual) {
col = &(dict_table_get_nth_v_col(index->table, n)->m_col);
} else {
col = dict_table_get_nth_col(index->table, n);
}
if (dict_index_is_clust(index)) {
return(dict_col_get_clust_pos(col, index));
}
n_fields = dict_index_get_n_fields(index);
for (pos = 0; pos < n_fields; pos++) {
field = dict_index_get_nth_field(index, pos);
if (col == field->col) {
if (prefix_col_pos) {
*prefix_col_pos = pos;
}
if (inc_prefix || field->prefix_len == 0) {
return(pos);
}
}
}
return(ULINT_UNDEFINED);
}
/** Check if the index contains a column or a prefix of that column.
@param[in] n column number
@param[in] is_virtual whether it is a virtual col
@return whether the index contains the column or its prefix */
bool dict_index_t::contains_col_or_prefix(ulint n, bool is_virtual) const
{
ut_ad(magic_n == DICT_INDEX_MAGIC_N);
if (is_primary()) {
return(!is_virtual);
}
const dict_col_t* col = is_virtual
? &dict_table_get_nth_v_col(table, n)->m_col
: dict_table_get_nth_col(table, n);
for (ulint pos = 0; pos < n_fields; pos++) {
if (col == fields[pos].col) {
return true;
}
}
return false;
}
/********************************************************************//**
Looks for a matching field in an index. The column has to be the same. The
column in index must be complete, or must contain a prefix longer than the
column in index2. That is, we must be able to construct the prefix in index2
from the prefix in index.
@return position in internal representation of the index;
ULINT_UNDEFINED if not contained */
ulint
dict_index_get_nth_field_pos(
/*=========================*/
const dict_index_t* index, /*!< in: index from which to search */
const dict_index_t* index2, /*!< in: index */
ulint n) /*!< in: field number in index2 */
{
const dict_field_t* field;
const dict_field_t* field2;
ulint n_fields;
ulint pos;
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
field2 = dict_index_get_nth_field(index2, n);
n_fields = dict_index_get_n_fields(index);
/* Are we looking for a MBR (Minimum Bound Box) field of
a spatial index */
bool is_mbr_fld = (n == 0 && dict_index_is_spatial(index2));
for (pos = 0; pos < n_fields; pos++) {
field = dict_index_get_nth_field(index, pos);
/* The first field of a spatial index is a transformed
MBR (Minimum Bound Box) field made out of original column,
so its field->col still points to original cluster index
col, but the actual content is different. So we cannot
consider them equal if neither of them is MBR field */
if (pos == 0 && dict_index_is_spatial(index) && !is_mbr_fld) {
continue;
}
if (field->col == field2->col
&& (field->prefix_len == 0
|| (field->prefix_len >= field2->prefix_len
&& field2->prefix_len != 0))) {
return(pos);
}
}
return(ULINT_UNDEFINED);
}
/** Parse the table file name into table name and database name.
@tparam dict_locked whether dict_sys.mutex is being held
@param[in,out] db_name database name buffer
@param[in,out] tbl_name table name buffer
@param[out] db_name_len database name length
@param[out] tbl_name_len table name length
@return whether the table name is visible to SQL */
template<bool dict_locked>
bool dict_table_t::parse_name(char (&db_name)[NAME_LEN + 1],
char (&tbl_name)[NAME_LEN + 1],
size_t *db_name_len, size_t *tbl_name_len) const
{
char db_buf[MAX_DATABASE_NAME_LEN + 1];
char tbl_buf[MAX_TABLE_NAME_LEN + 1];
if (!dict_locked)
dict_sys.mutex_lock(); /* protect against renaming */
dict_sys.assert_locked();
const size_t db_len= name.dblen();
ut_ad(db_len <= MAX_DATABASE_NAME_LEN);
memcpy(db_buf, mdl_name.m_name, db_len);
db_buf[db_len]= 0;
size_t tbl_len= strlen(mdl_name.m_name + db_len + 1);
const bool is_temp= mdl_name.is_temporary();
if (is_temp);
else if (const char *is_part= static_cast<const char*>
(memchr(mdl_name.m_name + db_len + 1, '#', tbl_len)))
tbl_len= static_cast<size_t>(is_part - &mdl_name.m_name[db_len + 1]);
memcpy(tbl_buf, mdl_name.m_name + db_len + 1, tbl_len);
tbl_buf[tbl_len]= 0;
if (!dict_locked)
dict_sys.mutex_unlock();
*db_name_len= filename_to_tablename(db_buf, db_name,
MAX_DATABASE_NAME_LEN + 1, true);
if (is_temp)
return false;
*tbl_name_len= filename_to_tablename(tbl_buf, tbl_name,
MAX_TABLE_NAME_LEN + 1, true);
return true;
}
template bool
dict_table_t::parse_name<>(char(&)[NAME_LEN + 1], char(&)[NAME_LEN + 1],
size_t*, size_t*) const;
/** Acquire MDL shared for the table name.
@tparam trylock whether to use non-blocking operation
@param[in,out] table table object
@param[in,out] thd background thread
@param[out] mdl mdl ticket
@param[in] table_op operation to perform when opening
@return table object after locking MDL shared
@retval nullptr if the table is not readable, or if trylock && MDL blocked */
template<bool trylock>
dict_table_t*
dict_acquire_mdl_shared(dict_table_t *table,
THD *thd,
MDL_ticket **mdl,
dict_table_op_t table_op)
{
if (!table || !mdl)
return table;
MDL_context *mdl_context= static_cast<MDL_context*>(thd_mdl_context(thd));
size_t db_len;
if (trylock)
{
dict_sys.mutex_lock();
db_len= dict_get_db_name_len(table->name.m_name);
dict_sys.mutex_unlock();
}
else
{
dict_sys.assert_locked();
db_len= dict_get_db_name_len(table->name.m_name);
}
if (db_len == 0)
return table; /* InnoDB system tables are not covered by MDL */
if (!mdl_context)
return nullptr;
table_id_t table_id= table->id;
char db_buf[NAME_LEN + 1], db_buf1[NAME_LEN + 1];
char tbl_buf[NAME_LEN + 1], tbl_buf1[NAME_LEN + 1];
size_t tbl_len;
bool unaccessible= false;
if (!table->parse_name<!trylock>(db_buf, tbl_buf, &db_len, &tbl_len))
/* The name of an intermediate table starts with #sql */
return table;
retry:
if (!unaccessible && (!table->is_readable() || table->corrupted))
{
is_unaccessible:
if (*mdl)
{
mdl_context->release_lock(*mdl);
*mdl= nullptr;
}
unaccessible= true;
}
if (!trylock)
table->release();
if (unaccessible)
return nullptr;
if (!trylock)
dict_sys.mutex_unlock();
{
MDL_request request;
MDL_REQUEST_INIT(&request,MDL_key::TABLE, db_buf, tbl_buf, MDL_SHARED, MDL_EXPLICIT);
if (trylock
? mdl_context->try_acquire_lock(&request)
: mdl_context->acquire_lock(&request,
/* FIXME: use compatible type, and maybe
remove this parameter altogether! */
static_cast<double>(global_system_variables
.lock_wait_timeout)))
{
*mdl= nullptr;
if (trylock)
return nullptr;
}
else
*mdl= request.ticket;
}
if (!trylock)
dict_sys.mutex_lock();
else if (!*mdl)
return nullptr;
table= dict_table_open_on_id(table_id, !trylock, table_op);
if (!table)
{
/* The table was dropped. */
if (*mdl)
{
mdl_context->release_lock(*mdl);
*mdl= nullptr;
}
return nullptr;
}
if (!table->is_accessible())
goto is_unaccessible;
size_t db1_len, tbl1_len;
if (!table->parse_name<!trylock>(db_buf1, tbl_buf1, &db1_len, &tbl1_len))
{
/* The table was renamed to #sql prefix.
Release MDL (if any) for the old name and return. */
if (*mdl)
{
mdl_context->release_lock(*mdl);
*mdl= nullptr;
}
return table;
}
if (*mdl)
{
if (db_len == db1_len && tbl_len == tbl1_len &&
!memcmp(db_buf, db_buf1, db_len) &&
!memcmp(tbl_buf, tbl_buf1, tbl_len))
return table;
/* The table was renamed. Release MDL for the old name and
try to acquire MDL for the new name. */
mdl_context->release_lock(*mdl);
*mdl= nullptr;
}
db_len= db1_len;
tbl_len= tbl1_len;
memcpy(tbl_buf, tbl_buf1, tbl_len + 1);
memcpy(db_buf, db_buf1, db_len + 1);
goto retry;
}
template dict_table_t*
dict_acquire_mdl_shared<true>(dict_table_t*,THD*,MDL_ticket**,dict_table_op_t);
/** Look up a table by numeric identifier.
@param[in] table_id table identifier
@param[in] dict_locked data dictionary locked
@param[in] table_op operation to perform when opening
@param[in,out] thd background thread, or NULL to not acquire MDL
@param[out] mdl mdl ticket, or NULL
@return table, NULL if does not exist */
dict_table_t*
dict_table_open_on_id(table_id_t table_id, bool dict_locked,
dict_table_op_t table_op, THD *thd,
MDL_ticket **mdl)
{
if (!dict_locked) {
dict_sys.mutex_lock();
}
dict_sys.assert_locked();
dict_table_t* table = dict_table_open_on_id_low(
table_id,
table_op == DICT_TABLE_OP_LOAD_TABLESPACE
? DICT_ERR_IGNORE_RECOVER_LOCK
: DICT_ERR_IGNORE_FK_NOKEY,
table_op == DICT_TABLE_OP_OPEN_ONLY_IF_CACHED);
if (table) {
dict_sys.acquire(table);
}
if (!dict_locked) {
if (thd) {
table = dict_acquire_mdl_shared<false>(
table, thd, mdl, table_op);
}
dict_table_try_drop_aborted_and_mutex_exit(
table, table_op == DICT_TABLE_OP_DROP_ORPHAN);
}
return table;
}
/********************************************************************//**
Looks for column n position in the clustered index.
@return position in internal representation of the clustered index */
unsigned
dict_table_get_nth_col_pos(
/*=======================*/
const dict_table_t* table, /*!< in: table */
ulint n, /*!< in: column number */
ulint* prefix_col_pos)
{
ulint pos= dict_index_get_nth_col_pos(dict_table_get_first_index(table),
n, prefix_col_pos);
DBUG_ASSERT(pos <= dict_index_t::MAX_N_FIELDS);
return static_cast<unsigned>(pos);
}
/********************************************************************//**
Checks if a column is in the ordering columns of the clustered index of a
table. Column prefixes are treated like whole columns.
@return TRUE if the column, or its prefix, is in the clustered key */
ibool
dict_table_col_in_clustered_key(
/*============================*/
const dict_table_t* table, /*!< in: table */
ulint n) /*!< in: column number */
{
const dict_index_t* index;
const dict_field_t* field;
const dict_col_t* col;
ulint pos;
ulint n_fields;
col = dict_table_get_nth_col(table, n);
index = dict_table_get_first_index(table);
n_fields = dict_index_get_n_unique(index);
for (pos = 0; pos < n_fields; pos++) {
field = dict_index_get_nth_field(index, pos);
if (col == field->col) {
return(TRUE);
}
}
return(FALSE);
}
/** Initialise the data dictionary cache. */
void dict_sys_t::create()
{
ut_ad(this == &dict_sys);
ut_ad(!is_initialised());
m_initialised= true;
UT_LIST_INIT(table_LRU, &dict_table_t::table_LRU);
UT_LIST_INIT(table_non_LRU, &dict_table_t::table_LRU);
mysql_mutex_init(dict_sys_mutex_key, &mutex, nullptr);
const ulint hash_size = buf_pool_get_curr_size()
/ (DICT_POOL_PER_TABLE_HASH * UNIV_WORD_SIZE);
table_hash.create(hash_size);
table_id_hash.create(hash_size);