1 : /* Copyright (C) 2006 MySQL AB & MySQL Finland AB & TCX DataKonsult 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 "maria_def.h"
19 :
20 :
21 : my_bool _ma_write_static_record(MARIA_HA *info, const uchar *record)
22 20779 : {
23 : uchar temp[8]; /* max pointer length */
24 20779 : if (info->s->state.dellink != HA_OFFSET_ERROR &&
25 : !info->append_insert_at_end)
26 : {
27 0 : my_off_t filepos=info->s->state.dellink;
28 0 : info->rec_cache.seek_not_done=1; /* We have done a seek */
29 0 : if (info->s->file_read(info, &temp[0],info->s->base.rec_reflength,
30 : info->s->state.dellink+1,
31 : MYF(MY_NABP)))
32 0 : goto err;
33 0 : info->s->state.dellink= _ma_rec_pos(info->s, temp);
34 0 : info->state->del--;
35 0 : info->state->empty-=info->s->base.pack_reclength;
36 0 : if (info->s->file_write(info, record, info->s->base.reclength,
37 : filepos, MYF(MY_NABP)))
38 : goto err;
39 : }
40 : else
41 : {
42 20779 : if (info->state->data_file_length > info->s->base.max_data_file_length-
43 : info->s->base.pack_reclength)
44 : {
45 0 : my_errno=HA_ERR_RECORD_FILE_FULL;
46 0 : return(2);
47 : }
48 20779 : if (info->opt_flag & WRITE_CACHE_USED)
49 : { /* Cash in use */
50 2281 : if (my_b_write(&info->rec_cache, record,
51 : info->s->base.reclength))
52 2281 : goto err;
53 2281 : if (info->s->base.pack_reclength != info->s->base.reclength)
54 : {
55 0 : uint length=info->s->base.pack_reclength - info->s->base.reclength;
56 0 : bzero(temp,length);
57 0 : if (my_b_write(&info->rec_cache, temp,length))
58 : goto err;
59 : }
60 : }
61 : else
62 : {
63 18498 : info->rec_cache.seek_not_done=1; /* We have done a seek */
64 18498 : if (info->s->file_write(info, record, info->s->base.reclength,
65 : info->state->data_file_length,
66 : info->s->write_flag))
67 18498 : goto err;
68 18498 : if (info->s->base.pack_reclength != info->s->base.reclength)
69 : {
70 0 : uint length=info->s->base.pack_reclength - info->s->base.reclength;
71 0 : bzero(temp,length);
72 0 : 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 20779 : goto err;
77 : }
78 : }
79 20779 : info->state->data_file_length+=info->s->base.pack_reclength;
80 20779 : info->s->state.split++;
81 : }
82 20779 : return 0;
83 0 : err:
84 0 : return 1;
85 : }
86 :
87 : my_bool _ma_update_static_record(MARIA_HA *info, MARIA_RECORD_POS pos,
88 : const uchar *oldrec __attribute__ ((unused)),
89 : const uchar *record)
90 2552 : {
91 2552 : info->rec_cache.seek_not_done=1; /* We have done a seek */
92 2552 : return (info->s->file_write(info,
93 : record, info->s->base.reclength,
94 : pos,
95 : MYF(MY_NABP)) != 0);
96 : }
97 :
98 :
99 : my_bool _ma_delete_static_record(MARIA_HA *info,
100 : const uchar *record __attribute__ ((unused)))
101 18363 : {
102 : uchar temp[9]; /* 1+sizeof(uint32) */
103 18363 : info->state->del++;
104 18363 : info->state->empty+=info->s->base.pack_reclength;
105 18363 : temp[0]= '\0'; /* Mark that record is deleted */
106 18363 : _ma_dpointer(info->s, temp+1, info->s->state.dellink);
107 18363 : info->s->state.dellink= info->cur_row.lastpos;
108 18363 : info->rec_cache.seek_not_done=1;
109 18363 : return (info->s->file_write(info, temp, 1+info->s->rec_reflength,
110 : info->cur_row.lastpos, MYF(MY_NABP)) != 0);
111 : }
112 :
113 :
114 : my_bool _ma_cmp_static_record(register MARIA_HA *info,
115 : register const uchar *old)
116 22949 : {
117 22949 : DBUG_ENTER("_ma_cmp_static_record");
118 :
119 : /* We are going to do changes; dont let anybody disturb */
120 : dont_break(); /* Dont allow SIGHUP or SIGINT */
121 :
122 22949 : if (info->opt_flag & WRITE_CACHE_USED)
123 : {
124 0 : if (flush_io_cache(&info->rec_cache))
125 : {
126 0 : DBUG_RETURN(1);
127 : }
128 0 : info->rec_cache.seek_not_done=1; /* We have done a seek */
129 : }
130 :
131 22949 : if ((info->opt_flag & READ_CHECK_USED))
132 : { /* If check isn't disabled */
133 22949 : info->rec_cache.seek_not_done=1; /* We have done a seek */
134 22949 : if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
135 : info->cur_row.lastpos, MYF(MY_NABP)))
136 0 : DBUG_RETURN(1);
137 22949 : if (memcmp(info->rec_buff, old, (uint) info->s->base.reclength))
138 : {
139 0 : DBUG_DUMP("read",old,info->s->base.reclength);
140 0 : DBUG_DUMP("disk",info->rec_buff,info->s->base.reclength);
141 0 : my_errno=HA_ERR_RECORD_CHANGED; /* Record have changed */
142 0 : DBUG_RETURN(1);
143 : }
144 : }
145 22949 : DBUG_RETURN(0);
146 : }
147 :
148 :
149 : my_bool _ma_cmp_static_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
150 : const uchar *record, MARIA_RECORD_POS pos)
151 12 : {
152 12 : DBUG_ENTER("_ma_cmp_static_unique");
153 :
154 12 : info->rec_cache.seek_not_done=1; /* We have done a seek */
155 12 : if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
156 : pos, MYF(MY_NABP)))
157 0 : DBUG_RETURN(1);
158 12 : DBUG_RETURN(_ma_unique_comp(def, record, info->rec_buff,
159 : def->null_are_equal));
160 : }
161 :
162 :
163 : /*
164 : Read a fixed-length-record
165 :
166 : RETURN
167 : 0 Ok
168 : 1 record delete
169 : -1 on read-error or locking-error
170 : */
171 :
172 : int _ma_read_static_record(register MARIA_HA *info, register uchar *record,
173 : MARIA_RECORD_POS pos)
174 104460 : {
175 : int error;
176 104460 : DBUG_ENTER("_ma_read_static_record");
177 :
178 104460 : if (pos != HA_OFFSET_ERROR)
179 : {
180 104460 : if (info->opt_flag & WRITE_CACHE_USED &&
181 : info->rec_cache.pos_in_file <= pos &&
182 : flush_io_cache(&info->rec_cache))
183 0 : DBUG_RETURN(my_errno);
184 104460 : info->rec_cache.seek_not_done=1; /* We have done a seek */
185 :
186 104460 : error= (int) info->s->file_read(info, record,info->s->base.reclength,
187 : pos, MYF(MY_NABP));
188 104460 : if (! error)
189 : {
190 104460 : fast_ma_writeinfo(info);
191 104460 : if (!*record)
192 : {
193 : /* Record is deleted */
194 11120 : DBUG_PRINT("warning", ("Record is deleted"));
195 11120 : DBUG_RETURN((my_errno=HA_ERR_RECORD_DELETED));
196 : }
197 93340 : info->update|= HA_STATE_AKTIV; /* Record is read */
198 93340 : DBUG_RETURN(0);
199 : }
200 : }
201 0 : fast_ma_writeinfo(info); /* No such record */
202 0 : DBUG_RETURN(my_errno);
203 : }
204 :
205 :
206 : /**
207 : @brief Read record from given position or next record
208 :
209 : @note
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.
214 : */
215 :
216 : int _ma_read_rnd_static_record(MARIA_HA *info, uchar *buf,
217 : MARIA_RECORD_POS filepos,
218 : my_bool skip_deleted_blocks)
219 74379 : {
220 : int locked,error,cache_read;
221 : uint cache_length;
222 74379 : MARIA_SHARE *share= info->s;
223 74379 : DBUG_ENTER("_ma_read_rnd_static_record");
224 :
225 74379 : cache_read=0;
226 74379 : cache_length=0;
227 74379 : if (info->opt_flag & READ_CACHE_USED)
228 : { /* Cache in use */
229 36818 : if (filepos == my_b_tell(&info->rec_cache) &&
230 : (skip_deleted_blocks || !filepos))
231 : {
232 18409 : cache_read=1; /* Read record using cache */
233 18409 : cache_length= (uint) (info->rec_cache.read_end -
234 : info->rec_cache.read_pos);
235 : }
236 : else
237 0 : info->rec_cache.seek_not_done=1; /* Filepos is changed */
238 : }
239 74379 : locked=0;
240 74379 : if (info->lock_type == F_UNLCK)
241 : {
242 43904 : if (filepos >= info->state->data_file_length)
243 : { /* Test if new records */
244 90 : if (_ma_readinfo(info,F_RDLCK,0))
245 0 : DBUG_RETURN(my_errno);
246 90 : locked=1;
247 : }
248 : else
249 : { /* We don't nead new info */
250 : #ifndef UNSAFE_LOCKING
251 43814 : if ((! cache_read || share->base.reclength > cache_length) &&
252 : share->tot_locks == 0)
253 : { /* record not in cache */
254 33377 : locked=1;
255 : }
256 : #else
257 : info->tmp_lock_type=F_RDLCK;
258 : #endif
259 : }
260 : }
261 74379 : if (filepos >= info->state->data_file_length)
262 : {
263 125 : 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 125 : fast_ma_writeinfo(info);
267 125 : DBUG_RETURN(my_errno=HA_ERR_END_OF_FILE);
268 : }
269 74254 : info->cur_row.lastpos= filepos;
270 74254 : info->cur_row.nextpos= filepos+share->base.pack_reclength;
271 :
272 74254 : if (! cache_read) /* No cacheing */
273 : {
274 55874 : error= _ma_read_static_record(info, buf, filepos);
275 55874 : DBUG_RETURN(error);
276 : }
277 :
278 : /* Read record with cacheing */
279 18380 : error=my_b_read(&info->rec_cache, buf, share->base.reclength);
280 18380 : if (info->s->base.pack_reclength != info->s->base.reclength && !error)
281 : {
282 : uchar tmp[8]; /* Skill fill bytes */
283 0 : error=my_b_read(&info->rec_cache, tmp,
284 : info->s->base.pack_reclength - info->s->base.reclength);
285 : }
286 18380 : if (locked)
287 18 : VOID(_ma_writeinfo(info,0)); /* Unlock keyfile */
288 18380 : if (!error)
289 : {
290 18380 : if (!buf[0])
291 : { /* Record is removed */
292 3693 : DBUG_RETURN(my_errno=HA_ERR_RECORD_DELETED);
293 : }
294 : /* Found and may be updated */
295 14687 : info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
296 14687 : DBUG_RETURN(0);
297 : }
298 : /* my_errno should be set if rec_cache.error == -1 */
299 0 : if (info->rec_cache.error != -1 || my_errno == 0)
300 0 : my_errno=HA_ERR_WRONG_IN_RECORD;
301 0 : DBUG_RETURN(my_errno); /* Something wrong (EOF?) */
302 : }
|