~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Andrew Hutchings
  • Date: 2010-12-09 13:44:51 UTC
  • mfrom: (1986 real-trunk)
  • mto: This revision was merged to the branch mainline in revision 2006.
  • Revision ID: andrew@linuxjedi.co.uk-20101209134451-hsu1l98ks2pwvucu
Merge trunk into branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
255
255
 
256
256
/** Get the mutex that protects index->stat_n_diff_key_vals[] */
257
257
#define GET_INDEX_STAT_MUTEX(index) \
258
 
        (&dict_index_stat_mutex[ut_fold_dulint(index->id) \
259
 
                                % DICT_INDEX_STAT_MUTEX_SIZE])
 
258
        (&dict_index_stat_mutex[ut_fold_ull(index->id) \
 
259
                                % DICT_INDEX_STAT_MUTEX_SIZE])
260
260
 
261
261
/**********************************************************************//**
262
262
Lock the appropriate mutex to protect index->stat_n_diff_key_vals[].
424
424
dict_index_get_on_id_low(
425
425
/*=====================*/
426
426
        dict_table_t*   table,  /*!< in: table */
427
 
        dulint          id)     /*!< in: index id */
 
427
        index_id_t      id)     /*!< in: index id */
428
428
{
429
429
        dict_index_t*   index;
430
430
 
431
431
        index = dict_table_get_first_index(table);
432
432
 
433
433
        while (index) {
434
 
                if (0 == ut_dulint_cmp(id, index->id)) {
 
434
                if (id == index->id) {
435
435
                        /* Found */
436
436
 
437
437
                        return(index);
573
573
dict_table_t*
574
574
dict_table_get_on_id(
575
575
/*=================*/
576
 
        dulint  table_id,       /*!< in: table id */
577
 
        trx_t*  trx)            /*!< in: transaction handle */
 
576
        table_id_t      table_id,       /*!< in: table id */
 
577
        trx_t*          trx)            /*!< in: transaction handle */
578
578
{
579
579
        dict_table_t*   table;
580
580
 
581
 
        if (ut_dulint_cmp(table_id, DICT_FIELDS_ID) <= 0
582
 
            || trx->dict_operation_lock_mode == RW_X_LATCH) {
583
 
                /* It is a system table which will always exist in the table
584
 
                cache: we avoid acquiring the dictionary mutex, because
585
 
                if we are doing a rollback to handle an error in TABLE
586
 
                CREATE, for example, we already have the mutex! */
587
 
 
588
 
                ut_ad(mutex_own(&(dict_sys->mutex))
589
 
                      || trx->dict_operation_lock_mode == RW_X_LATCH);
 
581
        if (trx->dict_operation_lock_mode == RW_X_LATCH) {
 
582
 
 
583
                /* Note: An X latch implies that the transaction
 
584
                already owns the dictionary mutex. */
 
585
 
 
586
                ut_ad(mutex_own(&dict_sys->mutex));
590
587
 
591
588
                return(dict_table_get_on_id_low(table_id));
592
589
        }
799
796
        table->cached = TRUE;
800
797
 
801
798
        fold = ut_fold_string(table->name);
802
 
        id_fold = ut_fold_dulint(table->id);
 
799
        id_fold = ut_fold_ull(table->id);
803
800
 
804
801
        row_len = 0;
805
802
        for (i = 0; i < table->n_def; i++) {
841
838
                dict_table_t*   table2;
842
839
                HASH_SEARCH(id_hash, dict_sys->table_id_hash, id_fold,
843
840
                            dict_table_t*, table2, ut_ad(table2->cached),
844
 
                            ut_dulint_cmp(table2->id, table->id) == 0);
 
841
                            table2->id == table->id);
845
842
                ut_a(table2 == NULL);
846
843
 
847
844
#ifdef UNIV_DEBUG
863
860
        /* Add table to LRU list of tables */
864
861
        UT_LIST_ADD_FIRST(table_LRU, dict_sys->table_LRU, table);
865
862
 
866
 
        dict_sys->size += mem_heap_get_size(table->heap);
 
863
        dict_sys->size += mem_heap_get_size(table->heap)
 
864
                + strlen(table->name) + 1;
867
865
}
868
866
 
869
867
/**********************************************************************//**
875
873
dict_index_t*
876
874
dict_index_find_on_id_low(
877
875
/*======================*/
878
 
        dulint  id)     /*!< in: index id */
 
876
        index_id_t      id)     /*!< in: index id */
879
877
{
880
878
        dict_table_t*   table;
881
879
        dict_index_t*   index;
886
884
                index = dict_table_get_first_index(table);
887
885
 
888
886
                while (index) {
889
 
                        if (0 == ut_dulint_cmp(id, index->id)) {
 
887
                        if (id == index->id) {
890
888
                                /* Found */
891
889
 
892
890
                                return(index);
917
915
        dict_foreign_t* foreign;
918
916
        dict_index_t*   index;
919
917
        ulint           fold;
920
 
        ulint           old_size;
921
 
        const char*     old_name;
 
918
        char            old_name[MAX_TABLE_NAME_LEN + 1];
922
919
 
923
920
        ut_ad(table);
924
921
        ut_ad(mutex_own(&(dict_sys->mutex)));
925
922
 
926
 
        old_size = mem_heap_get_size(table->heap);
927
 
        old_name = table->name;
 
923
        /* store the old/current name to an automatic variable */
 
924
        if (strlen(table->name) + 1 <= sizeof(old_name)) {
 
925
                memcpy(old_name, table->name, strlen(table->name) + 1);
 
926
        } else {
 
927
                ut_print_timestamp(stderr);
 
928
                fprintf(stderr, "InnoDB: too long table name: '%s', "
 
929
                        "max length is %d\n", table->name,
 
930
                        MAX_TABLE_NAME_LEN);
 
931
                ut_error;
 
932
        }
928
933
 
929
934
        fold = ut_fold_string(new_name);
930
935
 
970
975
        /* Remove table from the hash tables of tables */
971
976
        HASH_DELETE(dict_table_t, name_hash, dict_sys->table_hash,
972
977
                    ut_fold_string(old_name), table);
973
 
        table->name = mem_heap_strdup(table->heap, new_name);
 
978
 
 
979
        if (strlen(new_name) > strlen(table->name)) {
 
980
                /* We allocate MAX_TABLE_NAME_LEN+1 bytes here to avoid
 
981
                memory fragmentation, we assume a repeated calls of
 
982
                ut_realloc() with the same size do not cause fragmentation */
 
983
                ut_a(strlen(new_name) <= MAX_TABLE_NAME_LEN);
 
984
                table->name = ut_realloc(table->name, MAX_TABLE_NAME_LEN + 1);
 
985
        }
 
986
        memcpy(table->name, new_name, strlen(new_name) + 1);
974
987
 
975
988
        /* Add table to hash table of tables */
976
989
        HASH_INSERT(dict_table_t, name_hash, dict_sys->table_hash, fold,
977
990
                    table);
978
 
        dict_sys->size += (mem_heap_get_size(table->heap) - old_size);
 
991
 
 
992
        dict_sys->size += strlen(new_name) - strlen(old_name);
 
993
        ut_a(dict_sys->size > 0);
979
994
 
980
995
        /* Update the table_name field in indexes */
981
996
        index = dict_table_get_first_index(table);
1125
1140
dict_table_change_id_in_cache(
1126
1141
/*==========================*/
1127
1142
        dict_table_t*   table,  /*!< in/out: table object already in cache */
1128
 
        dulint          new_id) /*!< in: new id to set */
 
1143
        table_id_t      new_id) /*!< in: new id to set */
1129
1144
{
1130
1145
        ut_ad(table);
1131
1146
        ut_ad(mutex_own(&(dict_sys->mutex)));
1134
1149
        /* Remove the table from the hash table of id's */
1135
1150
 
1136
1151
        HASH_DELETE(dict_table_t, id_hash, dict_sys->table_id_hash,
1137
 
                    ut_fold_dulint(table->id), table);
 
1152
                    ut_fold_ull(table->id), table);
1138
1153
        table->id = new_id;
1139
1154
 
1140
1155
        /* Add the table back to the hash table */
1141
1156
        HASH_INSERT(dict_table_t, id_hash, dict_sys->table_id_hash,
1142
 
                    ut_fold_dulint(table->id), table);
 
1157
                    ut_fold_ull(table->id), table);
1143
1158
}
1144
1159
 
1145
1160
/**********************************************************************//**
1195
1210
        HASH_DELETE(dict_table_t, name_hash, dict_sys->table_hash,
1196
1211
                    ut_fold_string(table->name), table);
1197
1212
        HASH_DELETE(dict_table_t, id_hash, dict_sys->table_id_hash,
1198
 
                    ut_fold_dulint(table->id), table);
 
1213
                    ut_fold_ull(table->id), table);
1199
1214
 
1200
1215
        /* Remove table from LRU list of tables */
1201
1216
        UT_LIST_REMOVE(table_LRU, dict_sys->table_LRU, table);
1202
1217
 
1203
 
        size = mem_heap_get_size(table->heap);
 
1218
        size = mem_heap_get_size(table->heap) + strlen(table->name) + 1;
1204
1219
 
1205
1220
        ut_ad(dict_sys->size >= size);
1206
1221
 
2457
2472
                                /* We found a matching index, select
2458
2473
                                the index with the higher id*/
2459
2474
 
2460
 
                                if (!found
2461
 
                                    || ut_dulint_cmp(index->id, found->id) > 0) {
 
2475
                                if (!found || index->id > found->id) {
2462
2476
 
2463
2477
                                        found = index;
2464
2478
                                }
3946
3960
dict_index_t*
3947
3961
dict_index_get_if_in_cache_low(
3948
3962
/*===========================*/
3949
 
        dulint  index_id)       /*!< in: index id */
 
3963
        index_id_t      index_id)       /*!< in: index id */
3950
3964
{
3951
3965
        ut_ad(mutex_own(&(dict_sys->mutex)));
3952
3966
 
3961
3975
dict_index_t*
3962
3976
dict_index_get_if_in_cache(
3963
3977
/*=======================*/
3964
 
        dulint  index_id)       /*!< in: index id */
 
3978
        index_id_t      index_id)       /*!< in: index id */
3965
3979
{
3966
3980
        dict_index_t*   index;
3967
3981
 
4190
4204
                                        dictionary mutex */
4191
4205
{
4192
4206
        dict_index_t*   index;
4193
 
        ulint           size;
4194
4207
        ulint           sum_of_index_sizes      = 0;
4195
4208
 
4196
4209
        if (table->ibd_file_missing) {
4205
4218
                return;
4206
4219
        }
4207
4220
 
4208
 
        /* If we have set a high innodb_force_recovery level, do not calculate
4209
 
        statistics, as a badly corrupted index can cause a crash in it. */
4210
 
 
4211
 
        if (srv_force_recovery >= SRV_FORCE_NO_IBUF_MERGE) {
4212
 
 
4213
 
                return;
4214
 
        }
4215
 
 
4216
4221
        /* Find out the sizes of the indexes and how many different values
4217
4222
        for the key they approximately have */
4218
4223
 
4224
4229
                return;
4225
4230
        }
4226
4231
 
4227
 
        while (index) {
4228
 
                size = btr_get_size(index, BTR_TOTAL_SIZE);
4229
 
 
4230
 
                index->stat_index_size = size;
4231
 
 
4232
 
                sum_of_index_sizes += size;
4233
 
 
4234
 
                size = btr_get_size(index, BTR_N_LEAF_PAGES);
4235
 
 
4236
 
                if (size == 0) {
4237
 
                        /* The root node of the tree is a leaf */
4238
 
                        size = 1;
 
4232
 
 
4233
        do {
 
4234
                if (UNIV_LIKELY
 
4235
                    (srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE
 
4236
                     || (srv_force_recovery < SRV_FORCE_NO_LOG_REDO
 
4237
                         && dict_index_is_clust(index)))) {
 
4238
                        ulint   size;
 
4239
                        size = btr_get_size(index, BTR_TOTAL_SIZE);
 
4240
 
 
4241
                        index->stat_index_size = size;
 
4242
 
 
4243
                        sum_of_index_sizes += size;
 
4244
 
 
4245
                        size = btr_get_size(index, BTR_N_LEAF_PAGES);
 
4246
 
 
4247
                        if (size == 0) {
 
4248
                                /* The root node of the tree is a leaf */
 
4249
                                size = 1;
 
4250
                        }
 
4251
 
 
4252
                        index->stat_n_leaf_pages = size;
 
4253
 
 
4254
                        btr_estimate_number_of_different_key_vals(index);
 
4255
                } else {
 
4256
                        /* If we have set a high innodb_force_recovery
 
4257
                        level, do not calculate statistics, as a badly
 
4258
                        corrupted index can cause a crash in it.
 
4259
                        Initialize some bogus index cardinality
 
4260
                        statistics, so that the data can be queried in
 
4261
                        various means, also via secondary indexes. */
 
4262
                        ulint   i;
 
4263
 
 
4264
                        sum_of_index_sizes++;
 
4265
                        index->stat_index_size = index->stat_n_leaf_pages = 1;
 
4266
 
 
4267
                        for (i = dict_index_get_n_unique(index); i; ) {
 
4268
                                index->stat_n_diff_key_vals[i--] = 1;
 
4269
                        }
4239
4270
                }
4240
4271
 
4241
 
                index->stat_n_leaf_pages = size;
4242
 
 
4243
 
                btr_estimate_number_of_different_key_vals(index);
4244
 
 
4245
4272
                index = dict_table_get_next_index(index);
4246
 
        }
 
4273
        } while (index);
4247
4274
 
4248
4275
        index = dict_table_get_first_index(table);
4249
4276
 
4357
4384
 
4358
4385
        fprintf(stderr,
4359
4386
                "--------------------------------------\n"
4360
 
                "TABLE: name %s, id %lu %lu, flags %lx, columns %lu,"
 
4387
                "TABLE: name %s, id %llu, flags %lx, columns %lu,"
4361
4388
                " indexes %lu, appr.rows %lu\n"
4362
4389
                "  COLUMNS: ",
4363
4390
                table->name,
4364
 
                (ulong) ut_dulint_get_high(table->id),
4365
 
                (ulong) ut_dulint_get_low(table->id),
 
4391
                (ullint) table->id,
4366
4392
                (ulong) table->flags,
4367
4393
                (ulong) table->n_cols,
4368
4394
                (ulong) UT_LIST_GET_LEN(table->indexes),
4427
4453
{
4428
4454
        ib_int64_t      n_vals;
4429
4455
        ulint           i;
4430
 
        const char*     type_string;
4431
4456
 
4432
4457
        ut_ad(mutex_own(&(dict_sys->mutex)));
4433
4458
 
4442
4467
 
4443
4468
        dict_index_stat_mutex_exit(index);
4444
4469
 
4445
 
        if (dict_index_is_clust(index)) {
4446
 
                type_string = "clustered index";
4447
 
        } else if (dict_index_is_unique(index)) {
4448
 
                type_string = "unique index";
4449
 
        } else {
4450
 
                type_string = "secondary index";
4451
 
        }
4452
 
 
4453
4470
        fprintf(stderr,
4454
 
                "  INDEX: name %s, id %lu %lu, fields %lu/%lu,"
 
4471
                "  INDEX: name %s, id %llu, fields %lu/%lu,"
4455
4472
                " uniq %lu, type %lu\n"
4456
4473
                "   root page %lu, appr.key vals %lu,"
4457
4474
                " leaf pages %lu, size pages %lu\n"
4458
4475
                "   FIELDS: ",
4459
4476
                index->name,
4460
 
                (ulong) ut_dulint_get_high(index->id),
4461
 
                (ulong) ut_dulint_get_low(index->id),
 
4477
                (ullint) index->id,
4462
4478
                (ulong) index->n_user_defined_cols,
4463
4479
                (ulong) index->n_fields,
4464
4480
                (ulong) index->n_uniq,
4830
4846
 
4831
4847
        while (index != NULL) {
4832
4848
                if (ut_strcmp(index->name, name) == 0) {
4833
 
                        if (!min_index
4834
 
                            || ut_dulint_cmp(index->id, min_index->id) < 0) {
 
4849
                        if (!min_index || index->id < min_index->id) {
4835
4850
 
4836
4851
                                min_index = index;
4837
4852
                        }