maria.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* Copyright (C) 2006-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
  2. Copyright (c) 2009, 2013, Monty Program 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; either version 2 of the License, or
  6. (at your option) any later version.
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  14. /* This file should be included when using maria functions */
  15. #ifndef _maria_h
  16. #define _maria_h
  17. #include <my_base.h>
  18. #include <m_ctype.h>
  19. #include "my_compare.h"
  20. #include "ft_global.h"
  21. #include <myisamchk.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define MARIA_CANNOT_ROLLBACK
  26. /*
  27. Limit max keys according to HA_MAX_POSSIBLE_KEY; See myisamchk.h for details
  28. */
  29. #if MAX_INDEXES > HA_MAX_POSSIBLE_KEY
  30. #define MARIA_MAX_KEY HA_MAX_POSSIBLE_KEY /* Max allowed keys */
  31. #else
  32. #define MARIA_MAX_KEY MAX_INDEXES /* Max allowed keys */
  33. #endif
  34. #define MARIA_NAME_IEXT ".MAI"
  35. #define MARIA_NAME_DEXT ".MAD"
  36. /* Max extra space to use when sorting keys */
  37. #define MARIA_MAX_TEMP_LENGTH (2*1024L*1024L*1024L)
  38. /* Possible values for maria_block_size (must be power of 2) */
  39. #define MARIA_KEY_BLOCK_LENGTH 8192 /* default key block length */
  40. #define MARIA_MIN_KEY_BLOCK_LENGTH 1024 /* Min key block length */
  41. #define MARIA_MAX_KEY_BLOCK_LENGTH 32768
  42. /* Minimal page cache when we only want to be able to scan a table */
  43. #define MARIA_MIN_PAGE_CACHE_SIZE (8192L*16L)
  44. /*
  45. In the following macros '_keyno_' is 0 .. keys-1.
  46. If there can be more keys than bits in the key_map, the highest bit
  47. is for all upper keys. They cannot be switched individually.
  48. This means that clearing of high keys is ignored, setting one high key
  49. sets all high keys.
  50. */
  51. #define MARIA_KEYMAP_BITS (8 * SIZEOF_LONG_LONG)
  52. #define MARIA_KEYMAP_HIGH_MASK (1ULL << (MARIA_KEYMAP_BITS - 1))
  53. #define maria_get_mask_all_keys_active(_keys_) \
  54. (((_keys_) < MARIA_KEYMAP_BITS) ? \
  55. ((1ULL << (_keys_)) - 1ULL) : \
  56. (~ 0ULL))
  57. #if MARIA_MAX_KEY > MARIA_KEYMAP_BITS
  58. #define maria_is_key_active(_keymap_,_keyno_) \
  59. (((_keyno_) < MARIA_KEYMAP_BITS) ? \
  60. MY_TEST((_keymap_) & (1ULL << (_keyno_))) : \
  61. MY_TEST((_keymap_) & MARIA_KEYMAP_HIGH_MASK))
  62. #define maria_set_key_active(_keymap_,_keyno_) \
  63. (_keymap_)|= (((_keyno_) < MARIA_KEYMAP_BITS) ? \
  64. (1ULL << (_keyno_)) : \
  65. MARIA_KEYMAP_HIGH_MASK)
  66. #define maria_clear_key_active(_keymap_,_keyno_) \
  67. (_keymap_)&= (((_keyno_) < MARIA_KEYMAP_BITS) ? \
  68. (~ (1ULL << (_keyno_))) : \
  69. (~ (0ULL)) /*ignore*/ )
  70. #else
  71. #define maria_is_key_active(_keymap_,_keyno_) \
  72. MY_TEST((_keymap_) & (1ULL << (_keyno_)))
  73. #define maria_set_key_active(_keymap_,_keyno_) \
  74. (_keymap_)|= (1ULL << (_keyno_))
  75. #define maria_clear_key_active(_keymap_,_keyno_) \
  76. (_keymap_)&= (~ (1ULL << (_keyno_)))
  77. #endif
  78. #define maria_is_any_key_active(_keymap_) \
  79. MY_TEST((_keymap_))
  80. #define maria_is_all_keys_active(_keymap_,_keys_) \
  81. ((_keymap_) == maria_get_mask_all_keys_active(_keys_))
  82. #define maria_set_all_keys_active(_keymap_,_keys_) \
  83. (_keymap_)= maria_get_mask_all_keys_active(_keys_)
  84. #define maria_clear_all_keys_active(_keymap_) \
  85. (_keymap_)= 0
  86. #define maria_intersect_keys_active(_to_,_from_) \
  87. (_to_)&= (_from_)
  88. #define maria_is_any_intersect_keys_active(_keymap1_,_keys_,_keymap2_) \
  89. ((_keymap1_) & (_keymap2_) & \
  90. maria_get_mask_all_keys_active(_keys_))
  91. #define maria_copy_keys_active(_to_,_maxkeys_,_from_) \
  92. (_to_)= (maria_get_mask_all_keys_active(_maxkeys_) & \
  93. (_from_))
  94. /* Param to/from maria_info */
  95. typedef ulonglong MARIA_RECORD_POS;
  96. typedef struct st_maria_info
  97. {
  98. ha_rows records; /* Records in database */
  99. ha_rows deleted; /* Deleted records in database */
  100. MARIA_RECORD_POS recpos; /* Pos for last used record */
  101. MARIA_RECORD_POS newrecpos; /* Pos if we write new record */
  102. MARIA_RECORD_POS dup_key_pos; /* Position to record with dup key */
  103. my_off_t data_file_length; /* Length of data file */
  104. my_off_t max_data_file_length, index_file_length;
  105. my_off_t max_index_file_length, delete_length;
  106. ulonglong auto_increment;
  107. ulonglong key_map; /* Which keys are used */
  108. time_t create_time; /* When table was created */
  109. time_t check_time;
  110. time_t update_time;
  111. ulong record_offset;
  112. double *rec_per_key; /* for sql optimizing */
  113. ulong reclength; /* Recordlength */
  114. ulong mean_reclength; /* Mean recordlength (if packed) */
  115. char *data_file_name, *index_file_name;
  116. enum data_file_type data_file_type;
  117. uint keys; /* Number of keys in use */
  118. uint options; /* HA_OPTION_... used */
  119. uint reflength;
  120. int errkey, /* With key was dupplicated on err */
  121. sortkey; /* clustered by this key */
  122. File filenr; /* (uniq) filenr for datafile */
  123. } MARIA_INFO;
  124. typedef struct st_maria_create_info
  125. {
  126. const char *index_file_name, *data_file_name; /* If using symlinks */
  127. ha_rows max_rows;
  128. ha_rows reloc_rows;
  129. ulonglong auto_increment;
  130. ulonglong data_file_length;
  131. ulonglong key_file_length;
  132. /* Size of null bitmap at start of row */
  133. uint null_bytes;
  134. uint old_options;
  135. enum data_file_type org_data_file_type;
  136. uint8 language;
  137. my_bool with_auto_increment, transactional;
  138. } MARIA_CREATE_INFO;
  139. struct st_maria_share;
  140. struct st_maria_handler; /* For referense */
  141. typedef struct st_maria_handler MARIA_HA;
  142. struct st_maria_s_param;
  143. struct st_maria_keydef;
  144. struct st_maria_page;
  145. typedef struct st_maria_key /* Internal info about a key */
  146. {
  147. uchar *data; /* Data for key */
  148. struct st_maria_keydef *keyinfo; /* Definition for key */
  149. uint data_length; /* Length of key data */
  150. uint ref_length; /* record ref + transid */
  151. uint32 flag; /* 0 or SEARCH_PART_KEY */
  152. } MARIA_KEY;
  153. typedef struct st_maria_keydef /* Key definition with open & info */
  154. {
  155. struct st_maria_share *share; /* Pointer to base (set in open) */
  156. mysql_rwlock_t root_lock; /* locking of tree */
  157. uint16 keysegs; /* Number of key-segment */
  158. uint16 flag; /* NOSAME, PACK_USED */
  159. uint8 key_alg; /* BTREE, RTREE */
  160. uint8 key_nr; /* key number (auto) */
  161. uint16 block_length; /* Length of keyblock (auto) */
  162. uint16 underflow_block_length; /* When to execute underflow */
  163. uint16 keylength; /* Tot length of keyparts (auto) */
  164. uint16 minlength; /* min length of (packed) key (auto) */
  165. uint16 maxlength; /* max length of (packed) key (auto) */
  166. uint32 write_comp_flag; /* compare flag for write key (auto) */
  167. uint32 version; /* For concurrent read/write */
  168. uint32 ftkey_nr; /* full-text index number */
  169. HA_KEYSEG *seg, *end;
  170. struct st_mysql_ftparser *parser; /* Fulltext [pre]parser */
  171. int (*bin_search)(const MARIA_KEY *key, const struct st_maria_page *page,
  172. uint32 comp_flag, uchar **ret_pos, uchar *buff,
  173. my_bool *was_last_key);
  174. uint (*get_key)(MARIA_KEY *key, uint page_flag, uint nod_flag,
  175. uchar **page);
  176. uchar *(*skip_key)(MARIA_KEY *key, uint page_flag, uint nod_flag,
  177. uchar *page);
  178. int (*pack_key)(const MARIA_KEY *key, uint nod_flag,
  179. uchar *next_key, uchar *org_key, uchar *prev_key,
  180. struct st_maria_s_param *s_temp);
  181. void (*store_key)(struct st_maria_keydef *keyinfo, uchar *key_pos,
  182. struct st_maria_s_param *s_temp);
  183. my_bool (*ck_insert)(MARIA_HA *inf, MARIA_KEY *key);
  184. my_bool (*ck_delete)(MARIA_HA *inf, MARIA_KEY *klen);
  185. MARIA_KEY *(*make_key)(MARIA_HA *info, MARIA_KEY *int_key, uint keynr,
  186. uchar *key, const uchar *record,
  187. MARIA_RECORD_POS filepos, ulonglong trid);
  188. } MARIA_KEYDEF;
  189. #define MARIA_UNIQUE_HASH_LENGTH 4
  190. typedef struct st_maria_unique_def /* Segment definition of unique */
  191. {
  192. uint16 keysegs; /* Number of key-segment */
  193. uint8 key; /* Mapped to which key */
  194. uint8 null_are_equal;
  195. HA_KEYSEG *seg, *end;
  196. } MARIA_UNIQUEDEF;
  197. typedef struct st_maria_decode_tree /* Decode huff-table */
  198. {
  199. uint16 *table;
  200. uint quick_table_bits;
  201. uchar *intervalls;
  202. } MARIA_DECODE_TREE;
  203. struct st_maria_bit_buff;
  204. /*
  205. Note that null markers should always be first in a row !
  206. When creating a column, one should only specify:
  207. type, length, null_bit and null_pos
  208. */
  209. typedef struct st_maria_columndef /* column information */
  210. {
  211. enum en_fieldtype type;
  212. uint32 offset; /* Offset to position in row */
  213. uint16 length; /* length of field */
  214. uint16 column_nr;
  215. /* Intern variable (size of total storage area for the row) */
  216. uint16 fill_length;
  217. uint16 null_pos; /* Position for null marker */
  218. uint16 empty_pos; /* Position for empty marker */
  219. uint8 null_bit; /* If column may be NULL */
  220. /* Intern. Set if column should be zero packed (part of empty_bits) */
  221. uint8 empty_bit;
  222. #ifndef NOT_PACKED_DATABASES
  223. void(*unpack)(struct st_maria_columndef *rec,
  224. struct st_maria_bit_buff *buff,
  225. uchar *start, uchar *end);
  226. enum en_fieldtype base_type;
  227. uint space_length_bits, pack_type;
  228. MARIA_DECODE_TREE *huff_tree;
  229. #endif
  230. } MARIA_COLUMNDEF;
  231. extern ulong maria_block_size, maria_checkpoint_frequency;
  232. extern ulong maria_concurrent_insert;
  233. extern my_bool maria_flush, maria_single_user, maria_page_checksums;
  234. extern my_bool maria_delay_key_write;
  235. extern my_off_t maria_max_temp_length;
  236. extern ulong maria_bulk_insert_tree_size, maria_data_pointer_size;
  237. extern MY_TMPDIR *maria_tmpdir;
  238. extern my_bool maria_encrypt_tables;
  239. /*
  240. This is used to check if a symlink points into the mysql data home,
  241. which is normally forbidden as it can be used to get access to
  242. not privileged data
  243. */
  244. extern int (*maria_test_invalid_symlink)(const char *filename);
  245. /* Prototypes for maria-functions */
  246. extern int maria_init(void);
  247. extern void maria_end(void);
  248. extern my_bool maria_upgrade(void);
  249. extern int maria_close(MARIA_HA *file);
  250. extern int maria_delete(MARIA_HA *file, const uchar *buff);
  251. extern MARIA_HA *maria_open(const char *name, int mode,
  252. uint wait_if_locked);
  253. extern MARIA_HA *maria_clone(struct st_maria_share *share, int mode);
  254. extern int maria_panic(enum ha_panic_function function);
  255. extern int maria_rfirst(MARIA_HA *file, uchar *buf, int inx);
  256. extern int maria_rkey(MARIA_HA *file, uchar *buf, int inx,
  257. const uchar *key, key_part_map keypart_map,
  258. enum ha_rkey_function search_flag);
  259. extern int maria_rlast(MARIA_HA *file, uchar *buf, int inx);
  260. extern int maria_rnext(MARIA_HA *file, uchar *buf, int inx);
  261. extern int maria_rnext_same(MARIA_HA *info, uchar *buf);
  262. extern int maria_rprev(MARIA_HA *file, uchar *buf, int inx);
  263. extern int maria_rrnd(MARIA_HA *file, uchar *buf,
  264. MARIA_RECORD_POS pos);
  265. extern int maria_scan_init(MARIA_HA *file);
  266. extern int maria_scan(MARIA_HA *file, uchar *buf);
  267. extern void maria_scan_end(MARIA_HA *file);
  268. extern int maria_rsame(MARIA_HA *file, uchar *record, int inx);
  269. extern int maria_rsame_with_pos(MARIA_HA *file, uchar *record,
  270. int inx, MARIA_RECORD_POS pos);
  271. extern int maria_update(MARIA_HA *file, const uchar *old,
  272. uchar *new_record);
  273. extern int maria_write(MARIA_HA *file, uchar *buff);
  274. extern MARIA_RECORD_POS maria_position(MARIA_HA *file);
  275. extern int maria_status(MARIA_HA *info, MARIA_INFO *x, uint flag);
  276. extern int maria_lock_database(MARIA_HA *file, int lock_type);
  277. extern int maria_create(const char *name, enum data_file_type record_type,
  278. uint keys, MARIA_KEYDEF *keydef,
  279. uint columns, MARIA_COLUMNDEF *columndef,
  280. uint uniques, MARIA_UNIQUEDEF *uniquedef,
  281. MARIA_CREATE_INFO *create_info, uint flags);
  282. extern int maria_delete_table(const char *name);
  283. extern int maria_rename(const char *from, const char *to);
  284. extern int maria_extra(MARIA_HA *file,
  285. enum ha_extra_function function, void *extra_arg);
  286. extern int maria_reset(MARIA_HA *file);
  287. extern ha_rows maria_records_in_range(MARIA_HA *info, int inx,
  288. key_range *min_key, key_range *max_key);
  289. extern int maria_is_changed(MARIA_HA *info);
  290. extern int maria_delete_all_rows(MARIA_HA *info);
  291. extern uint maria_get_pointer_length(ulonglong file_length, uint def);
  292. extern int maria_commit(MARIA_HA *info);
  293. extern int maria_begin(MARIA_HA *info);
  294. extern void maria_disable_logging(MARIA_HA *info);
  295. extern void maria_enable_logging(MARIA_HA *info);
  296. #define HA_RECOVER_NONE 0 /* No automatic recover */
  297. #define HA_RECOVER_DEFAULT 1 /* Automatic recover active */
  298. #define HA_RECOVER_BACKUP 2 /* Make a backupfile on recover */
  299. #define HA_RECOVER_FORCE 4 /* Recover even if we loose rows */
  300. #define HA_RECOVER_QUICK 8 /* Don't check rows in data file */
  301. #define HA_RECOVER_ANY (HA_RECOVER_DEFAULT | HA_RECOVER_BACKUP | HA_RECOVER_FORCE | HA_RECOVER_QUICK)
  302. /* this is used to pass to mysql_mariachk_table */
  303. #define MARIA_CHK_REPAIR 1 /* equivalent to mariachk -r */
  304. #define MARIA_CHK_VERIFY 2 /* Verify, run repair if failure */
  305. typedef uint maria_bit_type;
  306. typedef struct st_maria_bit_buff
  307. { /* Used for packing of record */
  308. maria_bit_type current_byte;
  309. uint bits;
  310. uchar *pos, *end, *blob_pos, *blob_end;
  311. uint error;
  312. } MARIA_BIT_BUFF;
  313. /* functions in maria_check */
  314. void maria_chk_init(HA_CHECK *param);
  315. void maria_chk_init_for_check(HA_CHECK *param, MARIA_HA *info);
  316. int maria_chk_status(HA_CHECK *param, MARIA_HA *info);
  317. int maria_chk_del(HA_CHECK *param, MARIA_HA *info, ulonglong test_flag);
  318. int maria_chk_size(HA_CHECK *param, MARIA_HA *info);
  319. int maria_chk_key(HA_CHECK *param, MARIA_HA *info);
  320. int maria_chk_data_link(HA_CHECK *param, MARIA_HA *info, my_bool extend);
  321. int maria_repair(HA_CHECK *param, MARIA_HA *info, char * name, my_bool);
  322. int maria_sort_index(HA_CHECK *param, MARIA_HA *info, char * name);
  323. int maria_zerofill(HA_CHECK *param, MARIA_HA *info, const char *name);
  324. int maria_repair_by_sort(HA_CHECK *param, MARIA_HA *info,
  325. const char *name, my_bool rep_quick);
  326. int maria_repair_parallel(HA_CHECK *param, register MARIA_HA *info,
  327. const char *name, my_bool rep_quick);
  328. int maria_change_to_newfile(const char *filename, const char *old_ext,
  329. const char *new_ext, time_t backup_time,
  330. myf myflags);
  331. void maria_lock_memory(HA_CHECK *param);
  332. int maria_update_state_info(HA_CHECK *param, MARIA_HA *info, uint update);
  333. void maria_update_key_parts(MARIA_KEYDEF *keyinfo, double *rec_per_key_part,
  334. ulonglong *unique, ulonglong *notnull,
  335. ulonglong records);
  336. int maria_filecopy(HA_CHECK *param, File to, File from, my_off_t start,
  337. my_off_t length, const char *type);
  338. int maria_movepoint(MARIA_HA *info, uchar *record, my_off_t oldpos,
  339. my_off_t newpos, uint prot_key);
  340. int maria_test_if_almost_full(MARIA_HA *info);
  341. int maria_recreate_table(HA_CHECK *param, MARIA_HA **org_info, char *filename);
  342. int maria_disable_indexes(MARIA_HA *info);
  343. int maria_enable_indexes(MARIA_HA *info);
  344. int maria_indexes_are_disabled(MARIA_HA *info);
  345. void maria_disable_indexes_for_rebuild(MARIA_HA *info, ha_rows rows,
  346. my_bool all_keys);
  347. my_bool maria_test_if_sort_rep(MARIA_HA *info, ha_rows rows, ulonglong key_map,
  348. my_bool force);
  349. int maria_init_bulk_insert(MARIA_HA *info, size_t cache_size, ha_rows rows);
  350. void maria_flush_bulk_insert(MARIA_HA *info, uint inx);
  351. void maria_end_bulk_insert(MARIA_HA *info);
  352. int maria_preload(MARIA_HA *info, ulonglong key_map, my_bool ignore_leaves);
  353. void maria_versioning(MARIA_HA *info, my_bool versioning);
  354. void maria_ignore_trids(MARIA_HA *info);
  355. uint maria_max_key_length(void);
  356. #define maria_max_key_segments() HA_MAX_KEY_SEG
  357. /* fulltext functions */
  358. FT_INFO *maria_ft_init_search(uint,void *, uint, uchar *, size_t,
  359. CHARSET_INFO *, uchar *);
  360. /* 'Almost-internal' Maria functions */
  361. void _ma_update_auto_increment_key(HA_CHECK *param, MARIA_HA *info,
  362. my_bool repair);
  363. #ifdef __cplusplus
  364. }
  365. #endif
  366. #endif