1
/* Copyright (C) 2000-2004, 2006 MySQL AB
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* Read and write key blocks */
18
#include "myisamdef.h"
20
/* Fetch a key-page in memory */
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)
28
DBUG_ENTER("_mi_fetch_keypage");
29
DBUG_PRINT("enter",("page: %ld", (long) page));
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,
36
if (tmp == info->buff)
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;
46
info->last_keypage=page;
47
page_size=mi_getint(tmp);
48
if (page_size < 4 || page_size > keyinfo->block_length)
50
DBUG_PRINT("error",("page %lu had wrong page length: %u",
51
(ulong) page, page_size));
52
DBUG_DUMP("page", (uchar*) 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;
59
} /* _mi_fetch_keypage */
62
/* Write a key-page on disk */
64
int _mi_write_keypage(register MI_INFO *info, register MI_KEYDEF *keyinfo,
65
my_off_t page, int level, uchar *buff)
68
DBUG_ENTER("_mi_write_keypage");
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)))
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,
82
DBUG_PRINT("page",("write page at: %lu",(long) page));
83
DBUG_DUMP("buff",(uchar*) buff,mi_getint(buff));
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));
91
length=mi_getint(buff);
92
bzero((uchar*) buff+length,keyinfo->block_length-length);
93
length=keyinfo->block_length;
96
DBUG_RETURN((key_cache_write(info->s->key_cache,
97
info->s->kfile,page, level, (uchar*) buff,length,
98
(uint) keyinfo->block_length,
99
(int) ((info->lock_type != F_UNLCK) ||
100
info->s->delay_key_write))));
101
} /* mi_write_keypage */
104
/* Remove page from disk */
106
int _mi_dispose(register MI_INFO *info, MI_KEYDEF *keyinfo, my_off_t pos,
111
DBUG_ENTER("_mi_dispose");
112
DBUG_PRINT("enter",("pos: %ld", (long) pos));
114
old_link= info->s->state.key_del[keyinfo->block_size_index];
115
info->s->state.key_del[keyinfo->block_size_index]= pos;
116
mi_sizestore(buff,old_link);
117
info->s->state.changed|= STATE_NOT_SORTED_PAGES;
118
DBUG_RETURN(key_cache_write(info->s->key_cache,
119
info->s->kfile, pos , level, buff,
121
(uint) keyinfo->block_length,
122
(int) (info->lock_type != F_UNLCK)));
126
/* Make new page on disk */
128
my_off_t _mi_new(register MI_INFO *info, MI_KEYDEF *keyinfo, int level)
132
DBUG_ENTER("_mi_new");
134
if ((pos= info->s->state.key_del[keyinfo->block_size_index]) ==
137
if (info->state->key_file_length >=
138
info->s->base.max_key_file_length - keyinfo->block_length)
140
my_errno=HA_ERR_INDEX_FILE_FULL;
141
DBUG_RETURN(HA_OFFSET_ERROR);
143
pos=info->state->key_file_length;
144
info->state->key_file_length+= keyinfo->block_length;
148
if (!key_cache_read(info->s->key_cache,
149
info->s->kfile, pos, level,
152
(uint) keyinfo->block_length,0))
153
pos= HA_OFFSET_ERROR;
155
info->s->state.key_del[keyinfo->block_size_index]= mi_sizekorr(buff);
157
info->s->state.changed|= STATE_NOT_SORTED_PAGES;
158
DBUG_PRINT("exit",("Pos: %ld",(long) pos));