Drizzled Public API Documentation

mi_page.cc
1 /* Copyright (C) 2000-2004, 2006 MySQL AB
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 "myisam_priv.h"
19 
20 using namespace drizzled;
21 
22  /* Fetch a key-page in memory */
23 
24 unsigned char *_mi_fetch_keypage(MI_INFO *info, MI_KEYDEF *keyinfo,
25  internal::my_off_t page, int, unsigned char *buff, int)
26 {
27  if (not pread(info->s->kfile, buff, keyinfo->block_length, page))
28  {
29  info->last_keypage=HA_OFFSET_ERROR;
30  mi_print_error(info->s, HA_ERR_CRASHED);
31  errno=HA_ERR_CRASHED;
32  return(0);
33  }
34  unsigned char* tmp= buff;
35  if (tmp == info->buff)
36  info->buff_used=1;
37  info->last_keypage=page;
38  uint32_t page_size=mi_getint(tmp);
39  if (page_size < 4 || page_size > keyinfo->block_length)
40  {
41  info->last_keypage = HA_OFFSET_ERROR;
42  mi_print_error(info->s, HA_ERR_CRASHED);
43  errno = HA_ERR_CRASHED;
44  tmp = 0;
45  }
46  return(tmp);
47 } /* _mi_fetch_keypage */
48 
49 
50  /* Write a key-page on disk */
51 
52 int _mi_write_keypage(MI_INFO *info, MI_KEYDEF *keyinfo,
53  internal::my_off_t page, int, unsigned char *buff)
54 {
55  uint32_t length;
56 
57 #ifndef FAST /* Safety check */
58  if (page < info->s->base.keystart ||
59  page+keyinfo->block_length > info->state->key_file_length ||
60  (page & (MI_MIN_KEY_BLOCK_LENGTH-1)))
61  {
62  errno=EINVAL;
63  return((-1));
64  }
65 #endif
66 
67  if ((length=keyinfo->block_length) > IO_SIZE*2 &&
68  info->state->key_file_length != page+length)
69  length= ((mi_getint(buff)+IO_SIZE-1) & (uint) ~(IO_SIZE-1));
70 #ifdef HAVE_VALGRIND
71  {
72  length=mi_getint(buff);
73  memset(buff+length, 0, keyinfo->block_length-length);
74  length=keyinfo->block_length;
75  }
76 #endif
77  return not pwrite(info->s->kfile, buff, length, page);
78 } /* mi_write_keypage */
79 
80 
81  /* Remove page from disk */
82 
83 int _mi_dispose(MI_INFO *info, MI_KEYDEF *keyinfo, internal::my_off_t pos, int)
84 {
85  internal::my_off_t old_link;
86  unsigned char buff[8];
87 
88  old_link= info->s->state.key_del[keyinfo->block_size_index];
89  info->s->state.key_del[keyinfo->block_size_index]= pos;
90  mi_sizestore(buff,old_link);
91  info->s->state.changed|= STATE_NOT_SORTED_PAGES;
92  return not pwrite(info->s->kfile, buff, sizeof(buff), pos);
93 } /* _mi_dispose */
94 
95 
96  /* Make new page on disk */
97 
98 internal::my_off_t _mi_new(MI_INFO *info, MI_KEYDEF *keyinfo, int)
99 {
100  internal::my_off_t pos;
101  unsigned char buff[8];
102 
103  if ((pos= info->s->state.key_del[keyinfo->block_size_index]) ==
104  HA_OFFSET_ERROR)
105  {
106  if (info->state->key_file_length >=
107  info->s->base.max_key_file_length - keyinfo->block_length)
108  {
109  errno=HA_ERR_INDEX_FILE_FULL;
110  return(HA_OFFSET_ERROR);
111  }
112  pos=info->state->key_file_length;
113  info->state->key_file_length+= keyinfo->block_length;
114  }
115  else if (pread(info->s->kfile, buff, sizeof(buff), pos))
116  info->s->state.key_del[keyinfo->block_size_index]= mi_sizekorr(buff);
117  else
118  pos= HA_OFFSET_ERROR;
119  info->s->state.changed|= STATE_NOT_SORTED_PAGES;
120  return(pos);
121 } /* _mi_new */
TODO: Rename this file - func.h is stupid.