forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition_handler.h
1276 lines (1093 loc) · 42.5 KB
/
partition_handler.h
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
#ifndef PARTITION_HANDLER_INCLUDED
#define PARTITION_HANDLER_INCLUDED
/*
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
*/
#include "my_global.h" // uint etc.
#include "my_base.h" // ha_rows.
#include "handler.h" // Handler_share
#include "sql_partition.h" // part_id_range
#include "mysqld_error.h" // ER_ILLEGAL_HA
#include "priority_queue.h"
#include "key.h" // key_rec_cmp
#include <vector>
#define PARTITION_BYTES_IN_POS 2
/* forward declarations */
typedef struct st_ha_create_information HA_CREATE_INFO;
typedef struct st_mem_root MEM_ROOT;
static const uint NO_CURRENT_PART_ID= UINT_MAX32;
/**
bits in Partition_handler::alter_flags():
HA_PARTITION_FUNCTION_SUPPORTED indicates that the function is
supported at all.
HA_FAST_CHANGE_PARTITION means that optimized variants of the changes
exists but they are not necessarily done online.
HA_ONLINE_DOUBLE_WRITE means that the handler supports writing to both
the new partition and to the old partitions when updating through the
old partitioning schema while performing a change of the partitioning.
This means that we can support updating of the table while performing
the copy phase of the change. For no lock at all also a double write
from new to old must exist and this is not required when this flag is
set.
This is actually removed even before it was introduced the first time.
The new idea is that handlers will handle the lock level already in
store_lock for ALTER TABLE partitions.
TODO: Implement this via the alter-inplace api.
*/
#define HA_PARTITION_FUNCTION_SUPPORTED (1L << 0)
#define HA_FAST_CHANGE_PARTITION (1L << 1)
enum enum_part_operation {
OPTIMIZE_PARTS= 0,
ANALYZE_PARTS,
CHECK_PARTS,
REPAIR_PARTS,
ASSIGN_KEYCACHE_PARTS,
PRELOAD_KEYS_PARTS
};
/** Struct used for partition_name_hash */
typedef struct st_part_name_def
{
uchar *partition_name;
uint length;
uint32 part_id;
my_bool is_subpart;
} PART_NAME_DEF;
/**
Initialize partitioning (currently only PSI keys).
*/
void partitioning_init();
/**
Partition specific Handler_share.
*/
class Partition_share : public Handler_share
{
public:
Partition_share();
~Partition_share();
/** Set if auto increment is used an initialized. */
bool auto_inc_initialized;
/**
Mutex protecting next_auto_inc_val.
Initialized if table uses auto increment.
*/
mysql_mutex_t *auto_inc_mutex;
/** First non reserved auto increment value. */
ulonglong next_auto_inc_val;
/**
Hash of partition names. Initialized by the first handler instance of a
table_share calling populate_partition_name_hash().
After that it is read-only, i.e. no locking required for reading.
*/
HASH partition_name_hash;
/** flag that the name hash is initialized, so it only will do it once. */
bool partition_name_hash_initialized;
/**
Initializes and sets auto_inc_mutex.
Only needed to be called if the table have an auto increment.
Must hold TABLE_SHARE::LOCK_ha_data when calling.
*/
bool init_auto_inc_mutex(TABLE_SHARE *table_share);
/**
Release reserved auto increment values not used.
@param thd Thread.
@param table_share Table Share
@param next_insert_id Next insert id (first non used auto inc value).
@param max_reserved End of reserved auto inc range.
*/
void release_auto_inc_if_possible(THD *thd, TABLE_SHARE *table_share,
const ulonglong next_insert_id,
const ulonglong max_reserved);
/** lock mutex protecting auto increment value next_auto_inc_val. */
inline void lock_auto_inc()
{
assert(auto_inc_mutex);
mysql_mutex_lock(auto_inc_mutex);
}
/** unlock mutex protecting auto increment value next_auto_inc_val. */
inline void unlock_auto_inc()
{
assert(auto_inc_mutex);
mysql_mutex_unlock(auto_inc_mutex);
}
/**
Populate partition_name_hash with partition and subpartition names
from part_info.
@param part_info Partition info containing all partitions metadata.
@return Operation status.
@retval false Success.
@retval true Failure.
*/
bool populate_partition_name_hash(partition_info *part_info);
/** Get partition name.
@param part_id Partition id (for subpartitioned table only subpartition
names will be returned.)
@return partition name or NULL if error.
*/
const char *get_partition_name(size_t part_id) const;
private:
const uchar **partition_names;
/**
Insert [sub]partition name into partition_name_hash
@param name Partition name.
@param part_id Partition id.
@param is_subpart True if subpartition else partition.
@return Operation status.
@retval false Success.
@retval true Failure.
*/
bool insert_partition_name_in_hash(const char *name,
uint part_id,
bool is_subpart);
};
/**
Class for partitioning specific operations.
Returned from handler::get_partition_handler().
*/
class Partition_handler :public Sql_alloc
{
public:
Partition_handler() {}
~Partition_handler() {}
/**
Get dynamic table information from partition.
@param[out] stat_info Statistics struct to fill in.
@param[out] check_sum Check sum value to fill in if supported.
@param[in] part_id Partition to report for.
@note stat_info and check_sum are initialized by caller.
check_sum is only expected to be updated if HA_HAS_CHECKSUM.
*/
virtual void get_dynamic_partition_info(ha_statistics *stat_info,
ha_checksum *check_sum,
uint part_id) = 0;
/**
Get default number of partitions.
Used during creating a partitioned table.
@param info Create info.
@return Number of default partitions.
*/
virtual int get_default_num_partitions(HA_CREATE_INFO *info) { return 1;}
/**
Setup auto partitioning.
Called for engines with HA_USE_AUTO_PARTITION to setup the partition info
object
@param[in,out] part_info Partition object to setup.
*/
virtual void set_auto_partitions(partition_info *part_info) { return; }
/**
Get number of partitions for table in SE
@param name normalized path(same as open) to the table
@param[out] num_parts Number of partitions
@retval false for success
@retval true for failure, for example table didn't exist in engine
*/
virtual bool get_num_parts(const char *name,
uint *num_parts)
{
*num_parts= 0;
return false;
}
/**
Set the partition info object to be used by the handler.
@param part_info Partition info to be used by the handler.
@param early True if called when part_info only created and parsed,
but not setup, checked or fixed.
*/
virtual void set_part_info(partition_info *part_info, bool early) = 0;
/**
Initialize partition.
@param mem_root Memory root for memory allocations.
@return Operation status
@retval false Success.
@retval true Failure.
*/
virtual bool initialize_partition(MEM_ROOT *mem_root) {return false;}
/**
Truncate partitions.
Truncate all partitions matching table->part_info->read_partitions.
Handler level wrapper for truncating partitions, will ensure that
mark_trx_read_write() is called and also checks locking assertions.
@return Operation status.
@retval 0 Success.
@retval != 0 Error code.
*/
int truncate_partition()
{
handler *file= get_handler();
if (!file)
{
return HA_ERR_WRONG_COMMAND;
}
assert(file->table_share->tmp_table != NO_TMP_TABLE ||
file->m_lock_type == F_WRLCK);
file->mark_trx_read_write();
return truncate_partition_low();
}
/**
Change partitions.
Change partitions according to their partition_element::part_state set up
in prep_alter_part_table(). Will create new partitions and copy requested
partitions there. Also updating part_state to reflect current state.
Handler level wrapper for changing partitions.
This is the reason for having Partition_handler a friend class of handler,
mark_trx_read_write() is called and also checks locking assertions.
to ensure that mark_trx_read_write() is called and checking the asserts.
@param[in] create_info Table create info.
@param[in] path Path including table name.
@param[out] copied Number of rows copied.
@param[out] deleted Number of rows deleted.
*/
int change_partitions(HA_CREATE_INFO *create_info,
const char *path,
ulonglong * const copied,
ulonglong * const deleted)
{
handler *file= get_handler();
if (!file)
{
my_error(ER_ILLEGAL_HA, MYF(0), create_info->alias);
return HA_ERR_WRONG_COMMAND;
}
assert(file->table_share->tmp_table != NO_TMP_TABLE ||
file->m_lock_type != F_UNLCK);
file->mark_trx_read_write();
return change_partitions_low(create_info, path, copied, deleted);
}
/**
Alter flags.
Given a set of alter table flags, return which is supported.
@param flags Alter table operation flags.
@return Supported alter table flags.
*/
virtual uint alter_flags(uint flags) const
{ return 0; }
/**
Get partition row type from SE
@param part_id Id of partition for which row type to be retrieved
@return Partition row type.
*/
virtual enum row_type get_partition_row_type(uint part_id) {
return ROW_TYPE_NOT_USED;
}
private:
/**
Truncate partition.
Low-level primitive for handler, implementing
Partition_handler::truncate_partition().
@return Operation status
@retval 0 Success.
@retval != 0 Error code.
*/
virtual int truncate_partition_low()
{ return HA_ERR_WRONG_COMMAND; }
/**
Truncate partition.
Low-level primitive for handler, implementing
Partition_handler::change_partitions().
@param[in] create_info Table create info.
@param[in] path Path including table name.
@param[out] copied Number of rows copied.
@param[out] deleted Number of rows deleted.
@return Operation status
@retval 0 Success.
@retval != 0 Error code.
*/
virtual int change_partitions_low(HA_CREATE_INFO *create_info,
const char *path,
ulonglong * const copied,
ulonglong * const deleted)
{
my_error(ER_ILLEGAL_HA, MYF(0), create_info->alias);
return HA_ERR_WRONG_COMMAND;
}
/**
Return the table handler.
For some partitioning specific functions it is still needed to access
the handler directly for transaction handling (mark_trx_read_write())
and to assert correct locking.
@return handler or NULL if not supported.
*/
virtual handler *get_handler()
{ return NULL; }
};
/// Maps compare function to strict weak ordering required by Priority_queue.
struct Key_rec_less
{
typedef int (*key_compare_fun)(KEY**, uchar *, uchar *);
explicit Key_rec_less(KEY **keys)
: m_keys(keys), m_fun(key_rec_cmp), m_max_at_top(false)
{
}
bool operator()(uchar *first, uchar *second)
{
const int cmpval=
(*m_fun)(m_keys, first + m_rec_offset, second + m_rec_offset);
return m_max_at_top ? cmpval < 0 : cmpval > 0;
}
KEY **m_keys;
key_compare_fun m_fun;
uint m_rec_offset;
bool m_max_at_top;
};
/**
Partition_helper is a helper class that implements most generic partitioning
functionality such as:
table scan, index scan (both ordered and non-ordered),
insert (write_row()), delete and update.
And includes ALTER TABLE ... ADD/COALESCE/DROP/REORGANIZE/... PARTITION
support.
It also implements a cache for the auto increment value and check/repair for
rows in wrong partition.
How to use it:
Inherit it and implement:
- *_in_part() functions for row operations.
- prepare_for_new_partitions(), create_new_partition(), close_new_partitions()
write_row_in_new_part() for handling 'fast' alter partition.
*/
class Partition_helper : public Sql_alloc
{
typedef Priority_queue<uchar *, std::vector<uchar*>, Key_rec_less> Prio_queue;
public:
Partition_helper(handler *main_handler);
~Partition_helper();
/**
Set partition info.
To be called from Partition_handler.
@param part_info Partition info to use.
@param early True if called when part_info only created and parsed,
but not setup, checked or fixed.
*/
virtual void set_part_info_low(partition_info *part_info, bool early);
/**
Initialize variables used before the table is opened.
@param mem_root Memory root to allocate things from (not yet used).
@return Operation status.
@retval false success.
@retval true failure.
*/
inline bool init_partitioning(MEM_ROOT *mem_root)
{
#ifndef NDEBUG
m_key_not_found_partitions.bitmap= NULL;
#endif
return false;
}
/**
INSERT/UPDATE/DELETE functions.
@see handler.h
@{
*/
/**
Insert a row to the partitioned table.
@param buf The row in MySQL Row Format.
@return Operation status.
@retval 0 Success
@retval != 0 Error code
*/
int ph_write_row(uchar *buf);
/**
Update an existing row in the partitioned table.
Yes, update_row() does what you expect, it updates a row. old_data will
have the previous row record in it, while new_data will have the newest
data in it.
Keep in mind that the server can do updates based on ordering if an
ORDER BY clause was used. Consecutive ordering is not guaranteed.
If the new record belongs to a different partition than the old record
then it will be inserted into the new partition and deleted from the old.
new_data is always record[0]
old_data is always record[1]
@param old_data The old record in MySQL Row Format.
@param new_data The new record in MySQL Row Format.
@return Operation status.
@retval 0 Success
@retval != 0 Error code
*/
int ph_update_row(const uchar *old_data, uchar *new_data);
/**
Delete an existing row in the partitioned table.
This will delete a row. buf will contain a copy of the row to be deleted.
The server will call this right after the current row has been read
(from either a previous rnd_xxx() or index_xxx() call).
If you keep a pointer to the last row or can access a primary key it will
make doing the deletion quite a bit easier.
Keep in mind that the server does no guarantee consecutive deletions.
ORDER BY clauses can be used.
buf is either record[0] or record[1]
@param buf The record in MySQL Row Format.
@return Operation status.
@retval 0 Success
@retval != 0 Error code
*/
int ph_delete_row(const uchar *buf);
/** @} */
/** Release unused auto increment values. */
void ph_release_auto_increment();
/**
Calculate key hash value from an null terminated array of fields.
Support function for KEY partitioning.
@param field_array An array of the fields in KEY partitioning
@return hash_value calculated
@note Uses the hash function on the character set of the field.
Integer and floating point fields use the binary character set by default.
*/
static uint32 ph_calculate_key_hash_value(Field **field_array);
/** Get checksum for table.
@return Checksum or 0 if not supported (which also may be a correct checksum!).
*/
ha_checksum ph_checksum() const;
/**
MODULE full table scan
This module is used for the most basic access method for any table
handler. This is to fetch all data through a full table scan. No
indexes are needed to implement this part.
It contains one method to start the scan (rnd_init) that can also be
called multiple times (typical in a nested loop join). Then proceeding
to the next record (rnd_next) and closing the scan (rnd_end).
To remember a record for later access there is a method (position)
and there is a method used to retrieve the record based on the stored
position.
The position can be a file position, a primary key, a ROWID dependent
on the handler below.
unlike index_init(), rnd_init() can be called two times
without rnd_end() in between (it only makes sense if scan=1).
then the second call should prepare for the new table scan
(e.g if rnd_init allocates the cursor, second call should
position it to the start of the table, no need to deallocate
and allocate it again.
@see handler.h
@{
*/
int ph_rnd_init(bool scan);
int ph_rnd_end();
int ph_rnd_next(uchar *buf);
void ph_position(const uchar *record);
int ph_rnd_pos(uchar *buf, uchar *pos);
/** @} */
/**
MODULE index scan
This part of the handler interface is used to perform access through
indexes. The interface is defined as a scan interface but the handler
can also use key lookup if the index is a unique index or a primary
key index.
Index scans are mostly useful for SELECT queries but are an important
part also of UPDATE, DELETE, REPLACE and CREATE TABLE table AS SELECT
and so forth.
Naturally an index is needed for an index scan and indexes can either
be ordered, hash based. Some ordered indexes can return data in order
but not necessarily all of them.
There are many flags that define the behavior of indexes in the
various handlers. These methods are found in the optimizer module.
-------------------------------------------------------------------------
index_read is called to start a scan of an index. The find_flag defines
the semantics of the scan. These flags are defined in
include/my_base.h
index_read_idx is the same but also initializes index before calling doing
the same thing as index_read. Thus it is similar to index_init followed
by index_read. This is also how we implement it.
index_read/index_read_idx does also return the first row. Thus for
key lookups, the index_read will be the only call to the handler in
the index scan.
index_init initializes an index before using it and index_end does
any end processing needed.
@{
*/
int ph_index_init_setup(uint key_nr, bool sorted);
int ph_index_init(uint key_nr, bool sorted);
int ph_index_end();
/*
These methods are used to jump to next or previous entry in the index
scan. There are also methods to jump to first and last entry.
*/
int ph_index_first(uchar *buf);
int ph_index_last(uchar *buf);
int ph_index_next(uchar *buf);
int ph_index_next_same(uchar *buf, const uchar *key, uint keylen);
int ph_index_prev(uchar *buf);
int ph_index_read_map(uchar *buf,
const uchar *key,
key_part_map keypart_map,
enum ha_rkey_function find_flag);
int ph_index_read_last_map(uchar *buf,
const uchar *key,
key_part_map keypart_map);
int ph_index_read_idx_map(uchar *buf,
uint index,
const uchar *key,
key_part_map keypart_map,
enum ha_rkey_function find_flag);
int ph_read_range_first(const key_range *start_key,
const key_range *end_key,
bool eq_range_arg,
bool sorted);
int ph_read_range_next();
/** @} */
/**
Functions matching Partition_handler API.
@{
*/
/**
Get statistics from a specific partition.
@param[out] stat_info Area to report values into.
@param[out] check_sum Check sum of partition.
@param[in] part_id Partition to report from.
*/
virtual void get_dynamic_partition_info_low(ha_statistics *stat_info,
ha_checksum *check_sum,
uint part_id);
/**
Implement the partition changes defined by ALTER TABLE of partitions.
Add and copy if needed a number of partitions, during this operation
only read operation is ongoing in the server. This is used by
ADD PARTITION all types as well as by REORGANIZE PARTITION. For
one-phased implementations it is used also by DROP and COALESCE
PARTITIONs.
One-phased implementation needs the new frm file, other handlers will
get zero length and a NULL reference here.
@param[in] create_info HA_CREATE_INFO object describing all
fields and indexes in table
@param[in] path Complete path of db and table name
@param[out] copied Output parameter where number of copied
records are added
@param[out] deleted Output parameter where number of deleted
records are added
@return Operation status
@retval 0 Success
@retval != 0 Failure
*/
virtual int change_partitions(HA_CREATE_INFO *create_info,
const char *path,
ulonglong * const copied,
ulonglong * const deleted);
/** @} */
protected:
/* Common helper functions to be used by inheriting engines. */
/*
open/close functions.
*/
/**
Set m_part_share, Allocate internal bitmaps etc. used by open tables.
@param mem_root Memory root to allocate things from (not yet used).
@return Operation status.
@retval false success.
@retval true failure.
*/
bool open_partitioning(Partition_share *part_share);
/**
Close partitioning for a table.
Frees memory and release other resources.
*/
void close_partitioning();
/**
Lock auto increment value if needed.
*/
inline void lock_auto_increment()
{
/* lock already taken */
if (m_auto_increment_safe_stmt_log_lock)
return;
assert(!m_auto_increment_lock);
if(m_table->s->tmp_table == NO_TMP_TABLE)
{
m_auto_increment_lock= true;
m_part_share->lock_auto_inc();
}
}
/**
unlock auto increment.
*/
inline void unlock_auto_increment()
{
/*
If m_auto_increment_safe_stmt_log_lock is true, we have to keep the lock.
It will be set to false and thus unlocked at the end of the statement by
ha_partition::release_auto_increment.
*/
if(m_auto_increment_lock && !m_auto_increment_safe_stmt_log_lock)
{
m_part_share->unlock_auto_inc();
m_auto_increment_lock= false;
}
}
/**
Get auto increment.
Only to be used for auto increment values that are the first field in
an unique index.
@param[in] increment Increment between generated numbers.
@param[in] nb_desired_values Number of values requested.
@param[out] first_value First reserved value (ULLONG_MAX on error).
@param[out] nb_reserved_values Number of values reserved.
*/
void get_auto_increment_first_field(ulonglong increment,
ulonglong nb_desired_values,
ulonglong *first_value,
ulonglong *nb_reserved_values);
/**
Initialize the record priority queue used for sorted index scans.
@return Operation status.
@retval 0 Success.
@retval != 0 Error code.
*/
int init_record_priority_queue();
/**
Destroy the record priority queue used for sorted index scans.
*/
void destroy_record_priority_queue();
/*
Administrative support functions.
*/
/** Print partitioning specific error.
@param error Error code.
@param errflag Error flag.
@return false if error is printed else true.
*/
bool print_partition_error(int error, myf errflag);
/**
Print a message row formatted for ANALYZE/CHECK/OPTIMIZE/REPAIR TABLE.
Modeled after mi_check_print_msg.
@param thd Thread context.
@param len Needed length for message buffer.
@param msg_type Message type.
@param db_name Database name.
@param table_name Table name.
@param op_name Operation name.
@param fmt Message (in printf format with additional arguments).
@return Operation status.
@retval false for success else true.
*/
bool print_admin_msg(THD *thd,
uint len,
const char *msg_type,
const char *db_name,
const char *table_name,
const char *op_name,
const char *fmt,
...);
/**
Check/fix misplaced rows.
@param part_id Partition to check/fix.
@param repair If true, move misplaced rows to correct partition.
@return Operation status.
@retval 0 Success
@retval != 0 Error
*/
int check_misplaced_rows(uint part_id, bool repair);
/**
Set used partitions bitmap from Alter_info.
@return false if success else true.
*/
bool set_altered_partitions();
private:
enum partition_index_scan_type
{
PARTITION_INDEX_READ= 1,
PARTITION_INDEX_FIRST,
PARTITION_INDEX_FIRST_UNORDERED,
PARTITION_INDEX_LAST,
PARTITION_INDEX_READ_LAST,
PARTITION_READ_RANGE,
PARTITION_NO_INDEX_SCAN
};
/** handler to use (ha_partition, ha_innopart etc.) */
handler *m_handler;
/*
Access methods to protected areas in handler to avoid adding
friend class Partition_helper in class handler.
*/
virtual THD *get_thd() const = 0;
virtual TABLE *get_table() const = 0;
virtual bool get_eq_range() const = 0;
virtual void set_eq_range(bool eq_range) = 0;
virtual void set_range_key_part(KEY_PART_INFO *key_part) = 0;
/*
Implementation of per partition operation by instantiated engine.
These must be implemented in the 'real' partition_helper subclass.
*/
/**
Write a row in the specified partition.
@see handler::write_row().
@param part_id Partition to write to.
@param buf Buffer with data to write.
@return Operation status.
@retval 0 Success.
@retval != 0 Error code.
*/
virtual int write_row_in_part(uint part_id, uchar *buf) = 0;
/**
Update a row in the specified partition.
@see handler::update_row().
@param part_id Partition to update in.
@param old_data Buffer containing old row.
@param new_data Buffer containing new row.
@return Operation status.
@retval 0 Success.
@retval != 0 Error code.
*/
virtual int update_row_in_part(uint new_part_id,
const uchar *old_data,
uchar *new_data) = 0;
/**
Delete an existing row in the specified partition.
@see handler::delete_row().
@param part_id Partition to delete from.
@param buf Buffer containing row to delete.
@return Operation status.
@retval 0 Success.
@retval != 0 Error code.
*/
virtual int delete_row_in_part(uint part_id, const uchar *buf) = 0;
/**
Initialize the shared auto increment value.
@param no_lock If HA_STATUS_NO_LOCK should be used in info(HA_STATUS_AUTO).
Also sets stats.auto_increment_value.
*/
virtual int initialize_auto_increment(bool no_lock) = 0;
/** Release auto_increment in all underlying partitions. */
virtual void release_auto_increment_all_parts() {}
/** Save or persist the current max auto increment. */
virtual void save_auto_increment(ulonglong nr) {}
/**
Per partition equivalent of rnd_* and index_* functions.
@see class handler.
*/
virtual int rnd_init_in_part(uint part_id, bool table_scan) = 0;
int ph_rnd_next_in_part(uint part_id, uchar *buf);
virtual int rnd_next_in_part(uint part_id, uchar *buf) = 0;
virtual int rnd_end_in_part(uint part_id, bool scan) = 0;
virtual void position_in_last_part(uchar *ref, const uchar *row) = 0;
/* If ph_rnd_pos is used then this needs to be implemented! */
virtual int rnd_pos_in_part(uint part_id, uchar *buf, uchar *pos)
{ assert(0); return HA_ERR_WRONG_COMMAND; }
virtual int index_init_in_part(uint part, uint keynr, bool sorted)
{ assert(0); return HA_ERR_WRONG_COMMAND; }
virtual int index_end_in_part(uint part)
{ assert(0); return HA_ERR_WRONG_COMMAND; }
virtual int index_first_in_part(uint part, uchar *buf) = 0;
virtual int index_last_in_part(uint part, uchar *buf) = 0;
virtual int index_prev_in_part(uint part, uchar *buf) = 0;
virtual int index_next_in_part(uint part, uchar *buf) = 0;
virtual int index_next_same_in_part(uint part,
uchar *buf,
const uchar *key,
uint length) = 0;
virtual int index_read_map_in_part(uint part,
uchar *buf,
const uchar *key,
key_part_map keypart_map,
enum ha_rkey_function find_flag) = 0;
virtual int index_read_last_map_in_part(uint part,
uchar *buf,
const uchar *key,
key_part_map keypart_map) = 0;
/**
Do read_range_first in the specified partition.
If buf is set, then copy the result there instead of table->record[0].
*/
virtual int read_range_first_in_part(uint part,
uchar *buf,
const key_range *start_key,
const key_range *end_key,
bool eq_range,
bool sorted) = 0;
/**
Do read_range_next in the specified partition.
If buf is set, then copy the result there instead of table->record[0].
*/
virtual int read_range_next_in_part(uint part, uchar *buf) = 0;
virtual int index_read_idx_map_in_part(uint part,
uchar *buf,
uint index,
const uchar *key,
key_part_map keypart_map,
enum ha_rkey_function find_flag) = 0;
/**
Initialize engine specific resources for the record priority queue
used duing ordered index reads for multiple partitions.
@param used_parts Number of partitions used in query
(number of set bits in m_part_info->read_partitions).
@return Operation status.
@retval 0 Success.
@retval != 0 Error code.
*/
virtual int init_record_priority_queue_for_parts(uint used_parts)
{
return 0;
}
/**
Destroy and release engine specific resources used by the record
priority queue.
*/
virtual void destroy_record_priority_queue_for_parts() {}
/**
Checksum for a partition.
@param part_id Partition to checksum.
*/
virtual ha_checksum checksum_in_part(uint part_id) const
{ assert(0); return 0; }
/**
Copy a cached row.
Used when copying a row from the record priority queue to the return buffer.
For some engines, like InnoDB, only marked columns must be copied,
to preserve non-read columns.
@param[out] to_rec Buffer to copy to.
@param[in] from_rec Buffer to copy from.
*/
virtual void copy_cached_row(uchar *to_rec, const uchar *from_rec)
{ memcpy(to_rec, from_rec, m_rec_length); }
/**
Prepare for creating new partitions during ALTER TABLE ... PARTITION.