MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
myrg_open.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2 
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; version 2 of the License.
6 
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 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 /* open a MyISAM MERGE table */
17 
18 #include "myrg_def.h"
19 #include <stddef.h>
20 #include <errno.h>
21 
22 /*
23  open a MyISAM MERGE table
24  if handle_locking is 0 then exit with error if some table is locked
25  if handle_locking is 1 then wait if table is locked
26 
27  NOTE: This function is not used in the MySQL server. It is for
28  MERGE use independent from MySQL. Currently there is some code
29  duplication between myrg_open() and myrg_parent_open() +
30  myrg_attach_children(). Please duplicate changes in these
31  functions or make common sub-functions.
32 */
33 
34 MYRG_INFO *myrg_open(const char *name, int mode, int handle_locking)
35 {
36  int save_errno,errpos=0;
37  uint files= 0, i, dir_length, length, UNINIT_VAR(key_parts), min_keys= 0;
38  ulonglong file_offset=0;
39  char name_buff[FN_REFLEN*2],buff[FN_REFLEN],*end;
40  MYRG_INFO *m_info=0;
41  File fd;
42  IO_CACHE file;
43  MI_INFO *isam=0;
44  uint found_merge_insert_method= 0;
45  size_t name_buff_length;
46  my_bool bad_children= FALSE;
47  DBUG_ENTER("myrg_open");
48 
49  memset(&file, 0, sizeof(file));
50  if ((fd= mysql_file_open(rg_key_file_MRG,
51  fn_format(name_buff, name, "", MYRG_NAME_EXT,
52  MY_UNPACK_FILENAME|MY_APPEND_EXT),
53  O_RDONLY | O_SHARE, MYF(0))) < 0)
54  goto err;
55  errpos=1;
56  if (init_io_cache(&file, fd, 4*IO_SIZE, READ_CACHE, 0, 0,
57  MYF(MY_WME | MY_NABP)))
58  goto err;
59  errpos=2;
60  dir_length=dirname_part(name_buff, name, &name_buff_length);
61  while ((length=my_b_gets(&file,buff,FN_REFLEN-1)))
62  {
63  if ((end=buff+length)[-1] == '\n')
64  end[-1]='\0';
65  if (buff[0] && buff[0] != '#')
66  files++;
67  }
68 
69  my_b_seek(&file, 0);
70  while ((length=my_b_gets(&file,buff,FN_REFLEN-1)))
71  {
72  if ((end=buff+length)[-1] == '\n')
73  *--end='\0';
74  if (!buff[0])
75  continue; /* Skip empty lines */
76  if (buff[0] == '#')
77  {
78  if (!strncmp(buff+1,"INSERT_METHOD=",14))
79  { /* Lookup insert method */
80  int tmp= find_type(buff + 15, &merge_insert_method, FIND_TYPE_BASIC);
81  found_merge_insert_method = (uint) (tmp >= 0 ? tmp : 0);
82  }
83  continue; /* Skip comments */
84  }
85 
86  if (!has_path(buff))
87  {
88  (void) strmake(name_buff+dir_length,buff,
89  sizeof(name_buff)-1-dir_length);
90  (void) cleanup_dirname(buff,name_buff);
91  }
92  else
93  fn_format(buff, buff, "", "", 0);
94  if (!(isam=mi_open(buff,mode,(handle_locking?HA_OPEN_WAIT_IF_LOCKED:0))))
95  {
96  if (handle_locking & HA_OPEN_FOR_REPAIR)
97  {
98  myrg_print_wrong_table(buff);
99  bad_children= TRUE;
100  continue;
101  }
102  goto bad_children;
103  }
104  if (!m_info) /* First file */
105  {
106  key_parts=isam->s->base.key_parts;
107  if (!(m_info= (MYRG_INFO*) my_malloc(sizeof(MYRG_INFO) +
108  files*sizeof(MYRG_TABLE) +
109  key_parts*sizeof(long),
110  MYF(MY_WME|MY_ZEROFILL))))
111  goto err;
112  DBUG_ASSERT(files);
113  m_info->open_tables=(MYRG_TABLE *) (m_info+1);
114  m_info->rec_per_key_part=(ulong *) (m_info->open_tables+files);
115  m_info->tables= files;
116  files= 0;
117  m_info->reclength=isam->s->base.reclength;
118  min_keys= isam->s->base.keys;
119  errpos=3;
120  }
121  m_info->open_tables[files].table= isam;
122  m_info->open_tables[files].file_offset=(my_off_t) file_offset;
123  file_offset+=isam->state->data_file_length;
124  files++;
125  if (m_info->reclength != isam->s->base.reclength)
126  {
127  if (handle_locking & HA_OPEN_FOR_REPAIR)
128  {
129  myrg_print_wrong_table(buff);
130  bad_children= TRUE;
131  continue;
132  }
133  goto bad_children;
134  }
135  m_info->options|= isam->s->options;
136  m_info->records+= isam->state->records;
137  m_info->del+= isam->state->del;
138  m_info->data_file_length+= isam->state->data_file_length;
139  if (min_keys > isam->s->base.keys)
140  min_keys= isam->s->base.keys;
141  for (i=0; i < key_parts; i++)
142  m_info->rec_per_key_part[i]+= (isam->s->state.rec_per_key_part[i] /
143  m_info->tables);
144  }
145 
146  if (bad_children)
147  goto bad_children;
148  if (!m_info && !(m_info= (MYRG_INFO*) my_malloc(sizeof(MYRG_INFO),
149  MYF(MY_WME | MY_ZEROFILL))))
150  goto err;
151  /* Don't mark table readonly, for ALTER TABLE ... UNION=(...) to work */
152  m_info->options&= ~(HA_OPTION_COMPRESS_RECORD | HA_OPTION_READ_ONLY_DATA);
153  m_info->merge_insert_method= found_merge_insert_method;
154 
155  if (sizeof(my_off_t) == 4 && file_offset > (ulonglong) (ulong) ~0L)
156  {
157  my_errno=HA_ERR_RECORD_FILE_FULL;
158  goto err;
159  }
160  m_info->keys= min_keys;
161  memset(&m_info->by_key, 0, sizeof(m_info->by_key));
162 
163  /* this works ok if the table list is empty */
164  m_info->end_table=m_info->open_tables+files;
165  m_info->last_used_table=m_info->open_tables;
166  m_info->children_attached= TRUE;
167 
168  (void) mysql_file_close(fd, MYF(0));
169  end_io_cache(&file);
170  mysql_mutex_init(rg_key_mutex_MYRG_INFO_mutex,
171  &m_info->mutex, MY_MUTEX_INIT_FAST);
172  m_info->open_list.data=(void*) m_info;
173  mysql_mutex_lock(&THR_LOCK_open);
174  myrg_open_list=list_add(myrg_open_list,&m_info->open_list);
175  mysql_mutex_unlock(&THR_LOCK_open);
176  DBUG_RETURN(m_info);
177 
178 bad_children:
179  my_errno= HA_ERR_WRONG_MRG_TABLE_DEF;
180 err:
181  save_errno=my_errno;
182  switch (errpos) {
183  case 3:
184  while (files)
185  (void) mi_close(m_info->open_tables[--files].table);
186  my_free(m_info);
187  /* Fall through */
188  case 2:
189  end_io_cache(&file);
190  /* Fall through */
191  case 1:
192  (void) mysql_file_close(fd, MYF(0));
193  }
194  my_errno=save_errno;
195  DBUG_RETURN (NULL);
196 }
197 
198 
219 MYRG_INFO *myrg_parent_open(const char *parent_name,
220  int (*callback)(void*, const char*),
221  void *callback_param)
222 {
223  MYRG_INFO *UNINIT_VAR(m_info);
224  int rc;
225  int errpos;
226  int save_errno;
227  int insert_method;
228  uint length;
229  uint child_count;
230  File fd;
231  IO_CACHE file_cache;
232  char parent_name_buff[FN_REFLEN * 2];
233  char child_name_buff[FN_REFLEN];
234  DBUG_ENTER("myrg_parent_open");
235 
236  rc= 1;
237  errpos= 0;
238  memset(&file_cache, 0, sizeof(file_cache));
239 
240  /* Open MERGE meta file. */
241  if ((fd= mysql_file_open(rg_key_file_MRG,
242  fn_format(parent_name_buff, parent_name,
243  "", MYRG_NAME_EXT,
244  MY_UNPACK_FILENAME|MY_APPEND_EXT),
245  O_RDONLY | O_SHARE, MYF(0))) < 0)
246  goto err; /* purecov: inspected */
247  errpos= 1;
248 
249  if (init_io_cache(&file_cache, fd, 4 * IO_SIZE, READ_CACHE, 0, 0,
250  MYF(MY_WME | MY_NABP)))
251  goto err; /* purecov: inspected */
252  errpos= 2;
253 
254  /* Count children. Determine insert method. */
255  child_count= 0;
256  insert_method= 0;
257  while ((length= my_b_gets(&file_cache, child_name_buff, FN_REFLEN - 1)))
258  {
259  /* Remove line terminator. */
260  if (child_name_buff[length - 1] == '\n')
261  child_name_buff[--length]= '\0';
262 
263  /* Skip empty lines. */
264  if (!child_name_buff[0])
265  continue; /* purecov: inspected */
266 
267  /* Skip comments, but evaluate insert method. */
268  if (child_name_buff[0] == '#')
269  {
270  if (!strncmp(child_name_buff + 1, "INSERT_METHOD=", 14))
271  {
272  /* Compare buffer with global methods list: merge_insert_method. */
273  insert_method= find_type(child_name_buff + 15,
274  &merge_insert_method, FIND_TYPE_BASIC);
275  }
276  continue;
277  }
278 
279  /* Count the child. */
280  child_count++;
281  }
282 
283  /* Allocate MERGE parent table structure. */
284  if (!(m_info= (MYRG_INFO*) my_malloc(sizeof(MYRG_INFO) +
285  child_count * sizeof(MYRG_TABLE),
286  MYF(MY_WME | MY_ZEROFILL))))
287  goto err; /* purecov: inspected */
288  errpos= 3;
289  m_info->open_tables= (MYRG_TABLE*) (m_info + 1);
290  m_info->tables= child_count;
291  m_info->merge_insert_method= insert_method > 0 ? insert_method : 0;
292  /* This works even if the table list is empty. */
293  m_info->end_table= m_info->open_tables + child_count;
294  if (!child_count)
295  {
296  /* Do not attach/detach an empty child list. */
297  m_info->children_attached= TRUE;
298  }
299 
300  /* Call callback for each child. */
301  my_b_seek(&file_cache, 0);
302  while ((length= my_b_gets(&file_cache, child_name_buff, FN_REFLEN - 1)))
303  {
304  /* Remove line terminator. */
305  if (child_name_buff[length - 1] == '\n')
306  child_name_buff[--length]= '\0';
307 
308  /* Skip empty lines and comments. */
309  if (!child_name_buff[0] || (child_name_buff[0] == '#'))
310  continue;
311 
312  DBUG_PRINT("info", ("child: '%s'", child_name_buff));
313 
314  /* Callback registers child with handler table. */
315  if ((rc= (*callback)(callback_param, child_name_buff)))
316  goto err; /* purecov: inspected */
317  }
318 
319  end_io_cache(&file_cache);
320  (void) mysql_file_close(fd, MYF(0));
321  mysql_mutex_init(rg_key_mutex_MYRG_INFO_mutex,
322  &m_info->mutex, MY_MUTEX_INIT_FAST);
323 
324  m_info->open_list.data= (void*) m_info;
325  mysql_mutex_lock(&THR_LOCK_open);
326  myrg_open_list= list_add(myrg_open_list, &m_info->open_list);
327  mysql_mutex_unlock(&THR_LOCK_open);
328 
329  DBUG_RETURN(m_info);
330 
331  /* purecov: begin inspected */
332  err:
333  save_errno= my_errno;
334  switch (errpos) {
335  case 3:
336  my_free(m_info);
337  /* Fall through */
338  case 2:
339  end_io_cache(&file_cache);
340  /* Fall through */
341  case 1:
342  (void) mysql_file_close(fd, MYF(0));
343  }
344  my_errno= save_errno;
345  DBUG_RETURN (NULL);
346  /* purecov: end */
347 }
348 
349 
375 int myrg_attach_children(MYRG_INFO *m_info, int handle_locking,
376  MI_INFO *(*callback)(void*),
377  void *callback_param, my_bool *need_compat_check)
378 {
379  ulonglong file_offset;
380  MI_INFO *myisam;
381  int errpos;
382  int save_errno;
383  uint idx;
384  uint child_nr;
385  uint UNINIT_VAR(key_parts);
386  uint min_keys;
387  my_bool bad_children= FALSE;
388  my_bool first_child= TRUE;
389  DBUG_ENTER("myrg_attach_children");
390  DBUG_PRINT("myrg", ("handle_locking: %d", handle_locking));
391 
392  /*
393  This function can be called while another thread is trying to abort
394  locks of this MERGE table. If the processor reorders instructions or
395  write to memory, 'children_attached' could be set before
396  'open_tables' has all the pointers to the children. Use of a mutex
397  here and in ha_myisammrg::store_lock() forces consistent data.
398  */
399  mysql_mutex_lock(&m_info->mutex);
400  errpos= 0;
401  file_offset= 0;
402  min_keys= 0;
403  for (child_nr= 0; child_nr < m_info->tables; child_nr++)
404  {
405  if (! (myisam= (*callback)(callback_param)))
406  {
407  if (handle_locking & HA_OPEN_FOR_REPAIR)
408  {
409  /* An appropriate error should've been already pushed by callback. */
410  bad_children= TRUE;
411  continue;
412  }
413  goto bad_children;
414  }
415 
416  DBUG_PRINT("myrg", ("child_nr: %u table: '%s'",
417  child_nr, myisam->filename));
418 
419  /* Special handling when the first child is attached. */
420  if (first_child)
421  {
422  first_child= FALSE;
423  m_info->reclength= myisam->s->base.reclength;
424  min_keys= myisam->s->base.keys;
425  key_parts= myisam->s->base.key_parts;
426  if (*need_compat_check && m_info->rec_per_key_part)
427  {
428  my_free(m_info->rec_per_key_part);
429  m_info->rec_per_key_part= NULL;
430  }
431  if (!m_info->rec_per_key_part)
432  {
433  if(!(m_info->rec_per_key_part= (ulong*)
434  my_malloc(key_parts * sizeof(long), MYF(MY_WME))))
435  goto err; /* purecov: inspected */
436  errpos= 1;
437  }
438  memset(m_info->rec_per_key_part, 0, key_parts * sizeof(long));
439  }
440 
441  /* Add MyISAM table info. */
442  m_info->open_tables[child_nr].table= myisam;
443  m_info->open_tables[child_nr].file_offset= (my_off_t) file_offset;
444  file_offset+= myisam->state->data_file_length;
445 
446  /* Check table definition match. */
447  if (m_info->reclength != myisam->s->base.reclength)
448  {
449  DBUG_PRINT("error", ("definition mismatch table: '%s' repair: %d",
450  myisam->filename,
451  (handle_locking & HA_OPEN_FOR_REPAIR)));
452  if (handle_locking & HA_OPEN_FOR_REPAIR)
453  {
454  myrg_print_wrong_table(myisam->filename);
455  bad_children= TRUE;
456  continue;
457  }
458  goto bad_children;
459  }
460 
461  m_info->options|= myisam->s->options;
462  m_info->records+= myisam->state->records;
463  m_info->del+= myisam->state->del;
464  m_info->data_file_length+= myisam->state->data_file_length;
465  if (min_keys > myisam->s->base.keys)
466  min_keys= myisam->s->base.keys; /* purecov: inspected */
467  for (idx= 0; idx < key_parts; idx++)
468  m_info->rec_per_key_part[idx]+= (myisam->s->state.rec_per_key_part[idx] /
469  m_info->tables);
470  }
471 
472  if (bad_children)
473  goto bad_children;
474 
475  if (sizeof(my_off_t) == 4 && file_offset > (ulonglong) (ulong) ~0L)
476  {
477  my_errno= HA_ERR_RECORD_FILE_FULL;
478  goto err;
479  }
480  /* Don't mark table readonly, for ALTER TABLE ... UNION=(...) to work */
481  m_info->options&= ~(HA_OPTION_COMPRESS_RECORD | HA_OPTION_READ_ONLY_DATA);
482  m_info->keys= min_keys;
483  m_info->last_used_table= m_info->open_tables;
484  m_info->children_attached= TRUE;
485  mysql_mutex_unlock(&m_info->mutex);
486  DBUG_RETURN(0);
487 
488 bad_children:
489  my_errno= HA_ERR_WRONG_MRG_TABLE_DEF;
490 err:
491  save_errno= my_errno;
492  switch (errpos) {
493  case 1:
494  my_free(m_info->rec_per_key_part);
495  m_info->rec_per_key_part= NULL;
496  }
497  mysql_mutex_unlock(&m_info->mutex);
498  my_errno= save_errno;
499  DBUG_RETURN(1);
500 }
501 
502 
516 int myrg_detach_children(MYRG_INFO *m_info)
517 {
518  DBUG_ENTER("myrg_detach_children");
519  /* For symmetry with myrg_attach_children() we use the mutex here. */
520  mysql_mutex_lock(&m_info->mutex);
521  if (m_info->tables)
522  {
523  /* Do not attach/detach an empty child list. */
524  m_info->children_attached= FALSE;
525  memset(m_info->open_tables, 0, m_info->tables * sizeof(MYRG_TABLE));
526  }
527  m_info->records= 0;
528  m_info->del= 0;
529  m_info->data_file_length= 0;
530  m_info->options= 0;
531  mysql_mutex_unlock(&m_info->mutex);
532  DBUG_RETURN(0);
533 }
534