1
/* Copyright (C) 2000-2006 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 is included by all internal myisam files */
18
#include "myisam.h" /* Structs & some defines */
19
#include "myisampack.h" /* packing of keys */
22
#include <my_pthread.h>
25
#include <my_no_pthread.h>
28
#if defined(my_write) && !defined(MAP_TO_USE_RAID)
29
#undef my_write /* undef map from my_nosys; We need test-if-disk full */
32
typedef struct st_mi_status_info
34
ha_rows records; /* Rows in table */
35
ha_rows del; /* Removed rows */
36
my_off_t empty; /* lost space in datafile */
37
my_off_t key_empty; /* lost space in indexfile */
38
my_off_t key_file_length;
39
my_off_t data_file_length;
41
my_bool uncacheable; /* Active concurrent insert */
44
typedef struct st_mi_state_info
46
struct { /* Fileheader */
47
uchar file_version[4];
49
uchar header_length[2];
50
uchar state_info_length[2];
51
uchar base_info_length[2];
53
uchar key_parts[2]; /* Key parts */
54
uchar unique_key_parts[2]; /* Key parts + unique parts */
55
uchar keys; /* number of keys in file */
56
uchar uniques; /* number of UNIQUE definitions */
57
uchar language; /* Language for indexes */
58
uchar max_block_size_index; /* max keyblock size */
60
uchar not_used; /* To align to 8 */
64
ha_rows split; /* number of split blocks */
65
my_off_t dellink; /* Link to next removed block */
66
ulonglong auto_increment;
67
ulong process; /* process that updated table last */
68
ulong unique; /* Unique number for this process */
69
ulong update_count; /* Updated for each write lock */
71
ulong *rec_per_key_part;
72
my_off_t *key_root; /* Start of key trees */
73
my_off_t *key_del; /* delete links for trees */
74
my_off_t rec_per_key_rows; /* Rows when calculating rec_per_key */
76
ulong sec_index_changed; /* Updated when new sec_index */
77
ulong sec_index_used; /* which extra index are in use */
78
ulonglong key_map; /* Which keys are in use */
79
ha_checksum checksum; /* Table checksum */
80
ulong version; /* timestamp of create */
81
time_t create_time; /* Time when created database */
82
time_t recover_time; /* Time for last recover */
83
time_t check_time; /* Time for last check */
84
uint sortkey; /* sorted by this key (not used) */
86
uint8 changed; /* Changed since myisamchk */
88
/* the following isn't saved on disk */
89
uint state_diff_length; /* Should be 0 */
90
uint state_length; /* Length of state header in file */
94
#define MI_STATE_INFO_SIZE (24+14*8+7*4+2*2+8)
95
#define MI_STATE_KEY_SIZE 8
96
#define MI_STATE_KEYBLOCK_SIZE 8
97
#define MI_STATE_KEYSEG_SIZE 4
98
#define MI_STATE_EXTRA_SIZE ((MI_MAX_KEY+MI_MAX_KEY_BLOCK_SIZE)*MI_STATE_KEY_SIZE + MI_MAX_KEY*MI_MAX_KEY_SEG*MI_STATE_KEYSEG_SIZE)
99
#define MI_KEYDEF_SIZE (2+ 5*2)
100
#define MI_UNIQUEDEF_SIZE (2+1+1)
101
#define HA_KEYSEG_SIZE (6+ 2*2 + 4*2)
102
#define MI_COLUMNDEF_SIZE (2*3+1)
103
#define MI_BASE_INFO_SIZE (5*8 + 8*4 + 4 + 4*2 + 16)
104
#define MI_INDEX_BLOCK_MARGIN 16 /* Safety margin for .MYI tables */
106
typedef struct st_mi_base_info
108
my_off_t keystart; /* Start of keys */
109
my_off_t max_data_file_length;
110
my_off_t max_key_file_length;
111
my_off_t margin_key_file_length;
112
ha_rows records,reloc; /* Create information */
113
ulong mean_row_length; /* Create information */
114
ulong reclength; /* length of unpacked record */
115
ulong pack_reclength; /* Length of full packed rec. */
116
ulong min_pack_length;
117
ulong max_pack_length; /* Max possibly length of packed rec.*/
118
ulong min_block_length;
119
ulong fields, /* fields in table */
120
pack_fields; /* packed fields in table */
121
uint rec_reflength; /* = 2-8 */
122
uint key_reflength; /* = 2-8 */
123
uint keys; /* same as in state.header */
124
uint auto_key; /* Which key-1 is a auto key */
125
uint blobs; /* Number of blobs */
126
uint pack_bits; /* Length of packed bits */
127
uint max_key_block_length; /* Max block length */
128
uint max_key_length; /* Max key length */
129
/* Extra allocation when using dynamic record format */
130
uint extra_alloc_bytes;
131
uint extra_alloc_procent;
132
/* Info about raid */
133
uint raid_type,raid_chunks;
134
ulong raid_chunksize;
135
/* The following are from the header */
136
uint key_parts,all_key_parts;
140
/* Structs used intern in database */
142
typedef struct st_mi_blob /* Info of record */
144
ulong offset; /* Offset to blob in record */
145
uint pack_length; /* Type of packed length */
146
ulong length; /* Calc:ed for each record */
150
typedef struct st_mi_isam_pack {
156
#define MAX_NONMAPPED_INSERTS 1000
158
typedef struct st_mi_isam_share { /* Shared between opens */
161
MI_KEYDEF ft2_keyinfo; /* Second-level ft-key definition */
162
MI_KEYDEF *keyinfo; /* Key definitions */
163
MI_UNIQUEDEF *uniqueinfo; /* unique definitions */
164
HA_KEYSEG *keyparts; /* key part info */
165
MI_COLUMNDEF *rec; /* Pointer to field information */
166
MI_PACK pack; /* Data about packed records */
167
MI_BLOB *blobs; /* Pointer to blobs */
168
char *unique_file_name; /* realpath() of index file */
169
char *data_file_name, /* Resolved path names from symlinks */
171
uchar *file_map; /* mem-map of file if possible */
172
KEY_CACHE *key_cache; /* ref to the current key cache */
173
MI_DECODE_TREE *decode_trees;
174
uint16 *decode_tables;
175
int (*read_record)(struct st_myisam_info*, my_off_t, uchar*);
176
int (*write_record)(struct st_myisam_info*, const uchar*);
177
int (*update_record)(struct st_myisam_info*, my_off_t, const uchar*);
178
int (*delete_record)(struct st_myisam_info*);
179
int (*read_rnd)(struct st_myisam_info*, uchar*, my_off_t, my_bool);
180
int (*compare_record)(struct st_myisam_info*, const uchar *);
181
/* Function to use for a row checksum. */
182
ha_checksum (*calc_checksum)(struct st_myisam_info*, const uchar *);
183
int (*compare_unique)(struct st_myisam_info*, MI_UNIQUEDEF *,
184
const uchar *record, my_off_t pos);
185
size_t (*file_read)(MI_INFO *, uchar *, size_t, my_off_t, myf);
186
size_t (*file_write)(MI_INFO *, const uchar *, size_t, my_off_t, myf);
187
invalidator_by_filename invalidator; /* query cache invalidator */
188
ulong this_process; /* processid */
189
ulong last_process; /* For table-change-check */
190
ulong last_version; /* Version on start */
191
ulong options; /* Options used */
192
ulong min_pack_length; /* Theese are used by packed data */
193
ulong max_pack_length;
194
ulong state_diff_length;
195
uint rec_reflength; /* rec_reflength in use now */
196
uint unique_name_length;
197
uint32 ftkeys; /* Number of full-text keys + 1 */
198
File kfile; /* Shared keyfile */
199
File data_file; /* Shared data file */
200
int mode; /* mode of file on open */
201
uint reopen; /* How many times reopened */
202
uint w_locks,r_locks,tot_locks; /* Number of read/write locks */
203
uint blocksize; /* blocksize of keyfile */
205
enum data_file_type data_file_type;
206
/* Below flag is needed to make log tables work with concurrent insert */
207
my_bool is_log_table;
209
my_bool changed, /* If changed since lock */
210
global_changed, /* If changed since open */
212
temporary,delay_key_write,
216
pthread_mutex_t intern_lock; /* Locking for use with _locking */
217
rw_lock_t *key_root_lock;
219
my_off_t mmaped_length;
220
uint nonmmaped_inserts; /* counter of writing in non-mmaped
226
typedef uint mi_bit_type;
228
typedef struct st_mi_bit_buff { /* Used for packing of record */
229
mi_bit_type current_byte;
231
uchar *pos,*end,*blob_pos,*blob_end;
235
struct st_myisam_info {
236
MYISAM_SHARE *s; /* Shared between open:s */
237
MI_STATUS_INFO *state,save_state;
238
MI_BLOB *blobs; /* Pointer to blobs */
239
MI_BIT_BUFF bit_buff;
240
/* accumulate indexfile changes between write's */
242
DYNAMIC_ARRAY *ft1_to_ft2; /* used only in ft1->ft2 conversion */
243
MEM_ROOT ft_memroot; /* used by the parser */
244
MYSQL_FTPARSER_PARAM *ftparser_param; /* share info between init/deinit */
245
char *filename; /* parameter to open filename */
246
uchar *buff, /* Temp area for key */
247
*lastkey,*lastkey2; /* Last used search key */
248
uchar *first_mbr_key; /* Searhed spatial key */
249
uchar *rec_buff; /* Tempbuff for recordpack */
250
uchar *int_keypos, /* Save position for next/previous */
251
*int_maxpos; /* -""- */
252
uint int_nod_flag; /* -""- */
253
uint32 int_keytree_version; /* -""- */
254
int (*read_record)(struct st_myisam_info*, my_off_t, uchar*);
255
invalidator_by_filename invalidator; /* query cache invalidator */
256
ulong this_unique; /* uniq filenumber or thread */
257
ulong last_unique; /* last unique number */
258
ulong this_loop; /* counter for this open */
259
ulong last_loop; /* last used counter */
260
my_off_t lastpos, /* Last record position */
261
nextpos; /* Position to next record */
262
my_off_t save_lastpos;
263
my_off_t pos; /* Intern variable */
264
my_off_t last_keypage; /* Last key page read */
265
my_off_t last_search_keypage; /* Last keypage when searching */
266
my_off_t dupp_key_pos;
267
ha_checksum checksum; /* Temp storage for row checksum */
268
/* QQ: the folloing two xxx_length fields should be removed,
269
as they are not compatible with parallel repair */
270
ulong packed_length,blob_length; /* Length of found, packed record */
271
int dfile; /* The datafile */
272
uint opt_flag; /* Optim. for space/speed */
273
uint update; /* If file changed since open */
274
int lastinx; /* Last used index */
275
uint lastkey_length; /* Length of key in lastkey */
276
uint last_rkey_length; /* Last length in mi_rkey() */
277
enum ha_rkey_function last_key_func; /* CONTAIN, OVERLAP, etc */
278
uint save_lastkey_length;
279
uint pack_key_length; /* For MYISAMMRG */
280
uint16 last_used_keyseg; /* For MyISAMMRG */
281
int errkey; /* Got last error on this key */
282
int lock_type; /* How database was locked */
283
int tmp_lock_type; /* When locked by readinfo */
284
uint data_changed; /* Somebody has changed data */
285
uint save_update; /* When using KEY_READ */
288
IO_CACHE rec_cache; /* When cacheing records */
289
uint preload_buff_size; /* When preloading indexes */
290
myf lock_wait; /* is 0 or MY_DONT_WAIT */
291
my_bool was_locked; /* Was locked in panic */
292
my_bool append_insert_at_end; /* Set if concurrent insert */
294
my_bool page_changed; /* If info->buff can't be used for rnext */
295
my_bool buff_used; /* If info->buff has to be reread for rnext */
296
my_bool once_flags; /* For MYISAMMRG */
298
my_bool owned_by_merge; /* This MyISAM table is part of a merge union */
303
uchar *rtree_recursion_state; /* For RTREE */
304
int rtree_recursion_depth;
307
typedef struct st_buffpek {
308
my_off_t file_pos; /* Where we are in the sort file */
309
uchar *base,*key; /* Key pointers */
310
ha_rows count; /* Number of rows in table */
311
ulong mem_count; /* numbers of keys in memory */
312
ulong max_keys; /* Max keys in buffert */
315
typedef struct st_mi_sort_param
318
IO_CACHE read_cache, tempfile, tempfile_for_exceptions;
319
DYNAMIC_ARRAY buffpek;
320
MI_BIT_BUFF bit_buff; /* For parallel repair of packrec. */
323
The next two are used to collect statistics, see update_key_parts for
326
ulonglong unique[MI_MAX_KEY_SEG+1];
327
ulonglong notnull[MI_MAX_KEY_SEG+1];
329
my_off_t pos,max_pos,filepos,start_recpos;
330
uint key, key_length,real_key_length,sortbuff_size;
331
uint maxbuffers, keys, find_length, sort_keys_length;
332
my_bool fix_datafile, master;
333
my_bool calc_checksum; /* calculate table checksum */
336
SORT_INFO *sort_info;
339
void *wordlist, *wordptr;
343
int (*key_cmp)(struct st_mi_sort_param *, const void *, const void *);
344
int (*key_read)(struct st_mi_sort_param *,void *);
345
int (*key_write)(struct st_mi_sort_param *, const void *);
346
void (*lock_in_memory)(MI_CHECK *);
347
NEAR int (*write_keys)(struct st_mi_sort_param *, register uchar **,
348
uint , struct st_buffpek *, IO_CACHE *);
349
NEAR uint (*read_to_buffer)(IO_CACHE *,struct st_buffpek *, uint);
350
NEAR int (*write_key)(struct st_mi_sort_param *, IO_CACHE *,uchar *,
354
/* Some defines used by isam-funktions */
356
#define USE_WHOLE_KEY MI_MAX_KEY_BUFF*2 /* Use whole key in _mi_search() */
357
#define F_EXTRA_LCK -1
359
/* bits in opt_flag */
360
#define MEMMAP_USED 32
361
#define REMEMBER_OLD_POS 64
363
#define WRITEINFO_UPDATE_KEYFILE 1
364
#define WRITEINFO_NO_UNLOCK 2
367
#define USE_PACKED_KEYS 1
368
#define RRND_PRESERVE_LASTINX 2
370
/* bits in state.changed */
372
#define STATE_CHANGED 1
373
#define STATE_CRASHED 2
374
#define STATE_CRASHED_ON_REPAIR 4
375
#define STATE_NOT_ANALYZED 8
376
#define STATE_NOT_OPTIMIZED_KEYS 16
377
#define STATE_NOT_SORTED_PAGES 32
379
/* options to mi_read_cache */
381
#define READING_NEXT 1
382
#define READING_HEADER 2
384
#define mi_getint(x) ((uint) mi_uint2korr(x) & 32767)
385
#define mi_putint(x,y,nod) { uint16 boh=(nod ? (uint16) 32768 : 0) + (uint16) (y);\
386
mi_int2store(x,boh); }
387
#define mi_test_if_nod(x) (x[0] & 128 ? info->s->base.key_reflength : 0)
388
#define mi_mark_crashed(x) do{(x)->s->state.changed|= STATE_CRASHED; \
389
DBUG_PRINT("error", ("Marked table crashed")); \
391
#define mi_mark_crashed_on_repair(x) do{(x)->s->state.changed|= \
392
STATE_CRASHED|STATE_CRASHED_ON_REPAIR; \
393
(x)->update|= HA_STATE_CHANGED; \
394
DBUG_PRINT("error", \
395
("Marked table crashed")); \
397
#define mi_is_crashed(x) ((x)->s->state.changed & STATE_CRASHED)
398
#define mi_is_crashed_on_repair(x) ((x)->s->state.changed & STATE_CRASHED_ON_REPAIR)
399
#define mi_print_error(SHARE, ERRNO) \
400
mi_report_error((ERRNO), (SHARE)->index_file_name)
402
/* Functions to store length of space packed keys, VARCHAR or BLOB keys */
404
#define store_key_length(key,length) \
405
{ if ((length) < 255) \
406
{ *(key)=(length); } \
408
{ *(key)=255; mi_int2store((key)+1,(length)); } \
411
#define get_key_full_length(length,key) \
412
{ if ((uchar) *(key) != 255) \
413
length= ((uint) (uchar) *((key)++))+1; \
415
{ length=mi_uint2korr((key)+1)+3; (key)+=3; } \
418
#define get_key_full_length_rdonly(length,key) \
419
{ if ((uchar) *(key) != 255) \
420
length= ((uint) (uchar) *((key)))+1; \
422
{ length=mi_uint2korr((key)+1)+3; } \
425
#define get_pack_length(length) ((length) >= 255 ? 3 : 1)
427
#define MI_MIN_BLOCK_LENGTH 20 /* Because of delete-link */
428
#define MI_EXTEND_BLOCK_LENGTH 20 /* Don't use to small record-blocks */
429
#define MI_SPLIT_LENGTH ((MI_EXTEND_BLOCK_LENGTH+4)*2)
430
#define MI_MAX_DYN_BLOCK_HEADER 20 /* Max prefix of record-block */
431
#define MI_BLOCK_INFO_HEADER_LENGTH 20
432
#define MI_DYN_DELETE_BLOCK_HEADER 20 /* length of delete-block-header */
433
#define MI_DYN_MAX_BLOCK_LENGTH ((1L << 24)-4L)
434
#define MI_DYN_MAX_ROW_LENGTH (MI_DYN_MAX_BLOCK_LENGTH - MI_SPLIT_LENGTH)
435
#define MI_DYN_ALIGN_SIZE 4 /* Align blocks on this */
436
#define MI_MAX_DYN_HEADER_BYTE 13 /* max header byte for dynamic rows */
437
#define MI_MAX_BLOCK_LENGTH ((((ulong) 1 << 24)-1) & (~ (ulong) (MI_DYN_ALIGN_SIZE-1)))
438
#define MI_REC_BUFF_OFFSET ALIGN_SIZE(MI_DYN_DELETE_BLOCK_HEADER+sizeof(uint32))
441
#define PACK_TYPE_SELECTED 1 /* Bits in field->pack_type */
442
#define PACK_TYPE_SPACE_FIELDS 2
443
#define PACK_TYPE_ZERO_FILL 4
444
#define MI_FOUND_WRONG_KEY 32738 /* Impossible value from ha_key_cmp */
446
#define MI_MAX_KEY_BLOCK_SIZE (MI_MAX_KEY_BLOCK_LENGTH/MI_MIN_KEY_BLOCK_LENGTH)
447
#define MI_BLOCK_SIZE(key_length,data_pointer,key_pointer,block_size) (((((key_length)+(data_pointer)+(key_pointer))*4+(key_pointer)+2)/(block_size)+1)*(block_size))
448
#define MI_MAX_KEYPTR_SIZE 5 /* For calculating block lengths */
449
#define MI_MIN_KEYBLOCK_LENGTH 50 /* When to split delete blocks */
451
#define MI_MIN_SIZE_BULK_INSERT_TREE 16384 /* this is per key */
452
#define MI_MIN_ROWS_TO_USE_BULK_INSERT 100
453
#define MI_MIN_ROWS_TO_DISABLE_INDEXES 100
454
#define MI_MIN_ROWS_TO_USE_WRITE_CACHE 10
456
/* The UNIQUE check is done with a hashed long key */
458
#define MI_UNIQUE_HASH_TYPE HA_KEYTYPE_ULONG_INT
459
#define mi_unique_store(A,B) mi_int4store((A),(B))
462
extern pthread_mutex_t THR_LOCK_myisam;
464
#if !defined(THREAD) || defined(DONT_USE_RW_LOCKS)
465
#define rw_wrlock(A) {}
466
#define rw_rdlock(A) {}
467
#define rw_unlock(A) {}
470
/* Some extern variables */
472
extern LIST *myisam_open_list;
473
extern uchar NEAR myisam_file_magic[],NEAR myisam_pack_file_magic[];
474
extern uint NEAR myisam_read_vec[],NEAR myisam_readnext_vec[];
475
extern uint myisam_quick_table_bits;
476
extern File myisam_log_file;
477
extern ulong myisam_pid;
479
/* This is used by _mi_calc_xxx_key_length och _mi_store_key */
481
typedef struct st_mi_s_param
483
uint ref_length,key_length,
487
part_of_prev_key,prev_length,pack_marker;
488
uchar *key, *prev_key,*next_key_pos;
489
my_bool store_not_null;
492
/* Prototypes for intern functions */
494
extern int _mi_read_dynamic_record(MI_INFO *info,my_off_t filepos,uchar *buf);
495
extern int _mi_write_dynamic_record(MI_INFO*, const uchar*);
496
extern int _mi_update_dynamic_record(MI_INFO*, my_off_t, const uchar*);
497
extern int _mi_delete_dynamic_record(MI_INFO *info);
498
extern int _mi_cmp_dynamic_record(MI_INFO *info,const uchar *record);
499
extern int _mi_read_rnd_dynamic_record(MI_INFO *, uchar *,my_off_t, my_bool);
500
extern int _mi_write_blob_record(MI_INFO*, const uchar*);
501
extern int _mi_update_blob_record(MI_INFO*, my_off_t, const uchar*);
502
extern int _mi_read_static_record(MI_INFO *info, my_off_t filepos,uchar *buf);
503
extern int _mi_write_static_record(MI_INFO*, const uchar*);
504
extern int _mi_update_static_record(MI_INFO*, my_off_t, const uchar*);
505
extern int _mi_delete_static_record(MI_INFO *info);
506
extern int _mi_cmp_static_record(MI_INFO *info,const uchar *record);
507
extern int _mi_read_rnd_static_record(MI_INFO*, uchar *,my_off_t, my_bool);
508
extern int _mi_ck_write(MI_INFO *info,uint keynr,uchar *key,uint length);
509
extern int _mi_ck_real_write_btree(MI_INFO *info, MI_KEYDEF *keyinfo,
510
uchar *key, uint key_length,
511
my_off_t *root, uint comp_flag);
512
extern int _mi_enlarge_root(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key, my_off_t *root);
513
extern int _mi_insert(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,
514
uchar *anc_buff,uchar *key_pos,uchar *key_buff,
515
uchar *father_buff, uchar *father_keypos,
516
my_off_t father_page, my_bool insert_last);
517
extern int _mi_split_page(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,
518
uchar *buff,uchar *key_buff, my_bool insert_last);
519
extern uchar *_mi_find_half_pos(uint nod_flag,MI_KEYDEF *keyinfo,uchar *page,
520
uchar *key,uint *return_key_length,
522
extern int _mi_calc_static_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
523
uchar *key_pos, uchar *org_key,
525
uchar *key, MI_KEY_PARAM *s_temp);
526
extern int _mi_calc_var_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
527
uchar *key_pos, uchar *org_key,
529
uchar *key, MI_KEY_PARAM *s_temp);
530
extern int _mi_calc_var_pack_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
531
uchar *key_pos, uchar *org_key,
533
uchar *key, MI_KEY_PARAM *s_temp);
534
extern int _mi_calc_bin_pack_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
535
uchar *key_pos,uchar *org_key,
537
uchar *key, MI_KEY_PARAM *s_temp);
538
void _mi_store_static_key(MI_KEYDEF *keyinfo, uchar *key_pos,
539
MI_KEY_PARAM *s_temp);
540
void _mi_store_var_pack_key(MI_KEYDEF *keyinfo, uchar *key_pos,
541
MI_KEY_PARAM *s_temp);
543
void _mi_store_pack_key(MI_KEYDEF *keyinfo, uchar *key_pos,
544
MI_KEY_PARAM *s_temp);
546
void _mi_store_bin_pack_key(MI_KEYDEF *keyinfo, uchar *key_pos,
547
MI_KEY_PARAM *s_temp);
549
extern int _mi_ck_delete(MI_INFO *info,uint keynr,uchar *key,uint key_length);
550
extern int _mi_readinfo(MI_INFO *info,int lock_flag,int check_keybuffer);
551
extern int _mi_writeinfo(MI_INFO *info,uint options);
552
extern int _mi_test_if_changed(MI_INFO *info);
553
extern int _mi_mark_file_changed(MI_INFO *info);
554
extern int _mi_decrement_open_count(MI_INFO *info);
555
extern int _mi_check_index(MI_INFO *info,int inx);
556
extern int _mi_search(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,uint key_len,
557
uint nextflag,my_off_t pos);
558
extern int _mi_bin_search(struct st_myisam_info *info,MI_KEYDEF *keyinfo,
559
uchar *page,uchar *key,uint key_len,uint comp_flag,
560
uchar * *ret_pos,uchar *buff, my_bool *was_last_key);
561
extern int _mi_seq_search(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *page,
562
uchar *key,uint key_len,uint comp_flag,
563
uchar **ret_pos,uchar *buff, my_bool *was_last_key);
564
extern int _mi_prefix_search(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *page,
565
uchar *key,uint key_len,uint comp_flag,
566
uchar **ret_pos,uchar *buff, my_bool *was_last_key);
567
extern my_off_t _mi_kpos(uint nod_flag,uchar *after_key);
568
extern void _mi_kpointer(MI_INFO *info,uchar *buff,my_off_t pos);
569
extern my_off_t _mi_dpos(MI_INFO *info, uint nod_flag,uchar *after_key);
570
extern my_off_t _mi_rec_pos(MYISAM_SHARE *info, uchar *ptr);
571
extern void _mi_dpointer(MI_INFO *info, uchar *buff,my_off_t pos);
572
extern int ha_key_cmp(HA_KEYSEG *keyseg, uchar *a,uchar *b,
573
uint key_length,uint nextflag,uint *diff_length);
574
extern uint _mi_get_static_key(MI_KEYDEF *keyinfo,uint nod_flag,uchar * *page,
576
extern uint _mi_get_pack_key(MI_KEYDEF *keyinfo,uint nod_flag,uchar * *page,
578
extern uint _mi_get_binary_pack_key(MI_KEYDEF *keyinfo, uint nod_flag,
579
uchar **page_pos, uchar *key);
580
extern uchar *_mi_get_last_key(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *keypos,
581
uchar *lastkey,uchar *endpos,
582
uint *return_key_length);
583
extern uchar *_mi_get_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
584
uchar *key, uchar *keypos, uint *return_key_length);
585
extern uint _mi_keylength(MI_KEYDEF *keyinfo,uchar *key);
586
extern uint _mi_keylength_part(MI_KEYDEF *keyinfo, register uchar *key,
588
extern uchar *_mi_move_key(MI_KEYDEF *keyinfo,uchar *to,uchar *from);
589
extern int _mi_search_next(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,
590
uint key_length,uint nextflag,my_off_t pos);
591
extern int _mi_search_first(MI_INFO *info,MI_KEYDEF *keyinfo,my_off_t pos);
592
extern int _mi_search_last(MI_INFO *info,MI_KEYDEF *keyinfo,my_off_t pos);
593
extern uchar *_mi_fetch_keypage(MI_INFO *info,MI_KEYDEF *keyinfo,my_off_t page,
594
int level,uchar *buff,int return_buffer);
595
extern int _mi_write_keypage(MI_INFO *info,MI_KEYDEF *keyinfo,my_off_t page,
596
int level, uchar *buff);
597
extern int _mi_dispose(MI_INFO *info,MI_KEYDEF *keyinfo,my_off_t pos,
599
extern my_off_t _mi_new(MI_INFO *info,MI_KEYDEF *keyinfo,int level);
600
extern uint _mi_make_key(MI_INFO *info,uint keynr,uchar *key,
601
const uchar *record,my_off_t filepos);
602
extern uint _mi_pack_key(register MI_INFO *info, uint keynr, uchar *key,
603
uchar *old, key_part_map keypart_map,
604
HA_KEYSEG **last_used_keyseg);
605
extern int _mi_read_key_record(MI_INFO *info,my_off_t filepos,uchar *buf);
606
extern int _mi_read_cache(IO_CACHE *info,uchar *buff,my_off_t pos,
607
uint length,int re_read_if_possibly);
608
extern ulonglong retrieve_auto_increment(MI_INFO *info,const uchar *record);
610
extern uchar *mi_alloc_rec_buff(MI_INFO *,ulong, uchar**);
611
#define mi_get_rec_buff_ptr(info,buf) \
612
((((info)->s->options & HA_OPTION_PACK_RECORD) && (buf)) ? \
613
(buf) - MI_REC_BUFF_OFFSET : (buf))
614
#define mi_get_rec_buff_len(info,buf) \
615
(*((uint32 *)(mi_get_rec_buff_ptr(info,buf))))
617
extern ulong _mi_rec_unpack(MI_INFO *info,uchar *to,uchar *from,
619
extern my_bool _mi_rec_check(MI_INFO *info,const uchar *record, uchar *packpos,
620
ulong packed_length, my_bool with_checkum);
621
extern int _mi_write_part_record(MI_INFO *info,my_off_t filepos,ulong length,
622
my_off_t next_filepos,uchar **record,
623
ulong *reclength,int *flag);
624
extern void _mi_print_key(FILE *stream,HA_KEYSEG *keyseg,const uchar *key,
626
extern my_bool _mi_read_pack_info(MI_INFO *info,pbool fix_keys);
627
extern int _mi_read_pack_record(MI_INFO *info,my_off_t filepos,uchar *buf);
628
extern int _mi_read_rnd_pack_record(MI_INFO*, uchar *,my_off_t, my_bool);
629
extern int _mi_pack_rec_unpack(MI_INFO *info, MI_BIT_BUFF *bit_buff,
630
uchar *to, uchar *from, ulong reclength);
631
extern ulonglong mi_safe_mul(ulonglong a,ulonglong b);
632
extern int _mi_ft_update(MI_INFO *info, uint keynr, uchar *keybuf,
633
const uchar *oldrec, const uchar *newrec, my_off_t pos);
638
typedef struct st_mi_block_info { /* Parameter to _mi_get_block_info */
639
uchar header[MI_BLOCK_INFO_HEADER_LENGTH];
645
my_off_t next_filepos;
646
my_off_t prev_filepos;
651
/* bits in return from _mi_get_block_info */
653
#define BLOCK_FIRST 1
655
#define BLOCK_DELETED 4
656
#define BLOCK_ERROR 8 /* Wrong data */
657
#define BLOCK_SYNC_ERROR 16 /* Right data at wrong place */
658
#define BLOCK_FATAL_ERROR 32 /* hardware-error */
660
#define NEED_MEM ((uint) 10*4*(IO_SIZE+32)+32) /* Nead for recursion */
662
#define BUFFERS_WHEN_SORTING 16 /* Alloc for sort-key-tree */
663
#define WRITE_COUNT MY_HOW_OFTEN_TO_WRITE
664
#define INDEX_TMP_EXT ".TMM"
665
#define DATA_TMP_EXT ".TMD"
667
#define UPDATE_TIME 1
668
#define UPDATE_STAT 2
669
#define UPDATE_SORT 4
670
#define UPDATE_AUTO_INC 8
671
#define UPDATE_OPEN_COUNT 16
673
#define USE_BUFFER_INIT (((1024L*512L-MALLOC_OVERHEAD)/IO_SIZE)*IO_SIZE)
674
#define READ_BUFFER_INIT (1024L*256L-MALLOC_OVERHEAD)
675
#define SORT_BUFFER_INIT (2048L*1024L-MALLOC_OVERHEAD)
676
#define MIN_SORT_BUFFER (4096-MALLOC_OVERHEAD)
678
enum myisam_log_commands {
679
MI_LOG_OPEN,MI_LOG_WRITE,MI_LOG_UPDATE,MI_LOG_DELETE,MI_LOG_CLOSE,MI_LOG_EXTRA,MI_LOG_LOCK,MI_LOG_DELETE_ALL
682
#define myisam_log(a,b,c,d) if (myisam_log_file >= 0) _myisam_log(a,b,c,d)
683
#define myisam_log_command(a,b,c,d,e) if (myisam_log_file >= 0) _myisam_log_command(a,b,c,d,e)
684
#define myisam_log_record(a,b,c,d,e) if (myisam_log_file >= 0) _myisam_log_record(a,b,c,d,e)
686
#define fast_mi_writeinfo(INFO) if (!(INFO)->s->tot_locks) (void) _mi_writeinfo((INFO),0)
687
#define fast_mi_readinfo(INFO) ((INFO)->lock_type == F_UNLCK) && _mi_readinfo((INFO),F_RDLCK,1)
693
extern uint _mi_get_block_info(MI_BLOCK_INFO *,File, my_off_t);
694
extern uint _mi_rec_pack(MI_INFO *info,uchar *to,const uchar *from);
695
extern uint _mi_pack_get_block_info(MI_INFO *myisam, MI_BIT_BUFF *bit_buff,
696
MI_BLOCK_INFO *info, uchar **rec_buff_p,
697
File file, my_off_t filepos);
698
extern void _my_store_blob_length(uchar *pos,uint pack_length,uint length);
699
extern void _myisam_log(enum myisam_log_commands command,MI_INFO *info,
700
const uchar *buffert,uint length);
701
extern void _myisam_log_command(enum myisam_log_commands command,
702
MI_INFO *info, const uchar *buffert,
703
uint length, int result);
704
extern void _myisam_log_record(enum myisam_log_commands command,MI_INFO *info,
705
const uchar *record,my_off_t filepos,
707
extern void mi_report_error(int errcode, const char *file_name);
708
extern my_bool _mi_memmap_file(MI_INFO *info);
709
extern void _mi_unmap_file(MI_INFO *info);
710
extern uint save_pack_length(uint version, uchar *block_buff, ulong length);
711
extern uint read_pack_length(uint version, const uchar *buf, ulong *length);
712
extern uint calc_pack_length(uint version, ulong length);
713
extern size_t mi_mmap_pread(MI_INFO *info, uchar *Buffer,
714
size_t Count, my_off_t offset, myf MyFlags);
715
extern size_t mi_mmap_pwrite(MI_INFO *info, const uchar *Buffer,
716
size_t Count, my_off_t offset, myf MyFlags);
717
extern size_t mi_nommap_pread(MI_INFO *info, uchar *Buffer,
718
size_t Count, my_off_t offset, myf MyFlags);
719
extern size_t mi_nommap_pwrite(MI_INFO *info, const uchar *Buffer,
720
size_t Count, my_off_t offset, myf MyFlags);
722
uint mi_state_info_write(File file, MI_STATE_INFO *state, uint pWrite);
723
uchar *mi_state_info_read(uchar *ptr, MI_STATE_INFO *state);
724
uint mi_state_info_read_dsk(File file, MI_STATE_INFO *state, my_bool pRead);
725
uint mi_base_info_write(File file, MI_BASE_INFO *base);
726
uchar *my_n_base_info_read(uchar *ptr, MI_BASE_INFO *base);
727
int mi_keyseg_write(File file, const HA_KEYSEG *keyseg);
728
uchar *mi_keyseg_read(uchar *ptr, HA_KEYSEG *keyseg);
729
uint mi_keydef_write(File file, MI_KEYDEF *keydef);
730
uchar *mi_keydef_read(uchar *ptr, MI_KEYDEF *keydef);
731
uint mi_uniquedef_write(File file, MI_UNIQUEDEF *keydef);
732
uchar *mi_uniquedef_read(uchar *ptr, MI_UNIQUEDEF *keydef);
733
uint mi_recinfo_write(File file, MI_COLUMNDEF *recinfo);
734
uchar *mi_recinfo_read(uchar *ptr, MI_COLUMNDEF *recinfo);
735
extern int mi_disable_indexes(MI_INFO *info);
736
extern int mi_enable_indexes(MI_INFO *info);
737
extern int mi_indexes_are_disabled(MI_INFO *info);
738
ulong _my_calc_total_blob_length(MI_INFO *info, const uchar *record);
739
ha_checksum mi_checksum(MI_INFO *info, const uchar *buf);
740
ha_checksum mi_static_checksum(MI_INFO *info, const uchar *buf);
741
my_bool mi_check_unique(MI_INFO *info, MI_UNIQUEDEF *def, uchar *record,
742
ha_checksum unique_hash, my_off_t pos);
743
ha_checksum mi_unique_hash(MI_UNIQUEDEF *def, const uchar *buf);
744
int _mi_cmp_static_unique(MI_INFO *info, MI_UNIQUEDEF *def,
745
const uchar *record, my_off_t pos);
746
int _mi_cmp_dynamic_unique(MI_INFO *info, MI_UNIQUEDEF *def,
747
const uchar *record, my_off_t pos);
748
int mi_unique_comp(MI_UNIQUEDEF *def, const uchar *a, const uchar *b,
749
my_bool null_are_equal);
750
void mi_get_status(void* param, int concurrent_insert);
751
void mi_update_status(void* param);
752
void mi_restore_status(void* param);
753
void mi_copy_status(void* to,void *from);
754
my_bool mi_check_status(void* param);
755
void mi_disable_non_unique_index(MI_INFO *info, ha_rows rows);
757
extern MI_INFO *test_if_reopen(char *filename);
758
my_bool check_table_is_closed(const char *name, const char *where);
759
int mi_open_datafile(MI_INFO *info, MYISAM_SHARE *share, const char *orn_name,
762
int mi_open_keyfile(MYISAM_SHARE *share);
763
void mi_setup_functions(register MYISAM_SHARE *share);
764
my_bool mi_dynmap_file(MI_INFO *info, my_off_t size);
765
void mi_remap_file(MI_INFO *info, my_off_t size);
767
/* Functions needed by mi_check */
768
volatile int *killed_ptr(MI_CHECK *param);
769
void mi_check_print_error _VARARGS((MI_CHECK *param, const char *fmt,...));
770
void mi_check_print_warning _VARARGS((MI_CHECK *param, const char *fmt,...));
771
void mi_check_print_info _VARARGS((MI_CHECK *param, const char *fmt,...));
772
int flush_pending_blocks(MI_SORT_PARAM *param);
773
int sort_ft_buf_flush(MI_SORT_PARAM *sort_param);
774
int thr_write_keys(MI_SORT_PARAM *sort_param);
776
pthread_handler_t thr_find_all_keys(void *arg);
778
int flush_blocks(MI_CHECK *param, KEY_CACHE *key_cache, File file);
780
int sort_write_record(MI_SORT_PARAM *sort_param);
781
int _create_index_by_sort(MI_SORT_PARAM *info,my_bool no_messages, ulong);