~drizzle-trunk/drizzle/development

1 by brian
clean slate
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
29
  tmp=(uchar*) key_cache_read(info->s->key_cache,
30
                             info->s->kfile, page, level, (uchar*) buff,
31
			     (uint) keyinfo->block_length,
32
			     (uint) keyinfo->block_length,
33
			     return_buffer);
34
  if (tmp == info->buff)
35
    info->buff_used=1;
36
  else if (!tmp)
37
  {
38
    info->last_keypage=HA_OFFSET_ERROR;
39
    mi_print_error(info->s, HA_ERR_CRASHED);
40
    my_errno=HA_ERR_CRASHED;
51.1.105 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
41
    return(0);
1 by brian
clean slate
42
  }
43
  info->last_keypage=page;
44
  page_size=mi_getint(tmp);
45
  if (page_size < 4 || page_size > keyinfo->block_length)
46
  {
47
    info->last_keypage = HA_OFFSET_ERROR;
48
    mi_print_error(info->s, HA_ERR_CRASHED);
49
    my_errno = HA_ERR_CRASHED;
50
    tmp = 0;
51
  }
51.1.105 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
52
  return(tmp);
1 by brian
clean slate
53
} /* _mi_fetch_keypage */
54
55
56
	/* Write a key-page on disk */
57
58
int _mi_write_keypage(register MI_INFO *info, register MI_KEYDEF *keyinfo,
59
		      my_off_t page, int level, uchar *buff)
60
{
61
  register uint length;
62
63
#ifndef FAST					/* Safety check */
64
  if (page < info->s->base.keystart ||
65
      page+keyinfo->block_length > info->state->key_file_length ||
66
      (page & (MI_MIN_KEY_BLOCK_LENGTH-1)))
67
  {
68
    my_errno=EINVAL;
51.1.105 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
69
    return((-1));
1 by brian
clean slate
70
  }
71
#endif
72
73
  if ((length=keyinfo->block_length) > IO_SIZE*2 &&
74
      info->state->key_file_length != page+length)
75
    length= ((mi_getint(buff)+IO_SIZE-1) & (uint) ~(IO_SIZE-1));
76
#ifdef HAVE_purify
77
  {
78
    length=mi_getint(buff);
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
79
    memset((uchar*) buff+length, 0, keyinfo->block_length-length);
1 by brian
clean slate
80
    length=keyinfo->block_length;
81
  }
82
#endif
51.1.105 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
83
  return((key_cache_write(info->s->key_cache,
1 by brian
clean slate
84
                         info->s->kfile,page, level, (uchar*) buff,length,
85
			 (uint) keyinfo->block_length,
86
			 (int) ((info->lock_type != F_UNLCK) ||
87
				info->s->delay_key_write))));
88
} /* mi_write_keypage */
89
90
91
	/* Remove page from disk */
92
93
int _mi_dispose(register MI_INFO *info, MI_KEYDEF *keyinfo, my_off_t pos,
94
                int level)
95
{
96
  my_off_t old_link;
97
  uchar buff[8];
98
99
  old_link= info->s->state.key_del[keyinfo->block_size_index];
100
  info->s->state.key_del[keyinfo->block_size_index]= pos;
101
  mi_sizestore(buff,old_link);
102
  info->s->state.changed|= STATE_NOT_SORTED_PAGES;
51.1.105 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
103
  return(key_cache_write(info->s->key_cache,
1 by brian
clean slate
104
                              info->s->kfile, pos , level, buff,
105
			      sizeof(buff),
106
			      (uint) keyinfo->block_length,
107
			      (int) (info->lock_type != F_UNLCK)));
108
} /* _mi_dispose */
109
110
111
	/* Make new page on disk */
112
113
my_off_t _mi_new(register MI_INFO *info, MI_KEYDEF *keyinfo, int level)
114
{
115
  my_off_t pos;
116
  uchar buff[8];
117
118
  if ((pos= info->s->state.key_del[keyinfo->block_size_index]) ==
119
      HA_OFFSET_ERROR)
120
  {
121
    if (info->state->key_file_length >=
122
	info->s->base.max_key_file_length - keyinfo->block_length)
123
    {
124
      my_errno=HA_ERR_INDEX_FILE_FULL;
51.1.105 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
125
      return(HA_OFFSET_ERROR);
1 by brian
clean slate
126
    }
127
    pos=info->state->key_file_length;
128
    info->state->key_file_length+= keyinfo->block_length;
129
  }
130
  else
131
  {
132
    if (!key_cache_read(info->s->key_cache,
133
                        info->s->kfile, pos, level,
134
			buff,
135
			(uint) sizeof(buff),
136
			(uint) keyinfo->block_length,0))
137
      pos= HA_OFFSET_ERROR;
138
    else
139
      info->s->state.key_del[keyinfo->block_size_index]= mi_sizekorr(buff);
140
  }
141
  info->s->state.changed|= STATE_NOT_SORTED_PAGES;
51.1.105 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
142
  return(pos);
1 by brian
clean slate
143
} /* _mi_new */