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