forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathha_partition.cc
6195 lines (5259 loc) · 183 KB
/
ha_partition.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) 2005, 2023, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
This handler was developed by Mikael Ronstrom for version 5.1 of MySQL.
It is an abstraction layer on top of other handlers such as MyISAM,
InnoDB, Federated, Berkeley DB and so forth. Partitioned tables can also
be handled by a storage engine. The current example of this is NDB
Cluster that has internally handled partitioning. This have benefits in
that many loops needed in the partition handler can be avoided.
Partitioning has an inherent feature which in some cases is positive and
in some cases is negative. It splits the data into chunks. This makes
the data more manageable, queries can easily be parallelised towards the
parts and indexes are split such that there are less levels in the
index trees. The inherent disadvantage is that to use a split index
one has to scan all index parts which is ok for large queries but for
small queries it can be a disadvantage.
Partitioning lays the foundation for more manageable databases that are
extremely large. It does also lay the foundation for more parallelism
in the execution of queries. This functionality will grow with later
versions of MySQL.
You can enable it in your buld by doing the following during your build
process:
./configure --with-partition
The partition is setup to use table locks. It implements an partition "SHARE"
that is inserted into a hash by table name. You can use this to store
information of state that any partition handler object will be able to see
if it is using the same table.
Please read the object definition in ha_partition.h before reading the rest
if this file.
*/
/*
This engine need server classes (like THD etc.) which only is defined if
MYSQL_SERVER define is set!
*/
#define MYSQL_SERVER 1
#include "sql_parse.h" // append_file_to_dir
#include "partition_info.h" // partition_info
#include "ha_partition.h"
#include "sql_table.h" // tablename_to_filename
#include "key.h" // key_rec_cmp, field_unpack
#include "sql_show.h" // append_identifier
#include "sql_admin.h" // SQL_ADMIN_MSG_TEXT_SIZE
#include "myisam.h" // TT_FOR_UPGRADE
#include "sql_plugin.h" // plugin_unlock_list
#include "log.h" // sql_print_error
#include "debug_sync.h"
#ifndef NDEBUG
#include "sql_test.h" // print_where
#endif
#include "pfs_file_provider.h"
#include "mysql/psi/mysql_file.h"
using std::min;
using std::max;
/* First 4 bytes in the .par file is the number of 32-bit words in the file */
#define PAR_WORD_SIZE 4
/* offset to the .par file checksum */
#define PAR_CHECKSUM_OFFSET 4
/* offset to the total number of partitions */
#define PAR_NUM_PARTS_OFFSET 8
/* offset to the engines array */
#define PAR_ENGINES_OFFSET 12
#define PARTITION_ENABLED_TABLE_FLAGS (HA_FILE_BASED | \
HA_REC_NOT_IN_SEQ | \
HA_CAN_REPAIR)
#define PARTITION_DISABLED_TABLE_FLAGS (HA_CAN_GEOMETRY | \
HA_CAN_FULLTEXT | \
HA_DUPLICATE_POS | \
HA_READ_BEFORE_WRITE_REMOVAL)
static const char *ha_par_ext= ".par";
/** operation names for the enum_part_operation. */
static const char *opt_op_name[]= {"optimize", "analyze", "check", "repair",
"assign_to_keycache", "preload_keys"};
/****************************************************************************
MODULE create/delete handler object
****************************************************************************/
static handler *partition_create_handler(handlerton *hton,
TABLE_SHARE *share,
MEM_ROOT *mem_root);
static uint partition_flags();
static PSI_memory_key key_memory_ha_partition_file;
static PSI_memory_key key_memory_ha_partition_engine_array;
static PSI_memory_key key_memory_ha_partition_part_ids;
#ifdef HAVE_PSI_INTERFACE
static PSI_memory_info all_partition_memory[]=
{ { &key_memory_ha_partition_file, "ha_partition::file", 0},
{ &key_memory_ha_partition_engine_array, "ha_partition::engine_array", 0},
{ &key_memory_ha_partition_part_ids, "ha_partition::part_ids", 0} };
PSI_file_key key_file_ha_partition_par;
static PSI_file_info all_partition_file[]=
{ { &key_file_ha_partition_par, "ha_partition::parfile", 0} };
static void init_partition_psi_keys(void)
{
const char* category= "partition";
int count;
count= array_elements(all_partition_memory);
mysql_memory_register(category, all_partition_memory, count);
count= array_elements(all_partition_file);
mysql_file_register(category, all_partition_file, count);
}
#endif /* HAVE_PSI_INTERFACE */
static int partition_initialize(void *p)
{
handlerton *partition_hton;
partition_hton= (handlerton *)p;
partition_hton->state= SHOW_OPTION_YES;
partition_hton->db_type= DB_TYPE_PARTITION_DB;
partition_hton->create= partition_create_handler;
partition_hton->partition_flags= partition_flags;
partition_hton->flags= HTON_NOT_USER_SELECTABLE |
HTON_HIDDEN |
HTON_TEMPORARY_NOT_SUPPORTED;
#ifdef HAVE_PSI_INTERFACE
init_partition_psi_keys();
#endif
return 0;
}
Parts_share_refs::Parts_share_refs()
: num_parts(0), ha_shares(NULL)
{}
Parts_share_refs::~Parts_share_refs()
{
uint i;
if (ha_shares)
{
for (i= 0; i < num_parts; i++)
if (ha_shares[i])
delete ha_shares[i];
delete [] ha_shares;
}
}
bool Parts_share_refs::init(uint arg_num_parts)
{
assert(!num_parts && !ha_shares);
num_parts= arg_num_parts;
/* Allocate an array of Handler_share pointers */
ha_shares= new Handler_share *[num_parts];
if (!ha_shares)
{
num_parts= 0;
return true;
}
memset(ha_shares, 0, sizeof(Handler_share*) * num_parts);
return false;
}
Ha_partition_share::Ha_partition_share()
: Partition_share(), partitions_share_refs(NULL)
{}
Ha_partition_share::~Ha_partition_share()
{
if (partitions_share_refs)
delete partitions_share_refs;
}
/**
Initialize and allocate space for partitions shares.
@param num_parts Number of partitions to allocate storage for.
@return Operation status.
@retval true Failure (out of memory).
@retval false Success.
*/
bool Ha_partition_share::init(uint num_parts)
{
DBUG_ENTER("Ha_partition_share::init");
partitions_share_refs= new Parts_share_refs;
if (!partitions_share_refs)
DBUG_RETURN(true);
if (partitions_share_refs->init(num_parts))
{
delete partitions_share_refs;
DBUG_RETURN(true);
}
DBUG_RETURN(false);
}
/*
Create new partition handler
SYNOPSIS
partition_create_handler()
table Table object
RETURN VALUE
New partition object
*/
static handler *partition_create_handler(handlerton *hton,
TABLE_SHARE *share,
MEM_ROOT *mem_root)
{
ha_partition *file= new (mem_root) ha_partition(hton, share);
if (file && file->initialize_partition(mem_root))
{
delete file;
file= 0;
}
return file;
}
/*
HA_CAN_UPDATE_PARTITION_KEY:
Set if the handler can update fields that are part of the partition
function.
HA_CAN_PARTITION_UNIQUE:
Set if the handler can handle unique indexes where the fields of the
unique key are not part of the fields of the partition function. Thus
a unique key can be set on all fields.
HA_USE_AUTO_PARTITION
Set if the handler sets all tables to be partitioned by default.
HA_CAN_EXCHANGE_PARTITION:
Set if the handler can exchange a partition with a non-partitioned table
of the same handlerton/engine.
HA_CANNOT_PARTITION_FK:
Set if the handler does not support foreign keys on partitioned tables.
*/
static uint partition_flags()
{
return HA_CAN_EXCHANGE_PARTITION | HA_CANNOT_PARTITION_FK;
}
const uint32 ha_partition::NO_CURRENT_PART_ID= NOT_A_PARTITION_ID;
/*
Constructor method
SYNOPSIS
ha_partition()
table Table object
RETURN VALUE
NONE
*/
ha_partition::ha_partition(handlerton *hton, TABLE_SHARE *share)
: handler(hton, share),
Partition_helper(this)
{
DBUG_ENTER("ha_partition::ha_partition(table)");
init_handler_variables();
DBUG_VOID_RETURN;
}
/**
ha_partition constructor method used by ha_partition::clone()
@param hton Handlerton (partition_hton)
@param share Table share object
@param part_info_arg partition_info to use
@param clone_arg ha_partition to clone
@param clme_mem_root_arg MEM_ROOT to use
@return New partition handler
*/
ha_partition::ha_partition(handlerton *hton, TABLE_SHARE *share,
partition_info *part_info_arg,
ha_partition *clone_arg,
MEM_ROOT *clone_mem_root_arg)
: handler(hton, share),
Partition_helper(this)
{
DBUG_ENTER("ha_partition::ha_partition(clone)");
init_handler_variables();
m_part_info= part_info_arg;
m_is_sub_partitioned= m_part_info->is_sub_partitioned();
m_is_clone_of= clone_arg;
m_clone_mem_root= clone_mem_root_arg;
part_share= clone_arg->part_share;
m_tot_parts= clone_arg->m_tot_parts;
m_pkey_is_clustered= clone_arg->primary_key_is_clustered();
DBUG_VOID_RETURN;
}
/*
Initialize handler object
SYNOPSIS
init_handler_variables()
RETURN VALUE
NONE
*/
void ha_partition::init_handler_variables()
{
active_index= MAX_KEY;
m_mode= 0;
m_open_test_lock= 0;
m_file_buffer= NULL;
m_name_buffer_ptr= NULL;
m_engine_array= NULL;
m_file= NULL;
m_file_tot_parts= 0;
m_tot_parts= 0;
m_pkey_is_clustered= 0;
m_myisam= FALSE;
m_innodb= FALSE;
m_extra_cache= FALSE;
m_extra_cache_size= 0;
m_extra_prepare_for_update= FALSE;
m_extra_cache_part_id= NO_CURRENT_PART_ID;
m_handler_status= handler_not_initialized;
m_low_byte_first= 1;
m_part_func_monotonicity_info= NON_MONOTONIC;
/*
this allows blackhole to work properly
*/
m_num_locks= 0;
m_is_clone_of= NULL;
m_clone_mem_root= NULL;
part_share= NULL;
m_new_partitions_share_refs.empty();
m_part_ids_sorted_by_num_of_records= NULL;
m_new_file= NULL;
m_num_new_partitions= 0;
m_indexes_are_disabled= false;
}
const char *ha_partition::table_type() const
{
// we can do this since we only support a single engine type
return m_file[0]->table_type();
}
/*
Destructor method
SYNOPSIS
~ha_partition()
RETURN VALUE
NONE
*/
ha_partition::~ha_partition()
{
DBUG_ENTER("ha_partition::~ha_partition()");
if (m_new_partitions_share_refs.elements)
m_new_partitions_share_refs.delete_elements();
if (m_file != NULL)
{
uint i;
for (i= 0; i < m_tot_parts; i++)
delete m_file[i];
}
my_free(m_part_ids_sorted_by_num_of_records);
clear_handler_file();
DBUG_VOID_RETURN;
}
/*
Initialize partition handler object
SYNOPSIS
initialize_partition()
mem_root Allocate memory through this
RETURN VALUE
1 Error
0 Success
DESCRIPTION
The partition handler is only a layer on top of other engines. Thus it
can't really perform anything without the underlying handlers. Thus we
add this method as part of the allocation of a handler object.
1) Allocation of underlying handlers
If we have access to the partition info we will allocate one handler
instance for each partition.
2) Allocation without partition info
The cases where we don't have access to this information is when called
in preparation for delete_table and rename_table and in that case we
only need to set HA_FILE_BASED. In that case we will use the .par file
that contains information about the partitions and their engines and
the names of each partition.
3) Table flags initialisation
We need also to set table flags for the partition handler. This is not
static since it depends on what storage engines are used as underlying
handlers.
The table flags is set in this routine to simulate the behaviour of a
normal storage engine
The flag HA_FILE_BASED will be set independent of the underlying handlers
4) Index flags initialisation
When knowledge exists on the indexes it is also possible to initialize the
index flags. Again the index flags must be initialized by using the under-
lying handlers since this is storage engine dependent.
The flag HA_READ_ORDER will be reset for the time being to indicate no
ordered output is available from partition handler indexes. Later a merge
sort will be performed using the underlying handlers.
5) primary_key_is_clustered, has_transactions and low_byte_first is
calculated here.
*/
bool ha_partition::initialize_partition(MEM_ROOT *mem_root)
{
handler **file_array, *file;
ulonglong check_table_flags;
DBUG_ENTER("ha_partition::initialize_partition");
if (Partition_helper::init_partitioning(mem_root))
{
DBUG_RETURN(true);
}
if (m_part_info)
{
assert(m_tot_parts > 0);
if (new_handlers_from_part_info(mem_root))
DBUG_RETURN(true);
}
else if (!table_share || !table_share->normalized_path.str)
{
/*
Called with dummy table share (delete, rename and alter table).
Don't need to set-up anything.
*/
DBUG_RETURN(false);
}
else if (get_from_handler_file(table_share->normalized_path.str,
mem_root, false))
{
my_error(ER_FAILED_READ_FROM_PAR_FILE, MYF(0));
DBUG_RETURN(true);
}
/*
We create all underlying table handlers here. We do it in this special
method to be able to report allocation errors.
Set up low_byte_first, primary_key_is_clustered and
has_transactions since they are called often in all kinds of places,
other parameters are calculated on demand.
Verify that all partitions have the same table_flags.
*/
check_table_flags= m_file[0]->ha_table_flags();
m_low_byte_first= m_file[0]->low_byte_first();
m_pkey_is_clustered= TRUE;
file_array= m_file;
do
{
file= *file_array;
if (m_low_byte_first != file->low_byte_first())
{
// Cannot have handlers with different endian
my_error(ER_MIX_HANDLER_ERROR, MYF(0));
DBUG_RETURN(true);
}
if (!file->primary_key_is_clustered())
m_pkey_is_clustered= FALSE;
if (check_table_flags != file->ha_table_flags())
{
my_error(ER_MIX_HANDLER_ERROR, MYF(0));
DBUG_RETURN(true);
}
} while (*(++file_array));
m_handler_status= handler_initialized;
DBUG_RETURN(false);
}
/****************************************************************************
MODULE meta data changes
****************************************************************************/
/*
Delete a table
SYNOPSIS
delete_table()
name Full path of table name
RETURN VALUE
>0 Error
0 Success
DESCRIPTION
Used to delete a table. By the time delete_table() has been called all
opened references to this table will have been closed (and your globally
shared references released. The variable name will just be the name of
the table. You will need to remove any files you have created at this
point.
If you do not implement this, the default delete_table() is called from
handler.cc and it will delete all files with the file extentions returned
by bas_ext().
Called from handler.cc by delete_table and ha_create_table(). Only used
during create if the table_flag HA_DROP_BEFORE_CREATE was specified for
the storage engine.
*/
int ha_partition::delete_table(const char *name)
{
DBUG_ENTER("ha_partition::delete_table");
DBUG_RETURN(del_ren_table(name, NULL));
}
/*
Rename a table
SYNOPSIS
rename_table()
from Full path of old table name
to Full path of new table name
RETURN VALUE
>0 Error
0 Success
DESCRIPTION
Renames a table from one name to another from alter table call.
If you do not implement this, the default rename_table() is called from
handler.cc and it will rename all files with the file extentions returned
by bas_ext().
Called from sql_table.cc by mysql_rename_table().
*/
int ha_partition::rename_table(const char *from, const char *to)
{
DBUG_ENTER("ha_partition::rename_table");
DBUG_RETURN(del_ren_table(from, to));
}
/*
Create the handler file (.par-file)
SYNOPSIS
create_handler_files()
name Full path of table name
create_info Create info generated for CREATE TABLE
RETURN VALUE
>0 Error
0 Success
DESCRIPTION
create_handler_files is called to create any handler specific files
before opening the file with openfrm to later call ::create on the
file object.
In the partition handler this is used to store the names of partitions
and types of engines in the partitions.
*/
int ha_partition::create_handler_files(const char *path,
const char *old_path,
int action_flag,
HA_CREATE_INFO *create_info)
{
DBUG_ENTER("ha_partition::create_handler_files()");
/*
We need to update total number of parts since we might write the handler
file as part of a partition management command
*/
if (action_flag == CHF_DELETE_FLAG ||
action_flag == CHF_RENAME_FLAG)
{
char name[FN_REFLEN];
char old_name[FN_REFLEN];
strxmov(name, path, ha_par_ext, NullS);
strxmov(old_name, old_path, ha_par_ext, NullS);
if ((action_flag == CHF_DELETE_FLAG &&
mysql_file_delete(key_file_ha_partition_par, name, MYF(MY_WME))) ||
(action_flag == CHF_RENAME_FLAG &&
mysql_file_rename(key_file_ha_partition_par,
old_name,
name,
MYF(MY_WME))))
{
DBUG_RETURN(TRUE);
}
}
else if (action_flag == CHF_CREATE_FLAG)
{
if (create_handler_file(path))
{
my_error(ER_CANT_CREATE_HANDLER_FILE, MYF(0));
DBUG_RETURN(1);
}
}
DBUG_RETURN(0);
}
/*
Create a partitioned table
SYNOPSIS
create()
name Full path of table name
table_arg Table object
create_info Create info generated for CREATE TABLE
RETURN VALUE
>0 Error
0 Success
DESCRIPTION
create() is called to create a table. The variable name will have the name
of the table. When create() is called you do not need to worry about
opening the table. Also, the FRM file will have already been created so
adjusting create_info will not do you any good. You can overwrite the frm
file at this point if you wish to change the table definition, but there
are no methods currently provided for doing that.
Called from handler.cc by ha_create_table().
*/
int ha_partition::create(const char *name, TABLE *table_arg,
HA_CREATE_INFO *create_info)
{
int error;
char name_buff[FN_REFLEN], name_lc_buff[FN_REFLEN];
char *name_buffer_ptr;
const char *path;
uint i;
List_iterator_fast <partition_element> part_it(m_part_info->partitions);
partition_element *part_elem;
partition_element table_level_options;
handler **file, **abort_file;
THD *thd= ha_thd();
TABLE_SHARE *share= table_arg->s;
DBUG_ENTER("ha_partition::create");
assert(*fn_rext((char*)name) == '\0');
/* Not allowed to create temporary partitioned tables */
if (create_info && create_info->options & HA_LEX_CREATE_TMP_TABLE)
{
my_error(ER_PARTITION_NO_TEMPORARY, MYF(0));
DBUG_RETURN(TRUE);
}
if (get_from_handler_file(name, ha_thd()->mem_root, false))
DBUG_RETURN(TRUE);
assert(m_file_buffer);
DBUG_PRINT("enter", ("name: (%s)", name));
name_buffer_ptr= m_name_buffer_ptr;
file= m_file;
/*
Since ha_partition has HA_FILE_BASED, it must alter underlying table names
if they do not have HA_FILE_BASED and lower_case_table_names == 2.
See Bug#37402, for Mac OS X.
The appended #P#<partname>[#SP#<subpartname>] will remain in current case.
Using the first partitions handler, since mixing handlers is not allowed.
*/
path= get_canonical_filename(*file, name, name_lc_buff);
table_level_options.set_from_info(create_info);
for (i= 0; i < m_part_info->num_parts; i++)
{
part_elem= part_it++;
if (m_is_sub_partitioned)
{
uint j;
List_iterator_fast <partition_element> sub_it(part_elem->subpartitions);
for (j= 0; j < m_part_info->num_subparts; j++)
{
part_elem= sub_it++;
create_partition_name(name_buff, path, name_buffer_ptr,
NORMAL_PART_NAME, FALSE);
if ((error= set_up_table_before_create(thd, share, name_buff,
create_info, part_elem)) ||
((error= (*file)->ha_create(name_buff, table_arg, create_info))))
goto create_error;
table_level_options.put_to_info(create_info);
name_buffer_ptr= strend(name_buffer_ptr) + 1;
file++;
}
}
else
{
create_partition_name(name_buff, path, name_buffer_ptr,
NORMAL_PART_NAME, FALSE);
if ((error= set_up_table_before_create(thd, share, name_buff,
create_info, part_elem)) ||
((error= (*file)->ha_create(name_buff, table_arg, create_info))))
goto create_error;
table_level_options.put_to_info(create_info);
name_buffer_ptr= strend(name_buffer_ptr) + 1;
file++;
}
}
DBUG_RETURN(0);
create_error:
name_buffer_ptr= m_name_buffer_ptr;
for (abort_file= file, file= m_file; file < abort_file; file++)
{
create_partition_name(name_buff, path, name_buffer_ptr, NORMAL_PART_NAME,
FALSE);
(void) (*file)->ha_delete_table((const char*) name_buff);
name_buffer_ptr= strend(name_buffer_ptr) + 1;
}
handler::delete_table(name);
DBUG_RETURN(error);
}
/*
Optimize table
SYNOPSIS
optimize()
thd Thread object
check_opt Check/analyze/repair/optimize options
RETURN VALUES
>0 Error
0 Success
*/
int ha_partition::optimize(THD *thd, HA_CHECK_OPT *check_opt)
{
DBUG_ENTER("ha_partition::optimize");
DBUG_RETURN(handle_opt_partitions(thd, check_opt, OPTIMIZE_PARTS));
}
/*
Analyze table
SYNOPSIS
analyze()
thd Thread object
check_opt Check/analyze/repair/optimize options
RETURN VALUES
>0 Error
0 Success
*/
int ha_partition::analyze(THD *thd, HA_CHECK_OPT *check_opt)
{
DBUG_ENTER("ha_partition::analyze");
DBUG_RETURN(handle_opt_partitions(thd, check_opt, ANALYZE_PARTS));
}
/*
Check table
SYNOPSIS
check()
thd Thread object
check_opt Check/analyze/repair/optimize options
RETURN VALUES
>0 Error
0 Success
*/
int ha_partition::check(THD *thd, HA_CHECK_OPT *check_opt)
{
DBUG_ENTER("ha_partition::check");
DBUG_RETURN(handle_opt_partitions(thd, check_opt, CHECK_PARTS));
}
/*
Repair table
SYNOPSIS
repair()
thd Thread object
check_opt Check/analyze/repair/optimize options
RETURN VALUES
>0 Error
0 Success
*/
int ha_partition::repair(THD *thd, HA_CHECK_OPT *check_opt)
{
DBUG_ENTER("ha_partition::repair");
DBUG_RETURN(handle_opt_partitions(thd, check_opt, REPAIR_PARTS));
}
/**
Assign to keycache
@param thd Thread object
@param check_opt Check/analyze/repair/optimize options
@return
@retval >0 Error
@retval 0 Success
*/
int ha_partition::assign_to_keycache(THD *thd, HA_CHECK_OPT *check_opt)
{
DBUG_ENTER("ha_partition::assign_to_keycache");
DBUG_RETURN(handle_opt_partitions(thd, check_opt, ASSIGN_KEYCACHE_PARTS));
}
/**
Preload to keycache
@param thd Thread object
@param check_opt Check/analyze/repair/optimize options
@return
@retval >0 Error
@retval 0 Success
*/
int ha_partition::preload_keys(THD *thd, HA_CHECK_OPT *check_opt)
{
DBUG_ENTER("ha_partition::preload_keys");
DBUG_RETURN(handle_opt_partitions(thd, check_opt, PRELOAD_KEYS_PARTS));
}
/*
Handle optimize/analyze/check/repair of one partition
SYNOPSIS
handle_opt_part()
thd Thread object
check_opt Options
file Handler object of partition
flag Optimize/Analyze/Check/Repair flag
RETURN VALUE
>0 Failure
0 Success
*/
int ha_partition::handle_opt_part(THD *thd, HA_CHECK_OPT *check_opt,
uint part_id, enum_part_operation operation)
{
int error;
handler *file= m_file[part_id];
DBUG_ENTER("handle_opt_part");
DBUG_PRINT("enter", ("operation = %u", operation));
if (operation == OPTIMIZE_PARTS)
error= file->ha_optimize(thd, check_opt);
else if (operation == ANALYZE_PARTS)
error= file->ha_analyze(thd, check_opt);
else if (operation == CHECK_PARTS)
{
error= file->ha_check(thd, check_opt);
if (!error ||
error == HA_ADMIN_ALREADY_DONE ||
error == HA_ADMIN_NOT_IMPLEMENTED)
{
if (check_opt->flags & (T_MEDIUM | T_EXTEND))
error= Partition_helper::check_misplaced_rows(part_id, false);
}
}
else if (operation == REPAIR_PARTS)
{
error= file->ha_repair(thd, check_opt);
if (!error ||
error == HA_ADMIN_ALREADY_DONE ||
error == HA_ADMIN_NOT_IMPLEMENTED)
{
if (check_opt->flags & (T_MEDIUM | T_EXTEND))
error= Partition_helper::check_misplaced_rows(part_id, true);
}
}
else if (operation == ASSIGN_KEYCACHE_PARTS)
error= file->assign_to_keycache(thd, check_opt);
else if (operation == PRELOAD_KEYS_PARTS)
error= file->preload_keys(thd, check_opt);
else
{
assert(FALSE);
error= 1;
}
if (error == HA_ADMIN_ALREADY_DONE)
error= 0;
DBUG_RETURN(error);
}
/*
Handle optimize/analyze/check/repair of partitions
SYNOPSIS
handle_opt_partitions()
thd Thread object
check_opt Options
operation Optimize/Analyze/Check/Repair flag
RETURN VALUE
>0 Failure
0 Success
*/
int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt,
enum_part_operation operation)
{
List_iterator<partition_element> part_it(m_part_info->partitions);
uint num_parts= m_part_info->num_parts;
uint num_subparts= m_part_info->num_subparts;
uint i= 0;
bool use_all_parts= !(thd->lex->alter_info.flags &
Alter_info::ALTER_ADMIN_PARTITION);
int error;
DBUG_ENTER("ha_partition::handle_opt_partitions");
DBUG_PRINT("enter", ("operation= %u", operation));
do
{
partition_element *part_elem= part_it++;
/*
when ALTER TABLE <CMD> PARTITION ...
it should only do named [sub]partitions, otherwise all partitions
*/
if (m_is_sub_partitioned)
{
List_iterator<partition_element> subpart_it(part_elem->subpartitions);
partition_element *sub_elem;
uint j= 0, part;
do
{
sub_elem= subpart_it++;
if (use_all_parts ||
part_elem->part_state == PART_ADMIN ||
sub_elem->part_state == PART_ADMIN)