myisam.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. Copyright (c) 2000, 2013, Oracle and/or its affiliates.
  3. Copyright (c) 2009, 2013, Monty Program Ab.
  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. 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.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  14. /* This file should be included when using myisam_funktions */
  15. #ifndef _myisam_h
  16. #define _myisam_h
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include <my_base.h>
  21. #include <m_ctype.h>
  22. #include "keycache.h"
  23. #include "my_compare.h"
  24. #include <myisamchk.h>
  25. #include <mysql/plugin.h>
  26. #include <my_check_opt.h>
  27. /*
  28. Limit max keys according to HA_MAX_POSSIBLE_KEY; See myisamchk.h for details
  29. */
  30. #if MAX_INDEXES > HA_MAX_POSSIBLE_KEY
  31. #define MI_MAX_KEY HA_MAX_POSSIBLE_KEY /* Max allowed keys */
  32. #else
  33. #define MI_MAX_KEY MAX_INDEXES /* Max allowed keys */
  34. #endif
  35. #define MI_MAX_POSSIBLE_KEY_BUFF HA_MAX_POSSIBLE_KEY_BUFF
  36. /*
  37. The following defines can be increased if necessary.
  38. But beware the dependency of MI_MAX_POSSIBLE_KEY_BUFF and MI_MAX_KEY_LENGTH.
  39. */
  40. #define MI_MAX_KEY_LENGTH 1000 /* Max length in bytes */
  41. #define MI_MAX_KEY_SEG 16 /* Max segments for key */
  42. #define MI_NAME_IEXT ".MYI"
  43. #define MI_NAME_DEXT ".MYD"
  44. /* Possible values for myisam_block_size (must be power of 2) */
  45. #define MI_KEY_BLOCK_LENGTH 1024 /* default key block length */
  46. #define MI_MIN_KEY_BLOCK_LENGTH 1024 /* Min key block length */
  47. #define MI_MAX_KEY_BLOCK_LENGTH 16384
  48. /*
  49. In the following macros '_keyno_' is 0 .. keys-1.
  50. If there can be more keys than bits in the key_map, the highest bit
  51. is for all upper keys. They cannot be switched individually.
  52. This means that clearing of high keys is ignored, setting one high key
  53. sets all high keys.
  54. */
  55. #define MI_KEYMAP_BITS (8 * SIZEOF_LONG_LONG)
  56. #define MI_KEYMAP_HIGH_MASK (1ULL << (MI_KEYMAP_BITS - 1))
  57. #define mi_get_mask_all_keys_active(_keys_) \
  58. (((_keys_) < MI_KEYMAP_BITS) ? \
  59. ((1ULL << (_keys_)) - 1ULL) : \
  60. (~ 0ULL))
  61. #if MI_MAX_KEY > MI_KEYMAP_BITS
  62. #define mi_is_key_active(_keymap_,_keyno_) \
  63. (((_keyno_) < MI_KEYMAP_BITS) ? \
  64. MY_TEST((_keymap_) & (1ULL << (_keyno_))) : \
  65. MY_TEST((_keymap_) & MI_KEYMAP_HIGH_MASK))
  66. #define mi_set_key_active(_keymap_,_keyno_) \
  67. (_keymap_)|= (((_keyno_) < MI_KEYMAP_BITS) ? \
  68. (1ULL << (_keyno_)) : \
  69. MI_KEYMAP_HIGH_MASK)
  70. #define mi_clear_key_active(_keymap_,_keyno_) \
  71. (_keymap_)&= (((_keyno_) < MI_KEYMAP_BITS) ? \
  72. (~ (1ULL << (_keyno_))) : \
  73. (~ (0ULL)) /*ignore*/ )
  74. #else
  75. #define mi_is_key_active(_keymap_,_keyno_) \
  76. MY_TEST((_keymap_) & (1ULL << (_keyno_)))
  77. #define mi_set_key_active(_keymap_,_keyno_) \
  78. (_keymap_)|= (1ULL << (_keyno_))
  79. #define mi_clear_key_active(_keymap_,_keyno_) \
  80. (_keymap_)&= (~ (1ULL << (_keyno_)))
  81. #endif
  82. #define mi_is_any_key_active(_keymap_) \
  83. MY_TEST((_keymap_))
  84. #define mi_is_all_keys_active(_keymap_,_keys_) \
  85. ((_keymap_) == mi_get_mask_all_keys_active(_keys_))
  86. #define mi_set_all_keys_active(_keymap_,_keys_) \
  87. (_keymap_)= mi_get_mask_all_keys_active(_keys_)
  88. #define mi_clear_all_keys_active(_keymap_) \
  89. (_keymap_)= 0
  90. #define mi_intersect_keys_active(_to_,_from_) \
  91. (_to_)&= (_from_)
  92. #define mi_is_any_intersect_keys_active(_keymap1_,_keys_,_keymap2_) \
  93. ((_keymap1_) & (_keymap2_) & \
  94. mi_get_mask_all_keys_active(_keys_))
  95. #define mi_copy_keys_active(_to_,_maxkeys_,_from_) \
  96. (_to_)= (mi_get_mask_all_keys_active(_maxkeys_) & \
  97. (_from_))
  98. /* Param to/from mi_info */
  99. typedef struct st_mi_isaminfo /* Struct from h_info */
  100. {
  101. ha_rows records; /* Records in database */
  102. ha_rows deleted; /* Deleted records in database */
  103. my_off_t recpos; /* Pos for last used record */
  104. my_off_t newrecpos; /* Pos if we write new record */
  105. my_off_t dupp_key_pos; /* Position to record with dupp key */
  106. my_off_t data_file_length, /* Length of data file */
  107. max_data_file_length,
  108. index_file_length,
  109. max_index_file_length,
  110. delete_length;
  111. ulong reclength; /* Recordlength */
  112. ulong mean_reclength; /* Mean recordlength (if packed) */
  113. ulonglong auto_increment;
  114. ulonglong key_map; /* Which keys are used */
  115. char *data_file_name, *index_file_name;
  116. uint keys; /* Number of keys in use */
  117. uint options; /* HA_OPTION_... used */
  118. int errkey, /* With key was dupplicated on err */
  119. sortkey; /* clustered by this key */
  120. File filenr; /* (uniq) filenr for datafile */
  121. time_t create_time; /* When table was created */
  122. time_t check_time;
  123. time_t update_time;
  124. uint reflength;
  125. ulong record_offset;
  126. ulong *rec_per_key; /* for sql optimizing */
  127. } MI_ISAMINFO;
  128. typedef struct st_mi_create_info
  129. {
  130. const char *index_file_name, *data_file_name; /* If using symlinks */
  131. ha_rows max_rows;
  132. ha_rows reloc_rows;
  133. ulonglong auto_increment;
  134. ulonglong data_file_length;
  135. ulonglong key_file_length;
  136. uint old_options;
  137. uint16 language;
  138. my_bool with_auto_increment;
  139. } MI_CREATE_INFO;
  140. struct st_myisam_info; /* For referense */
  141. struct st_mi_isam_share;
  142. typedef struct st_myisam_info MI_INFO;
  143. struct st_mi_s_param;
  144. typedef struct st_mi_keydef /* Key definition with open & info */
  145. {
  146. struct st_mi_isam_share *share; /* Pointer to base (set in mi_open) */
  147. uint16 keysegs; /* Number of key-segment */
  148. uint16 flag; /* NOSAME, PACK_USED */
  149. uint8 key_alg; /* BTREE, RTREE */
  150. uint16 block_length; /* Length of keyblock (auto) */
  151. uint16 underflow_block_length; /* When to execute underflow */
  152. uint16 keylength; /* Tot length of keyparts (auto) */
  153. uint16 minlength; /* min length of (packed) key (auto) */
  154. uint16 maxlength; /* max length of (packed) key (auto) */
  155. uint16 block_size_index; /* block_size (auto) */
  156. uint32 version; /* For concurrent read/write */
  157. uint32 ftkey_nr; /* full-text index number */
  158. HA_KEYSEG *seg,*end;
  159. struct st_mysql_ftparser *parser; /* Fulltext [pre]parser */
  160. int (*bin_search)(struct st_myisam_info *info,struct st_mi_keydef *keyinfo,
  161. uchar *page,uchar *key,
  162. uint key_len,uint comp_flag,uchar * *ret_pos,
  163. uchar *buff, my_bool *was_last_key);
  164. uint (*get_key)(struct st_mi_keydef *keyinfo,uint nod_flag,uchar * *page,
  165. uchar *key);
  166. int (*pack_key)(struct st_mi_keydef *keyinfo,uint nod_flag,uchar *next_key,
  167. uchar *org_key, uchar *prev_key, uchar *key,
  168. struct st_mi_s_param *s_temp);
  169. void (*store_key)(struct st_mi_keydef *keyinfo, uchar *key_pos,
  170. struct st_mi_s_param *s_temp);
  171. int (*ck_insert)(struct st_myisam_info *inf, uint k_nr, uchar *k, uint klen);
  172. int (*ck_delete)(struct st_myisam_info *inf, uint k_nr, uchar *k, uint klen);
  173. } MI_KEYDEF;
  174. #define MI_UNIQUE_HASH_LENGTH 4
  175. typedef struct st_unique_def /* Segment definition of unique */
  176. {
  177. uint16 keysegs; /* Number of key-segment */
  178. uchar key; /* Mapped to which key */
  179. uint8 null_are_equal;
  180. HA_KEYSEG *seg,*end;
  181. } MI_UNIQUEDEF;
  182. typedef struct st_mi_decode_tree /* Decode huff-table */
  183. {
  184. uint16 *table;
  185. uint quick_table_bits;
  186. uchar *intervalls;
  187. } MI_DECODE_TREE;
  188. struct st_mi_bit_buff;
  189. /*
  190. Note that null markers should always be first in a row !
  191. When creating a column, one should only specify:
  192. type, length, null_bit and null_pos
  193. */
  194. typedef struct st_columndef /* column information */
  195. {
  196. enum en_fieldtype type;
  197. uint16 length; /* length of field */
  198. uint32 offset; /* Offset to position in row */
  199. uint8 null_bit; /* If column may be 0 */
  200. uint16 null_pos; /* position for null marker */
  201. #ifndef NOT_PACKED_DATABASES
  202. void (*unpack)(struct st_columndef *rec,struct st_mi_bit_buff *buff,
  203. uchar *start,uchar *end);
  204. enum en_fieldtype base_type;
  205. uint space_length_bits,pack_type;
  206. MI_DECODE_TREE *huff_tree;
  207. #endif
  208. } MI_COLUMNDEF;
  209. extern char * myisam_log_filename; /* Name of logfile */
  210. extern ulong myisam_block_size;
  211. extern ulong myisam_concurrent_insert;
  212. extern my_bool myisam_flush,myisam_delay_key_write,myisam_single_user;
  213. extern my_off_t myisam_max_temp_length;
  214. extern ulong myisam_data_pointer_size;
  215. /* usually used to check if a symlink points into the mysql data home */
  216. /* which is normally forbidden */
  217. extern int (*myisam_test_invalid_symlink)(const char *filename);
  218. extern ulonglong myisam_mmap_size, myisam_mmap_used;
  219. extern mysql_mutex_t THR_LOCK_myisam_mmap;
  220. /* Prototypes for myisam-functions */
  221. extern int mi_close(struct st_myisam_info *file);
  222. extern int mi_delete(struct st_myisam_info *file,const uchar *buff);
  223. extern struct st_myisam_info *mi_open(const char *name,int mode,
  224. uint wait_if_locked);
  225. extern int mi_panic(enum ha_panic_function function);
  226. extern int mi_rfirst(struct st_myisam_info *file,uchar *buf,int inx);
  227. extern int mi_rkey(MI_INFO *info, uchar *buf, int inx, const uchar *key,
  228. key_part_map keypart_map, enum ha_rkey_function search_flag);
  229. extern int mi_rlast(struct st_myisam_info *file,uchar *buf,int inx);
  230. extern int mi_rnext(struct st_myisam_info *file,uchar *buf,int inx);
  231. extern int mi_rnext_same(struct st_myisam_info *info, uchar *buf);
  232. extern int mi_rprev(struct st_myisam_info *file,uchar *buf,int inx);
  233. extern int mi_rrnd(struct st_myisam_info *file,uchar *buf, my_off_t pos);
  234. extern int mi_scan_init(struct st_myisam_info *file);
  235. extern int mi_scan(struct st_myisam_info *file,uchar *buf);
  236. extern int mi_rsame(struct st_myisam_info *file,uchar *record,int inx);
  237. extern int mi_rsame_with_pos(struct st_myisam_info *file,uchar *record,
  238. int inx, my_off_t pos);
  239. extern int mi_update(struct st_myisam_info *file,const uchar *old,
  240. uchar *new_record);
  241. extern int mi_write(struct st_myisam_info *file,uchar *buff);
  242. extern my_off_t mi_position(struct st_myisam_info *file);
  243. extern int mi_status(struct st_myisam_info *info, MI_ISAMINFO *x, uint flag);
  244. extern int mi_lock_database(struct st_myisam_info *file,int lock_type);
  245. extern int mi_create(const char *name,uint keys,MI_KEYDEF *keydef,
  246. uint columns, MI_COLUMNDEF *columndef,
  247. uint uniques, MI_UNIQUEDEF *uniquedef,
  248. MI_CREATE_INFO *create_info, uint flags);
  249. extern int mi_delete_table(const char *name);
  250. extern int mi_rename(const char *from, const char *to);
  251. extern int mi_extra(struct st_myisam_info *file,
  252. enum ha_extra_function function,
  253. void *extra_arg);
  254. extern int mi_reset(struct st_myisam_info *file);
  255. extern ha_rows mi_records_in_range(MI_INFO *info,int inx,
  256. key_range *min_key, key_range *max_key);
  257. extern int mi_log(int activate_log);
  258. extern int mi_is_changed(struct st_myisam_info *info);
  259. extern int mi_delete_all_rows(struct st_myisam_info *info);
  260. extern ulong _mi_calc_blob_length(uint length , const uchar *pos);
  261. extern uint mi_get_pointer_length(ulonglong file_length, uint def);
  262. extern int mi_make_backup_of_index(struct st_myisam_info *info,
  263. time_t backup_time, myf flags);
  264. #define myisam_max_key_length() HA_MAX_KEY_LENGTH
  265. #define myisam_max_key_segments() HA_MAX_KEY_SEG
  266. #define MEMMAP_EXTRA_MARGIN 7 /* Write this as a suffix for mmap file */
  267. /* this is used to pass to mysql_myisamchk_table */
  268. #define MYISAMCHK_REPAIR 1 /* equivalent to myisamchk -r */
  269. #define MYISAMCHK_VERIFY 2 /* Verify, run repair if failure */
  270. typedef uint mi_bit_type;
  271. typedef struct st_mi_bit_buff
  272. { /* Used for packing of record */
  273. mi_bit_type current_byte;
  274. uint bits;
  275. uchar *pos, *end, *blob_pos, *blob_end;
  276. uint error;
  277. } MI_BIT_BUFF;
  278. typedef struct st_sort_info
  279. {
  280. /* sync things */
  281. mysql_mutex_t mutex;
  282. mysql_cond_t cond;
  283. MI_INFO *info;
  284. HA_CHECK *param;
  285. uchar *buff;
  286. SORT_KEY_BLOCKS *key_block, *key_block_end;
  287. SORT_FT_BUF *ft_buf;
  288. my_off_t filelength, dupp, buff_length;
  289. ha_rows max_records;
  290. uint current_key, total_keys;
  291. volatile uint got_error;
  292. uint threads_running;
  293. myf myf_rw;
  294. enum data_file_type new_data_file_type;
  295. } MI_SORT_INFO;
  296. typedef struct st_mi_sort_param
  297. {
  298. pthread_t thr;
  299. IO_CACHE read_cache, tempfile, tempfile_for_exceptions;
  300. DYNAMIC_ARRAY buffpek;
  301. MI_BIT_BUFF bit_buff; /* For parallel repair of packrec. */
  302. MI_KEYDEF *keyinfo;
  303. MI_SORT_INFO *sort_info;
  304. HA_KEYSEG *seg;
  305. uchar **sort_keys;
  306. uchar *rec_buff;
  307. void *wordlist, *wordptr;
  308. MEM_ROOT wordroot;
  309. uchar *record;
  310. MY_TMPDIR *tmpdir;
  311. /*
  312. The next two are used to collect statistics, see update_key_parts for
  313. description.
  314. */
  315. ulonglong unique[HA_MAX_KEY_SEG+1];
  316. ulonglong notnull[HA_MAX_KEY_SEG+1];
  317. my_off_t pos,max_pos,filepos,start_recpos;
  318. uint key, key_length,real_key_length;
  319. uint maxbuffers, find_length;
  320. ulonglong sortbuff_size;
  321. ha_rows keys;
  322. my_bool fix_datafile, master;
  323. my_bool calc_checksum; /* calculate table checksum */
  324. int (*key_cmp)(struct st_mi_sort_param *, const void *, const void *);
  325. int (*key_read)(struct st_mi_sort_param *,void *);
  326. int (*key_write)(struct st_mi_sort_param *, const void *);
  327. void (*lock_in_memory)(HA_CHECK *);
  328. int (*write_keys)(struct st_mi_sort_param *, register uchar **,
  329. ulonglong , struct st_buffpek *, IO_CACHE *);
  330. my_off_t (*read_to_buffer)(IO_CACHE *,struct st_buffpek *, uint);
  331. int (*write_key)(struct st_mi_sort_param *, IO_CACHE *,uchar *,
  332. uint, ulonglong);
  333. } MI_SORT_PARAM;
  334. /* functions in mi_check */
  335. void myisamchk_init(HA_CHECK *param);
  336. int chk_status(HA_CHECK *param, MI_INFO *info);
  337. int chk_del(HA_CHECK *param, register MI_INFO *info, ulonglong test_flag);
  338. int chk_size(HA_CHECK *param, MI_INFO *info);
  339. int chk_key(HA_CHECK *param, MI_INFO *info);
  340. int chk_data_link(HA_CHECK *param, MI_INFO *info, my_bool extend);
  341. int mi_repair(HA_CHECK *param, register MI_INFO *info,
  342. char * name, int rep_quick);
  343. int mi_sort_index(HA_CHECK *param, register MI_INFO *info, char * name);
  344. int mi_repair_by_sort(HA_CHECK *param, register MI_INFO *info,
  345. const char * name, int rep_quick);
  346. int mi_repair_parallel(HA_CHECK *param, register MI_INFO *info,
  347. const char * name, int rep_quick);
  348. int change_to_newfile(const char * filename, const char * old_ext,
  349. const char * new_ext, time_t backup_time, myf myflags);
  350. int lock_file(HA_CHECK *param, File file, my_off_t start, int lock_type,
  351. const char *filetype, const char *filename);
  352. void lock_memory(HA_CHECK *param);
  353. void update_auto_increment_key(HA_CHECK *param, MI_INFO *info,
  354. my_bool repair);
  355. int update_state_info(HA_CHECK *param, MI_INFO *info,uint update);
  356. void update_key_parts(MI_KEYDEF *keyinfo, ulong *rec_per_key_part,
  357. ulonglong *unique, ulonglong *notnull,
  358. ulonglong records);
  359. int filecopy(HA_CHECK *param, File to,File from,my_off_t start,
  360. my_off_t length, const char *type);
  361. int movepoint(MI_INFO *info,uchar *record,my_off_t oldpos,
  362. my_off_t newpos, uint prot_key);
  363. int write_data_suffix(MI_SORT_INFO *sort_info, my_bool fix_datafile);
  364. int test_if_almost_full(MI_INFO *info);
  365. int recreate_table(HA_CHECK *param, MI_INFO **org_info, char *filename);
  366. void mi_disable_non_unique_index(MI_INFO *info, ha_rows rows);
  367. my_bool mi_test_if_sort_rep(MI_INFO *info, ha_rows rows, ulonglong key_map,
  368. my_bool force);
  369. int mi_init_bulk_insert(MI_INFO *info, size_t cache_size, ha_rows rows);
  370. void mi_flush_bulk_insert(MI_INFO *info, uint inx);
  371. void mi_end_bulk_insert(MI_INFO *info);
  372. int mi_assign_to_key_cache(MI_INFO *info, ulonglong key_map,
  373. KEY_CACHE *key_cache);
  374. void mi_change_key_cache(KEY_CACHE *old_key_cache,
  375. KEY_CACHE *new_key_cache);
  376. int mi_preload(MI_INFO *info, ulonglong key_map, my_bool ignore_leaves);
  377. int write_data_suffix(MI_SORT_INFO *sort_info, my_bool fix_datafile);
  378. int flush_pending_blocks(MI_SORT_PARAM *param);
  379. int sort_ft_buf_flush(MI_SORT_PARAM *sort_param);
  380. int thr_write_keys(MI_SORT_PARAM *sort_param);
  381. int sort_write_record(MI_SORT_PARAM *sort_param);
  382. int _create_index_by_sort(MI_SORT_PARAM *info,my_bool no_messages, ulonglong);
  383. #ifdef __cplusplus
  384. }
  385. #endif
  386. #endif