1 /*
2    Copyright (c) 2000, 2013, 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, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
23 
24 
25 /* class for the the myisam handler */
26 
27 #include <myisam.h>
28 #include <ft_global.h>
29 #include "handler.h"                            /* handler */
30 #include "table.h"                              /* TABLE_SHARE */
31 
32 struct TABLE_SHARE;
33 typedef struct st_ha_create_information HA_CREATE_INFO;
34 
35 #define HA_RECOVER_DEFAULT	1	/* Automatic recover active */
36 #define HA_RECOVER_BACKUP	2	/* Make a backupfile on recover */
37 #define HA_RECOVER_FORCE	4	/* Recover even if we loose rows */
38 #define HA_RECOVER_QUICK	8	/* Don't check rows in data file */
39 #define HA_RECOVER_OFF         16	/* No automatic recover */
40 
41 extern TYPELIB myisam_recover_typelib;
42 extern const char *myisam_recover_names[];
43 extern ulonglong myisam_recover_options;
44 
45 C_MODE_START
46 ICP_RESULT index_cond_func_myisam(void *arg);
47 C_MODE_END
48 
49 class ha_myisam: public handler
50 {
51   MI_INFO *file;
52   ulonglong int_table_flags;
53   char    *data_file_name, *index_file_name;
54   bool can_enable_indexes;
55   int repair(THD *thd, MI_CHECK &param, bool optimize);
56 
57  public:
58   ha_myisam(handlerton *hton, TABLE_SHARE *table_arg);
~ha_myisam()59   ~ha_myisam() {}
60   handler *clone(const char *name, MEM_ROOT *mem_root);
table_type()61   const char *table_type() const { return "MyISAM"; }
62   const char *index_type(uint key_number);
63   const char **bas_ext() const;
table_flags()64   ulonglong table_flags() const { return int_table_flags; }
65   int index_init(uint idx, bool sorted);
66   int index_end();
67   int rnd_end();
68 
index_flags(uint inx,uint part,bool all_parts)69   ulong index_flags(uint inx, uint part, bool all_parts) const
70   {
71     if (table_share->key_info[inx].algorithm == HA_KEY_ALG_FULLTEXT)
72       return 0;
73 
74     ulong flags= HA_READ_NEXT | HA_READ_PREV | HA_READ_RANGE |
75                  HA_READ_ORDER | HA_KEYREAD_ONLY | HA_DO_INDEX_COND_PUSHDOWN;
76 
77     // @todo: Check if spatial indexes really have all these properties
78     if (table_share->key_info[inx].flags & HA_SPATIAL)
79       flags|= HA_KEY_SCAN_NOT_ROR;
80 
81     return flags;
82   }
max_supported_keys()83   uint max_supported_keys()          const { return MI_MAX_KEY; }
max_supported_key_length()84   uint max_supported_key_length()    const { return MI_MAX_KEY_LENGTH; }
max_supported_key_part_length()85   uint max_supported_key_part_length() const { return MI_MAX_KEY_LENGTH; }
86   uint checksum() const;
87 
88   int open(const char *name, int mode, uint test_if_locked);
89   int close(void);
90   int write_row(uchar * buf);
91   int update_row(const uchar * old_data, uchar * new_data);
92   int delete_row(const uchar * buf);
93   int index_read_map(uchar *buf, const uchar *key, key_part_map keypart_map,
94                      enum ha_rkey_function find_flag);
95   int index_read_idx_map(uchar *buf, uint index, const uchar *key,
96                          key_part_map keypart_map,
97                          enum ha_rkey_function find_flag);
98   int index_read_last_map(uchar *buf, const uchar *key, key_part_map keypart_map);
99   int index_next(uchar * buf);
100   int index_prev(uchar * buf);
101   int index_first(uchar * buf);
102   int index_last(uchar * buf);
103   int index_next_same(uchar *buf, const uchar *key, uint keylen);
ft_init()104   int ft_init()
105   {
106     if (!ft_handler)
107       return 1;
108     ft_handler->please->reinit_search(ft_handler);
109     return 0;
110   }
ft_init_ext(uint flags,uint inx,String * key)111   FT_INFO *ft_init_ext(uint flags, uint inx,String *key)
112   {
113     return ft_init_search(flags,file,inx,
114                           (uchar *)key->ptr(), key->length(), key->charset(),
115                           table->record[0]);
116   }
117   int ft_read(uchar *buf);
118   int rnd_init(bool scan);
119   int rnd_next(uchar *buf);
120   int rnd_pos(uchar * buf, uchar *pos);
121   int restart_rnd_next(uchar *buf, uchar *pos);
122   void position(const uchar *record);
123   int info(uint);
124   int extra(enum ha_extra_function operation);
125   int extra_opt(enum ha_extra_function operation, ulong cache_size);
126   int reset(void);
127   int external_lock(THD *thd, int lock_type);
128   int delete_all_rows(void);
129   int truncate();
130   int reset_auto_increment(ulonglong value);
131   int disable_indexes(uint mode);
132   int enable_indexes(uint mode);
133   int indexes_are_disabled(void);
134   void start_bulk_insert(ha_rows rows);
135   int end_bulk_insert();
136   ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key);
137   void update_create_info(HA_CREATE_INFO *create_info);
138   int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info);
139   THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to,
140 			     enum thr_lock_type lock_type);
141   virtual void get_auto_increment(ulonglong offset, ulonglong increment,
142                                   ulonglong nb_desired_values,
143                                   ulonglong *first_value,
144                                   ulonglong *nb_reserved_values);
145   int rename_table(const char * from, const char * to);
146   int delete_table(const char *name);
147   int check(THD* thd, HA_CHECK_OPT* check_opt);
148   int analyze(THD* thd,HA_CHECK_OPT* check_opt);
149   int repair(THD* thd, HA_CHECK_OPT* check_opt);
150   bool check_and_repair(THD *thd);
151   bool is_crashed() const;
auto_repair()152   bool auto_repair() const { return myisam_recover_options != HA_RECOVER_OFF; }
153   int optimize(THD* thd, HA_CHECK_OPT* check_opt);
154   int assign_to_keycache(THD* thd, HA_CHECK_OPT* check_opt);
155   int preload_keys(THD* thd, HA_CHECK_OPT* check_opt);
156   bool check_if_incompatible_data(HA_CREATE_INFO *info, uint table_changes);
157 #ifdef HAVE_QUERY_CACHE
158   my_bool register_query_cache_table(THD *thd, char *table_key,
159                                      uint key_length,
160                                      qc_engine_callback
161                                      *engine_callback,
162                                      ulonglong *engine_data);
163 #endif
file_ptr(void)164   MI_INFO *file_ptr(void)
165   {
166     return file;
167   }
168 public:
169   /**
170    * Multi Range Read interface
171    */
172   int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
173                             uint n_ranges, uint mode, HANDLER_BUFFER *buf);
174   int multi_range_read_next(char **range_info);
175   ha_rows multi_range_read_info_const(uint keyno, RANGE_SEQ_IF *seq,
176                                       void *seq_init_param,
177                                       uint n_ranges, uint *bufsz,
178                                       uint *flags, Cost_estimate *cost);
179   ha_rows multi_range_read_info(uint keyno, uint n_ranges, uint keys,
180                                 uint *bufsz, uint *flags, Cost_estimate *cost);
181 
182   /* Index condition pushdown implementation */
183   Item *idx_cond_push(uint keyno, Item* idx_cond);
184 private:
185   DsMrr_impl ds_mrr;
186   friend ICP_RESULT index_cond_func_myisam(void *arg);
187 };
188 
189