~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to storage/myisam/mi_page.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
  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", (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;
 
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
  register 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
#ifdef HAVE_purify
 
90
  {
 
91
    length=mi_getint(buff);
 
92
    bzero((uchar*) buff+length,keyinfo->block_length-length);
 
93
    length=keyinfo->block_length;
 
94
  }
 
95
#endif
 
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 */
 
102
 
 
103
 
 
104
        /* Remove page from disk */
 
105
 
 
106
int _mi_dispose(register MI_INFO *info, MI_KEYDEF *keyinfo, my_off_t pos,
 
107
                int level)
 
108
{
 
109
  my_off_t old_link;
 
110
  uchar buff[8];
 
111
  DBUG_ENTER("_mi_dispose");
 
112
  DBUG_PRINT("enter",("pos: %ld", (long) pos));
 
113
 
 
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,
 
120
                              sizeof(buff),
 
121
                              (uint) keyinfo->block_length,
 
122
                              (int) (info->lock_type != F_UNLCK)));
 
123
} /* _mi_dispose */
 
124
 
 
125
 
 
126
        /* Make new page on disk */
 
127
 
 
128
my_off_t _mi_new(register MI_INFO *info, MI_KEYDEF *keyinfo, int level)
 
129
{
 
130
  my_off_t pos;
 
131
  uchar buff[8];
 
132
  DBUG_ENTER("_mi_new");
 
133
 
 
134
  if ((pos= info->s->state.key_del[keyinfo->block_size_index]) ==
 
135
      HA_OFFSET_ERROR)
 
136
  {
 
137
    if (info->state->key_file_length >=
 
138
        info->s->base.max_key_file_length - keyinfo->block_length)
 
139
    {
 
140
      my_errno=HA_ERR_INDEX_FILE_FULL;
 
141
      DBUG_RETURN(HA_OFFSET_ERROR);
 
142
    }
 
143
    pos=info->state->key_file_length;
 
144
    info->state->key_file_length+= keyinfo->block_length;
 
145
  }
 
146
  else
 
147
  {
 
148
    if (!key_cache_read(info->s->key_cache,
 
149
                        info->s->kfile, pos, level,
 
150
                        buff,
 
151
                        (uint) sizeof(buff),
 
152
                        (uint) keyinfo->block_length,0))
 
153
      pos= HA_OFFSET_ERROR;
 
154
    else
 
155
      info->s->state.key_del[keyinfo->block_size_index]= mi_sizekorr(buff);
 
156
  }
 
157
  info->s->state.changed|= STATE_NOT_SORTED_PAGES;
 
158
  DBUG_PRINT("exit",("Pos: %ld",(long) pos));
 
159
  DBUG_RETURN(pos);
 
160
} /* _mi_new */