1
/* Copyright (C) 2000-2002, 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
/* Functions to handle fixed-length-records */
18
#include "myisam_priv.h"
20
using namespace drizzled;
22
int _mi_write_static_record(MI_INFO *info, const unsigned char *record)
24
unsigned char temp[8]; /* max pointer length */
25
if (info->s->state.dellink != HA_OFFSET_ERROR &&
26
!info->append_insert_at_end)
28
internal::my_off_t filepos=info->s->state.dellink;
29
info->rec_cache.seek_not_done=1; /* We have done a seek */
30
if (info->s->file_read(info, &temp[0],info->s->base.rec_reflength,
31
info->s->state.dellink+1,
34
info->s->state.dellink= _mi_rec_pos(info->s,temp);
36
info->state->empty-=info->s->base.pack_reclength;
37
if (info->s->file_write(info, record, info->s->base.reclength,
44
if (info->state->data_file_length > info->s->base.max_data_file_length-
45
info->s->base.pack_reclength)
47
errno=HA_ERR_RECORD_FILE_FULL;
50
if (info->opt_flag & WRITE_CACHE_USED)
52
if (my_b_write(&info->rec_cache, record,
53
info->s->base.reclength))
55
if (info->s->base.pack_reclength != info->s->base.reclength)
57
uint32_t length=info->s->base.pack_reclength - info->s->base.reclength;
58
memset(temp, 0, length);
59
if (my_b_write(&info->rec_cache, temp,length))
65
info->rec_cache.seek_not_done=1; /* We have done a seek */
66
if (info->s->file_write(info, record, info->s->base.reclength,
67
info->state->data_file_length,
70
if (info->s->base.pack_reclength != info->s->base.reclength)
72
uint32_t length=info->s->base.pack_reclength - info->s->base.reclength;
73
memset(temp, 0, length);
74
if (info->s->file_write(info, temp,length,
75
info->state->data_file_length+
76
info->s->base.reclength,
81
info->state->data_file_length+=info->s->base.pack_reclength;
82
info->s->state.split++;
89
int _mi_update_static_record(MI_INFO *info, internal::my_off_t pos, const unsigned char *record)
91
info->rec_cache.seek_not_done=1; /* We have done a seek */
92
return (info->s->file_write(info,
93
record, info->s->base.reclength,
99
int _mi_delete_static_record(MI_INFO *info)
101
unsigned char temp[9]; /* 1+sizeof(uint32) */
104
info->state->empty+=info->s->base.pack_reclength;
105
temp[0]= '\0'; /* Mark that record is deleted */
106
_mi_dpointer(info,temp+1,info->s->state.dellink);
107
info->s->state.dellink = info->lastpos;
108
info->rec_cache.seek_not_done=1;
109
return (info->s->file_write(info,(unsigned char*) temp, 1+info->s->rec_reflength,
110
info->lastpos, MYF(MY_NABP)) != 0);
114
int _mi_cmp_static_record(register MI_INFO *info, register const unsigned char *old)
116
if (info->opt_flag & WRITE_CACHE_USED)
118
if (flush_io_cache(&info->rec_cache))
122
info->rec_cache.seek_not_done=1; /* We have done a seek */
125
if ((info->opt_flag & READ_CHECK_USED))
126
{ /* If check isn't disabled */
127
info->rec_cache.seek_not_done=1; /* We have done a seek */
128
if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
132
if (memcmp(info->rec_buff, old,
133
(uint) info->s->base.reclength))
135
errno=HA_ERR_RECORD_CHANGED; /* Record have changed */
143
int _mi_cmp_static_unique(MI_INFO *info, MI_UNIQUEDEF *def,
144
const unsigned char *record, internal::my_off_t pos)
146
info->rec_cache.seek_not_done=1; /* We have done a seek */
147
if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
150
return(mi_unique_comp(def, record, info->rec_buff,
151
def->null_are_equal));
155
/* Read a fixed-length-record */
156
/* Returns 0 if Ok. */
157
/* 1 if record is deleted */
158
/* MY_FILE_ERROR on read-error or locking-error */
160
int _mi_read_static_record(register MI_INFO *info, register internal::my_off_t pos,
161
register unsigned char *record)
165
if (pos != HA_OFFSET_ERROR)
167
if (info->opt_flag & WRITE_CACHE_USED &&
168
info->rec_cache.pos_in_file <= pos &&
169
flush_io_cache(&info->rec_cache))
171
info->rec_cache.seek_not_done=1; /* We have done a seek */
173
error=info->s->file_read(info, record, info->s->base.reclength,
174
pos,MYF(MY_NABP)) != 0;
175
fast_mi_writeinfo(info);
180
errno=HA_ERR_RECORD_DELETED;
181
return(1); /* Record is deleted */
183
info->update|= HA_STATE_AKTIV; /* Record is read */
186
return(-1); /* Error on read */
188
fast_mi_writeinfo(info); /* No such record */
194
int _mi_read_rnd_static_record(MI_INFO *info, unsigned char *buf,
195
register internal::my_off_t filepos,
196
bool skip_deleted_blocks)
198
int locked,error,cache_read;
199
uint32_t cache_length;
200
MYISAM_SHARE *share=info->s;
204
if (info->opt_flag & WRITE_CACHE_USED &&
205
(info->rec_cache.pos_in_file <= filepos || skip_deleted_blocks) &&
206
flush_io_cache(&info->rec_cache))
208
if (info->opt_flag & READ_CACHE_USED)
210
if (filepos == my_b_tell(&info->rec_cache) &&
211
(skip_deleted_blocks || !filepos))
213
cache_read=1; /* Read record using cache */
214
cache_length=(uint) (info->rec_cache.read_end - info->rec_cache.read_pos);
217
info->rec_cache.seek_not_done=1; /* Filepos is changed */
220
if (info->lock_type == F_UNLCK)
222
if (filepos >= info->state->data_file_length)
223
{ /* Test if new records */
224
if (_mi_readinfo(info,F_RDLCK,0))
229
{ /* We don't nead new info */
230
#ifndef UNSAFE_LOCKING
233
info->tmp_lock_type=F_RDLCK;
237
if (filepos >= info->state->data_file_length)
239
fast_mi_writeinfo(info);
240
return(errno=HA_ERR_END_OF_FILE);
242
info->lastpos= filepos;
243
info->nextpos= filepos+share->base.pack_reclength;
245
if (! cache_read) /* No cacheing */
247
if ((error=_mi_read_static_record(info,filepos,buf)))
250
error=errno=HA_ERR_RECORD_DELETED;
257
/* Read record with cacheing */
258
error=my_b_read(&info->rec_cache,(unsigned char*) buf,share->base.reclength);
259
if (info->s->base.pack_reclength != info->s->base.reclength && !error)
261
char tmp[8]; /* Skill fill bytes */
262
error=my_b_read(&info->rec_cache,(unsigned char*) tmp,
263
info->s->base.pack_reclength - info->s->base.reclength);
266
_mi_writeinfo(info,0); /* Unlock keyfile */
270
{ /* Record is removed */
271
return(errno=HA_ERR_RECORD_DELETED);
273
/* Found and may be updated */
274
info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
277
/* errno should be set if rec_cache.error == -1 */
278
if (info->rec_cache.error != -1 || errno == 0)
279
errno=HA_ERR_WRONG_IN_RECORD;
280
return(errno); /* Something wrong (EOF?) */