MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
myisamdef.h
1 /*
2  Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 /* This file is included by all internal myisam files */
18 
19 #include "myisam.h" /* Structs & some defines */
20 #include "myisampack.h" /* packing of keys */
21 #include <my_tree.h>
22 #include <my_pthread.h>
23 #include <thr_lock.h>
24 #include <mysql/psi/mysql_file.h>
25 
26 /* undef map from my_nosys; We need test-if-disk full */
27 #if defined(my_write)
28 #undef my_write
29 #endif
30 
31 typedef struct st_mi_status_info
32 {
33  ha_rows records; /* Rows in table */
34  ha_rows del; /* Removed rows */
35  my_off_t empty; /* lost space in datafile */
36  my_off_t key_empty; /* lost space in indexfile */
37  my_off_t key_file_length;
38  my_off_t data_file_length;
39  ha_checksum checksum;
40  my_bool uncacheable; /* Active concurrent insert */
42 
43 typedef struct st_mi_state_info
44 {
45  struct { /* Fileheader */
46  uchar file_version[4];
47  uchar options[2];
48  uchar header_length[2];
49  uchar state_info_length[2];
50  uchar base_info_length[2];
51  uchar base_pos[2];
52  uchar key_parts[2]; /* Key parts */
53  uchar unique_key_parts[2]; /* Key parts + unique parts */
54  uchar keys; /* number of keys in file */
55  uchar uniques; /* number of UNIQUE definitions */
56  uchar language; /* Language for indexes */
57  uchar max_block_size_index; /* max keyblock size */
58  uchar fulltext_keys;
59  uchar not_used; /* To align to 8 */
60  } header;
61 
62  MI_STATUS_INFO state;
63  ha_rows split; /* number of split blocks */
64  my_off_t dellink; /* Link to next removed block */
65  ulonglong auto_increment;
66  ulong process; /* process that updated table last */
67  ulong unique; /* Unique number for this process */
68  ulong update_count; /* Updated for each write lock */
69  ulong status;
70  ulong *rec_per_key_part;
71  my_off_t *key_root; /* Start of key trees */
72  my_off_t *key_del; /* delete links for trees */
73  my_off_t rec_per_key_rows; /* Rows when calculating rec_per_key */
74 
75  ulong sec_index_changed; /* Updated when new sec_index */
76  ulong sec_index_used; /* which extra index are in use */
77  ulonglong key_map; /* Which keys are in use */
78  ha_checksum checksum; /* Table checksum */
79  ulong version; /* timestamp of create */
80  time_t create_time; /* Time when created database */
81  time_t recover_time; /* Time for last recover */
82  time_t check_time; /* Time for last check */
83  uint sortkey; /* sorted by this key (not used) */
84  uint open_count;
85  uint8 changed; /* Changed since myisamchk */
86 
87  /* the following isn't saved on disk */
88  uint state_diff_length; /* Should be 0 */
89  uint state_length; /* Length of state header in file */
90  ulong *key_info;
92 
93 #define MI_STATE_INFO_SIZE (24+14*8+7*4+2*2+8)
94 #define MI_STATE_KEY_SIZE 8
95 #define MI_STATE_KEYBLOCK_SIZE 8
96 #define MI_STATE_KEYSEG_SIZE 4
97 #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)
98 #define MI_KEYDEF_SIZE (2+ 5*2)
99 #define MI_UNIQUEDEF_SIZE (2+1+1)
100 #define HA_KEYSEG_SIZE (6+ 2*2 + 4*2)
101 #define MI_COLUMNDEF_SIZE (2*3+1)
102 #define MI_BASE_INFO_SIZE (5*8 + 8*4 + 4 + 4*2 + 16)
103 #define MI_INDEX_BLOCK_MARGIN 16 /* Safety margin for .MYI tables */
104 
105 typedef struct st_mi_base_info
106 {
107  my_off_t keystart; /* Start of keys */
108  my_off_t max_data_file_length;
109  my_off_t max_key_file_length;
110  my_off_t margin_key_file_length;
111  ha_rows records,reloc; /* Create information */
112  ulong mean_row_length; /* Create information */
113  ulong reclength; /* length of unpacked record */
114  ulong pack_reclength; /* Length of full packed rec. */
115  ulong min_pack_length;
116  ulong max_pack_length; /* Max possibly length of packed rec.*/
117  ulong min_block_length;
118  ulong fields, /* fields in table */
119  pack_fields; /* packed fields in table */
120  uint rec_reflength; /* = 2-8 */
121  uint key_reflength; /* = 2-8 */
122  uint keys; /* same as in state.header */
123  uint auto_key; /* Which key-1 is a auto key */
124  uint blobs; /* Number of blobs */
125  uint pack_bits; /* Length of packed bits */
126  uint max_key_block_length; /* Max block length */
127  uint max_key_length; /* Max key length */
128  /* Extra allocation when using dynamic record format */
129  uint extra_alloc_bytes;
130  uint extra_alloc_procent;
131  /* The following are from the header */
132  uint key_parts,all_key_parts;
133 } MI_BASE_INFO;
134 
135 
136  /* Structs used intern in database */
137 
138 typedef struct st_mi_blob /* Info of record */
139 {
140  ulong offset; /* Offset to blob in record */
141  uint pack_length; /* Type of packed length */
142  ulong length; /* Calc:ed for each record */
143 } MI_BLOB;
144 
145 
146 typedef struct st_mi_isam_pack {
147  ulong header_length;
148  uint ref_length;
149  uchar version;
150 } MI_PACK;
151 
152 #define MAX_NONMAPPED_INSERTS 1000
153 
154 typedef struct st_mi_isam_share { /* Shared between opens */
155  MI_STATE_INFO state;
156  MI_BASE_INFO base;
157  MI_KEYDEF ft2_keyinfo; /* Second-level ft-key definition */
158  MI_KEYDEF *keyinfo; /* Key definitions */
159  MI_UNIQUEDEF *uniqueinfo; /* unique definitions */
160  HA_KEYSEG *keyparts; /* key part info */
161  MI_COLUMNDEF *rec; /* Pointer to field information */
162  MI_PACK pack; /* Data about packed records */
163  MI_BLOB *blobs; /* Pointer to blobs */
164  LIST *in_use; /* List of threads using this table */
165  char *unique_file_name; /* realpath() of index file */
166  char *data_file_name, /* Resolved path names from symlinks */
167  *index_file_name;
168  uchar *file_map; /* mem-map of file if possible */
169  KEY_CACHE *key_cache; /* ref to the current key cache */
170  MI_DECODE_TREE *decode_trees;
171  uint16 *decode_tables;
172  int (*read_record)(struct st_myisam_info*, my_off_t, uchar*);
173  int (*write_record)(struct st_myisam_info*, const uchar*);
174  int (*update_record)(struct st_myisam_info*, my_off_t, const uchar*);
175  int (*delete_record)(struct st_myisam_info*);
176  int (*read_rnd)(struct st_myisam_info*, uchar*, my_off_t, my_bool);
177  int (*compare_record)(struct st_myisam_info*, const uchar *);
178  /* Function to use for a row checksum. */
179  ha_checksum (*calc_checksum)(struct st_myisam_info*, const uchar *);
180  int (*compare_unique)(struct st_myisam_info*, MI_UNIQUEDEF *,
181  const uchar *record, my_off_t pos);
182  size_t (*file_read)(MI_INFO *, uchar *, size_t, my_off_t, myf);
183  size_t (*file_write)(MI_INFO *, const uchar *, size_t, my_off_t, myf);
184  invalidator_by_filename invalidator; /* query cache invalidator */
185  ulong this_process; /* processid */
186  ulong last_process; /* For table-change-check */
187  ulong last_version; /* Version on start */
188  ulong options; /* Options used */
189  ulong min_pack_length; /* Theese are used by packed data */
190  ulong max_pack_length;
191  ulong state_diff_length;
192  uint rec_reflength; /* rec_reflength in use now */
193  uint unique_name_length;
194  uint32 ftkeys; /* Number of full-text keys + 1 */
195  File kfile; /* Shared keyfile */
196  File data_file; /* Shared data file */
197  int mode; /* mode of file on open */
198  uint reopen; /* How many times reopened */
199  uint w_locks,r_locks,tot_locks; /* Number of read/write locks */
200  uint blocksize; /* blocksize of keyfile */
201  myf write_flag;
202  enum data_file_type data_file_type;
203  /* Below flag is needed to make log tables work with concurrent insert */
204  my_bool is_log_table;
205 
206  my_bool changed, /* If changed since lock */
207  global_changed, /* If changed since open */
208  not_flushed,
209  temporary,delay_key_write,
210  concurrent_insert;
211 
212  THR_LOCK lock;
213  mysql_mutex_t intern_lock; /* Locking for use with _locking */
214  mysql_rwlock_t *key_root_lock;
215  my_off_t mmaped_length;
216  uint nonmmaped_inserts; /* counter of writing in non-mmaped
217  area */
218  mysql_rwlock_t mmap_lock;
219 } MYISAM_SHARE;
220 
221 typedef uint mi_bit_type;
222 
223 typedef struct st_mi_bit_buff { /* Used for packing of record */
224  mi_bit_type current_byte;
225  uint bits;
226  uchar *pos,*end,*blob_pos,*blob_end;
227  uint error;
228 } MI_BIT_BUFF;
229 
230 C_MODE_START
231 typedef ICP_RESULT (*index_cond_func_t)(void *param);
232 C_MODE_END
233 
235  MYISAM_SHARE *s; /* Shared between open:s */
236  MI_STATUS_INFO *state,save_state;
237  MI_BLOB *blobs; /* Pointer to blobs */
238  MI_BIT_BUFF bit_buff;
239  /* accumulate indexfile changes between write's */
240  TREE *bulk_insert;
241  DYNAMIC_ARRAY *ft1_to_ft2; /* used only in ft1->ft2 conversion */
242  MEM_ROOT ft_memroot; /* used by the parser */
243  MYSQL_FTPARSER_PARAM *ftparser_param; /* share info between init/deinit */
244  LIST in_use; /* Thread using this table */
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 */
286  int save_lastinx;
287  LIST open_list;
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 */
293  my_bool quick_mode;
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 */
297 
298  index_cond_func_t index_cond_func; /* Index condition function */
299  void *index_cond_func_arg; /* parameter for the func */
300 #ifdef __WIN__
301  my_bool owned_by_merge; /* This MyISAM table is part of a merge union */
302 #endif
303  THR_LOCK_DATA lock;
304  uchar *rtree_recursion_state; /* For RTREE */
305  int rtree_recursion_depth;
306 };
307 
308 typedef struct st_buffpek {
309  my_off_t file_pos; /* Where we are in the sort file */
310  uchar *base,*key; /* Key pointers */
311  ha_rows count; /* Number of rows in table */
312  ulong mem_count; /* numbers of keys in memory */
313  ulong max_keys; /* Max keys in buffert */
314 } BUFFPEK;
315 
316 typedef struct st_mi_sort_param
317 {
318  pthread_t thr;
319  IO_CACHE read_cache, tempfile, tempfile_for_exceptions;
320  DYNAMIC_ARRAY buffpek;
321  MI_BIT_BUFF bit_buff; /* For parallel repair of packrec. */
322 
323  /*
324  The next two are used to collect statistics, see update_key_parts for
325  description.
326  */
327  ulonglong unique[MI_MAX_KEY_SEG+1];
328  ulonglong notnull[MI_MAX_KEY_SEG+1];
329  ulonglong sortbuff_size;
330 
331  my_off_t pos,max_pos,filepos,start_recpos;
332  uint key, key_length,real_key_length;
333  uint maxbuffers, keys, find_length, sort_keys_length;
334  my_bool fix_datafile, master;
335  my_bool calc_checksum; /* calculate table checksum */
336  MI_KEYDEF *keyinfo;
337  HA_KEYSEG *seg;
338  SORT_INFO *sort_info;
339  uchar **sort_keys;
340  uchar *rec_buff;
341  void *wordlist, *wordptr;
342  MEM_ROOT wordroot;
343  uchar *record;
344  MY_TMPDIR *tmpdir;
345  int (*key_cmp)(struct st_mi_sort_param *, const void *, const void *);
346  int (*key_read)(struct st_mi_sort_param *,void *);
347  int (*key_write)(struct st_mi_sort_param *, const void *);
348  void (*lock_in_memory)(MI_CHECK *);
349  int (*write_keys)(struct st_mi_sort_param *, register uchar **,
350  uint , struct st_buffpek *, IO_CACHE *);
351  uint (*read_to_buffer)(IO_CACHE *,struct st_buffpek *, uint);
352  int (*write_key)(struct st_mi_sort_param *, IO_CACHE *,uchar *,
353  uint, uint);
354 } MI_SORT_PARAM;
355 
356  /* Some defines used by isam-funktions */
357 
358 #define USE_WHOLE_KEY MI_MAX_KEY_BUFF*2 /* Use whole key in _mi_search() */
359 #define F_EXTRA_LCK -1
360 
361  /* bits in opt_flag */
362 #define MEMMAP_USED 32
363 #define REMEMBER_OLD_POS 64
364 
365 #define WRITEINFO_UPDATE_KEYFILE 1
366 #define WRITEINFO_NO_UNLOCK 2
367 
368  /* once_flags */
369 #define USE_PACKED_KEYS 1
370 #define RRND_PRESERVE_LASTINX 2
371 
372  /* bits in state.changed */
373 
374 #define STATE_CHANGED 1
375 #define STATE_CRASHED 2
376 #define STATE_CRASHED_ON_REPAIR 4
377 #define STATE_NOT_ANALYZED 8
378 #define STATE_NOT_OPTIMIZED_KEYS 16
379 #define STATE_NOT_SORTED_PAGES 32
380 
381  /* options to mi_read_cache */
382 
383 #define READING_NEXT 1
384 #define READING_HEADER 2
385 
386 #define mi_getint(x) ((uint) mi_uint2korr(x) & 32767)
387 #define mi_putint(x,y,nod) { uint16 boh=(nod ? (uint16) 32768 : 0) + (uint16) (y);\
388  mi_int2store(x,boh); }
389 #define mi_test_if_nod(x) (x[0] & 128 ? info->s->base.key_reflength : 0)
390 #define mi_report_crashed(A, B) _mi_report_crashed((A), (B), __FILE__, __LINE__)
391 #define mi_mark_crashed(x) do{(x)->s->state.changed|= STATE_CRASHED; \
392  DBUG_PRINT("error", ("Marked table crashed")); \
393  mi_report_crashed((x), 0); \
394  }while(0)
395 #define mi_mark_crashed_on_repair(x) do{(x)->s->state.changed|= \
396  STATE_CRASHED|STATE_CRASHED_ON_REPAIR; \
397  (x)->update|= HA_STATE_CHANGED; \
398  DBUG_PRINT("error", \
399  ("Marked table crashed")); \
400  }while(0)
401 #define mi_is_crashed(x) ((x)->s->state.changed & STATE_CRASHED)
402 #define mi_is_crashed_on_repair(x) ((x)->s->state.changed & STATE_CRASHED_ON_REPAIR)
403 #define mi_print_error(SHARE, ERRNO) \
404  mi_report_error((ERRNO), (SHARE)->index_file_name)
405 
406 /* Functions to store length of space packed keys, VARCHAR or BLOB keys */
407 
408 #define store_key_length(key,length) \
409 { if ((length) < 255) \
410  { *(key)=(length); } \
411  else \
412  { *(key)=255; mi_int2store((key)+1,(length)); } \
413 }
414 
415 #define get_key_full_length(length,key) \
416 { if ((uchar) *(key) != 255) \
417  length= ((uint) (uchar) *((key)++))+1; \
418  else \
419  { length=mi_uint2korr((key)+1)+3; (key)+=3; } \
420 }
421 
422 #define get_key_full_length_rdonly(length,key) \
423 { if ((uchar) *(key) != 255) \
424  length= ((uint) (uchar) *((key)))+1; \
425  else \
426  { length=mi_uint2korr((key)+1)+3; } \
427 }
428 
429 #define get_pack_length(length) ((length) >= 255 ? 3 : 1)
430 
431 #define MI_MIN_BLOCK_LENGTH 20 /* Because of delete-link */
432 #define MI_EXTEND_BLOCK_LENGTH 20 /* Don't use to small record-blocks */
433 #define MI_SPLIT_LENGTH ((MI_EXTEND_BLOCK_LENGTH+4)*2)
434 #define MI_MAX_DYN_BLOCK_HEADER 20 /* Max prefix of record-block */
435 #define MI_BLOCK_INFO_HEADER_LENGTH 20
436 #define MI_DYN_DELETE_BLOCK_HEADER 20 /* length of delete-block-header */
437 #define MI_DYN_MAX_BLOCK_LENGTH ((1L << 24)-4L)
438 #define MI_DYN_MAX_ROW_LENGTH (MI_DYN_MAX_BLOCK_LENGTH - MI_SPLIT_LENGTH)
439 #define MI_DYN_ALIGN_SIZE 4 /* Align blocks on this */
440 #define MI_MAX_DYN_HEADER_BYTE 13 /* max header byte for dynamic rows */
441 #define MI_MAX_BLOCK_LENGTH ((((ulong) 1 << 24)-1) & (~ (ulong) (MI_DYN_ALIGN_SIZE-1)))
442 #define MI_REC_BUFF_OFFSET ALIGN_SIZE(MI_DYN_DELETE_BLOCK_HEADER+sizeof(uint32))
443 
444 
445 #define PACK_TYPE_SELECTED 1 /* Bits in field->pack_type */
446 #define PACK_TYPE_SPACE_FIELDS 2
447 #define PACK_TYPE_ZERO_FILL 4
448 #define MI_FOUND_WRONG_KEY 32738 /* Impossible value from ha_key_cmp */
449 
450 #define MI_MAX_KEY_BLOCK_SIZE (MI_MAX_KEY_BLOCK_LENGTH/MI_MIN_KEY_BLOCK_LENGTH)
451 #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))
452 #define MI_MAX_KEYPTR_SIZE 5 /* For calculating block lengths */
453 #define MI_MIN_KEYBLOCK_LENGTH 50 /* When to split delete blocks */
454 
455 #define MI_MIN_SIZE_BULK_INSERT_TREE 16384 /* this is per key */
456 #define MI_MIN_ROWS_TO_USE_BULK_INSERT 100
457 #define MI_MIN_ROWS_TO_DISABLE_INDEXES 100
458 #define MI_MIN_ROWS_TO_USE_WRITE_CACHE 10
459 
460 /* The UNIQUE check is done with a hashed long key */
461 
462 #define MI_UNIQUE_HASH_TYPE HA_KEYTYPE_ULONG_INT
463 #define mi_unique_store(A,B) mi_int4store((A),(B))
464 
465 extern mysql_mutex_t THR_LOCK_myisam;
466 
467 #if defined(DONT_USE_RW_LOCKS)
468 #define mysql_rwlock_wrlock(A) {}
469 #define mysql_rwlock_rdlock(A) {}
470 #define mysql_rwlock_unlock(A) {}
471 #endif
472 
473  /* Some extern variables */
474 
475 extern LIST *myisam_open_list;
476 extern uchar myisam_file_magic[], myisam_pack_file_magic[];
477 extern uint myisam_read_vec[], myisam_readnext_vec[];
478 extern uint myisam_quick_table_bits;
479 extern File myisam_log_file;
480 extern ulong myisam_pid;
481 
482  /* This is used by _mi_calc_xxx_key_length och _mi_store_key */
483 
484 typedef struct st_mi_s_param
485 {
486  uint ref_length,key_length,
487  n_ref_length,
488  n_length,
489  totlength,
490  part_of_prev_key,prev_length,pack_marker;
491  uchar *key, *prev_key,*next_key_pos;
492  my_bool store_not_null;
493 } MI_KEY_PARAM;
494 
495  /* Prototypes for intern functions */
496 
497 extern int _mi_read_dynamic_record(MI_INFO *info,my_off_t filepos,uchar *buf);
498 extern int _mi_write_dynamic_record(MI_INFO*, const uchar*);
499 extern int _mi_update_dynamic_record(MI_INFO*, my_off_t, const uchar*);
500 extern int _mi_delete_dynamic_record(MI_INFO *info);
501 extern int _mi_cmp_dynamic_record(MI_INFO *info,const uchar *record);
502 extern int _mi_read_rnd_dynamic_record(MI_INFO *, uchar *,my_off_t, my_bool);
503 extern int _mi_write_blob_record(MI_INFO*, const uchar*);
504 extern int _mi_update_blob_record(MI_INFO*, my_off_t, const uchar*);
505 extern int _mi_read_static_record(MI_INFO *info, my_off_t filepos,uchar *buf);
506 extern int _mi_write_static_record(MI_INFO*, const uchar*);
507 extern int _mi_update_static_record(MI_INFO*, my_off_t, const uchar*);
508 extern int _mi_delete_static_record(MI_INFO *info);
509 extern int _mi_cmp_static_record(MI_INFO *info,const uchar *record);
510 extern int _mi_read_rnd_static_record(MI_INFO*, uchar *,my_off_t, my_bool);
511 extern int _mi_ck_write(MI_INFO *info,uint keynr,uchar *key,uint length);
512 extern int _mi_ck_real_write_btree(MI_INFO *info, MI_KEYDEF *keyinfo,
513  uchar *key, uint key_length,
514  my_off_t *root, uint comp_flag);
515 extern int _mi_enlarge_root(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key, my_off_t *root);
516 extern int _mi_insert(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,
517  uchar *anc_buff,uchar *key_pos,uchar *key_buff,
518  uchar *father_buff, uchar *father_keypos,
519  my_off_t father_page, my_bool insert_last);
520 extern int _mi_split_page(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,
521  uchar *buff,uchar *key_buff, my_bool insert_last);
522 extern uchar *_mi_find_half_pos(uint nod_flag,MI_KEYDEF *keyinfo,uchar *page,
523  uchar *key,uint *return_key_length,
524  uchar **after_key);
525 extern int _mi_calc_static_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
526  uchar *key_pos, uchar *org_key,
527  uchar *key_buff,
528  uchar *key, MI_KEY_PARAM *s_temp);
529 extern int _mi_calc_var_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
530  uchar *key_pos, uchar *org_key,
531  uchar *key_buff,
532  uchar *key, MI_KEY_PARAM *s_temp);
533 extern int _mi_calc_var_pack_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
534  uchar *key_pos, uchar *org_key,
535  uchar *prev_key,
536  uchar *key, MI_KEY_PARAM *s_temp);
537 extern int _mi_calc_bin_pack_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
538  uchar *key_pos,uchar *org_key,
539  uchar *prev_key,
540  uchar *key, MI_KEY_PARAM *s_temp);
541 void _mi_store_static_key(MI_KEYDEF *keyinfo, uchar *key_pos,
542  MI_KEY_PARAM *s_temp);
543 void _mi_store_var_pack_key(MI_KEYDEF *keyinfo, uchar *key_pos,
544  MI_KEY_PARAM *s_temp);
545 void _mi_store_bin_pack_key(MI_KEYDEF *keyinfo, uchar *key_pos,
546  MI_KEY_PARAM *s_temp);
547 
548 extern int _mi_ck_delete(MI_INFO *info,uint keynr,uchar *key,uint key_length);
549 extern int _mi_readinfo(MI_INFO *info,int lock_flag,int check_keybuffer);
550 extern int _mi_writeinfo(MI_INFO *info,uint options);
551 extern int _mi_test_if_changed(MI_INFO *info);
552 extern int _mi_mark_file_changed(MI_INFO *info);
553 extern int _mi_decrement_open_count(MI_INFO *info);
554 extern int _mi_check_index(MI_INFO *info,int inx);
555 extern int _mi_search(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,uint key_len,
556  uint nextflag,my_off_t pos);
557 extern int _mi_bin_search(struct st_myisam_info *info,MI_KEYDEF *keyinfo,
558  uchar *page,uchar *key,uint key_len,uint comp_flag,
559  uchar * *ret_pos,uchar *buff, my_bool *was_last_key);
560 extern int _mi_seq_search(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *page,
561  uchar *key,uint key_len,uint comp_flag,
562  uchar **ret_pos,uchar *buff, my_bool *was_last_key);
563 extern int _mi_prefix_search(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *page,
564  uchar *key,uint key_len,uint comp_flag,
565  uchar **ret_pos,uchar *buff, my_bool *was_last_key);
566 extern my_off_t _mi_kpos(uint nod_flag,uchar *after_key);
567 extern void _mi_kpointer(MI_INFO *info,uchar *buff,my_off_t pos);
568 extern my_off_t _mi_dpos(MI_INFO *info, uint nod_flag,uchar *after_key);
569 extern my_off_t _mi_rec_pos(MYISAM_SHARE *info, uchar *ptr);
570 extern void _mi_dpointer(MI_INFO *info, uchar *buff,my_off_t pos);
571 extern int ha_key_cmp(HA_KEYSEG *keyseg, uchar *a,uchar *b,
572  uint key_length,uint nextflag,uint *diff_length);
573 extern uint _mi_get_static_key(MI_KEYDEF *keyinfo,uint nod_flag,uchar * *page,
574  uchar *key);
575 extern uint _mi_get_pack_key(MI_KEYDEF *keyinfo,uint nod_flag,uchar * *page,
576  uchar *key);
577 extern uint _mi_get_binary_pack_key(MI_KEYDEF *keyinfo, uint nod_flag,
578  uchar **page_pos, uchar *key);
579 extern uchar *_mi_get_last_key(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *keypos,
580  uchar *lastkey,uchar *endpos,
581  uint *return_key_length);
582 extern uchar *_mi_get_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
583  uchar *key, uchar *keypos, uint *return_key_length);
584 extern uint _mi_keylength(MI_KEYDEF *keyinfo,uchar *key);
585 extern uint _mi_keylength_part(MI_KEYDEF *keyinfo, register uchar *key,
586  HA_KEYSEG *end);
587 extern uchar *_mi_move_key(MI_KEYDEF *keyinfo,uchar *to,uchar *from);
588 extern int _mi_search_next(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,
589  uint key_length,uint nextflag,my_off_t pos);
590 extern int _mi_search_first(MI_INFO *info,MI_KEYDEF *keyinfo,my_off_t pos);
591 extern int _mi_search_last(MI_INFO *info,MI_KEYDEF *keyinfo,my_off_t pos);
592 extern uchar *_mi_fetch_keypage(MI_INFO *info,MI_KEYDEF *keyinfo,my_off_t page,
593  int level,uchar *buff,int return_buffer);
594 extern int _mi_write_keypage(MI_INFO *info,MI_KEYDEF *keyinfo,my_off_t page,
595  int level, uchar *buff);
596 extern int _mi_dispose(MI_INFO *info,MI_KEYDEF *keyinfo,my_off_t pos,
597  int level);
598 extern my_off_t _mi_new(MI_INFO *info,MI_KEYDEF *keyinfo,int level);
599 extern uint _mi_make_key(MI_INFO *info,uint keynr,uchar *key,
600  const uchar *record,my_off_t filepos);
601 extern uint _mi_pack_key(register MI_INFO *info, uint keynr, uchar *key,
602  uchar *old, key_part_map keypart_map,
603  HA_KEYSEG **last_used_keyseg);
604 extern int _mi_read_key_record(MI_INFO *info,my_off_t filepos,uchar *buf);
605 extern int _mi_read_cache(IO_CACHE *info,uchar *buff,my_off_t pos,
606  uint length,int re_read_if_possibly);
607 extern ulonglong retrieve_auto_increment(MI_INFO *info,const uchar *record);
608 
609 extern uchar *mi_alloc_rec_buff(MI_INFO *,ulong, uchar**);
610 #define mi_get_rec_buff_ptr(info,buf) \
611  ((((info)->s->options & HA_OPTION_PACK_RECORD) && (buf)) ? \
612  (buf) - MI_REC_BUFF_OFFSET : (buf))
613 #define mi_get_rec_buff_len(info,buf) \
614  (*((uint32 *)(mi_get_rec_buff_ptr(info,buf))))
615 
616 extern ulong _mi_rec_unpack(MI_INFO *info,uchar *to,uchar *from,
617  ulong reclength);
618 extern my_bool _mi_rec_check(MI_INFO *info,const uchar *record, uchar *packpos,
619  ulong packed_length, my_bool with_checkum);
620 extern int _mi_write_part_record(MI_INFO *info,my_off_t filepos,ulong length,
621  my_off_t next_filepos,uchar **record,
622  ulong *reclength,int *flag);
623 extern void _mi_print_key(FILE *stream,HA_KEYSEG *keyseg,const uchar *key,
624  uint length);
625 extern my_bool _mi_read_pack_info(MI_INFO *info,pbool fix_keys);
626 extern int _mi_read_pack_record(MI_INFO *info,my_off_t filepos,uchar *buf);
627 extern int _mi_read_rnd_pack_record(MI_INFO*, uchar *,my_off_t, my_bool);
628 extern int _mi_pack_rec_unpack(MI_INFO *info, MI_BIT_BUFF *bit_buff,
629  uchar *to, uchar *from, ulong reclength);
630 extern ulonglong mi_safe_mul(ulonglong a,ulonglong b);
631 extern int _mi_ft_update(MI_INFO *info, uint keynr, uchar *keybuf,
632  const uchar *oldrec, const uchar *newrec, my_off_t pos);
633 
634 struct st_sort_info;
635 
636 
637 typedef struct st_mi_block_info { /* Parameter to _mi_get_block_info */
638  uchar header[MI_BLOCK_INFO_HEADER_LENGTH];
639  ulong rec_len;
640  ulong data_len;
641  ulong block_len;
642  ulong blob_len;
643  my_off_t filepos;
644  my_off_t next_filepos;
645  my_off_t prev_filepos;
646  uint second_read;
647  uint offset;
648 } MI_BLOCK_INFO;
649 
650  /* bits in return from _mi_get_block_info */
651 
652 #define BLOCK_FIRST 1
653 #define BLOCK_LAST 2
654 #define BLOCK_DELETED 4
655 #define BLOCK_ERROR 8 /* Wrong data */
656 #define BLOCK_SYNC_ERROR 16 /* Right data at wrong place */
657 #define BLOCK_FATAL_ERROR 32 /* hardware-error */
658 
659 #define NEED_MEM ((uint) 10*4*(IO_SIZE+32)+32) /* Nead for recursion */
660 #define MAXERR 20
661 #define BUFFERS_WHEN_SORTING 16 /* Alloc for sort-key-tree */
662 #define WRITE_COUNT MY_HOW_OFTEN_TO_WRITE
663 #define INDEX_TMP_EXT ".TMM"
664 #define DATA_TMP_EXT ".TMD"
665 
666 #define UPDATE_TIME 1
667 #define UPDATE_STAT 2
668 #define UPDATE_SORT 4
669 #define UPDATE_AUTO_INC 8
670 #define UPDATE_OPEN_COUNT 16
671 
672 #define USE_BUFFER_INIT (((1024L*512L-MALLOC_OVERHEAD)/IO_SIZE)*IO_SIZE)
673 #define READ_BUFFER_INIT (1024L*256L-MALLOC_OVERHEAD)
674 #define SORT_BUFFER_INIT (2048L*1024L-MALLOC_OVERHEAD)
675 #define MIN_SORT_BUFFER (4096-MALLOC_OVERHEAD)
676 
677 enum myisam_log_commands {
678  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
679 };
680 
681 #define myisam_log(a,b,c,d) if (myisam_log_file >= 0) _myisam_log(a,b,c,d)
682 #define myisam_log_command(a,b,c,d,e) if (myisam_log_file >= 0) _myisam_log_command(a,b,c,d,e)
683 #define myisam_log_record(a,b,c,d,e) if (myisam_log_file >= 0) _myisam_log_record(a,b,c,d,e)
684 
685 #define fast_mi_writeinfo(INFO) if (!(INFO)->s->tot_locks) (void) _mi_writeinfo((INFO),0)
686 #define fast_mi_readinfo(INFO) ((INFO)->lock_type == F_UNLCK) && _mi_readinfo((INFO),F_RDLCK,1)
687 
688 #ifdef __cplusplus
689 extern "C" {
690 #endif
691 
692 extern uint _mi_get_block_info(MI_BLOCK_INFO *,File, my_off_t);
693 extern uint _mi_rec_pack(MI_INFO *info,uchar *to,const uchar *from);
694 extern uint _mi_pack_get_block_info(MI_INFO *myisam, MI_BIT_BUFF *bit_buff,
695  MI_BLOCK_INFO *info, uchar **rec_buff_p,
696  File file, my_off_t filepos);
697 extern void _mi_store_blob_length(uchar *pos,uint pack_length,uint length);
698 extern void _myisam_log(enum myisam_log_commands command,MI_INFO *info,
699  const uchar *buffert,uint length);
700 extern void _myisam_log_command(enum myisam_log_commands command,
701  MI_INFO *info, const uchar *buffert,
702  uint length, int result);
703 extern void _myisam_log_record(enum myisam_log_commands command,MI_INFO *info,
704  const uchar *record,my_off_t filepos,
705  int result);
706 extern void mi_report_error(int errcode, const char *file_name);
707 extern my_bool _mi_memmap_file(MI_INFO *info);
708 extern void _mi_unmap_file(MI_INFO *info);
709 extern uint save_pack_length(uint version, uchar *block_buff, ulong length);
710 extern uint read_pack_length(uint version, const uchar *buf, ulong *length);
711 extern uint calc_pack_length(uint version, ulong length);
712 extern size_t mi_mmap_pread(MI_INFO *info, uchar *Buffer,
713  size_t Count, my_off_t offset, myf MyFlags);
714 extern size_t mi_mmap_pwrite(MI_INFO *info, const uchar *Buffer,
715  size_t Count, my_off_t offset, myf MyFlags);
716 extern size_t mi_nommap_pread(MI_INFO *info, uchar *Buffer,
717  size_t Count, my_off_t offset, myf MyFlags);
718 extern size_t mi_nommap_pwrite(MI_INFO *info, const uchar *Buffer,
719  size_t Count, my_off_t offset, myf MyFlags);
720 
721 uint mi_state_info_write(File file, MI_STATE_INFO *state, uint pWrite);
722 uchar *mi_state_info_read(uchar *ptr, MI_STATE_INFO *state);
723 uint mi_state_info_read_dsk(File file, MI_STATE_INFO *state, my_bool pRead);
724 uint mi_base_info_write(File file, MI_BASE_INFO *base);
725 uchar *my_n_base_info_read(uchar *ptr, MI_BASE_INFO *base);
726 int mi_keyseg_write(File file, const HA_KEYSEG *keyseg);
727 uchar *mi_keyseg_read(uchar *ptr, HA_KEYSEG *keyseg);
728 uint mi_keydef_write(File file, MI_KEYDEF *keydef);
729 uchar *mi_keydef_read(uchar *ptr, MI_KEYDEF *keydef);
730 uint mi_uniquedef_write(File file, MI_UNIQUEDEF *keydef);
731 uchar *mi_uniquedef_read(uchar *ptr, MI_UNIQUEDEF *keydef);
732 uint mi_recinfo_write(File file, MI_COLUMNDEF *recinfo);
733 uchar *mi_recinfo_read(uchar *ptr, MI_COLUMNDEF *recinfo);
734 extern int mi_disable_indexes(MI_INFO *info);
735 extern int mi_enable_indexes(MI_INFO *info);
736 extern int mi_indexes_are_disabled(MI_INFO *info);
737 ulong _my_calc_total_blob_length(MI_INFO *info, const uchar *record);
738 ha_checksum mi_checksum(MI_INFO *info, const uchar *buf);
739 ha_checksum mi_static_checksum(MI_INFO *info, const uchar *buf);
740 my_bool mi_check_unique(MI_INFO *info, MI_UNIQUEDEF *def, uchar *record,
741  ha_checksum unique_hash, my_off_t pos);
742 ha_checksum mi_unique_hash(MI_UNIQUEDEF *def, const uchar *buf);
743 int _mi_cmp_static_unique(MI_INFO *info, MI_UNIQUEDEF *def,
744  const uchar *record, my_off_t pos);
745 int _mi_cmp_dynamic_unique(MI_INFO *info, MI_UNIQUEDEF *def,
746  const uchar *record, my_off_t pos);
747 int mi_unique_comp(MI_UNIQUEDEF *def, const uchar *a, const uchar *b,
748  my_bool null_are_equal);
749 void mi_get_status(void* param, int concurrent_insert);
750 void mi_update_status(void* param);
751 void mi_restore_status(void* param);
752 void mi_copy_status(void* to,void *from);
753 my_bool mi_check_status(void* param);
754 void mi_disable_non_unique_index(MI_INFO *info, ha_rows rows);
755 
756 extern MI_INFO *test_if_reopen(char *filename);
757 my_bool check_table_is_closed(const char *name, const char *where);
758 int mi_open_datafile(MI_INFO *info, MYISAM_SHARE *share, const char *orn_name,
759  File file_to_dup);
760 
761 int mi_open_keyfile(MYISAM_SHARE *share);
762 void mi_setup_functions(register MYISAM_SHARE *share);
763 my_bool mi_dynmap_file(MI_INFO *info, my_off_t size);
764 int mi_munmap_file(MI_INFO *info);
765 void mi_remap_file(MI_INFO *info, my_off_t size);
766 void _mi_report_crashed(MI_INFO *file, const char *message,
767  const char *sfile, uint sline);
768 
769 int mi_check_index_cond(register MI_INFO *info, uint keynr, uchar *record);
770 
771  /* Functions needed by mi_check */
772 volatile int *killed_ptr(MI_CHECK *param);
773 void mi_check_print_error(MI_CHECK *param, const char *fmt,...);
774 void mi_check_print_warning(MI_CHECK *param, const char *fmt,...);
775 void mi_check_print_info(MI_CHECK *param, const char *fmt,...);
776 int flush_pending_blocks(MI_SORT_PARAM *param);
777 int sort_ft_buf_flush(MI_SORT_PARAM *sort_param);
778 int thr_write_keys(MI_SORT_PARAM *sort_param);
779 pthread_handler_t thr_find_all_keys(void *arg);
780 int flush_blocks(MI_CHECK *param, KEY_CACHE *key_cache, File file);
781 
782 int sort_write_record(MI_SORT_PARAM *sort_param);
783 int _create_index_by_sort(MI_SORT_PARAM *info, my_bool no_messages, ulonglong);
784 
785 extern void mi_set_index_cond_func(MI_INFO *info, index_cond_func_t func,
786  void *func_arg);
787 #ifdef __cplusplus
788 }
789 #endif
790 
791 #ifdef HAVE_PSI_INTERFACE
792 C_MODE_START
793 extern PSI_mutex_key mi_key_mutex_MYISAM_SHARE_intern_lock,
794  mi_key_mutex_MI_SORT_INFO_mutex, mi_key_mutex_MI_CHECK_print_msg;
795 
796 extern PSI_rwlock_key mi_key_rwlock_MYISAM_SHARE_key_root_lock,
797  mi_key_rwlock_MYISAM_SHARE_mmap_lock;
798 
799 extern PSI_cond_key mi_key_cond_MI_SORT_INFO_cond;
800 
801 extern PSI_file_key mi_key_file_datatmp, mi_key_file_dfile, mi_key_file_kfile,
802  mi_key_file_log;
803 
804 extern PSI_thread_key mi_key_thread_find_all_keys;
805 
806 void init_myisam_psi_keys();
807 C_MODE_END
808 #endif /* HAVE_PSI_INTERFACE */
809