/
dict0dict.cc
7404 lines (6107 loc) · 196 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, 2017, 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, Suite 500, 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>
/** dummy index for ROW_FORMAT=REDUNDANT supremum and infimum records */
dict_index_t* dict_ind_redundant;
#if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG
/** Flag to control insert buffer debugging. */
extern uint ibuf_debug;
#endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */
/**********************************************************************
Issue a warning that the row is too big. */
void
ib_warn_row_too_big(const dict_table_t* table);
#include "btr0btr.h"
#include "btr0cur.h"
#include "btr0sea.h"
#include "buf0buf.h"
#include "data0type.h"
#include "dict0boot.h"
#include "dict0crea.h"
#include "dict0mem.h"
#include "dict0priv.h"
#include "dict0stats.h"
#include "fsp0sysspace.h"
#include "fts0fts.h"
#include "fts0types.h"
#include "lock0lock.h"
#include "mach0data.h"
#include "mem0mem.h"
#include "os0once.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 "sync0sync.h"
#include "trx0undo.h"
#include "ut0new.h"
#include <vector>
#include <algorithm>
/** the dictionary system */
dict_sys_t* dict_sys = NULL;
/** @brief the data dictionary rw-latch protecting dict_sys
table create, drop, etc. reserve this in X-mode; implicit or
backround operations purge, rollback, foreign key checks reserve this
in S-mode; we cannot trust that MySQL protects implicit or background
operations a table drop since MySQL does not know of them; therefore
we need this; NOTE: a transaction which reserves this must keep book
on the mode in trx_t::dict_operation_lock_mode */
rw_lock_t* dict_operation_lock;
/** 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_";
bool innodb_table_stats_not_found = false;
bool innodb_index_stats_not_found = false;
static bool innodb_table_stats_not_found_reported = false;
static bool innodb_index_stats_not_found_reported = false;
/*******************************************************************//**
Tries to find column names for the index and sets the col field of the
index.
@param[in] table table
@param[in] index index
@param[in] add_v new virtual columns added along with an add index call
@return TRUE if the column names were found */
static
ibool
dict_index_find_cols(
const dict_table_t* table,
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(
/*============================*/
const dict_table_t* table, /*!< in: table */
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(
/*================================*/
const dict_table_t* table, /*!< in: table */
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_table_t* table, /*!< in: table */
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);
/*===================*/
/**********************************************************************//**
Check if table is in the dictionary table LRU list.
@return TRUE if table found */
static
ibool
dict_lru_find_table(
/*================*/
const dict_table_t* find_table); /*!< in: table to find */
/**********************************************************************//**
Check if a table exists in the dict table non-LRU list.
@return TRUE if table found */
static
ibool
dict_non_lru_find_table(
/*====================*/
const dict_table_t* find_table); /*!< in: table to find */
#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 */
ib_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);
}
/********************************************************************//**
Get the database name length in a table name.
@return database name length */
ulint
dict_get_db_name_len(
/*=================*/
const char* name) /*!< in: table name in the form
dbname '/' tablename */
{
const char* s;
s = strchr(name, '/');
ut_a(s);
return(s - name);
}
/** Reserve the dictionary system mutex. */
void
dict_mutex_enter_for_mysql_func(const char *file, unsigned line)
{
mutex_enter_loc(&dict_sys->mutex, file, line);
}
/********************************************************************//**
Releases the dictionary system mutex for MySQL. */
void
dict_mutex_exit_for_mysql(void)
/*===========================*/
{
mutex_exit(&dict_sys->mutex);
}
/** Allocate and init a dict_table_t's stats latch.
This function must not be called concurrently on the same table object.
@param[in,out] table_void table whose stats latch to create */
static
void
dict_table_stats_latch_alloc(
void* table_void)
{
dict_table_t* table = static_cast<dict_table_t*>(table_void);
/* Note: rw_lock_create() will call the constructor */
table->stats_latch = static_cast<rw_lock_t*>(
ut_malloc_nokey(sizeof(rw_lock_t)));
ut_a(table->stats_latch != NULL);
rw_lock_create(dict_table_stats_key, table->stats_latch,
SYNC_INDEX_TREE);
}
/** Deinit and free a dict_table_t's stats latch.
This function must not be called concurrently on the same table object.
@param[in,out] table table whose stats latch to free */
static
void
dict_table_stats_latch_free(
dict_table_t* table)
{
rw_lock_free(table->stats_latch);
ut_free(table->stats_latch);
}
/** Create a dict_table_t's stats latch or delay for lazy creation.
This function is only called from either single threaded environment
or from a thread that has not shared the table object with other threads.
@param[in,out] table table whose stats latch to create
@param[in] enabled if false then the latch is disabled
and dict_table_stats_lock()/unlock() become noop on this table. */
void
dict_table_stats_latch_create(
dict_table_t* table,
bool enabled)
{
if (!enabled) {
table->stats_latch = NULL;
table->stats_latch_created = os_once::DONE;
return;
}
/* We create this lazily the first time it is used. */
table->stats_latch = NULL;
table->stats_latch_created = os_once::NEVER_DONE;
}
/** Destroy a dict_table_t's stats latch.
This function is only called from either single threaded environment
or from a thread that has not shared the table object with other threads.
@param[in,out] table table whose stats latch to destroy */
void
dict_table_stats_latch_destroy(
dict_table_t* table)
{
if (table->stats_latch_created == os_once::DONE
&& table->stats_latch != NULL) {
dict_table_stats_latch_free(table);
}
}
/** Lock the appropriate latch to protect a given table's statistics.
@param[in] table table whose stats to lock
@param[in] latch_mode RW_S_LATCH or RW_X_LATCH */
void
dict_table_stats_lock(
dict_table_t* table,
ulint latch_mode)
{
ut_ad(table != NULL);
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
os_once::do_or_wait_for_done(
&table->stats_latch_created,
dict_table_stats_latch_alloc, table);
if (table->stats_latch == NULL) {
/* This is a dummy table object that is private in the current
thread and is not shared between multiple threads, thus we
skip any locking. */
return;
}
switch (latch_mode) {
case RW_S_LATCH:
rw_lock_s_lock(table->stats_latch);
break;
case RW_X_LATCH:
rw_lock_x_lock(table->stats_latch);
break;
case RW_NO_LATCH:
/* fall through */
default:
ut_error;
}
}
/** Unlock the latch that has been locked by dict_table_stats_lock().
@param[in] table table whose stats to unlock
@param[in] latch_mode RW_S_LATCH or RW_X_LATCH */
void
dict_table_stats_unlock(
dict_table_t* table,
ulint latch_mode)
{
ut_ad(table != NULL);
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
if (table->stats_latch == NULL) {
/* This is a dummy table object that is private in the current
thread and is not shared between multiple threads, thus we
skip any locking. */
return;
}
switch (latch_mode) {
case RW_S_LATCH:
rw_lock_s_unlock(table->stats_latch);
break;
case RW_X_LATCH:
rw_lock_x_unlock(table->stats_latch);
break;
case RW_NO_LATCH:
/* fall through */
default:
ut_error;
}
}
/**********************************************************************//**
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 */
ulint ref_count) /*!< in: expected table->n_ref_count */
{
trx_t* trx;
trx = trx_allocate_for_background();
trx->op_info = "try to drop any indexes after an aborted index creation";
row_mysql_lock_data_dictionary(trx);
trx_set_dict_operation(trx, TRX_DICT_OP_INDEX);
if (table == NULL) {
table = dict_table_open_on_id_low(
table_id, DICT_ERR_IGNORE_NONE, FALSE);
} else {
ut_ad(table->id == table_id);
}
if (table && table->get_ref_count() == ref_count && table->drop_aborted) {
/* 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_for_background(trx);
}
/**********************************************************************//**
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;
mutex_exit(&dict_sys->mutex);
dict_table_try_drop_aborted(table, table_id, 1);
} else {
mutex_exit(&dict_sys->mutex);
}
}
/********************************************************************//**
Decrements the count of open handles to a table. */
void
dict_table_close(
/*=============*/
dict_table_t* table, /*!< in/out: table */
ibool dict_locked, /*!< in: TRUE=data dictionary locked */
ibool try_drop) /*!< in: TRUE=try to drop any orphan
indexes after an aborted online
index creation */
{
if (!dict_locked) {
mutex_enter(&dict_sys->mutex);
}
ut_ad(mutex_own(&dict_sys->mutex));
ut_a(table->get_ref_count() > 0);
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 (strchr(table->name.m_name, '/') != NULL
&& table->get_ref_count() == 0
&& dict_stats_is_persistent_enabled(table)) {
dict_stats_deinit(table);
}
MONITOR_DEC(MONITOR_TABLE_REFERENCE);
ut_ad(dict_lru_validate());
#ifdef UNIV_DEBUG
if (table->can_be_evicted) {
ut_ad(dict_lru_find_table(table));
} else {
ut_ad(dict_non_lru_find_table(table));
}
#endif /* UNIV_DEBUG */
if (!dict_locked) {
table_id_t table_id = table->id;
ibool drop_aborted;
drop_aborted = try_drop
&& table->drop_aborted
&& table->get_ref_count() == 1
&& dict_table_get_first_index(table);
mutex_exit(&dict_sys->mutex);
if (drop_aborted) {
dict_table_try_drop_aborted(NULL, table_id, 0);
}
}
}
/********************************************************************//**
Closes the only open handle to a table and drops a table while assuring
that dict_sys->mutex is held the whole time. This assures that the table
is not evicted after the close when the count of open handles goes to zero.
Because dict_sys->mutex is held, we do not need to call
dict_table_prevent_eviction(). */
void
dict_table_close_and_drop(
/*======================*/
trx_t* trx, /*!< in: data dictionary transaction */
dict_table_t* table) /*!< in/out: table */
{
dberr_t err = DB_SUCCESS;
ut_ad(mutex_own(&dict_sys->mutex));
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X));
ut_ad(trx->dict_operation != TRX_DICT_OP_NONE);
ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE));
dict_table_close(table, TRUE, FALSE);
#if defined UNIV_DEBUG || defined UNIV_DDL_DEBUG
/* Nobody should have initialized the stats of the newly created
table when this is called. So we know that it has not been added
for background stats gathering. */
ut_a(!table->stat_initialized);
#endif /* UNIV_DEBUG || UNIV_DDL_DEBUG */
err = row_merge_drop_table(trx, table);
if (err != DB_SUCCESS) {
ib::error() << "At " << __FILE__ << ":" << __LINE__
<< " row_merge_drop_table returned error: " << err
<< " table: " << table->name.m_name;
}
}
/** 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);
}
/**********************************************************************//**
Returns a column's name.
@return column name. NOTE: not guaranteed to stay valid if table is
modified in any way (columns added, etc.). */
const char*
dict_table_get_col_name(
/*====================*/
const dict_table_t* table, /*!< in: table */
ulint col_nr) /*!< in: column number */
{
ulint i;
const char* s;
ut_ad(table);
ut_ad(col_nr < table->n_def);
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
s = table->col_names;
if (s) {
for (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));
}
/** Allocate and init the autoinc latch of a given table.
This function must not be called concurrently on the same table object.
@param[in,out] table_void table whose autoinc latch to create */
static
void
dict_table_autoinc_alloc(
void* table_void)
{
dict_table_t* table = static_cast<dict_table_t*>(table_void);
table->autoinc_mutex = UT_NEW_NOKEY(ib_mutex_t());
ut_a(table->autoinc_mutex != NULL);
mutex_create(LATCH_ID_AUTOINC, table->autoinc_mutex);
}
/** Allocate and init the zip_pad_mutex of a given index.
This function must not be called concurrently on the same index object.
@param[in,out] index_void index whose zip_pad_mutex to create */
static
void
dict_index_zip_pad_alloc(
void* index_void)
{
dict_index_t* index = static_cast<dict_index_t*>(index_void);
index->zip_pad.mutex = UT_NEW_NOKEY(SysMutex());
ut_a(index->zip_pad.mutex != NULL);
mutex_create(LATCH_ID_ZIP_PAD_MUTEX, index->zip_pad.mutex);
}
/********************************************************************//**
Acquire the autoinc lock. */
void
dict_table_autoinc_lock(
/*====================*/
dict_table_t* table) /*!< in/out: table */
{
os_once::do_or_wait_for_done(
&table->autoinc_mutex_created,
dict_table_autoinc_alloc, table);
mutex_enter(table->autoinc_mutex);
}
/** Acquire the zip_pad_mutex latch.
@param[in,out] index the index whose zip_pad_mutex to acquire.*/
static
void
dict_index_zip_pad_lock(
dict_index_t* index)
{
os_once::do_or_wait_for_done(
&index->zip_pad.mutex_created,
dict_index_zip_pad_alloc, index);
mutex_enter(index->zip_pad.mutex);
}
/** 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));
}
/********************************************************************//**
Release the autoinc lock. */
void
dict_table_autoinc_unlock(
/*======================*/
dict_table_t* table) /*!< in/out: table */
{
mutex_exit(table->autoinc_mutex);
}
/** 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);
}
/** Returns TRUE if the index contains a column or a prefix of that column.
@param[in] index index
@param[in] n column number
@param[in] is_virtual whether it is a virtual col
@return TRUE if contains the column or its prefix */
ibool
dict_index_contains_col_or_prefix(
const dict_index_t* index,
ulint n,
bool is_virtual)
{
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 (dict_index_is_clust(index)) {
return(TRUE);
}
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);
}
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) {
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);
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);
}
/**********************************************************************//**
Returns a table object based on table id.
@return table, NULL if does not exist */
dict_table_t*
dict_table_open_on_id(
/*==================*/
table_id_t table_id, /*!< in: table id */
ibool dict_locked, /*!< in: TRUE=data dictionary locked */