~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/dict/dict0dict.c

  • Committer: Brian Aker
  • Date: 2010-10-27 21:00:49 UTC
  • mto: This revision was merged to the branch mainline in revision 1886.
  • Revision ID: brian@tangent.org-20101027210049-zfpgx2cfbrh8maq9
A couple of fixes to documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
2
 
3
 
Copyright (C) 1996, 2010, Innobase Oy. All Rights Reserved.
 
3
Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.
4
4
 
5
5
This program is free software; you can redistribute it and/or modify it under
6
6
the terms of the GNU General Public License as published by the Free Software
56
56
 
57
57
#include <ctype.h>
58
58
 
59
 
#include <drizzled/session.h>
60
 
 
61
59
/** the dictionary system */
62
60
UNIV_INTERN dict_sys_t* dict_sys        = NULL;
63
61
 
71
69
on the mode in trx_struct::dict_operation_lock_mode */
72
70
UNIV_INTERN rw_lock_t   dict_operation_lock;
73
71
 
74
 
/* Keys to register rwlocks and mutexes with performance schema */
75
 
#ifdef UNIV_PFS_RWLOCK
76
 
UNIV_INTERN mysql_pfs_key_t     dict_operation_lock_key;
77
 
UNIV_INTERN mysql_pfs_key_t     index_tree_rw_lock_key;
78
 
#endif /* UNIV_PFS_RWLOCK */
79
 
 
80
 
#ifdef UNIV_PFS_MUTEX
81
 
UNIV_INTERN mysql_pfs_key_t     dict_sys_mutex_key;
82
 
UNIV_INTERN mysql_pfs_key_t     dict_foreign_err_mutex_key;
83
 
#endif /* UNIV_PFS_MUTEX */
84
 
 
85
72
#define DICT_HEAP_SIZE          100     /*!< initial memory heap size when
86
73
                                        creating a table or index object */
87
74
#define DICT_POOL_PER_TABLE_HASH 512    /*!< buffer pool max size per table
92
79
/** Identifies generated InnoDB foreign key names */
93
80
static char     dict_ibfk[] = "_ibfk_";
94
81
 
95
 
/** array of rw locks protecting
96
 
dict_table_t::stat_initialized
97
 
dict_table_t::stat_n_rows (*)
98
 
dict_table_t::stat_clustered_index_size
99
 
dict_table_t::stat_sum_of_other_index_sizes
100
 
dict_table_t::stat_modified_counter (*)
101
 
dict_table_t::indexes*::stat_n_diff_key_vals[]
102
 
dict_table_t::indexes*::stat_index_size
103
 
dict_table_t::indexes*::stat_n_leaf_pages
104
 
(*) those are not always protected for performance reasons */
105
 
#define DICT_TABLE_STATS_LATCHES_SIZE   64
106
 
static rw_lock_t        dict_table_stats_latches[DICT_TABLE_STATS_LATCHES_SIZE];
107
 
 
108
82
/*******************************************************************//**
109
83
Tries to find column names for the index and sets the col field of the
110
84
index.
165
139
void
166
140
dict_field_print_low(
167
141
/*=================*/
168
 
        const dict_field_t*     field); /*!< in: field */
 
142
        dict_field_t*   field); /*!< in: field */
169
143
/*********************************************************************//**
170
144
Frees a foreign key struct. */
171
145
static
264
238
        mutex_exit(&(dict_sys->mutex));
265
239
}
266
240
 
267
 
/** Get the latch that protects the stats of a given table */
268
 
#define GET_TABLE_STATS_LATCH(table) \
269
 
        (&dict_table_stats_latches[ut_fold_ull(table->id) \
270
 
                                   % DICT_TABLE_STATS_LATCHES_SIZE])
271
 
 
272
 
/**********************************************************************//**
273
 
Lock the appropriate latch to protect a given table's statistics.
274
 
table->id is used to pick the corresponding latch from a global array of
275
 
latches. */
276
 
UNIV_INTERN
277
 
void
278
 
dict_table_stats_lock(
279
 
/*==================*/
280
 
        const dict_table_t*     table,          /*!< in: table */
281
 
        ulint                   latch_mode)     /*!< in: RW_S_LATCH or
282
 
                                                RW_X_LATCH */
283
 
{
284
 
        ut_ad(table != NULL);
285
 
        ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
286
 
 
287
 
        switch (latch_mode) {
288
 
        case RW_S_LATCH:
289
 
                rw_lock_s_lock(GET_TABLE_STATS_LATCH(table));
290
 
                break;
291
 
        case RW_X_LATCH:
292
 
                rw_lock_x_lock(GET_TABLE_STATS_LATCH(table));
293
 
                break;
294
 
        case RW_NO_LATCH:
295
 
                /* fall through */
296
 
        default:
297
 
                ut_error;
298
 
        }
299
 
}
300
 
 
301
 
/**********************************************************************//**
302
 
Unlock the latch that has been locked by dict_table_stats_lock() */
303
 
UNIV_INTERN
304
 
void
305
 
dict_table_stats_unlock(
306
 
/*====================*/
307
 
        const dict_table_t*     table,          /*!< in: table */
308
 
        ulint                   latch_mode)     /*!< in: RW_S_LATCH or
309
 
                                                RW_X_LATCH */
310
 
{
311
 
        ut_ad(table != NULL);
312
 
        ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
313
 
 
314
 
        switch (latch_mode) {
315
 
        case RW_S_LATCH:
316
 
                rw_lock_s_unlock(GET_TABLE_STATS_LATCH(table));
317
 
                break;
318
 
        case RW_X_LATCH:
319
 
                rw_lock_x_unlock(GET_TABLE_STATS_LATCH(table));
320
 
                break;
321
 
        case RW_NO_LATCH:
322
 
                /* fall through */
323
 
        default:
324
 
                ut_error;
325
 
        }
326
 
}
327
 
 
328
241
/********************************************************************//**
329
242
Decrements the count of open MySQL handles to a table. */
330
243
UNIV_INTERN
457
370
dict_index_get_on_id_low(
458
371
/*=====================*/
459
372
        dict_table_t*   table,  /*!< in: table */
460
 
        index_id_t      id)     /*!< in: index id */
 
373
        dulint          id)     /*!< in: index id */
461
374
{
462
375
        dict_index_t*   index;
463
376
 
464
377
        index = dict_table_get_first_index(table);
465
378
 
466
379
        while (index) {
467
 
                if (id == index->id) {
 
380
                if (0 == ut_dulint_cmp(id, index->id)) {
468
381
                        /* Found */
469
382
 
470
383
                        return(index);
606
519
dict_table_t*
607
520
dict_table_get_on_id(
608
521
/*=================*/
609
 
        table_id_t      table_id,       /*!< in: table id */
610
 
        trx_t*          trx)            /*!< in: transaction handle */
 
522
        dulint  table_id,       /*!< in: table id */
 
523
        trx_t*  trx)            /*!< in: transaction handle */
611
524
{
612
525
        dict_table_t*   table;
613
526
 
614
 
        if (trx->dict_operation_lock_mode == RW_X_LATCH) {
615
 
 
616
 
                /* Note: An X latch implies that the transaction
617
 
                already owns the dictionary mutex. */
618
 
 
619
 
                ut_ad(mutex_own(&dict_sys->mutex));
 
527
        if (ut_dulint_cmp(table_id, DICT_FIELDS_ID) <= 0
 
528
            || trx->dict_operation_lock_mode == RW_X_LATCH) {
 
529
                /* It is a system table which will always exist in the table
 
530
                cache: we avoid acquiring the dictionary mutex, because
 
531
                if we are doing a rollback to handle an error in TABLE
 
532
                CREATE, for example, we already have the mutex! */
 
533
 
 
534
                ut_ad(mutex_own(&(dict_sys->mutex))
 
535
                      || trx->dict_operation_lock_mode == RW_X_LATCH);
620
536
 
621
537
                return(dict_table_get_on_id_low(table_id));
622
538
        }
688
604
dict_init(void)
689
605
/*===========*/
690
606
{
691
 
        int     i;
692
 
 
693
 
        dict_sys = static_cast<dict_sys_t *>(mem_alloc(sizeof(dict_sys_t)));
694
 
 
695
 
        mutex_create(dict_sys_mutex_key, &dict_sys->mutex, SYNC_DICT);
 
607
        dict_sys = mem_alloc(sizeof(dict_sys_t));
 
608
 
 
609
        mutex_create(&dict_sys->mutex, SYNC_DICT);
696
610
 
697
611
        dict_sys->table_hash = hash_create(buf_pool_get_curr_size()
698
612
                                           / (DICT_POOL_PER_TABLE_HASH
704
618
 
705
619
        UT_LIST_INIT(dict_sys->table_LRU);
706
620
 
707
 
        rw_lock_create(dict_operation_lock_key,
708
 
                       &dict_operation_lock, SYNC_DICT_OPERATION);
 
621
        rw_lock_create(&dict_operation_lock, SYNC_DICT_OPERATION);
709
622
 
710
623
        dict_foreign_err_file = os_file_create_tmpfile();
711
624
        ut_a(dict_foreign_err_file);
712
625
 
713
 
        mutex_create(dict_foreign_err_mutex_key,
714
 
                     &dict_foreign_err_mutex, SYNC_ANY_LATCH);
715
 
 
716
 
        for (i = 0; i < DICT_TABLE_STATS_LATCHES_SIZE; i++) {
717
 
                rw_lock_create(PFS_NOT_INSTRUMENTED,
718
 
                               &dict_table_stats_latches[i], SYNC_INDEX_TREE);
719
 
        }
 
626
        mutex_create(&dict_foreign_err_mutex, SYNC_ANY_LATCH);
720
627
}
721
628
 
722
629
/**********************************************************************//**
746
653
        mutex_exit(&(dict_sys->mutex));
747
654
 
748
655
        if (table != NULL) {
749
 
                /* If table->ibd_file_missing == TRUE, this will
750
 
                print an error message and return without doing
751
 
                anything. */
752
 
                dict_update_statistics(table, TRUE /* only update stats
753
 
                                       if they have not been initialized */);
 
656
                if (!table->stat_initialized) {
 
657
                        /* If table->ibd_file_missing == TRUE, this will
 
658
                        print an error message and return without doing
 
659
                        anything. */
 
660
                        dict_update_statistics(table);
 
661
                }
754
662
        }
755
663
 
756
664
        return(table);
828
736
        table->cached = TRUE;
829
737
 
830
738
        fold = ut_fold_string(table->name);
831
 
        id_fold = ut_fold_ull(table->id);
 
739
        id_fold = ut_fold_dulint(table->id);
832
740
 
833
741
        row_len = 0;
834
742
        for (i = 0; i < table->n_def; i++) {
870
778
                dict_table_t*   table2;
871
779
                HASH_SEARCH(id_hash, dict_sys->table_id_hash, id_fold,
872
780
                            dict_table_t*, table2, ut_ad(table2->cached),
873
 
                            table2->id == table->id);
 
781
                            ut_dulint_cmp(table2->id, table->id) == 0);
874
782
                ut_a(table2 == NULL);
875
783
 
876
784
#ifdef UNIV_DEBUG
892
800
        /* Add table to LRU list of tables */
893
801
        UT_LIST_ADD_FIRST(table_LRU, dict_sys->table_LRU, table);
894
802
 
895
 
        dict_sys->size += mem_heap_get_size(table->heap)
896
 
                + strlen(table->name) + 1;
 
803
        dict_sys->size += mem_heap_get_size(table->heap);
897
804
}
898
805
 
899
806
/**********************************************************************//**
905
812
dict_index_t*
906
813
dict_index_find_on_id_low(
907
814
/*======================*/
908
 
        index_id_t      id)     /*!< in: index id */
 
815
        dulint  id)     /*!< in: index id */
909
816
{
910
817
        dict_table_t*   table;
911
818
        dict_index_t*   index;
916
823
                index = dict_table_get_first_index(table);
917
824
 
918
825
                while (index) {
919
 
                        if (id == index->id) {
 
826
                        if (0 == ut_dulint_cmp(id, index->id)) {
920
827
                                /* Found */
921
828
 
922
829
                                return(index);
947
854
        dict_foreign_t* foreign;
948
855
        dict_index_t*   index;
949
856
        ulint           fold;
950
 
        char            old_name[MAX_TABLE_NAME_LEN + 1];
 
857
        ulint           old_size;
 
858
        const char*     old_name;
951
859
 
952
860
        ut_ad(table);
953
861
        ut_ad(mutex_own(&(dict_sys->mutex)));
954
862
 
955
 
        /* store the old/current name to an automatic variable */
956
 
        if (strlen(table->name) + 1 <= sizeof(old_name)) {
957
 
                memcpy(old_name, table->name, strlen(table->name) + 1);
958
 
        } else {
959
 
                ut_print_timestamp(stderr);
960
 
                fprintf(stderr, "InnoDB: too long table name: '%s', "
961
 
                        "max length is %d\n", table->name,
962
 
                        MAX_TABLE_NAME_LEN);
963
 
                ut_error;
964
 
        }
 
863
        old_size = mem_heap_get_size(table->heap);
 
864
        old_name = table->name;
965
865
 
966
866
        fold = ut_fold_string(new_name);
967
867
 
1007
907
        /* Remove table from the hash tables of tables */
1008
908
        HASH_DELETE(dict_table_t, name_hash, dict_sys->table_hash,
1009
909
                    ut_fold_string(old_name), table);
1010
 
 
1011
 
        if (strlen(new_name) > strlen(table->name)) {
1012
 
                /* We allocate MAX_TABLE_NAME_LEN+1 bytes here to avoid
1013
 
                memory fragmentation, we assume a repeated calls of
1014
 
                ut_realloc() with the same size do not cause fragmentation */
1015
 
                ut_a(strlen(new_name) <= MAX_TABLE_NAME_LEN);
1016
 
                table->name = static_cast<char *>(ut_realloc(table->name, MAX_TABLE_NAME_LEN + 1));
1017
 
        }
1018
 
        memcpy(table->name, new_name, strlen(new_name) + 1);
 
910
        table->name = mem_heap_strdup(table->heap, new_name);
1019
911
 
1020
912
        /* Add table to hash table of tables */
1021
913
        HASH_INSERT(dict_table_t, name_hash, dict_sys->table_hash, fold,
1022
914
                    table);
1023
 
 
1024
 
        dict_sys->size += strlen(new_name) - strlen(old_name);
1025
 
        ut_a(dict_sys->size > 0);
 
915
        dict_sys->size += (mem_heap_get_size(table->heap) - old_size);
1026
916
 
1027
917
        /* Update the table_name field in indexes */
1028
918
        index = dict_table_get_first_index(table);
1081
971
                        TODO: store buf len to save memory */
1082
972
 
1083
973
                        foreign->foreign_table_name
1084
 
                                = static_cast<char *>(mem_heap_alloc(foreign->heap,
1085
 
                                                 ut_strlen(table->name) + 1));
 
974
                                = mem_heap_alloc(foreign->heap,
 
975
                                                 ut_strlen(table->name) + 1);
1086
976
                }
1087
977
 
1088
978
                strcpy(foreign->foreign_table_name, table->name);
1105
995
                                /* This is a generated >= 4.0.18 format id */
1106
996
 
1107
997
                                if (strlen(table->name) > strlen(old_name)) {
1108
 
                                        foreign->id = static_cast<char *>(mem_heap_alloc(
 
998
                                        foreign->id = mem_heap_alloc(
1109
999
                                                foreign->heap,
1110
1000
                                                strlen(table->name)
1111
 
                                                + strlen(old_id) + 1));
 
1001
                                                + strlen(old_id) + 1);
1112
1002
                                }
1113
1003
 
1114
1004
                                /* Replace the prefix 'databasename/tablename'
1124
1014
                                if (dict_get_db_name_len(table->name)
1125
1015
                                    > dict_get_db_name_len(foreign->id)) {
1126
1016
 
1127
 
                                        foreign->id = static_cast<char *>(mem_heap_alloc(
 
1017
                                        foreign->id = mem_heap_alloc(
1128
1018
                                                foreign->heap,
1129
 
                                                db_len + strlen(old_id) + 1));
 
1019
                                                db_len + strlen(old_id) + 1);
1130
1020
                                }
1131
1021
 
1132
1022
                                /* Replace the database prefix in id with the
1152
1042
                        /* Allocate a longer name buffer;
1153
1043
                        TODO: store buf len to save memory */
1154
1044
 
1155
 
                        foreign->referenced_table_name = static_cast<char *>(mem_heap_alloc(
1156
 
                                foreign->heap, strlen(table->name) + 1));
 
1045
                        foreign->referenced_table_name = mem_heap_alloc(
 
1046
                                foreign->heap, strlen(table->name) + 1);
1157
1047
                }
1158
1048
 
1159
1049
                strcpy(foreign->referenced_table_name, table->name);
1172
1062
dict_table_change_id_in_cache(
1173
1063
/*==========================*/
1174
1064
        dict_table_t*   table,  /*!< in/out: table object already in cache */
1175
 
        table_id_t      new_id) /*!< in: new id to set */
 
1065
        dulint          new_id) /*!< in: new id to set */
1176
1066
{
1177
1067
        ut_ad(table);
1178
1068
        ut_ad(mutex_own(&(dict_sys->mutex)));
1181
1071
        /* Remove the table from the hash table of id's */
1182
1072
 
1183
1073
        HASH_DELETE(dict_table_t, id_hash, dict_sys->table_id_hash,
1184
 
                    ut_fold_ull(table->id), table);
 
1074
                    ut_fold_dulint(table->id), table);
1185
1075
        table->id = new_id;
1186
1076
 
1187
1077
        /* Add the table back to the hash table */
1188
1078
        HASH_INSERT(dict_table_t, id_hash, dict_sys->table_id_hash,
1189
 
                    ut_fold_ull(table->id), table);
 
1079
                    ut_fold_dulint(table->id), table);
1190
1080
}
1191
1081
 
1192
1082
/**********************************************************************//**
1242
1132
        HASH_DELETE(dict_table_t, name_hash, dict_sys->table_hash,
1243
1133
                    ut_fold_string(table->name), table);
1244
1134
        HASH_DELETE(dict_table_t, id_hash, dict_sys->table_id_hash,
1245
 
                    ut_fold_ull(table->id), table);
 
1135
                    ut_fold_dulint(table->id), table);
1246
1136
 
1247
1137
        /* Remove table from LRU list of tables */
1248
1138
        UT_LIST_REMOVE(table_LRU, dict_sys->table_LRU, table);
1249
1139
 
1250
 
        size = mem_heap_get_size(table->heap) + strlen(table->name) + 1;
 
1140
        size = mem_heap_get_size(table->heap);
1251
1141
 
1252
1142
        ut_ad(dict_sys->size >= size);
1253
1143
 
1569
1459
 
1570
1460
        if (!dict_index_find_cols(table, index)) {
1571
1461
 
1572
 
                dict_mem_index_free(index);
1573
1462
                return(DB_CORRUPTION);
1574
1463
        }
1575
1464
 
1676
1565
        new_index->stat_n_leaf_pages = 1;
1677
1566
 
1678
1567
        new_index->page = page_no;
1679
 
        rw_lock_create(index_tree_rw_lock_key, &new_index->lock,
1680
 
                       SYNC_INDEX_TREE);
 
1568
        rw_lock_create(&new_index->lock, SYNC_INDEX_TREE);
1681
1569
 
1682
1570
        if (!UNIV_UNLIKELY(new_index->type & DICT_UNIVERSAL)) {
1683
1571
 
1684
 
                new_index->stat_n_diff_key_vals = static_cast<ib_int64_t *>(mem_heap_alloc(
 
1572
                new_index->stat_n_diff_key_vals = mem_heap_alloc(
1685
1573
                        new_index->heap,
1686
1574
                        (1 + dict_index_get_n_unique(new_index))
1687
 
                        * sizeof(ib_int64_t)));
 
1575
                        * sizeof(ib_int64_t));
1688
1576
                /* Give some sensible values to stat_n_... in case we do
1689
1577
                not calculate statistics quickly enough */
1690
1578
 
2065
1953
        }
2066
1954
 
2067
1955
        /* Remember the table columns already contained in new_index */
2068
 
        void *indexed_ptr= mem_zalloc(table->n_cols * sizeof *indexed);
2069
 
        indexed = static_cast<unsigned long *>(indexed_ptr);
 
1956
        indexed = mem_zalloc(table->n_cols * sizeof *indexed);
2070
1957
 
2071
1958
        /* Mark the table columns already contained in new_index */
2072
1959
        for (i = 0; i < new_index->n_def; i++) {
2150
2037
        dict_index_copy(new_index, index, table, 0, index->n_fields);
2151
2038
 
2152
2039
        /* Remember the table columns already contained in new_index */
2153
 
        void *indexed_ptr= mem_zalloc(table->n_cols * sizeof *indexed);
2154
 
        indexed = static_cast<unsigned long *>(indexed_ptr);
 
2040
        indexed = mem_zalloc(table->n_cols * sizeof *indexed);
2155
2041
 
2156
2042
        /* Mark the table columns already contained in new_index */
2157
2043
        for (i = 0; i < new_index->n_def; i++) {
2506
2392
                                /* We found a matching index, select
2507
2393
                                the index with the higher id*/
2508
2394
 
2509
 
                                if (!found || index->id > found->id) {
 
2395
                                if (!found
 
2396
                                    || ut_dulint_cmp(index->id, found->id) > 0) {
2510
2397
 
2511
2398
                                        found = index;
2512
2399
                                }
2842
2729
 
2843
2730
        if (quote) {
2844
2731
                char*   d;
2845
 
                str = d = static_cast<char *>(mem_heap_alloc(heap, len + 1));
 
2732
                str = d = mem_heap_alloc(heap, len + 1);
2846
2733
                while (len--) {
2847
2734
                        if ((*d++ = *s++) == quote) {
2848
2735
                                s++;
2861
2748
                /* Convert the identifier from connection character set
2862
2749
                to UTF-8. */
2863
2750
                len = 3 * len + 1;
2864
 
                *id = dst = static_cast<char *>(mem_heap_alloc(heap, len));
 
2751
                *id = dst = mem_heap_alloc(heap, len);
2865
2752
 
2866
2753
                innobase_convert_from_id(cs, dst, str, len);
2867
 
        } else if (!strncmp(str, srv_mysql50_table_name_prefix.c_str(),
2868
 
                            srv_mysql50_table_name_prefix.size())) {
 
2754
        } else if (!strncmp(str, srv_mysql50_table_name_prefix,
 
2755
                            sizeof srv_mysql50_table_name_prefix)) {
2869
2756
                /* This is a pre-5.1 table name
2870
2757
                containing chars other than [A-Za-z0-9].
2871
2758
                Discard the prefix and use raw UTF-8 encoding. */
2872
 
                str += srv_mysql50_table_name_prefix.size();
2873
 
                len -= srv_mysql50_table_name_prefix.size();
 
2759
                str += sizeof srv_mysql50_table_name_prefix;
 
2760
                len -= sizeof srv_mysql50_table_name_prefix;
2874
2761
                goto convert_id;
2875
2762
        } else {
2876
2763
                /* Encode using filename-safe characters. */
2877
2764
                len = 5 * len + 1;
2878
 
                *id = dst = static_cast<char *>(mem_heap_alloc(heap, len));
 
2765
                *id = dst = mem_heap_alloc(heap, len);
2879
2766
 
2880
2767
                innobase_convert_from_table_id(cs, dst, str, len);
2881
2768
        }
3013
2900
        table_name_len = strlen(table_name);
3014
2901
 
3015
2902
        /* Copy database_name, '/', table_name, '\0' */
3016
 
        ref = static_cast<char *>(mem_heap_alloc(heap, database_name_len + table_name_len + 2));
 
2903
        ref = mem_heap_alloc(heap, database_name_len + table_name_len + 2);
3017
2904
        memcpy(ref, database_name, database_name_len);
3018
2905
        ref[database_name_len] = '/';
3019
2906
        memcpy(ref + database_name_len + 1, table_name, table_name_len + 1);
3070
2957
char*
3071
2958
dict_strip_comments(
3072
2959
/*================*/
3073
 
        const char*     sql_string,     /*!< in: SQL string */
3074
 
        size_t          sql_length)     /*!< in: length of sql_string */
 
2960
        const char*     sql_string)     /*!< in: SQL string */
3075
2961
{
3076
2962
        char*           str;
3077
2963
        const char*     sptr;
3078
 
        const char*     eptr    = sql_string + sql_length;
3079
2964
        char*           ptr;
3080
2965
        /* unclosed quote character (0 if none) */
3081
2966
        char            quote   = 0;
3082
2967
 
3083
 
        str = static_cast<char *>(mem_alloc(sql_length + 1));
 
2968
        str = mem_alloc(strlen(sql_string) + 1);
3084
2969
 
3085
2970
        sptr = sql_string;
3086
2971
        ptr = str;
3087
2972
 
3088
2973
        for (;;) {
3089
2974
scan_more:
3090
 
                if (sptr >= eptr || *sptr == '\0') {
3091
 
end_of_string:
 
2975
                if (*sptr == '\0') {
3092
2976
                        *ptr = '\0';
3093
2977
 
3094
 
                        ut_a(ptr <= str + sql_length);
 
2978
                        ut_a(ptr <= str + strlen(sql_string));
3095
2979
 
3096
2980
                        return(str);
3097
2981
                }
3110
2994
                           || (sptr[0] == '-' && sptr[1] == '-'
3111
2995
                               && sptr[2] == ' ')) {
3112
2996
                        for (;;) {
3113
 
                                if (++sptr >= eptr) {
3114
 
                                        goto end_of_string;
3115
 
                                }
3116
 
 
3117
2997
                                /* In Unix a newline is 0x0A while in Windows
3118
2998
                                it is 0x0D followed by 0x0A */
3119
2999
 
3120
 
                                switch (*sptr) {
3121
 
                                case (char) 0X0A:
3122
 
                                case (char) 0x0D:
3123
 
                                case '\0':
 
3000
                                if (*sptr == (char)0x0A
 
3001
                                    || *sptr == (char)0x0D
 
3002
                                    || *sptr == '\0') {
 
3003
 
3124
3004
                                        goto scan_more;
3125
3005
                                }
 
3006
 
 
3007
                                sptr++;
3126
3008
                        }
3127
3009
                } else if (!quote && *sptr == '/' && *(sptr + 1) == '*') {
3128
 
                        sptr += 2;
3129
3010
                        for (;;) {
3130
 
                                if (sptr >= eptr) {
3131
 
                                        goto end_of_string;
 
3011
                                if (*sptr == '*' && *(sptr + 1) == '/') {
 
3012
 
 
3013
                                        sptr += 2;
 
3014
 
 
3015
                                        goto scan_more;
3132
3016
                                }
3133
3017
 
3134
 
                                switch (*sptr) {
3135
 
                                case '\0':
 
3018
                                if (*sptr == '\0') {
 
3019
 
3136
3020
                                        goto scan_more;
3137
 
                                case '*':
3138
 
                                        if (sptr[1] == '/') {
3139
 
                                                sptr += 2;
3140
 
                                                goto scan_more;
3141
 
                                        }
3142
3021
                                }
3143
3022
 
3144
3023
                                sptr++;
3485
3364
                        start_of_latest_foreign);
3486
3365
                mutex_exit(&dict_foreign_err_mutex);
3487
3366
 
3488
 
                return(DB_CHILD_NO_INDEX);
 
3367
                return(DB_CANNOT_ADD_CONSTRAINT);
3489
3368
        }
3490
3369
        ptr = dict_accept(cs, ptr, "REFERENCES", &success);
3491
3370
 
3509
3388
 
3510
3389
                db_len = dict_get_db_name_len(table->name);
3511
3390
 
3512
 
                foreign->id = static_cast<char*>(mem_heap_alloc(
3513
 
                        foreign->heap, db_len + strlen(constraint_name) + 2));
 
3391
                foreign->id = mem_heap_alloc(
 
3392
                        foreign->heap, db_len + strlen(constraint_name) + 2);
3514
3393
 
3515
3394
                ut_memcpy(foreign->id, table->name, db_len);
3516
3395
                foreign->id[db_len] = '/';
3522
3401
                                                      table->name);
3523
3402
        foreign->foreign_index = index;
3524
3403
        foreign->n_fields = (unsigned int) i;
3525
 
        foreign->foreign_col_names = static_cast<const char **>(mem_heap_alloc(foreign->heap,
3526
 
                                                    i * sizeof(void*)));
 
3404
        foreign->foreign_col_names = mem_heap_alloc(foreign->heap,
 
3405
                                                    i * sizeof(void*));
3527
3406
        for (i = 0; i < foreign->n_fields; i++) {
3528
3407
                foreign->foreign_col_names[i] = mem_heap_strdup(
3529
3408
                        foreign->heap,
3766
3645
                                start_of_latest_foreign);
3767
3646
                        mutex_exit(&dict_foreign_err_mutex);
3768
3647
 
3769
 
                        return(DB_PARENT_NO_INDEX);
 
3648
                        return(DB_CANNOT_ADD_CONSTRAINT);
3770
3649
                }
3771
3650
        } else {
3772
3651
                ut_a(trx->check_foreigns == FALSE);
3779
3658
        foreign->referenced_table_name
3780
3659
                = mem_heap_strdup(foreign->heap, referenced_table_name);
3781
3660
 
3782
 
        foreign->referenced_col_names = static_cast<const char **>(mem_heap_alloc(foreign->heap,
3783
 
                                                       i * sizeof(void*)));
 
3661
        foreign->referenced_col_names = mem_heap_alloc(foreign->heap,
 
3662
                                                       i * sizeof(void*));
3784
3663
        for (i = 0; i < foreign->n_fields; i++) {
3785
3664
                foreign->referenced_col_names[i]
3786
3665
                        = mem_heap_strdup(foreign->heap, column_names[i]);
3819
3698
                                        name before it: test.table2; the
3820
3699
                                        default database id the database of
3821
3700
                                        parameter name */
3822
 
        size_t          sql_length,     /*!< in: length of sql_string */
3823
3701
        const char*     name,           /*!< in: table full name in the
3824
3702
                                        normalized form
3825
3703
                                        database_name/table_name */
3834
3712
        ut_a(trx);
3835
3713
        ut_a(trx->mysql_thd);
3836
3714
 
3837
 
        str = dict_strip_comments(sql_string, sql_length);
 
3715
        str = dict_strip_comments(sql_string);
3838
3716
        heap = mem_heap_create(10000);
3839
3717
 
3840
3718
        err = dict_create_foreign_constraints_low(
3841
 
                trx, heap, trx->session()->charset(), str, name,
 
3719
                trx, heap, innobase_get_charset(trx->mysql_thd), str, name,
3842
3720
                reject_fks);
3843
3721
 
3844
3722
        mem_heap_free(heap);
3867
3745
        dict_foreign_t*         foreign;
3868
3746
        ibool                   success;
3869
3747
        char*                   str;
3870
 
        size_t                  len;
3871
3748
        const char*             ptr;
3872
3749
        const char*             id;
3873
3750
        FILE*                   ef      = dict_foreign_err_file;
3876
3753
        ut_a(trx);
3877
3754
        ut_a(trx->mysql_thd);
3878
3755
 
3879
 
        cs = trx->session()->charset();
 
3756
        cs = innobase_get_charset(trx->mysql_thd);
3880
3757
 
3881
3758
        *n = 0;
3882
3759
 
3883
 
        *constraints_to_drop = static_cast<const char **>(mem_heap_alloc(heap, 1000 * sizeof(char*)));
3884
 
 
3885
 
        ptr= trx->session()->getQueryStringCopy(len);
3886
 
 
3887
 
        str = dict_strip_comments(ptr, len);
3888
 
 
 
3760
        *constraints_to_drop = mem_heap_alloc(heap, 1000 * sizeof(char*));
 
3761
 
 
3762
        str = dict_strip_comments((trx->mysql_query_str));
3889
3763
        ptr = str;
3890
3764
 
3891
3765
        ut_ad(mutex_own(&(dict_sys->mutex)));
3994
3868
dict_index_t*
3995
3869
dict_index_get_if_in_cache_low(
3996
3870
/*===========================*/
3997
 
        index_id_t      index_id)       /*!< in: index id */
 
3871
        dulint  index_id)       /*!< in: index id */
3998
3872
{
3999
3873
        ut_ad(mutex_own(&(dict_sys->mutex)));
4000
3874
 
4009
3883
dict_index_t*
4010
3884
dict_index_get_if_in_cache(
4011
3885
/*=======================*/
4012
 
        index_id_t      index_id)       /*!< in: index id */
 
3886
        dulint  index_id)       /*!< in: index id */
4013
3887
{
4014
3888
        dict_index_t*   index;
4015
3889
 
4097
3971
 
4098
3972
        dict_index_copy_types(tuple, index, n_unique);
4099
3973
 
4100
 
        buf = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
3974
        buf = mem_heap_alloc(heap, 4);
4101
3975
 
4102
3976
        mach_write_to_4(buf, page_no);
4103
3977
 
4230
4104
are used in query optimization. */
4231
4105
UNIV_INTERN
4232
4106
void
4233
 
dict_update_statistics(
4234
 
/*===================*/
 
4107
dict_update_statistics_low(
 
4108
/*=======================*/
4235
4109
        dict_table_t*   table,          /*!< in/out: table */
4236
 
        ibool           only_calc_if_missing_stats)/*!< in: only
4237
 
                                        update/recalc the stats if they have
4238
 
                                        not been initialized yet, otherwise
4239
 
                                        do nothing */
 
4110
        ibool           has_dict_mutex __attribute__((unused)))
 
4111
                                        /*!< in: TRUE if the caller has the
 
4112
                                        dictionary mutex */
4240
4113
{
4241
4114
        dict_index_t*   index;
 
4115
        ulint           size;
4242
4116
        ulint           sum_of_index_sizes      = 0;
4243
4117
 
4244
4118
        if (table->ibd_file_missing) {
4253
4127
                return;
4254
4128
        }
4255
4129
 
 
4130
        /* If we have set a high innodb_force_recovery level, do not calculate
 
4131
        statistics, as a badly corrupted index can cause a crash in it. */
 
4132
 
 
4133
        if (srv_force_recovery >= SRV_FORCE_NO_IBUF_MERGE) {
 
4134
 
 
4135
                return;
 
4136
        }
 
4137
 
4256
4138
        /* Find out the sizes of the indexes and how many different values
4257
4139
        for the key they approximately have */
4258
4140
 
4264
4146
                return;
4265
4147
        }
4266
4148
 
4267
 
        dict_table_stats_lock(table, RW_X_LATCH);
4268
 
 
4269
 
        if (only_calc_if_missing_stats && table->stat_initialized) {
4270
 
                dict_table_stats_unlock(table, RW_X_LATCH);
4271
 
                return;
4272
 
        }
4273
 
 
4274
 
        do {
4275
 
                if (UNIV_LIKELY
4276
 
                    (srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE
4277
 
                     || (srv_force_recovery < SRV_FORCE_NO_LOG_REDO
4278
 
                         && dict_index_is_clust(index)))) {
4279
 
                        ulint   size;
4280
 
                        size = btr_get_size(index, BTR_TOTAL_SIZE);
4281
 
 
4282
 
                        index->stat_index_size = size;
4283
 
 
4284
 
                        sum_of_index_sizes += size;
4285
 
 
4286
 
                        size = btr_get_size(index, BTR_N_LEAF_PAGES);
4287
 
 
4288
 
                        if (size == 0) {
4289
 
                                /* The root node of the tree is a leaf */
4290
 
                                size = 1;
4291
 
                        }
4292
 
 
4293
 
                        index->stat_n_leaf_pages = size;
4294
 
 
4295
 
                        btr_estimate_number_of_different_key_vals(index);
4296
 
                } else {
4297
 
                        /* If we have set a high innodb_force_recovery
4298
 
                        level, do not calculate statistics, as a badly
4299
 
                        corrupted index can cause a crash in it.
4300
 
                        Initialize some bogus index cardinality
4301
 
                        statistics, so that the data can be queried in
4302
 
                        various means, also via secondary indexes. */
4303
 
                        ulint   i;
4304
 
 
4305
 
                        sum_of_index_sizes++;
4306
 
                        index->stat_index_size = index->stat_n_leaf_pages = 1;
4307
 
 
4308
 
                        for (i = dict_index_get_n_unique(index); i; ) {
4309
 
                                index->stat_n_diff_key_vals[i--] = 1;
4310
 
                        }
 
4149
        while (index) {
 
4150
                size = btr_get_size(index, BTR_TOTAL_SIZE);
 
4151
 
 
4152
                index->stat_index_size = size;
 
4153
 
 
4154
                sum_of_index_sizes += size;
 
4155
 
 
4156
                size = btr_get_size(index, BTR_N_LEAF_PAGES);
 
4157
 
 
4158
                if (size == 0) {
 
4159
                        /* The root node of the tree is a leaf */
 
4160
                        size = 1;
4311
4161
                }
4312
4162
 
 
4163
                index->stat_n_leaf_pages = size;
 
4164
 
 
4165
                btr_estimate_number_of_different_key_vals(index);
 
4166
 
4313
4167
                index = dict_table_get_next_index(index);
4314
 
        } while (index);
 
4168
        }
4315
4169
 
4316
4170
        index = dict_table_get_first_index(table);
4317
4171
 
4326
4180
        table->stat_initialized = TRUE;
4327
4181
 
4328
4182
        table->stat_modified_counter = 0;
 
4183
}
4329
4184
 
4330
 
        dict_table_stats_unlock(table, RW_X_LATCH);
 
4185
/*********************************************************************//**
 
4186
Calculates new estimates for table and index statistics. The statistics
 
4187
are used in query optimization. */
 
4188
UNIV_INTERN
 
4189
void
 
4190
dict_update_statistics(
 
4191
/*===================*/
 
4192
        dict_table_t*   table)  /*!< in/out: table */
 
4193
{
 
4194
        dict_update_statistics_low(table, FALSE);
4331
4195
}
4332
4196
 
4333
4197
/**********************************************************************//**
4407
4271
 
4408
4272
        ut_ad(mutex_own(&(dict_sys->mutex)));
4409
4273
 
4410
 
        dict_update_statistics(table, FALSE /* update even if initialized */);
4411
 
 
4412
 
        dict_table_stats_lock(table, RW_S_LATCH);
 
4274
        dict_update_statistics_low(table, TRUE);
4413
4275
 
4414
4276
        fprintf(stderr,
4415
4277
                "--------------------------------------\n"
4416
 
                "TABLE: name %s, id %llu, flags %lx, columns %lu,"
 
4278
                "TABLE: name %s, id %lu %lu, flags %lx, columns %lu,"
4417
4279
                " indexes %lu, appr.rows %lu\n"
4418
4280
                "  COLUMNS: ",
4419
4281
                table->name,
4420
 
                (ullint) table->id,
 
4282
                (ulong) ut_dulint_get_high(table->id),
 
4283
                (ulong) ut_dulint_get_low(table->id),
4421
4284
                (ulong) table->flags,
4422
4285
                (ulong) table->n_cols,
4423
4286
                (ulong) UT_LIST_GET_LEN(table->indexes),
4437
4300
                index = UT_LIST_GET_NEXT(indexes, index);
4438
4301
        }
4439
4302
 
4440
 
        dict_table_stats_unlock(table, RW_S_LATCH);
4441
 
 
4442
4303
        foreign = UT_LIST_GET_FIRST(table->foreign_list);
4443
4304
 
4444
4305
        while (foreign != NULL) {
4484
4345
{
4485
4346
        ib_int64_t      n_vals;
4486
4347
        ulint           i;
 
4348
        const char*     type_string;
4487
4349
 
4488
4350
        ut_ad(mutex_own(&(dict_sys->mutex)));
4489
4351
 
4494
4356
                n_vals = index->stat_n_diff_key_vals[1];
4495
4357
        }
4496
4358
 
 
4359
        if (dict_index_is_clust(index)) {
 
4360
                type_string = "clustered index";
 
4361
        } else if (dict_index_is_unique(index)) {
 
4362
                type_string = "unique index";
 
4363
        } else {
 
4364
                type_string = "secondary index";
 
4365
        }
 
4366
 
4497
4367
        fprintf(stderr,
4498
 
                "  INDEX: name %s, id %llu, fields %lu/%lu,"
 
4368
                "  INDEX: name %s, id %lu %lu, fields %lu/%lu,"
4499
4369
                " uniq %lu, type %lu\n"
4500
4370
                "   root page %lu, appr.key vals %lu,"
4501
4371
                " leaf pages %lu, size pages %lu\n"
4502
4372
                "   FIELDS: ",
4503
4373
                index->name,
4504
 
                (ullint) index->id,
 
4374
                (ulong) ut_dulint_get_high(index->id),
 
4375
                (ulong) ut_dulint_get_low(index->id),
4505
4376
                (ulong) index->n_user_defined_cols,
4506
4377
                (ulong) index->n_fields,
4507
4378
                (ulong) index->n_uniq,
4530
4401
void
4531
4402
dict_field_print_low(
4532
4403
/*=================*/
4533
 
        const dict_field_t*     field)  /*!< in: field */
 
4404
        dict_field_t*   field)  /*!< in: field */
4534
4405
{
4535
4406
        ut_ad(mutex_own(&(dict_sys->mutex)));
4536
4407
 
4836
4707
dict_table_replace_index_in_foreign_list(
4837
4708
/*=====================================*/
4838
4709
        dict_table_t*   table,  /*!< in/out: table */
4839
 
        dict_index_t*   index,  /*!< in: index to be replaced */
4840
 
        const trx_t*    trx)    /*!< in: transaction handle */
 
4710
        dict_index_t*   index)  /*!< in: index to be replaced */
4841
4711
{
4842
4712
        dict_foreign_t* foreign;
4843
4713
 
4848
4718
                if (foreign->foreign_index == index) {
4849
4719
                        dict_index_t*   new_index
4850
4720
                                = dict_foreign_find_equiv_index(foreign);
4851
 
 
4852
 
                        /* There must exist an alternative index if
4853
 
                        check_foreigns (FOREIGN_KEY_CHECKS) is on, 
4854
 
                        since ha_innobase::prepare_drop_index had done
4855
 
                        the check before we reach here. */
4856
 
 
4857
 
                        ut_a(new_index || !trx->check_foreigns);
 
4721
                        ut_a(new_index);
4858
4722
 
4859
4723
                        foreign->foreign_index = new_index;
4860
4724
                }
4880
4744
 
4881
4745
        while (index != NULL) {
4882
4746
                if (ut_strcmp(index->name, name) == 0) {
4883
 
                        if (!min_index || index->id < min_index->id) {
 
4747
                        if (!min_index
 
4748
                            || ut_dulint_cmp(index->id, min_index->id) < 0) {
4884
4749
 
4885
4750
                                min_index = index;
4886
4751
                        }
4900
4765
void
4901
4766
dict_table_check_for_dup_indexes(
4902
4767
/*=============================*/
4903
 
        const dict_table_t*     table,  /*!< in: Check for dup indexes
 
4768
        const dict_table_t*     table)  /*!< in: Check for dup indexes
4904
4769
                                        in this table */
4905
 
        ibool                   tmp_ok) /*!< in: TRUE=allow temporary
4906
 
                                        index names */
4907
4770
{
4908
4771
        /* Check for duplicates, ignoring indexes that are marked
4909
4772
        as to be dropped */
4911
4774
        const dict_index_t*     index1;
4912
4775
        const dict_index_t*     index2;
4913
4776
 
4914
 
        ut_ad(mutex_own(&dict_sys->mutex));
4915
 
 
4916
4777
        /* The primary index _must_ exist */
4917
4778
        ut_a(UT_LIST_GET_LEN(table->indexes) > 0);
4918
4779
 
4919
4780
        index1 = UT_LIST_GET_FIRST(table->indexes);
4920
 
 
4921
 
        do {
4922
 
                ut_ad(tmp_ok || *index1->name != TEMP_INDEX_PREFIX);
4923
 
 
4924
 
                index2 = UT_LIST_GET_NEXT(indexes, index1);
 
4781
        index2 = UT_LIST_GET_NEXT(indexes, index1);
 
4782
 
 
4783
        while (index1 && index2) {
4925
4784
 
4926
4785
                while (index2) {
4927
4786
 
4933
4792
                }
4934
4793
 
4935
4794
                index1 = UT_LIST_GET_NEXT(indexes, index1);
4936
 
        } while (index1);
 
4795
                index2 = UT_LIST_GET_NEXT(indexes, index1);
 
4796
        }
4937
4797
}
4938
4798
#endif /* UNIV_DEBUG */
4939
4799
 
4951
4811
        for (i = 0; i < hash_get_n_cells(dict_sys->table_hash); i++) {
4952
4812
                dict_table_t*   table;
4953
4813
 
4954
 
                table = static_cast<dict_table_t *>(HASH_GET_FIRST(dict_sys->table_hash, i));
 
4814
                table = HASH_GET_FIRST(dict_sys->table_hash, i);
4955
4815
 
4956
4816
                while (table) {
4957
4817
                        dict_table_t*   prev_table = table;
4958
4818
 
4959
 
                        table = static_cast<dict_table_t *>(HASH_GET_NEXT(name_hash, prev_table));
 
4819
                        table = HASH_GET_NEXT(name_hash, prev_table);
4960
4820
#ifdef UNIV_DEBUG
4961
4821
                        ut_a(prev_table->magic_n == DICT_TABLE_MAGIC_N);
4962
4822
#endif
4986
4846
 
4987
4847
        mem_free(dict_sys);
4988
4848
        dict_sys = NULL;
4989
 
 
4990
 
        for (i = 0; i < DICT_TABLE_STATS_LATCHES_SIZE; i++) {
4991
 
                rw_lock_free(&dict_table_stats_latches[i]);
4992
 
        }
4993
4849
}
4994
4850
#endif /* !UNIV_HOTBACKUP */