1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* This file should be included when using myisam_funktions */
33
#include "my_handler.h"
36
There is a hard limit for the maximum number of keys as there are only
37
8 bits in the index file header for the number of keys in a table.
38
This means that 0..255 keys can exist for a table. The idea of
39
MI_MAX_POSSIBLE_KEY is to ensure that one can use myisamchk & tools on
40
a MyISAM table for which one has more keys than MyISAM is normally
41
compiled for. If you don't have this, you will get a core dump when
42
running myisamchk compiled for 128 keys on a table with 255 keys.
44
#define MI_MAX_POSSIBLE_KEY 255 /* For myisam_chk */
45
#if MAX_INDEXES > MI_MAX_POSSIBLE_KEY
46
#define MI_MAX_KEY MI_MAX_POSSIBLE_KEY /* Max allowed keys */
48
#define MI_MAX_KEY MAX_INDEXES /* Max allowed keys */
51
#define MI_MAX_POSSIBLE_KEY_BUFF (1024+6+6) /* For myisam_chk */
53
The following defines can be increased if necessary.
54
But beware the dependency of MI_MAX_POSSIBLE_KEY_BUFF and MI_MAX_KEY_LENGTH.
56
#define MI_MAX_KEY_LENGTH 1000 /* Max length in bytes */
57
#define MI_MAX_KEY_SEG 16 /* Max segments for key */
59
#define MI_MAX_KEY_BUFF (MI_MAX_KEY_LENGTH+MI_MAX_KEY_SEG*6+8+8)
60
#define MI_MAX_MSG_BUF 1024 /* used in CHECK TABLE, REPAIR TABLE */
61
#define MI_NAME_IEXT ".MYI"
62
#define MI_NAME_DEXT ".MYD"
63
/* Max extra space to use when sorting keys */
64
#define MI_MAX_TEMP_LENGTH 2*1024L*1024L*1024L
66
/* Possible values for myisam_block_size (must be power of 2) */
67
#define MI_KEY_BLOCK_LENGTH 1024 /* default key block length */
68
#define MI_MIN_KEY_BLOCK_LENGTH 1024 /* Min key block length */
69
#define MI_MAX_KEY_BLOCK_LENGTH 16384
71
#define mi_portable_sizeof_char_ptr 8
74
In the following macros '_keyno_' is 0 .. keys-1.
75
If there can be more keys than bits in the key_map, the highest bit
76
is for all upper keys. They cannot be switched individually.
77
This means that clearing of high keys is ignored, setting one high key
80
#define MI_KEYMAP_BITS (8 * SIZEOF_LONG_LONG)
81
#define MI_KEYMAP_HIGH_MASK (ULL(1) << (MI_KEYMAP_BITS - 1))
82
#define mi_get_mask_all_keys_active(_keys_) \
83
(((_keys_) < MI_KEYMAP_BITS) ? \
84
((ULL(1) << (_keys_)) - ULL(1)) : \
87
#if MI_MAX_KEY > MI_KEYMAP_BITS
89
#define mi_is_key_active(_keymap_,_keyno_) \
90
(((_keyno_) < MI_KEYMAP_BITS) ? \
91
test((_keymap_) & (ULL(1) << (_keyno_))) : \
92
test((_keymap_) & MI_KEYMAP_HIGH_MASK))
93
#define mi_set_key_active(_keymap_,_keyno_) \
94
(_keymap_)|= (((_keyno_) < MI_KEYMAP_BITS) ? \
95
(ULL(1) << (_keyno_)) : \
97
#define mi_clear_key_active(_keymap_,_keyno_) \
98
(_keymap_)&= (((_keyno_) < MI_KEYMAP_BITS) ? \
99
(~ (ULL(1) << (_keyno_))) : \
100
(~ (ULL(0))) /*ignore*/ )
104
#define mi_is_key_active(_keymap_,_keyno_) \
105
test((_keymap_) & (ULL(1) << (_keyno_)))
106
#define mi_set_key_active(_keymap_,_keyno_) \
107
(_keymap_)|= (ULL(1) << (_keyno_))
108
#define mi_clear_key_active(_keymap_,_keyno_) \
109
(_keymap_)&= (~ (ULL(1) << (_keyno_)))
113
#define mi_is_any_key_active(_keymap_) \
115
#define mi_is_all_keys_active(_keymap_,_keys_) \
116
((_keymap_) == mi_get_mask_all_keys_active(_keys_))
117
#define mi_set_all_keys_active(_keymap_,_keys_) \
118
(_keymap_)= mi_get_mask_all_keys_active(_keys_)
119
#define mi_clear_all_keys_active(_keymap_) \
121
#define mi_intersect_keys_active(_to_,_from_) \
123
#define mi_is_any_intersect_keys_active(_keymap1_,_keys_,_keymap2_) \
124
((_keymap1_) & (_keymap2_) & \
125
mi_get_mask_all_keys_active(_keys_))
126
#define mi_copy_keys_active(_to_,_maxkeys_,_from_) \
127
(_to_)= (mi_get_mask_all_keys_active(_maxkeys_) & \
130
/* Param to/from mi_info */
132
typedef struct st_mi_isaminfo /* Struct from h_info */
134
ha_rows records; /* Records in database */
135
ha_rows deleted; /* Deleted records in database */
136
my_off_t recpos; /* Pos for last used record */
137
my_off_t newrecpos; /* Pos if we write new record */
138
my_off_t dupp_key_pos; /* Position to record with dupp key */
139
my_off_t data_file_length, /* Length of data file */
140
max_data_file_length,
142
max_index_file_length,
144
ulong reclength; /* Recordlength */
145
ulong mean_reclength; /* Mean recordlength (if packed) */
146
ulonglong auto_increment;
147
ulonglong key_map; /* Which keys are used */
148
char *data_file_name, *index_file_name;
149
uint keys; /* Number of keys in use */
150
uint options; /* HA_OPTION_... used */
151
int errkey, /* With key was dupplicated on err */
152
sortkey; /* clustered by this key */
153
File filenr; /* (uniq) filenr for datafile */
154
time_t create_time; /* When table was created */
159
ulong *rec_per_key; /* for sql optimizing */
160
uint raid_type,raid_chunks;
161
ulong raid_chunksize;
165
typedef struct st_mi_create_info
167
const char *index_file_name, *data_file_name; /* If using symlinks */
170
ulonglong auto_increment;
171
ulonglong data_file_length;
172
ulonglong key_file_length;
173
uint raid_type,raid_chunks;
174
ulong raid_chunksize;
177
my_bool with_auto_increment;
180
struct st_myisam_info; /* For referense */
181
struct st_mi_isam_share;
182
typedef struct st_myisam_info MI_INFO;
183
struct st_mi_s_param;
185
typedef struct st_mi_keydef /* Key definition with open & info */
187
struct st_mi_isam_share *share; /* Pointer to base (set in mi_open) */
188
uint16 keysegs; /* Number of key-segment */
189
uint16 flag; /* NOSAME, PACK_USED */
191
uint8 key_alg; /* BTREE, RTREE */
192
uint16 block_length; /* Length of keyblock (auto) */
193
uint16 underflow_block_length; /* When to execute underflow */
194
uint16 keylength; /* Tot length of keyparts (auto) */
195
uint16 minlength; /* min length of (packed) key (auto) */
196
uint16 maxlength; /* max length of (packed) key (auto) */
197
uint16 block_size; /* block_size (auto) */
198
uint32 version; /* For concurrent read/write */
201
int (*bin_search)(struct st_myisam_info *info,struct st_mi_keydef *keyinfo,
202
uchar *page,uchar *key,
203
uint key_len,uint comp_flag,uchar * *ret_pos,
204
uchar *buff, my_bool *was_last_key);
205
uint (*get_key)(struct st_mi_keydef *keyinfo,uint nod_flag,uchar * *page,
207
int (*pack_key)(struct st_mi_keydef *keyinfo,uint nod_flag,uchar *next_key,
208
uchar *org_key, uchar *prev_key, uchar *key,
209
struct st_mi_s_param *s_temp);
210
void (*store_key)(struct st_mi_keydef *keyinfo, uchar *key_pos,
211
struct st_mi_s_param *s_temp);
212
int (*ck_insert)(struct st_myisam_info *inf, uint k_nr, uchar *k, uint klen);
213
int (*ck_delete)(struct st_myisam_info *inf, uint k_nr, uchar *k, uint klen);
217
#define MI_UNIQUE_HASH_LENGTH 4
219
typedef struct st_unique_def /* Segment definition of unique */
221
uint16 keysegs; /* Number of key-segment */
222
uchar key; /* Mapped to which key */
223
uint8 null_are_equal;
227
typedef struct st_mi_decode_tree /* Decode huff-table */
230
uint quick_table_bits;
235
struct st_mi_bit_buff;
238
Note that null markers should always be first in a row !
239
When creating a column, one should only specify:
240
type, length, null_bit and null_pos
243
typedef struct st_columndef /* column information */
245
int16 type; /* en_fieldtype */
246
uint16 length; /* length of field */
247
uint32 offset; /* Offset to position in row */
248
uint8 null_bit; /* If column may be 0 */
249
uint16 null_pos; /* position for null marker */
251
#ifndef NOT_PACKED_DATABASES
252
void (*unpack)(struct st_columndef *rec,struct st_mi_bit_buff *buff,
253
uchar *start,uchar *end);
254
enum en_fieldtype base_type;
255
uint space_length_bits,pack_type;
256
MI_DECODE_TREE *huff_tree;
260
/* invalidator function reference for Query Cache */
261
typedef void (* invalidator_by_filename)(const char * filename);
263
extern my_string myisam_log_filename; /* Name of logfile */
264
extern ulong myisam_block_size;
265
extern ulong myisam_concurrent_insert;
266
extern my_bool myisam_flush,myisam_delay_key_write,myisam_single_user;
267
extern my_off_t myisam_max_temp_length;
268
extern ulong myisam_bulk_insert_tree_size, myisam_data_pointer_size;
270
/* Prototypes for myisam-functions */
272
extern int mi_close(struct st_myisam_info *file);
273
extern int mi_delete(struct st_myisam_info *file,const byte *buff);
274
extern struct st_myisam_info *mi_open(const char *name,int mode,
275
uint wait_if_locked);
276
extern int mi_panic(enum ha_panic_function function);
277
extern int mi_rfirst(struct st_myisam_info *file,byte *buf,int inx);
278
extern int mi_rkey(struct st_myisam_info *file,byte *buf,int inx,
280
uint key_len, enum ha_rkey_function search_flag);
281
extern int mi_rlast(struct st_myisam_info *file,byte *buf,int inx);
282
extern int mi_rnext(struct st_myisam_info *file,byte *buf,int inx);
283
extern int mi_rnext_same(struct st_myisam_info *info, byte *buf);
284
extern int mi_rprev(struct st_myisam_info *file,byte *buf,int inx);
285
extern int mi_rrnd(struct st_myisam_info *file,byte *buf, my_off_t pos);
286
extern int mi_scan_init(struct st_myisam_info *file);
287
extern int mi_scan(struct st_myisam_info *file,byte *buf);
288
extern int mi_rsame(struct st_myisam_info *file,byte *record,int inx);
289
extern int mi_rsame_with_pos(struct st_myisam_info *file,byte *record,
290
int inx, my_off_t pos);
291
extern int mi_update(struct st_myisam_info *file,const byte *old,
293
extern int mi_write(struct st_myisam_info *file,byte *buff);
294
extern my_off_t mi_position(struct st_myisam_info *file);
295
extern int mi_status(struct st_myisam_info *info, MI_ISAMINFO *x, uint flag);
296
extern int mi_lock_database(struct st_myisam_info *file,int lock_type);
297
extern int mi_create(const char *name,uint keys,MI_KEYDEF *keydef,
298
uint columns, MI_COLUMNDEF *columndef,
299
uint uniques, MI_UNIQUEDEF *uniquedef,
300
MI_CREATE_INFO *create_info, uint flags);
301
extern int mi_delete_table(const char *name);
302
extern int mi_rename(const char *from, const char *to);
303
extern int mi_extra(struct st_myisam_info *file,
304
enum ha_extra_function function,
306
extern ha_rows mi_records_in_range(struct st_myisam_info *info,int inx,
307
key_range *min_key, key_range *max_key);
308
extern int mi_log(int activate_log);
309
extern int mi_is_changed(struct st_myisam_info *info);
310
extern int mi_delete_all_rows(struct st_myisam_info *info);
311
extern ulong _mi_calc_blob_length(uint length , const byte *pos);
312
extern uint mi_get_pointer_length(ulonglong file_length, uint def);
314
/* this is used to pass to mysql_myisamchk_table -- by Sasha Pachev */
316
#define MYISAMCHK_REPAIR 1 /* equivalent to myisamchk -r */
317
#define MYISAMCHK_VERIFY 2 /* Verify, run repair if failure */
320
Definitions needed for myisamchk.c
322
Entries marked as "QQ to be removed" are NOT used to
323
pass check/repair options to mi_check.c. They are used
324
internally by myisamchk.c or/and ha_myisam.cc and should NOT
325
be stored together with other flags. They should be removed
326
from the following list to make addition of new flags possible.
330
#define T_AUTO_REPAIR 2 /* QQ to be removed */
331
#define T_BACKUP_DATA 4
332
#define T_CALC_CHECKSUM 8
333
#define T_CHECK 16 /* QQ to be removed */
334
#define T_CHECK_ONLY_CHANGED 32 /* QQ to be removed */
335
#define T_CREATE_MISSING_KEYS 64
336
#define T_DESCRIPT 128
337
#define T_DONT_CHECK_CHECKSUM 256
339
#define T_FAST (1L << 10) /* QQ to be removed */
340
#define T_FORCE_CREATE (1L << 11) /* QQ to be removed */
341
#define T_FORCE_UNIQUENESS (1L << 12)
342
#define T_INFO (1L << 13)
343
#define T_MEDIUM (1L << 14)
344
#define T_QUICK (1L << 15) /* QQ to be removed */
345
#define T_READONLY (1L << 16) /* QQ to be removed */
346
#define T_REP (1L << 17)
347
#define T_REP_BY_SORT (1L << 18) /* QQ to be removed */
348
#define T_REP_PARALLEL (1L << 19) /* QQ to be removed */
349
#define T_RETRY_WITHOUT_QUICK (1L << 20)
350
#define T_SAFE_REPAIR (1L << 21)
351
#define T_SILENT (1L << 22)
352
#define T_SORT_INDEX (1L << 23) /* QQ to be removed */
353
#define T_SORT_RECORDS (1L << 24) /* QQ to be removed */
354
#define T_STATISTICS (1L << 25)
355
#define T_UNPACK (1L << 26)
356
#define T_UPDATE_STATE (1L << 27)
357
#define T_VERBOSE (1L << 28)
358
#define T_VERY_SILENT (1L << 29)
359
#define T_WAIT_FOREVER (1L << 30)
360
#define T_WRITE_LOOP ((ulong) 1L << 31)
362
#define T_REP_ANY (T_REP | T_REP_BY_SORT | T_REP_PARALLEL)
365
Flags used by myisamchk.c or/and ha_myisam.cc that are NOT passed
366
to mi_check.c follows:
370
#define TT_FOR_UPGRADE 2
372
#define O_NEW_INDEX 1 /* Bits set in out_flag */
374
#define O_DATA_LOST 4
376
/* these struct is used by my_check to tell it what to do */
378
typedef struct st_sort_key_blocks /* Used when sorting */
380
uchar *buff,*end_pos;
381
uchar lastkey[MI_MAX_POSSIBLE_KEY_BUFF];
388
MyISAM supports several statistics collection methods. Currently statistics
389
collection method is not stored in MyISAM file and has to be specified for
390
each table analyze/repair operation in MI_CHECK::stats_method.
395
/* Treat NULLs as inequal when collecting statistics (default for 4.1/5.0) */
396
MI_STATS_METHOD_NULLS_NOT_EQUAL,
397
/* Treat NULLs as equal when collecting statistics (like 4.0 did) */
398
MI_STATS_METHOD_NULLS_EQUAL,
399
/* Ignore NULLs - count only tuples without NULLs in the index components */
400
MI_STATS_METHOD_IGNORE_NULLS
401
} enum_mi_stats_method;
403
typedef struct st_mi_check_param
405
ulonglong auto_increment_value;
406
ulonglong max_data_file_length;
407
ulonglong keys_in_use;
408
ulonglong max_record_length;
409
my_off_t search_after_block;
410
my_off_t new_file_pos,key_file_blocks;
411
my_off_t keydata,totaldata,key_blocks,start_check_pos;
412
ha_rows total_records,total_deleted;
413
ha_checksum record_checksum,glob_crc;
414
ulong use_buffers,read_buffer_length,write_buffer_length,
415
sort_buffer_length,sort_key_blocks;
416
uint out_flag,warning_printed,error_printed,verbose;
417
uint opt_sort_key,total_files,max_level;
418
uint testflag, key_cache_block_size;
420
my_bool using_global_keycache, opt_lock_memory, opt_follow_links;
421
my_bool retry_repair, force_sort;
422
char temp_filename[FN_REFLEN],*isam_file_name;
424
int tmpfile_createflag;
429
The next two are used to collect statistics, see update_key_parts for
432
ulonglong unique_count[MI_MAX_KEY_SEG+1];
433
ulonglong notnull_count[MI_MAX_KEY_SEG+1];
435
ha_checksum key_crc[MI_MAX_POSSIBLE_KEY];
436
ulong rec_per_key_part[MI_MAX_KEY_SEG*MI_MAX_POSSIBLE_KEY];
438
const char *db_name, *table_name;
440
enum_mi_stats_method stats_method;
443
typedef struct st_sort_ft_buf
447
uchar lastkey[MI_MAX_KEY_BUFF];
450
typedef struct st_sort_info
452
my_off_t filelength,dupp,buff_length;
454
uint current_key, total_keys;
456
enum data_file_type new_data_file_type;
460
SORT_KEY_BLOCKS *key_block,*key_block_end;
463
uint got_error, threads_running;
465
pthread_mutex_t mutex;
470
/* functions in mi_check */
471
void myisamchk_init(MI_CHECK *param);
472
int chk_status(MI_CHECK *param, MI_INFO *info);
473
int chk_del(MI_CHECK *param, register MI_INFO *info, uint test_flag);
474
int chk_size(MI_CHECK *param, MI_INFO *info);
475
int chk_key(MI_CHECK *param, MI_INFO *info);
476
int chk_data_link(MI_CHECK *param, MI_INFO *info,int extend);
477
int mi_repair(MI_CHECK *param, register MI_INFO *info,
478
my_string name, int rep_quick);
479
int mi_sort_index(MI_CHECK *param, register MI_INFO *info, my_string name);
480
int mi_repair_by_sort(MI_CHECK *param, register MI_INFO *info,
481
const char * name, int rep_quick);
482
int mi_repair_parallel(MI_CHECK *param, register MI_INFO *info,
483
const char * name, int rep_quick);
484
int change_to_newfile(const char * filename, const char * old_ext,
485
const char * new_ext, uint raid_chunks,
487
int lock_file(MI_CHECK *param, File file, my_off_t start, int lock_type,
488
const char *filetype, const char *filename);
489
void lock_memory(MI_CHECK *param);
490
void update_auto_increment_key(MI_CHECK *param, MI_INFO *info,
492
int update_state_info(MI_CHECK *param, MI_INFO *info,uint update);
493
void update_key_parts(MI_KEYDEF *keyinfo, ulong *rec_per_key_part,
494
ulonglong *unique, ulonglong *notnull,
496
int filecopy(MI_CHECK *param, File to,File from,my_off_t start,
497
my_off_t length, const char *type);
498
int movepoint(MI_INFO *info,byte *record,my_off_t oldpos,
499
my_off_t newpos, uint prot_key);
500
int write_data_suffix(SORT_INFO *sort_info, my_bool fix_datafile);
501
int test_if_almost_full(MI_INFO *info);
502
int recreate_table(MI_CHECK *param, MI_INFO **org_info, char *filename);
503
void mi_disable_non_unique_index(MI_INFO *info, ha_rows rows);
504
my_bool mi_test_if_sort_rep(MI_INFO *info, ha_rows rows, ulonglong key_map,
507
int mi_init_bulk_insert(MI_INFO *info, ulong cache_size, ha_rows rows);
508
void mi_flush_bulk_insert(MI_INFO *info, uint inx);
509
void mi_end_bulk_insert(MI_INFO *info);
510
int mi_assign_to_key_cache(MI_INFO *info, ulonglong key_map,
511
KEY_CACHE *key_cache);
512
void mi_change_key_cache(KEY_CACHE *old_key_cache,
513
KEY_CACHE *new_key_cache);
514
int mi_preload(MI_INFO *info, ulonglong key_map, my_bool ignore_leaves);