1
/* Copyright (C) 2006 MySQL AB & MySQL Finland AB & TCX DataKonsult 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 "maria_def.h"
21
my_bool _ma_write_static_record(MARIA_HA *info, const uchar *record)
23
uchar temp[8]; /* max pointer length */
24
if (info->s->state.dellink != HA_OFFSET_ERROR &&
25
!info->append_insert_at_end)
27
my_off_t filepos=info->s->state.dellink;
28
info->rec_cache.seek_not_done=1; /* We have done a seek */
29
if (info->s->file_read(info, &temp[0],info->s->base.rec_reflength,
30
info->s->state.dellink+1,
33
info->s->state.dellink= _ma_rec_pos(info->s, temp);
35
info->state->empty-=info->s->base.pack_reclength;
36
if (info->s->file_write(info, record, info->s->base.reclength,
37
filepos, MYF(MY_NABP)))
42
if (info->state->data_file_length > info->s->base.max_data_file_length-
43
info->s->base.pack_reclength)
45
my_errno=HA_ERR_RECORD_FILE_FULL;
48
if (info->opt_flag & WRITE_CACHE_USED)
50
if (my_b_write(&info->rec_cache, record,
51
info->s->base.reclength))
53
if (info->s->base.pack_reclength != info->s->base.reclength)
55
uint length=info->s->base.pack_reclength - info->s->base.reclength;
57
if (my_b_write(&info->rec_cache, temp,length))
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,
68
if (info->s->base.pack_reclength != info->s->base.reclength)
70
uint length=info->s->base.pack_reclength - info->s->base.reclength;
72
if (info->s->file_write(info, temp,length,
73
info->state->data_file_length+
74
info->s->base.reclength,
79
info->state->data_file_length+=info->s->base.pack_reclength;
80
info->s->state.split++;
87
my_bool _ma_update_static_record(MARIA_HA *info, MARIA_RECORD_POS pos,
88
const uchar *oldrec __attribute__ ((unused)),
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
my_bool _ma_delete_static_record(MARIA_HA *info,
100
const uchar *record __attribute__ ((unused)))
102
uchar temp[9]; /* 1+sizeof(uint32) */
104
info->state->empty+=info->s->base.pack_reclength;
105
temp[0]= '\0'; /* Mark that record is deleted */
106
_ma_dpointer(info->s, temp+1, info->s->state.dellink);
107
info->s->state.dellink= info->cur_row.lastpos;
108
info->rec_cache.seek_not_done=1;
109
return (info->s->file_write(info, temp, 1+info->s->rec_reflength,
110
info->cur_row.lastpos, MYF(MY_NABP)) != 0);
114
my_bool _ma_cmp_static_record(register MARIA_HA *info,
115
register const uchar *old)
117
DBUG_ENTER("_ma_cmp_static_record");
119
/* We are going to do changes; dont let anybody disturb */
120
dont_break(); /* Dont allow SIGHUP or SIGINT */
122
if (info->opt_flag & WRITE_CACHE_USED)
124
if (flush_io_cache(&info->rec_cache))
128
info->rec_cache.seek_not_done=1; /* We have done a seek */
131
if ((info->opt_flag & READ_CHECK_USED))
132
{ /* If check isn't disabled */
133
info->rec_cache.seek_not_done=1; /* We have done a seek */
134
if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
135
info->cur_row.lastpos, MYF(MY_NABP)))
137
if (memcmp(info->rec_buff, old, (uint) info->s->base.reclength))
139
DBUG_DUMP("read",old,info->s->base.reclength);
140
DBUG_DUMP("disk",info->rec_buff,info->s->base.reclength);
141
my_errno=HA_ERR_RECORD_CHANGED; /* Record have changed */
149
my_bool _ma_cmp_static_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
150
const uchar *record, MARIA_RECORD_POS pos)
152
DBUG_ENTER("_ma_cmp_static_unique");
154
info->rec_cache.seek_not_done=1; /* We have done a seek */
155
if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
158
DBUG_RETURN(_ma_unique_comp(def, record, (uchar*) info->rec_buff,
159
def->null_are_equal));
164
Read a fixed-length-record
169
-1 on read-error or locking-error
172
int _ma_read_static_record(register MARIA_HA *info, register uchar *record,
173
MARIA_RECORD_POS pos)
176
DBUG_ENTER("_ma_read_static_record");
178
if (pos != HA_OFFSET_ERROR)
180
if (info->opt_flag & WRITE_CACHE_USED &&
181
info->rec_cache.pos_in_file <= pos &&
182
flush_io_cache(&info->rec_cache))
183
DBUG_RETURN(my_errno);
184
info->rec_cache.seek_not_done=1; /* We have done a seek */
186
error= (int) info->s->file_read(info, record,info->s->base.reclength,
190
fast_ma_writeinfo(info);
193
/* Record is deleted */
194
DBUG_PRINT("warning", ("Record is deleted"));
195
DBUG_RETURN((my_errno=HA_ERR_RECORD_DELETED));
197
info->update|= HA_STATE_AKTIV; /* Record is read */
201
fast_ma_writeinfo(info); /* No such record */
202
DBUG_RETURN(my_errno);
207
@brief Read record from given position or next record
210
When scanning, this function will return HA_ERR_RECORD_DELETED
211
for deleted rows even if skip_deleted_blocks is set.
212
The reason for this is to allow the caller to calculate the record
213
position without having to do call maria_position() for each record.
216
int _ma_read_rnd_static_record(MARIA_HA *info, uchar *buf,
217
MARIA_RECORD_POS filepos,
218
my_bool skip_deleted_blocks)
220
int locked,error,cache_read;
222
MARIA_SHARE *share= info->s;
223
DBUG_ENTER("_ma_read_rnd_static_record");
227
if (info->opt_flag & READ_CACHE_USED)
229
if (filepos == my_b_tell(&info->rec_cache) &&
230
(skip_deleted_blocks || !filepos))
232
cache_read=1; /* Read record using cache */
233
cache_length= (uint) (info->rec_cache.read_end -
234
info->rec_cache.read_pos);
237
info->rec_cache.seek_not_done=1; /* Filepos is changed */
240
if (info->lock_type == F_UNLCK)
242
if (filepos >= info->state->data_file_length)
243
{ /* Test if new records */
244
if (_ma_readinfo(info,F_RDLCK,0))
245
DBUG_RETURN(my_errno);
249
{ /* We don't nead new info */
250
#ifndef UNSAFE_LOCKING
251
if ((! cache_read || share->base.reclength > cache_length) &&
252
share->tot_locks == 0)
253
{ /* record not in cache */
257
info->tmp_lock_type=F_RDLCK;
261
if (filepos >= info->state->data_file_length)
263
DBUG_PRINT("test",("filepos: %ld (%ld) records: %ld del: %ld",
264
(long) filepos/share->base.reclength, (long) filepos,
265
(long) info->state->records, (long) info->state->del));
266
fast_ma_writeinfo(info);
267
DBUG_RETURN(my_errno=HA_ERR_END_OF_FILE);
269
info->cur_row.lastpos= filepos;
270
info->cur_row.nextpos= filepos+share->base.pack_reclength;
272
if (! cache_read) /* No cacheing */
274
error= _ma_read_static_record(info, buf, filepos);
278
/* Read record with cacheing */
279
error=my_b_read(&info->rec_cache,(uchar*) buf,share->base.reclength);
280
if (info->s->base.pack_reclength != info->s->base.reclength && !error)
282
char tmp[8]; /* Skill fill bytes */
283
error=my_b_read(&info->rec_cache,(uchar*) tmp,
284
info->s->base.pack_reclength - info->s->base.reclength);
287
(void)(_ma_writeinfo(info,0)); /* Unlock keyfile */
291
{ /* Record is removed */
292
DBUG_RETURN(my_errno=HA_ERR_RECORD_DELETED);
294
/* Found and may be updated */
295
info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
298
/* my_errno should be set if rec_cache.error == -1 */
299
if (info->rec_cache.error != -1 || my_errno == 0)
300
my_errno=HA_ERR_WRONG_IN_RECORD;
301
DBUG_RETURN(my_errno); /* Something wrong (EOF?) */