~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2002, 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
	/* Functions to handle fixed-length-records */
17
18
#include "myisamdef.h"
19
20
int _mi_write_static_record(MI_INFO *info, const uchar *record)
21
{
22
  uchar temp[8];				/* max pointer length */
23
  if (info->s->state.dellink != HA_OFFSET_ERROR &&
24
      !info->append_insert_at_end)
25
  {
26
    my_off_t filepos=info->s->state.dellink;
27
    info->rec_cache.seek_not_done=1;		/* We have done a seek */
28
    if (info->s->file_read(info, &temp[0],info->s->base.rec_reflength,
29
		info->s->state.dellink+1,
30
		 MYF(MY_NABP)))
31
      goto err;
32
    info->s->state.dellink= _mi_rec_pos(info->s,temp);
33
    info->state->del--;
34
    info->state->empty-=info->s->base.pack_reclength;
35
    if (info->s->file_write(info, record, info->s->base.reclength,
36
		  filepos,
37
		  MYF(MY_NABP)))
38
      goto err;
39
  }
40
  else
41
  {
42
    if (info->state->data_file_length > info->s->base.max_data_file_length-
43
	info->s->base.pack_reclength)
44
    {
45
      my_errno=HA_ERR_RECORD_FILE_FULL;
46
      return(2);
47
    }
48
    if (info->opt_flag & WRITE_CACHE_USED)
49
    {				/* Cash in use */
50
      if (my_b_write(&info->rec_cache, record,
51
		     info->s->base.reclength))
52
	goto err;
53
      if (info->s->base.pack_reclength != info->s->base.reclength)
54
      {
55
	uint length=info->s->base.pack_reclength - info->s->base.reclength;
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
56
	memset(temp, 0, length);
1 by brian
clean slate
57
	if (my_b_write(&info->rec_cache, temp,length))
58
	  goto err;
59
      }
60
    }
61
    else
62
    {
63
      info->rec_cache.seek_not_done=1;		/* We have done a seek */
64
      if (info->s->file_write(info, record, info->s->base.reclength,
65
		    info->state->data_file_length,
66
		    info->s->write_flag))
67
        goto err;
68
      if (info->s->base.pack_reclength != info->s->base.reclength)
69
      {
70
	uint length=info->s->base.pack_reclength - info->s->base.reclength;
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
71
	memset(temp, 0, length);
1 by brian
clean slate
72
	if (info->s->file_write(info, temp,length,
73
		      info->state->data_file_length+
74
		      info->s->base.reclength,
75
		      info->s->write_flag))
76
    goto err;
77
      }
78
    }
79
    info->state->data_file_length+=info->s->base.pack_reclength;
80
    info->s->state.split++;
81
  }
82
  return 0;
83
 err:
84
  return 1;
85
}
86
87
int _mi_update_static_record(MI_INFO *info, my_off_t pos, const uchar *record)
88
{
89
  info->rec_cache.seek_not_done=1;		/* We have done a seek */
90
  return (info->s->file_write(info,
91
                              record, info->s->base.reclength,
92
                              pos,
93
                              MYF(MY_NABP)) != 0);
94
}
95
96
97
int _mi_delete_static_record(MI_INFO *info)
98
{
99
  uchar temp[9];				/* 1+sizeof(uint32) */
100
101
  info->state->del++;
102
  info->state->empty+=info->s->base.pack_reclength;
103
  temp[0]= '\0';			/* Mark that record is deleted */
104
  _mi_dpointer(info,temp+1,info->s->state.dellink);
105
  info->s->state.dellink = info->lastpos;
106
  info->rec_cache.seek_not_done=1;
107
  return (info->s->file_write(info,(uchar*) temp, 1+info->s->rec_reflength,
108
		    info->lastpos, MYF(MY_NABP)) != 0);
109
}
110
111
112
int _mi_cmp_static_record(register MI_INFO *info, register const uchar *old)
113
{
114
  if (info->opt_flag & WRITE_CACHE_USED)
115
  {
116
    if (flush_io_cache(&info->rec_cache))
117
    {
51.1.112 by Jay Pipes
Removed DBUG symbols
118
      return(-1);
1 by brian
clean slate
119
    }
120
    info->rec_cache.seek_not_done=1;		/* We have done a seek */
121
  }
122
123
  if ((info->opt_flag & READ_CHECK_USED))
124
  {						/* If check isn't disabled  */
125
    info->rec_cache.seek_not_done=1;		/* We have done a seek */
126
    if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
127
		 info->lastpos,
128
		 MYF(MY_NABP)))
51.1.112 by Jay Pipes
Removed DBUG symbols
129
      return(-1);
1 by brian
clean slate
130
    if (memcmp(info->rec_buff, old,
131
	       (uint) info->s->base.reclength))
132
    {
133
      my_errno=HA_ERR_RECORD_CHANGED;		/* Record have changed */
51.1.112 by Jay Pipes
Removed DBUG symbols
134
      return(1);
1 by brian
clean slate
135
    }
136
  }
51.1.112 by Jay Pipes
Removed DBUG symbols
137
  return(0);
1 by brian
clean slate
138
}
139
140
141
int _mi_cmp_static_unique(MI_INFO *info, MI_UNIQUEDEF *def,
142
			  const uchar *record, my_off_t pos)
143
{
144
  info->rec_cache.seek_not_done=1;		/* We have done a seek */
145
  if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
146
	       pos, MYF(MY_NABP)))
51.1.112 by Jay Pipes
Removed DBUG symbols
147
    return(-1);
148
  return(mi_unique_comp(def, record, info->rec_buff,
1 by brian
clean slate
149
			     def->null_are_equal));
150
}
151
152
153
	/* Read a fixed-length-record */
154
	/* Returns 0 if Ok. */
155
	/*	   1 if record is deleted */
156
	/*	  MY_FILE_ERROR on read-error or locking-error */
157
158
int _mi_read_static_record(register MI_INFO *info, register my_off_t pos,
159
			   register uchar *record)
160
{
161
  int error;
162
163
  if (pos != HA_OFFSET_ERROR)
164
  {
165
    if (info->opt_flag & WRITE_CACHE_USED &&
166
	info->rec_cache.pos_in_file <= pos &&
167
	flush_io_cache(&info->rec_cache))
168
      return(-1);
169
    info->rec_cache.seek_not_done=1;		/* We have done a seek */
170
171
    error=info->s->file_read(info, record, info->s->base.reclength,
172
		   pos,MYF(MY_NABP)) != 0;
173
    fast_mi_writeinfo(info);
174
    if (! error)
175
    {
176
      if (!*record)
177
      {
178
	my_errno=HA_ERR_RECORD_DELETED;
179
	return(1);				/* Record is deleted */
180
      }
181
      info->update|= HA_STATE_AKTIV;		/* Record is read */
182
      return(0);
183
    }
184
    return(-1);					/* Error on read */
185
  }
186
  fast_mi_writeinfo(info);			/* No such record */
187
  return(-1);
188
}
189
190
191
192
int _mi_read_rnd_static_record(MI_INFO *info, uchar *buf,
193
			       register my_off_t filepos,
194
			       my_bool skip_deleted_blocks)
195
{
196
  int locked,error,cache_read;
197
  uint cache_length;
198
  MYISAM_SHARE *share=info->s;
199
200
  cache_read=0;
201
  cache_length=0;
202
  if (info->opt_flag & WRITE_CACHE_USED &&
203
      (info->rec_cache.pos_in_file <= filepos || skip_deleted_blocks) &&
204
      flush_io_cache(&info->rec_cache))
51.1.112 by Jay Pipes
Removed DBUG symbols
205
    return(my_errno);
1 by brian
clean slate
206
  if (info->opt_flag & READ_CACHE_USED)
207
  {						/* Cache in use */
208
    if (filepos == my_b_tell(&info->rec_cache) &&
209
	(skip_deleted_blocks || !filepos))
210
    {
211
      cache_read=1;				/* Read record using cache */
212
      cache_length=(uint) (info->rec_cache.read_end - info->rec_cache.read_pos);
213
    }
214
    else
215
      info->rec_cache.seek_not_done=1;		/* Filepos is changed */
216
  }
217
  locked=0;
218
  if (info->lock_type == F_UNLCK)
219
  {
220
    if (filepos >= info->state->data_file_length)
221
    {						/* Test if new records */
222
      if (_mi_readinfo(info,F_RDLCK,0))
51.1.112 by Jay Pipes
Removed DBUG symbols
223
	return(my_errno);
1 by brian
clean slate
224
      locked=1;
225
    }
226
    else
227
    {						/* We don't nead new info */
228
#ifndef UNSAFE_LOCKING
77.1.96 by Monty Taylor
Removed skip-external-locking.
229
      locked=1;
1 by brian
clean slate
230
#else
231
      info->tmp_lock_type=F_RDLCK;
232
#endif
233
    }
234
  }
235
  if (filepos >= info->state->data_file_length)
236
  {
237
    fast_mi_writeinfo(info);
51.1.112 by Jay Pipes
Removed DBUG symbols
238
    return(my_errno=HA_ERR_END_OF_FILE);
1 by brian
clean slate
239
  }
240
  info->lastpos= filepos;
241
  info->nextpos= filepos+share->base.pack_reclength;
242
243
  if (! cache_read)			/* No cacheing */
244
  {
245
    if ((error=_mi_read_static_record(info,filepos,buf)))
246
    {
247
      if (error > 0)
248
	error=my_errno=HA_ERR_RECORD_DELETED;
249
      else
250
	error=my_errno;
251
    }
51.1.112 by Jay Pipes
Removed DBUG symbols
252
    return(error);
1 by brian
clean slate
253
  }
254
255
	/* Read record with cacheing */
256
  error=my_b_read(&info->rec_cache,(uchar*) buf,share->base.reclength);
257
  if (info->s->base.pack_reclength != info->s->base.reclength && !error)
258
  {
259
    char tmp[8];				/* Skill fill bytes */
260
    error=my_b_read(&info->rec_cache,(uchar*) tmp,
261
		    info->s->base.pack_reclength - info->s->base.reclength);
262
  }
263
  if (locked)
264
    VOID(_mi_writeinfo(info,0));		/* Unlock keyfile */
265
  if (!error)
266
  {
267
    if (!buf[0])
268
    {						/* Record is removed */
51.1.112 by Jay Pipes
Removed DBUG symbols
269
      return(my_errno=HA_ERR_RECORD_DELETED);
1 by brian
clean slate
270
    }
271
						/* Found and may be updated */
272
    info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
51.1.112 by Jay Pipes
Removed DBUG symbols
273
    return(0);
1 by brian
clean slate
274
  }
275
  /* my_errno should be set if rec_cache.error == -1 */
276
  if (info->rec_cache.error != -1 || my_errno == 0)
277
    my_errno=HA_ERR_WRONG_IN_RECORD;
51.1.112 by Jay Pipes
Removed DBUG symbols
278
  return(my_errno);			/* Something wrong (EOF?) */
1 by brian
clean slate
279
}