MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mi_page.c
1 /* Copyright (c) 2000, 2010, 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 St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /* Read and write key blocks */
17 
18 #include "myisamdef.h"
19 
20  /* Fetch a key-page in memory */
21 
22 uchar *_mi_fetch_keypage(register MI_INFO *info, MI_KEYDEF *keyinfo,
23  my_off_t page, int level,
24  uchar *buff, int return_buffer)
25 {
26  uchar *tmp;
27  uint page_size;
28  DBUG_ENTER("_mi_fetch_keypage");
29  DBUG_PRINT("enter",("page: %ld", (long) page));
30 
31  tmp=(uchar*) key_cache_read(info->s->key_cache,
32  info->s->kfile, page, level, (uchar*) buff,
33  (uint) keyinfo->block_length,
34  (uint) keyinfo->block_length,
35  return_buffer);
36  if (tmp == info->buff)
37  info->buff_used=1;
38  else if (!tmp)
39  {
40  DBUG_PRINT("error",("Got errno: %d from key_cache_read",my_errno));
41  info->last_keypage=HA_OFFSET_ERROR;
42  mi_print_error(info->s, HA_ERR_CRASHED);
43  my_errno=HA_ERR_CRASHED;
44  DBUG_RETURN(0);
45  }
46  info->last_keypage=page;
47  page_size=mi_getint(tmp);
48  if (page_size < 4 || page_size > keyinfo->block_length)
49  {
50  DBUG_PRINT("error",("page %lu had wrong page length: %u",
51  (ulong) page, page_size));
52  DBUG_DUMP("page", tmp, keyinfo->block_length);
53  info->last_keypage = HA_OFFSET_ERROR;
54  mi_print_error(info->s, HA_ERR_CRASHED);
55  my_errno = HA_ERR_CRASHED;
56  tmp = 0;
57  }
58  DBUG_RETURN(tmp);
59 } /* _mi_fetch_keypage */
60 
61 
62  /* Write a key-page on disk */
63 
64 int _mi_write_keypage(register MI_INFO *info, register MI_KEYDEF *keyinfo,
65  my_off_t page, int level, uchar *buff)
66 {
67  reg3 uint length;
68  DBUG_ENTER("_mi_write_keypage");
69 
70 #ifndef FAST /* Safety check */
71  if (page < info->s->base.keystart ||
72  page+keyinfo->block_length > info->state->key_file_length ||
73  (page & (MI_MIN_KEY_BLOCK_LENGTH-1)))
74  {
75  DBUG_PRINT("error",("Trying to write inside key status region: key_start: %lu length: %lu page: %lu",
76  (long) info->s->base.keystart,
77  (long) info->state->key_file_length,
78  (long) page));
79  my_errno=EINVAL;
80  DBUG_RETURN((-1));
81  }
82  DBUG_PRINT("page",("write page at: %lu",(long) page));
83  DBUG_DUMP("buff",(uchar*) buff,mi_getint(buff));
84 #endif
85 
86  if ((length=keyinfo->block_length) > IO_SIZE*2 &&
87  info->state->key_file_length != page+length)
88  length= ((mi_getint(buff)+IO_SIZE-1) & (uint) ~(IO_SIZE-1));
89  DBUG_RETURN((key_cache_write(info->s->key_cache,
90  info->s->kfile,page, level, (uchar*) buff,length,
91  (uint) keyinfo->block_length,
92  (int) ((info->lock_type != F_UNLCK) ||
93  info->s->delay_key_write))));
94 } /* mi_write_keypage */
95 
96 
97  /* Remove page from disk */
98 
99 int _mi_dispose(register MI_INFO *info, MI_KEYDEF *keyinfo, my_off_t pos,
100  int level)
101 {
102  my_off_t old_link;
103  uchar buff[8];
104  DBUG_ENTER("_mi_dispose");
105  DBUG_PRINT("enter",("pos: %ld", (long) pos));
106 
107  old_link= info->s->state.key_del[keyinfo->block_size_index];
108  info->s->state.key_del[keyinfo->block_size_index]= pos;
109  mi_sizestore(buff,old_link);
110  info->s->state.changed|= STATE_NOT_SORTED_PAGES;
111  DBUG_RETURN(key_cache_write(info->s->key_cache,
112  info->s->kfile, pos , level, buff,
113  sizeof(buff),
114  (uint) keyinfo->block_length,
115  (int) (info->lock_type != F_UNLCK)));
116 } /* _mi_dispose */
117 
118 
119  /* Make new page on disk */
120 
121 my_off_t _mi_new(register MI_INFO *info, MI_KEYDEF *keyinfo, int level)
122 {
123  my_off_t pos;
124  uchar buff[8];
125  DBUG_ENTER("_mi_new");
126 
127  if ((pos= info->s->state.key_del[keyinfo->block_size_index]) ==
128  HA_OFFSET_ERROR)
129  {
130  if (info->state->key_file_length >=
131  info->s->base.max_key_file_length - keyinfo->block_length)
132  {
133  my_errno=HA_ERR_INDEX_FILE_FULL;
134  DBUG_RETURN(HA_OFFSET_ERROR);
135  }
136  pos=info->state->key_file_length;
137  info->state->key_file_length+= keyinfo->block_length;
138  }
139  else
140  {
141  if (!key_cache_read(info->s->key_cache,
142  info->s->kfile, pos, level,
143  buff,
144  (uint) sizeof(buff),
145  (uint) keyinfo->block_length,0))
146  pos= HA_OFFSET_ERROR;
147  else
148  info->s->state.key_del[keyinfo->block_size_index]= mi_sizekorr(buff);
149  }
150  info->s->state.changed|= STATE_NOT_SORTED_PAGES;
151  DBUG_PRINT("exit",("Pos: %ld",(long) pos));
152  DBUG_RETURN(pos);
153 } /* _mi_new */