drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
mi_dynrec.cc
1/* Copyright (C) 2000-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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16/*
17 Functions to handle space-packed-records and blobs
18
19 A row may be stored in one or more linked blocks.
20 The block size is between MI_MIN_BLOCK_LENGTH and MI_MAX_BLOCK_LENGTH.
21 Each block is aligned on MI_DYN_ALIGN_SIZE.
22 The reson for the max block size is to not have too many different types
23 of blocks. For the differnet block types, look at _mi_get_block_info()
24*/
25
26#include "myisam_priv.h"
27
28#ifdef HAVE_SYS_TYPES
29#include <sys/types.h>
30#endif
31#include <sys/mman.h>
32#include <drizzled/util/test.h>
33#include <drizzled/error.h>
34
35#include <cassert>
36#include <algorithm>
37
38using namespace drizzled;
39using namespace std;
40
41/* Enough for comparing if number is zero */
42static char zero_string[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
43
44static int write_dynamic_record(MI_INFO *info,const unsigned char *record,
45 ulong reclength);
46static int _mi_find_writepos(MI_INFO *info,ulong reclength,internal::my_off_t *filepos,
47 ulong *length);
48static int update_dynamic_record(MI_INFO *info,internal::my_off_t filepos,unsigned char *record,
49 ulong reclength);
50static int delete_dynamic_record(MI_INFO *info,internal::my_off_t filepos,
51 uint32_t second_read);
52static int _mi_cmp_buffer(int file, const unsigned char *buff, internal::my_off_t filepos,
53 uint32_t length);
54
55 /* Interface function from MI_INFO */
56
57
58/*
59 Create mmaped area for MyISAM handler
60
61 SYNOPSIS
62 mi_dynmap_file()
63 info MyISAM handler
64
65 RETURN
66 0 ok
67 1 error.
68*/
69
70bool mi_dynmap_file(MI_INFO *info, internal::my_off_t size)
71{
72 if (size > (internal::my_off_t) (~((size_t) 0)) - MEMMAP_EXTRA_MARGIN)
73 {
74 return(1);
75 }
76 /*
77 I wonder if it is good to use MAP_NORESERVE. From the Linux man page:
78 MAP_NORESERVE
79 Do not reserve swap space for this mapping. When swap space is
80 reserved, one has the guarantee that it is possible to modify the
81 mapping. When swap space is not reserved one might get SIGSEGV
82 upon a write if no physical memory is available.
83 */
84 info->s->file_map= (unsigned char*)
85 mmap(NULL, (size_t)(size + MEMMAP_EXTRA_MARGIN),
86 info->s->mode==O_RDONLY ? PROT_READ :
87 PROT_READ | PROT_WRITE,
88 MAP_SHARED | MAP_NORESERVE,
89 info->dfile, 0L);
90 if (info->s->file_map == (unsigned char*) MAP_FAILED)
91 {
92 info->s->file_map= NULL;
93 return(1);
94 }
95/* per krow we should look at removing the following code */
96 madvise((char*) info->s->file_map, size, MADV_RANDOM);
97 info->s->mmaped_length= size;
98 return(0);
99}
100
101
102/*
103 Resize mmaped area for MyISAM handler
104
105 SYNOPSIS
106 mi_remap_file()
107 info MyISAM handler
108
109 RETURN
110*/
111
112void mi_remap_file(MI_INFO *info, internal::my_off_t size)
113{
114 if (info->s->file_map)
115 {
116 munmap((char*) info->s->file_map,
117 (size_t) info->s->mmaped_length + MEMMAP_EXTRA_MARGIN);
118 mi_dynmap_file(info, size);
119 }
120}
121
122
123/*
124 Read bytes from MySAM handler, using mmap or pread
125
126 SYNOPSIS
127 mi_mmap_pread()
128 info MyISAM handler
129 Buffer Input buffer
130 Count Count of bytes for read
131 offset Start position
132 MyFlags
133
134 RETURN
135 0 ok
136*/
137
138size_t mi_mmap_pread(MI_INFO *info, unsigned char *Buffer,
139 size_t Count, internal::my_off_t offset, myf MyFlags)
140{
141 /*
142 The following test may fail in the following cases:
143 - We failed to remap a memory area (fragmented memory?)
144 - This thread has done some writes, but not yet extended the
145 memory mapped area.
146 */
147
148 if (info->s->mmaped_length >= offset + Count)
149 {
150 memcpy(Buffer, info->s->file_map + offset, Count);
151 return 0;
152 }
153 else
154 {
155 return my_pread(info->dfile, Buffer, Count, offset, MyFlags);
156 }
157}
158
159
160 /* wrapper for my_pread in case if mmap isn't used */
161
162size_t mi_nommap_pread(MI_INFO *info, unsigned char *Buffer,
163 size_t Count, internal::my_off_t offset, myf MyFlags)
164{
165 return my_pread(info->dfile, Buffer, Count, offset, MyFlags);
166}
167
168
169/*
170 Write bytes to MySAM handler, using mmap or pwrite
171
172 SYNOPSIS
173 mi_mmap_pwrite()
174 info MyISAM handler
175 Buffer Output buffer
176 Count Count of bytes for write
177 offset Start position
178 MyFlags
179
180 RETURN
181 0 ok
182 !=0 error. In this case return error from pwrite
183*/
184
185size_t mi_mmap_pwrite(MI_INFO *info, const unsigned char *Buffer,
186 size_t Count, internal::my_off_t offset, myf MyFlags)
187{
188
189 /*
190 The following test may fail in the following cases:
191 - We failed to remap a memory area (fragmented memory?)
192 - This thread has done some writes, but not yet extended the
193 memory mapped area.
194 */
195
196 if (info->s->mmaped_length >= offset + Count)
197 {
198 memcpy(info->s->file_map + offset, Buffer, Count);
199 return 0;
200 }
201 else
202 {
203 info->s->nonmmaped_inserts++;
204 return my_pwrite(info->dfile, Buffer, Count, offset, MyFlags);
205 }
206
207}
208
209
210 /* wrapper for my_pwrite in case if mmap isn't used */
211
212size_t mi_nommap_pwrite(MI_INFO *info, const unsigned char *Buffer,
213 size_t Count, internal::my_off_t offset, myf MyFlags)
214{
215 return my_pwrite(info->dfile, Buffer, Count, offset, MyFlags);
216}
217
218
219int _mi_write_dynamic_record(MI_INFO *info, const unsigned char *record)
220{
221 ulong reclength=_mi_rec_pack(info,info->rec_buff,record);
222 return (write_dynamic_record(info,info->rec_buff,reclength));
223}
224
225int _mi_update_dynamic_record(MI_INFO *info, internal::my_off_t pos, const unsigned char *record)
226{
227 uint32_t length=_mi_rec_pack(info,info->rec_buff,record);
228 return (update_dynamic_record(info,pos,info->rec_buff,length));
229}
230
231int _mi_write_blob_record(MI_INFO *info, const unsigned char *record)
232{
233 unsigned char *rec_buff;
234 int error;
235 ulong reclength,reclength2,extra;
236
237 extra= (ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER)+MI_SPLIT_LENGTH+
238 MI_DYN_DELETE_BLOCK_HEADER+1);
239 reclength= (info->s->base.pack_reclength +
240 _my_calc_total_blob_length(info,record)+ extra);
241#ifdef NOT_USED /* We now support big rows */
242 if (reclength > MI_DYN_MAX_ROW_LENGTH)
243 {
244 errno=HA_ERR_TO_BIG_ROW;
245 return -1;
246 }
247#endif
248 if (!(rec_buff=(unsigned char*) malloc(reclength)))
249 {
250 errno= HA_ERR_OUT_OF_MEM;
251 return(-1);
252 }
253 reclength2= _mi_rec_pack(info,rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
254 record);
255 assert(reclength2 <= reclength);
256 error=write_dynamic_record(info,rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
257 reclength2);
258 free(rec_buff);
259 return(error);
260}
261
262
263int _mi_update_blob_record(MI_INFO *info, internal::my_off_t pos, const unsigned char *record)
264{
265 unsigned char *rec_buff;
266 int error;
267 ulong reclength,extra;
268
269 extra= (ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER)+MI_SPLIT_LENGTH+
270 MI_DYN_DELETE_BLOCK_HEADER);
271 reclength= (info->s->base.pack_reclength+
272 _my_calc_total_blob_length(info,record)+ extra);
273#ifdef NOT_USED /* We now support big rows */
274 if (reclength > MI_DYN_MAX_ROW_LENGTH)
275 {
276 errno=HA_ERR_TO_BIG_ROW;
277 return -1;
278 }
279#endif
280 if (!(rec_buff=(unsigned char*) malloc(reclength)))
281 {
282 errno= HA_ERR_OUT_OF_MEM;
283 return(-1);
284 }
285 reclength=_mi_rec_pack(info,rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
286 record);
287 error=update_dynamic_record(info,pos,
288 rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
289 reclength);
290 free(rec_buff);
291 return(error);
292}
293
294
295int _mi_delete_dynamic_record(MI_INFO *info)
296{
297 return delete_dynamic_record(info,info->lastpos,0);
298}
299
300
301 /* Write record to data-file */
302
303static int write_dynamic_record(MI_INFO *info, const unsigned char *record,
304 ulong reclength)
305{
306 int flag;
307 ulong length;
308 internal::my_off_t filepos;
309
310 flag=0;
311
312 /*
313 Check if we have enough room for the new record.
314 First we do simplified check to make usual case faster.
315 Then we do more precise check for the space left.
316 Though it still is not absolutely precise, as
317 we always use MI_MAX_DYN_BLOCK_HEADER while it can be
318 less in the most of the cases.
319 */
320
321 if (DRIZZLE_UNLIKELY(info->s->base.max_data_file_length -
322 info->state->data_file_length <
323 reclength + MI_MAX_DYN_BLOCK_HEADER))
324 {
325 if (info->s->base.max_data_file_length - info->state->data_file_length +
326 info->state->empty - info->state->del * MI_MAX_DYN_BLOCK_HEADER <
327 reclength + MI_MAX_DYN_BLOCK_HEADER)
328 {
329 errno=HA_ERR_RECORD_FILE_FULL;
330 return(1);
331 }
332 }
333
334 do
335 {
336 if (_mi_find_writepos(info,reclength,&filepos,&length))
337 goto err;
338 if (_mi_write_part_record(info,filepos,length,
339 (info->append_insert_at_end ?
340 HA_OFFSET_ERROR : info->s->state.dellink),
341 (unsigned char**) &record,&reclength,&flag))
342 goto err;
343 } while (reclength);
344
345 return(0);
346err:
347 return(1);
348}
349
350
351 /* Get a block for data ; The given data-area must be used !! */
352
353static int _mi_find_writepos(MI_INFO *info,
354 ulong reclength, /* record length */
355 internal::my_off_t *filepos, /* Return file pos */
356 ulong *length) /* length of block at filepos */
357{
358 MI_BLOCK_INFO block_info;
359 ulong tmp;
360
361 if (info->s->state.dellink != HA_OFFSET_ERROR &&
362 !info->append_insert_at_end)
363 {
364 /* Deleted blocks exists; Get last used block */
365 *filepos=info->s->state.dellink;
366 block_info.second_read=0;
367 info->rec_cache.seek_not_done=1;
368 if (!(_mi_get_block_info(&block_info,info->dfile,info->s->state.dellink) &
369 BLOCK_DELETED))
370 {
371 errno=HA_ERR_WRONG_IN_RECORD;
372 return(-1);
373 }
374 info->s->state.dellink=block_info.next_filepos;
375 info->state->del--;
376 info->state->empty-= block_info.block_len;
377 *length= block_info.block_len;
378 }
379 else
380 {
381 /* No deleted blocks; Allocate a new block */
382 *filepos=info->state->data_file_length;
383 if ((tmp=reclength+3 + test(reclength >= (65520-3))) <
384 info->s->base.min_block_length)
385 tmp= info->s->base.min_block_length;
386 else
387 tmp= ((tmp+MI_DYN_ALIGN_SIZE-1) &
388 (~ (ulong) (MI_DYN_ALIGN_SIZE-1)));
389 if (info->state->data_file_length >
390 (info->s->base.max_data_file_length - tmp))
391 {
392 errno=HA_ERR_RECORD_FILE_FULL;
393 return(-1);
394 }
395 if (tmp > MI_MAX_BLOCK_LENGTH)
396 tmp=MI_MAX_BLOCK_LENGTH;
397 *length= tmp;
398 info->state->data_file_length+= tmp;
399 info->s->state.split++;
400 info->update|=HA_STATE_WRITE_AT_END;
401 }
402 return(0);
403} /* _mi_find_writepos */
404
405
406
407/*
408 Unlink a deleted block from the deleted list.
409 This block will be combined with the preceding or next block to form
410 a big block.
411*/
412
413static bool unlink_deleted_block(MI_INFO *info, MI_BLOCK_INFO *block_info)
414{
415 if (block_info->filepos == info->s->state.dellink)
416 {
417 /* First deleted block; We can just use this ! */
418 info->s->state.dellink=block_info->next_filepos;
419 }
420 else
421 {
422 MI_BLOCK_INFO tmp;
423 tmp.second_read=0;
424 /* Unlink block from the previous block */
425 if (!(_mi_get_block_info(&tmp,info->dfile,block_info->prev_filepos)
426 & BLOCK_DELETED))
427 return(1); /* Something is wrong */
428 mi_sizestore(tmp.header+4,block_info->next_filepos);
429 if (info->s->file_write(info, tmp.header+4,8,
430 block_info->prev_filepos+4, MYF(MY_NABP)))
431 return(1);
432 /* Unlink block from next block */
433 if (block_info->next_filepos != HA_OFFSET_ERROR)
434 {
435 if (!(_mi_get_block_info(&tmp,info->dfile,block_info->next_filepos)
436 & BLOCK_DELETED))
437 return(1); /* Something is wrong */
438 mi_sizestore(tmp.header+12,block_info->prev_filepos);
439 if (info->s->file_write(info, tmp.header+12,8,
440 block_info->next_filepos+12,
441 MYF(MY_NABP)))
442 return(1);
443 }
444 }
445 /* We now have one less deleted block */
446 info->state->del--;
447 info->state->empty-= block_info->block_len;
448 info->s->state.split--;
449
450 /*
451 If this was a block that we where accessing through table scan
452 (mi_rrnd() or mi_scan(), then ensure that we skip over this block
453 when doing next mi_rrnd() or mi_scan().
454 */
455 if (info->nextpos == block_info->filepos)
456 info->nextpos+=block_info->block_len;
457 return(0);
458}
459
460
461/*
462 Add a backward link to delete block
463
464 SYNOPSIS
465 update_backward_delete_link()
466 info MyISAM handler
467 delete_block Position to delete block to update.
468 If this is 'HA_OFFSET_ERROR', nothing will be done
469 filepos Position to block that 'delete_block' should point to
470
471 RETURN
472 0 ok
473 1 error. In this case my_error is set.
474*/
475
476static int update_backward_delete_link(MI_INFO *info, internal::my_off_t delete_block,
477 internal::my_off_t filepos)
478{
479 MI_BLOCK_INFO block_info;
480
481 if (delete_block != HA_OFFSET_ERROR)
482 {
483 block_info.second_read=0;
484 if (_mi_get_block_info(&block_info,info->dfile,delete_block)
485 & BLOCK_DELETED)
486 {
487 unsigned char buff[8];
488 mi_sizestore(buff,filepos);
489 if (info->s->file_write(info,buff, 8, delete_block+12, MYF(MY_NABP)))
490 return(1); /* Error on write */
491 }
492 else
493 {
494 errno=HA_ERR_WRONG_IN_RECORD;
495 return(1); /* Wrong delete link */
496 }
497 }
498 return(0);
499}
500
501 /* Delete datarecord from database */
502 /* info->rec_cache.seek_not_done is updated in cmp_record */
503
504static int delete_dynamic_record(MI_INFO *info, internal::my_off_t filepos,
505 uint32_t second_read)
506{
507 uint32_t length,b_type;
508 MI_BLOCK_INFO block_info,del_block;
509 int error;
510 bool remove_next_block;
511
512 /* First add a link from the last block to the new one */
513 error= update_backward_delete_link(info, info->s->state.dellink, filepos);
514
515 block_info.second_read=second_read;
516 do
517 {
518 /* Remove block at 'filepos' */
519 if ((b_type=_mi_get_block_info(&block_info,info->dfile,filepos))
520 & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
521 BLOCK_FATAL_ERROR) ||
522 (length=(uint) (block_info.filepos-filepos) +block_info.block_len) <
523 MI_MIN_BLOCK_LENGTH)
524 {
525 errno=HA_ERR_WRONG_IN_RECORD;
526 return(1);
527 }
528 /* Check if next block is a delete block */
529 del_block.second_read=0;
530 remove_next_block=0;
531 if (_mi_get_block_info(&del_block,info->dfile,filepos+length) &
532 BLOCK_DELETED && del_block.block_len+length < MI_DYN_MAX_BLOCK_LENGTH)
533 {
534 /* We can't remove this yet as this block may be the head block */
535 remove_next_block=1;
536 length+=del_block.block_len;
537 }
538
539 block_info.header[0]=0;
540 mi_int3store(block_info.header+1,length);
541 mi_sizestore(block_info.header+4,info->s->state.dellink);
542 if (b_type & BLOCK_LAST)
543 memset(block_info.header+12, 255, 8);
544 else
545 mi_sizestore(block_info.header+12,block_info.next_filepos);
546 if (info->s->file_write(info,(unsigned char*) block_info.header,20,filepos,
547 MYF(MY_NABP)))
548 return(1);
549 info->s->state.dellink = filepos;
550 info->state->del++;
551 info->state->empty+=length;
552 filepos=block_info.next_filepos;
553
554 /* Now it's safe to unlink the deleted block directly after this one */
555 if (remove_next_block && unlink_deleted_block(info,&del_block))
556 error=1;
557 } while (!(b_type & BLOCK_LAST));
558
559 return(error);
560}
561
562
563 /* Write a block to datafile */
564
565int _mi_write_part_record(MI_INFO *info,
566 internal::my_off_t filepos, /* points at empty block */
567 ulong length, /* length of block */
568 internal::my_off_t next_filepos,/* Next empty block */
569 unsigned char **record, /* pointer to record ptr */
570 ulong *reclength, /* length of *record */
571 int *flag) /* *flag == 0 if header */
572{
573 ulong head_length,res_length,extra_length,long_block,del_length;
574 unsigned char *pos,*record_end;
575 internal::my_off_t next_delete_block;
576 unsigned char temp[MI_SPLIT_LENGTH+MI_DYN_DELETE_BLOCK_HEADER];
577
578 next_delete_block=HA_OFFSET_ERROR;
579
580 res_length=extra_length=0;
581 if (length > *reclength + MI_SPLIT_LENGTH)
582 { /* Splitt big block */
583 res_length=MY_ALIGN(length- *reclength - MI_EXTEND_BLOCK_LENGTH,
584 MI_DYN_ALIGN_SIZE);
585 length-= res_length; /* Use this for first part */
586 }
587 long_block= (length < 65520L && *reclength < 65520L) ? 0 : 1;
588 if (length == *reclength+ 3 + long_block)
589 {
590 /* Block is exactly of the right length */
591 temp[0]=(unsigned char) (1+ *flag)+(unsigned char) long_block; /* Flag is 0 or 6 */
592 if (long_block)
593 {
594 mi_int3store(temp+1,*reclength);
595 head_length=4;
596 }
597 else
598 {
599 mi_int2store(temp+1,*reclength);
600 head_length=3;
601 }
602 }
603 else if (length-long_block < *reclength+4)
604 { /* To short block */
605 if (next_filepos == HA_OFFSET_ERROR)
606 next_filepos= (info->s->state.dellink != HA_OFFSET_ERROR &&
607 !info->append_insert_at_end ?
608 info->s->state.dellink : info->state->data_file_length);
609 if (*flag == 0) /* First block */
610 {
611 if (*reclength > MI_MAX_BLOCK_LENGTH)
612 {
613 head_length= 16;
614 temp[0]=13;
615 mi_int4store(temp+1,*reclength);
616 mi_int3store(temp+5,length-head_length);
617 mi_sizestore((unsigned char*) temp+8,next_filepos);
618 }
619 else
620 {
621 head_length=5+8+long_block*2;
622 temp[0]=5+(unsigned char) long_block;
623 if (long_block)
624 {
625 mi_int3store(temp+1,*reclength);
626 mi_int3store(temp+4,length-head_length);
627 mi_sizestore((unsigned char*) temp+7,next_filepos);
628 }
629 else
630 {
631 mi_int2store(temp+1,*reclength);
632 mi_int2store(temp+3,length-head_length);
633 mi_sizestore((unsigned char*) temp+5,next_filepos);
634 }
635 }
636 }
637 else
638 {
639 head_length=3+8+long_block;
640 temp[0]=11+(unsigned char) long_block;
641 if (long_block)
642 {
643 mi_int3store(temp+1,length-head_length);
644 mi_sizestore((unsigned char*) temp+4,next_filepos);
645 }
646 else
647 {
648 mi_int2store(temp+1,length-head_length);
649 mi_sizestore((unsigned char*) temp+3,next_filepos);
650 }
651 }
652 }
653 else
654 { /* Block with empty info last */
655 head_length=4+long_block;
656 extra_length= length- *reclength-head_length;
657 temp[0]= (unsigned char) (3+ *flag)+(unsigned char) long_block; /* 3,4 or 9,10 */
658 if (long_block)
659 {
660 mi_int3store(temp+1,*reclength);
661 temp[4]= (unsigned char) (extra_length);
662 }
663 else
664 {
665 mi_int2store(temp+1,*reclength);
666 temp[3]= (unsigned char) (extra_length);
667 }
668 length= *reclength+head_length; /* Write only what is needed */
669 }
670
671 /* Make a long block for one write */
672 record_end= *record+length-head_length;
673 del_length=(res_length ? MI_DYN_DELETE_BLOCK_HEADER : 0);
674 memmove(*record - head_length, temp, head_length);
675 memcpy(temp,record_end,(size_t) (extra_length+del_length));
676 memset(record_end, 0, extra_length);
677
678 if (res_length)
679 {
680 /* Check first if we can join this block with the next one */
681 MI_BLOCK_INFO del_block;
682 internal::my_off_t next_block=filepos+length+extra_length+res_length;
683
684 del_block.second_read=0;
685 if (next_block < info->state->data_file_length &&
686 info->s->state.dellink != HA_OFFSET_ERROR)
687 {
688 if ((_mi_get_block_info(&del_block,info->dfile,next_block)
689 & BLOCK_DELETED) &&
690 res_length + del_block.block_len < MI_DYN_MAX_BLOCK_LENGTH)
691 {
692 if (unlink_deleted_block(info,&del_block))
693 goto err;
694 res_length+=del_block.block_len;
695 }
696 }
697
698 /* Create a delete link of the last part of the block */
699 pos=record_end+extra_length;
700 pos[0]= '\0';
701 mi_int3store(pos+1,res_length);
702 mi_sizestore(pos+4,info->s->state.dellink);
703 memset(pos+12, 255, 8); /* End link */
704 next_delete_block=info->s->state.dellink;
705 info->s->state.dellink= filepos+length+extra_length;
706 info->state->del++;
707 info->state->empty+=res_length;
708 info->s->state.split++;
709 }
710 if (info->opt_flag & WRITE_CACHE_USED &&
711 info->update & HA_STATE_WRITE_AT_END)
712 {
713 if (info->update & HA_STATE_EXTEND_BLOCK)
714 {
715 info->update&= ~HA_STATE_EXTEND_BLOCK;
716 if (info->rec_cache.block_write(*record - head_length,
717 length+extra_length+del_length,filepos))
718 goto err;
719 }
720 else if (info->rec_cache.write(*record-head_length, length+extra_length+del_length))
721 goto err;
722 }
723 else
724 {
725 info->rec_cache.seek_not_done=1;
726 if (info->s->file_write(info,(unsigned char*) *record-head_length,length+extra_length+
727 del_length,filepos,info->s->write_flag))
728 goto err;
729 }
730 memcpy(record_end, temp, extra_length + del_length);
731 *record=record_end;
732 *reclength-=(length-head_length);
733 *flag=6;
734
735 if (del_length)
736 {
737 /* link the next delete block to this */
738 if (update_backward_delete_link(info, next_delete_block,
739 info->s->state.dellink))
740 goto err;
741 }
742
743 return(0);
744err:
745 return(1);
746} /*_mi_write_part_record */
747
748
749 /* update record from datafile */
750
751static int update_dynamic_record(MI_INFO *info, internal::my_off_t filepos, unsigned char *record,
752 ulong reclength)
753{
754 int flag;
755 uint32_t error;
756 ulong length;
757 MI_BLOCK_INFO block_info;
758
759 flag=block_info.second_read=0;
760 /*
761 Check if we have enough room for the record.
762 First we do simplified check to make usual case faster.
763 Then we do more precise check for the space left.
764 Though it still is not absolutely precise, as
765 we always use MI_MAX_DYN_BLOCK_HEADER while it can be
766 less in the most of the cases.
767 */
768
769 /*
770 compare with just the reclength as we're going
771 to get some space from the old replaced record
772 */
773 if (DRIZZLE_UNLIKELY(info->s->base.max_data_file_length -
774 info->state->data_file_length < reclength))
775 {
776 /*
777 let's read the old record's block to find out the length of the
778 old record
779 */
780 if ((error=_mi_get_block_info(&block_info,info->dfile,filepos))
781 & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR | BLOCK_FATAL_ERROR))
782 {
783 if (!(error & BLOCK_FATAL_ERROR))
784 errno=HA_ERR_WRONG_IN_RECORD;
785 goto err;
786 }
787
788 /*
789 if new record isn't longer, we can go on safely
790 */
791 if (block_info.rec_len < reclength)
792 {
793 if (info->s->base.max_data_file_length - info->state->data_file_length +
794 info->state->empty - info->state->del * MI_MAX_DYN_BLOCK_HEADER <
795 reclength - block_info.rec_len + MI_MAX_DYN_BLOCK_HEADER)
796 {
797 errno=HA_ERR_RECORD_FILE_FULL;
798 goto err;
799 }
800 }
801 block_info.second_read=0;
802 }
803
804 while (reclength > 0)
805 {
806 if (filepos != info->s->state.dellink)
807 {
808 block_info.next_filepos= HA_OFFSET_ERROR;
809 if ((error=_mi_get_block_info(&block_info,info->dfile,filepos))
810 & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
811 BLOCK_FATAL_ERROR))
812 {
813 if (!(error & BLOCK_FATAL_ERROR))
814 errno=HA_ERR_WRONG_IN_RECORD;
815 goto err;
816 }
817 length=(ulong) (block_info.filepos-filepos) + block_info.block_len;
818 if (length < reclength)
819 {
820 uint32_t tmp=MY_ALIGN(reclength - length + 3 +
821 test(reclength >= 65520L),MI_DYN_ALIGN_SIZE);
822 /* Don't create a block bigger than MI_MAX_BLOCK_LENGTH */
823 tmp= min(length+tmp, MI_MAX_BLOCK_LENGTH)-length;
824 /* Check if we can extend this block */
825 if (block_info.filepos + block_info.block_len ==
826 info->state->data_file_length &&
827 info->state->data_file_length <
828 info->s->base.max_data_file_length-tmp)
829 {
830 /* extend file */
831 if (info->nextpos == info->state->data_file_length)
832 info->nextpos+= tmp;
833 info->state->data_file_length+= tmp;
834 info->update|= HA_STATE_WRITE_AT_END | HA_STATE_EXTEND_BLOCK;
835 length+=tmp;
836 }
837 else if (length < MI_MAX_BLOCK_LENGTH - MI_MIN_BLOCK_LENGTH)
838 {
839 /*
840 Check if next block is a deleted block
841 Above we have MI_MIN_BLOCK_LENGTH to avoid the problem where
842 the next block is so small it can't be splited which could
843 casue problems
844 */
845
846 MI_BLOCK_INFO del_block;
847 del_block.second_read=0;
848 if (_mi_get_block_info(&del_block,info->dfile,
849 block_info.filepos + block_info.block_len) &
850 BLOCK_DELETED)
851 {
852 /* Use; Unlink it and extend the current block */
853 if (unlink_deleted_block(info,&del_block))
854 goto err;
855 if ((length+=del_block.block_len) > MI_MAX_BLOCK_LENGTH)
856 {
857 /*
858 New block was too big, link overflow part back to
859 delete list
860 */
861 internal::my_off_t next_pos;
862 ulong rest_length= length-MI_MAX_BLOCK_LENGTH;
863 set_if_bigger(rest_length, (ulong)MI_MIN_BLOCK_LENGTH);
864 next_pos= del_block.filepos+ del_block.block_len - rest_length;
865
866 if (update_backward_delete_link(info, info->s->state.dellink,
867 next_pos))
868 return(1);
869
870 /* create delete link for data that didn't fit into the page */
871 del_block.header[0]=0;
872 mi_int3store(del_block.header+1, rest_length);
873 mi_sizestore(del_block.header+4,info->s->state.dellink);
874 memset(del_block.header+12, 255, 8);
875 if (info->s->file_write(info,(unsigned char*) del_block.header,20, next_pos,
876 MYF(MY_NABP)))
877 return(1);
878 info->s->state.dellink= next_pos;
879 info->s->state.split++;
880 info->state->del++;
881 info->state->empty+= rest_length;
882 length-= rest_length;
883 }
884 }
885 }
886 }
887 }
888 else
889 {
890 if (_mi_find_writepos(info,reclength,&filepos,&length))
891 goto err;
892 }
893 if (_mi_write_part_record(info,filepos,length,block_info.next_filepos,
894 &record,&reclength,&flag))
895 goto err;
896 if ((filepos=block_info.next_filepos) == HA_OFFSET_ERROR)
897 {
898 /* Start writing data on deleted blocks */
899 filepos=info->s->state.dellink;
900 }
901 }
902
903 if (block_info.next_filepos != HA_OFFSET_ERROR)
904 if (delete_dynamic_record(info,block_info.next_filepos,1))
905 goto err;
906 return(0);
907err:
908 return(1);
909}
910
911
912 /* Pack a record. Return new reclength */
913
914uint32_t _mi_rec_pack(MI_INFO *info, unsigned char *to,
915 const unsigned char *from)
916{
917 uint length,new_length,flag,bit,i;
918 unsigned char *pos,*end,*startpos,*packpos;
919 enum en_fieldtype type;
920 MI_COLUMNDEF *rec;
921 MI_BLOB *blob;
922
923 flag=0 ; bit=1;
924 startpos=packpos=to; to+= info->s->base.pack_bits; blob=info->blobs;
925 rec=info->s->rec;
926
927 for (i=info->s->base.fields ; i-- > 0; from+= length,rec++)
928 {
929 length=(uint) rec->length;
930 if ((type = (enum en_fieldtype) rec->type) != FIELD_NORMAL)
931 {
932 if (type == FIELD_BLOB)
933 {
934 if (!blob->length)
935 flag|=bit;
936 else
937 {
938 char *temp_pos;
939 size_t tmp_length=length-portable_sizeof_char_ptr;
940 memcpy(to,from,tmp_length);
941 memcpy(&temp_pos,from+tmp_length,sizeof(char*));
942 memcpy(to + tmp_length, temp_pos, blob->length);
943 to+=tmp_length+blob->length;
944 }
945 blob++;
946 }
947 else if (type == FIELD_SKIP_ZERO)
948 {
949 if (memcmp(from,zero_string,length) == 0)
950 flag|=bit;
951 else
952 {
953 memcpy(to, from, length);
954 to+=length;
955 }
956 }
957 else if (type == FIELD_SKIP_ENDSPACE ||
958 type == FIELD_SKIP_PRESPACE)
959 {
960 pos= (unsigned char*) from; end= (unsigned char*) from + length;
961 if (type == FIELD_SKIP_ENDSPACE)
962 { /* Pack trailing spaces */
963 while (end > from && *(end-1) == ' ')
964 end--;
965 }
966 else
967 { /* Pack pref-spaces */
968 while (pos < end && *pos == ' ')
969 pos++;
970 }
971 new_length=(uint) (end-pos);
972 if (new_length +1 + test(rec->length > 255 && new_length > 127)
973 < length)
974 {
975 if (rec->length > 255 && new_length > 127)
976 {
977 to[0]= (unsigned char) ((new_length & 127) + 128);
978 to[1]= (unsigned char) (new_length >> 7);
979 to+=2;
980 }
981 else
982 *to++= (unsigned char) new_length;
983 memcpy(to, pos, new_length);
984 to+=new_length;
985 flag|=bit;
986 }
987 else
988 {
989 memcpy(to, from, length);
990 to+=length;
991 }
992 }
993 else if (type == FIELD_VARCHAR)
994 {
995 uint32_t pack_length= ha_varchar_packlength(rec->length -1);
996 uint32_t tmp_length;
997 if (pack_length == 1)
998 {
999 tmp_length= (uint) *(unsigned char*) from;
1000 *to++= *from;
1001 }
1002 else
1003 {
1004 tmp_length= uint2korr(from);
1005 store_key_length_inc(to,tmp_length);
1006 }
1007 memcpy(to, from+pack_length, tmp_length);
1008 to+= tmp_length;
1009 continue;
1010 }
1011 else
1012 {
1013 memcpy(to, from, length);
1014 to+=length;
1015 continue; /* Normal field */
1016 }
1017 if ((bit= bit << 1) >= 256)
1018 {
1019 *packpos++= (unsigned char) flag;
1020 bit=1; flag=0;
1021 }
1022 }
1023 else
1024 {
1025 memcpy(to, from, length);
1026 to+=length;
1027 }
1028 }
1029 if (bit != 1)
1030 *packpos= (unsigned char) flag;
1031 if (info->s->calc_checksum)
1032 *to++= (unsigned char) info->checksum;
1033 return((uint) (to-startpos));
1034} /* _mi_rec_pack */
1035
1036
1037
1038/*
1039 Check if a record was correctly packed. Used only by myisamchk
1040 Returns 0 if record is ok.
1041*/
1042
1043bool _mi_rec_check(MI_INFO *info,const unsigned char *record, unsigned char *rec_buff,
1044 ulong packed_length, bool with_checksum)
1045{
1046 uint length,new_length,flag,bit,i;
1047 unsigned char *pos,*end,*packpos,*to;
1048 enum en_fieldtype type;
1049 MI_COLUMNDEF *rec;
1050
1051 packpos=rec_buff; to= rec_buff+info->s->base.pack_bits;
1052 rec=info->s->rec;
1053 flag= *packpos; bit=1;
1054
1055 for (i=info->s->base.fields ; i-- > 0; record+= length, rec++)
1056 {
1057 length=(uint) rec->length;
1058 if ((type = (enum en_fieldtype) rec->type) != FIELD_NORMAL)
1059 {
1060 if (type == FIELD_BLOB)
1061 {
1062 uint32_t blob_length=
1063 _mi_calc_blob_length(length-portable_sizeof_char_ptr,record);
1064 if (!blob_length && !(flag & bit))
1065 goto err;
1066 if (blob_length)
1067 to+=length - portable_sizeof_char_ptr+ blob_length;
1068 }
1069 else if (type == FIELD_SKIP_ZERO)
1070 {
1071 if (memcmp(record,zero_string,length) == 0)
1072 {
1073 if (!(flag & bit))
1074 goto err;
1075 }
1076 else
1077 to+=length;
1078 }
1079 else if (type == FIELD_SKIP_ENDSPACE ||
1080 type == FIELD_SKIP_PRESPACE)
1081 {
1082 pos= (unsigned char*) record; end= (unsigned char*) record + length;
1083 if (type == FIELD_SKIP_ENDSPACE)
1084 { /* Pack trailing spaces */
1085 while (end > record && *(end-1) == ' ')
1086 end--;
1087 }
1088 else
1089 { /* Pack pre-spaces */
1090 while (pos < end && *pos == ' ')
1091 pos++;
1092 }
1093 new_length=(uint) (end-pos);
1094 if (new_length +1 + test(rec->length > 255 && new_length > 127)
1095 < length)
1096 {
1097 if (!(flag & bit))
1098 goto err;
1099 if (rec->length > 255 && new_length > 127)
1100 {
1101 if (to[0] != (unsigned char) ((new_length & 127) + 128) ||
1102 to[1] != (unsigned char) (new_length >> 7))
1103 goto err;
1104 to+=2;
1105 }
1106 else if (*to++ != (unsigned char) new_length)
1107 goto err;
1108 to+=new_length;
1109 }
1110 else
1111 to+=length;
1112 }
1113 else if (type == FIELD_VARCHAR)
1114 {
1115 uint32_t pack_length= ha_varchar_packlength(rec->length -1);
1116 uint32_t tmp_length;
1117 if (pack_length == 1)
1118 {
1119 tmp_length= (uint) *(unsigned char*) record;
1120 to+= 1+ tmp_length;
1121 continue;
1122 }
1123 else
1124 {
1125 tmp_length= uint2korr(record);
1126 to+= get_pack_length(tmp_length)+tmp_length;
1127 }
1128 continue;
1129 }
1130 else
1131 {
1132 to+=length;
1133 continue; /* Normal field */
1134 }
1135 if ((bit= bit << 1) >= 256)
1136 {
1137 flag= *++packpos;
1138 bit=1;
1139 }
1140 }
1141 else
1142 to+= length;
1143 }
1144 if (packed_length != (uint) (to - rec_buff) + test(info->s->calc_checksum) ||
1145 (bit != 1 && (flag & ~(bit - 1))))
1146 goto err;
1147 if (with_checksum && ((unsigned char) info->checksum != (unsigned char) *to))
1148 {
1149 goto err;
1150 }
1151 return(0);
1152
1153err:
1154 return(1);
1155}
1156
1157
1158
1159 /* Unpacks a record */
1160 /* Returns -1 and errno =HA_ERR_RECORD_DELETED if reclength isn't */
1161 /* right. Returns reclength (>0) if ok */
1162
1163ulong _mi_rec_unpack(MI_INFO *info, unsigned char *to, unsigned char *from,
1164 ulong found_length)
1165{
1166 uint32_t flag,bit,length,rec_length,min_pack_length;
1167 enum en_fieldtype type;
1168 unsigned char *from_end,*to_end,*packpos;
1169 MI_COLUMNDEF *rec,*end_field;
1170
1171 to_end=to + info->s->base.reclength;
1172 from_end=from+found_length;
1173 flag= (unsigned char) *from; bit=1; packpos=from;
1174 if (found_length < info->s->base.min_pack_length)
1175 goto err;
1176 from+= info->s->base.pack_bits;
1177 min_pack_length=info->s->base.min_pack_length - info->s->base.pack_bits;
1178
1179 for (rec=info->s->rec , end_field=rec+info->s->base.fields ;
1180 rec < end_field ; to+= rec_length, rec++)
1181 {
1182 rec_length=rec->length;
1183 if ((type = (enum en_fieldtype) rec->type) != FIELD_NORMAL &&
1184 (type != FIELD_CHECK))
1185 {
1186 if (type == FIELD_VARCHAR)
1187 {
1188 uint32_t pack_length= ha_varchar_packlength(rec_length-1);
1189 if (pack_length == 1)
1190 {
1191 length= (uint) *(unsigned char*) from;
1192 if (length > rec_length-1)
1193 goto err;
1194 *to= *from++;
1195 }
1196 else
1197 {
1198 get_key_length(length, from);
1199 if (length > rec_length-2)
1200 goto err;
1201 int2store(to,length);
1202 }
1203 if (from+length > from_end)
1204 goto err;
1205 memcpy(to+pack_length, from, length);
1206 from+= length;
1207 min_pack_length--;
1208 continue;
1209 }
1210 if (flag & bit)
1211 {
1212 if (type == FIELD_BLOB || type == FIELD_SKIP_ZERO)
1213 memset(to, 0, rec_length);
1214 else if (type == FIELD_SKIP_ENDSPACE ||
1215 type == FIELD_SKIP_PRESPACE)
1216 {
1217 if (rec->length > 255 && *from & 128)
1218 {
1219 if (from + 1 >= from_end)
1220 goto err;
1221 length= (*from & 127)+ ((uint) (unsigned char) *(from+1) << 7); from+=2;
1222 }
1223 else
1224 {
1225 if (from == from_end)
1226 goto err;
1227 length= (unsigned char) *from++;
1228 }
1229 min_pack_length--;
1230 if (length >= rec_length ||
1231 min_pack_length + length > (uint) (from_end - from))
1232 goto err;
1233 if (type == FIELD_SKIP_ENDSPACE)
1234 {
1235 memcpy(to, from, length);
1236 memset(to+length, ' ', rec_length-length);
1237 }
1238 else
1239 {
1240 memset(to, ' ', rec_length-length);
1241 memcpy(to + rec_length - length, from, length);
1242 }
1243 from+=length;
1244 }
1245 }
1246 else if (type == FIELD_BLOB)
1247 {
1248 uint32_t size_length=rec_length- portable_sizeof_char_ptr;
1249 ulong blob_length=_mi_calc_blob_length(size_length,from);
1250 ulong from_left= (ulong) (from_end - from);
1251 if (from_left < size_length ||
1252 from_left - size_length < blob_length ||
1253 from_left - size_length - blob_length < min_pack_length)
1254 goto err;
1255 memcpy(to, from, size_length);
1256 from+=size_length;
1257 memcpy(to+size_length, &from, sizeof(char*));
1258 from+=blob_length;
1259 }
1260 else
1261 {
1262 if (type == FIELD_SKIP_ENDSPACE || type == FIELD_SKIP_PRESPACE)
1263 min_pack_length--;
1264 if (min_pack_length + rec_length > (uint) (from_end - from))
1265 goto err;
1266 memcpy(to, from, rec_length);
1267 from+=rec_length;
1268 }
1269 if ((bit= bit << 1) >= 256)
1270 {
1271 flag= (unsigned char) *++packpos; bit=1;
1272 }
1273 }
1274 else
1275 {
1276 if (min_pack_length > (uint) (from_end - from))
1277 goto err;
1278 min_pack_length-=rec_length;
1279 memcpy(to, from, rec_length);
1280 from+=rec_length;
1281 }
1282 }
1283 if (info->s->calc_checksum)
1284 from++;
1285 if (to == to_end && from == from_end && (bit == 1 || !(flag & ~(bit-1))))
1286 return(found_length);
1287
1288err:
1289 errno= HA_ERR_WRONG_IN_RECORD;
1290 return(MY_FILE_ERROR);
1291} /* _mi_rec_unpack */
1292
1293
1294 /* Calc length of blob. Update info in blobs->length */
1295
1296ulong _my_calc_total_blob_length(MI_INFO *info, const unsigned char *record)
1297{
1298 ulong length;
1299 MI_BLOB *blob,*end;
1300
1301 for (length=0, blob= info->blobs, end=blob+info->s->base.blobs ;
1302 blob != end;
1303 blob++)
1304 {
1305 blob->length=_mi_calc_blob_length(blob->pack_length,record + blob->offset);
1306 length+=blob->length;
1307 }
1308 return length;
1309}
1310
1311
1312ulong _mi_calc_blob_length(uint32_t length, const unsigned char *pos)
1313{
1314 switch (length) {
1315 case 1:
1316 return (uint) (unsigned char) *pos;
1317 case 2:
1318 return (uint) uint2korr(pos);
1319 case 3:
1320 return uint3korr(pos);
1321 case 4:
1322 return uint4korr(pos);
1323 default:
1324 break;
1325 }
1326 return 0; /* Impossible */
1327}
1328
1329
1330void _my_store_blob_length(unsigned char *pos,uint32_t pack_length,uint32_t length)
1331{
1332 switch (pack_length) {
1333 case 1:
1334 *pos= (unsigned char) length;
1335 break;
1336 case 2:
1337 int2store(pos,length);
1338 break;
1339 case 3:
1340 int3store(pos,length);
1341 break;
1342 case 4:
1343 int4store(pos,length);
1344 default:
1345 break;
1346 }
1347 return;
1348}
1349
1350
1351/*
1352 Read record from datafile.
1353
1354 SYNOPSIS
1355 _mi_read_dynamic_record()
1356 info MI_INFO pointer to table.
1357 filepos From where to read the record.
1358 buf Destination for record.
1359
1360 NOTE
1361
1362 If a write buffer is active, it needs to be flushed if its contents
1363 intersects with the record to read. We always check if the position
1364 of the first byte of the write buffer is lower than the position
1365 past the last byte to read. In theory this is also true if the write
1366 buffer is completely below the read segment. That is, if there is no
1367 intersection. But this case is unusual. We flush anyway. Only if the
1368 first byte in the write buffer is above the last byte to read, we do
1369 not flush.
1370
1371 A dynamic record may need several reads. So this check must be done
1372 before every read. Reading a dynamic record starts with reading the
1373 block header. If the record does not fit into the free space of the
1374 header, the block may be longer than the header. In this case a
1375 second read is necessary. These one or two reads repeat for every
1376 part of the record.
1377
1378 RETURN
1379 0 OK
1380 -1 Error
1381*/
1382
1383int _mi_read_dynamic_record(MI_INFO *info, internal::my_off_t filepos, unsigned char *buf)
1384{
1385 int block_of_record;
1386 uint32_t b_type, left_length= 0;
1387 unsigned char *to= NULL;
1388 MI_BLOCK_INFO block_info;
1389 int file;
1390
1391 if (filepos != HA_OFFSET_ERROR)
1392 {
1393 file=info->dfile;
1394 block_of_record= 0; /* First block of record is numbered as zero. */
1395 block_info.second_read= 0;
1396 do
1397 {
1398 /* A corrupted table can have wrong pointers. (Bug# 19835) */
1399 if (filepos == HA_OFFSET_ERROR)
1400 goto panic;
1401 if (info->opt_flag & WRITE_CACHE_USED &&
1402 info->rec_cache.pos_in_file < filepos + MI_BLOCK_INFO_HEADER_LENGTH &&
1403 info->rec_cache.flush())
1404 goto err;
1405 info->rec_cache.seek_not_done=1;
1406 if ((b_type= _mi_get_block_info(&block_info, file, filepos))
1407 & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
1408 BLOCK_FATAL_ERROR))
1409 {
1410 if (b_type & (BLOCK_SYNC_ERROR | BLOCK_DELETED))
1411 errno=HA_ERR_RECORD_DELETED;
1412 goto err;
1413 }
1414 if (block_of_record++ == 0) /* First block */
1415 {
1416 if (block_info.rec_len > (uint) info->s->base.max_pack_length)
1417 goto panic;
1418 if (info->s->base.blobs)
1419 {
1420 if (!(to=mi_alloc_rec_buff(info, block_info.rec_len,
1421 &info->rec_buff)))
1422 goto err;
1423 }
1424 else
1425 to= info->rec_buff;
1426 left_length=block_info.rec_len;
1427 }
1428 if (left_length < block_info.data_len || ! block_info.data_len)
1429 goto panic; /* Wrong linked record */
1430 /* copy information that is already read */
1431 {
1432 uint32_t offset= (uint) (block_info.filepos - filepos);
1433 uint32_t prefetch_len= (sizeof(block_info.header) - offset);
1434 filepos+= sizeof(block_info.header);
1435
1436 if (prefetch_len > block_info.data_len)
1437 prefetch_len= block_info.data_len;
1438 if (prefetch_len)
1439 {
1440 memcpy(to, block_info.header + offset, prefetch_len);
1441 block_info.data_len-= prefetch_len;
1442 left_length-= prefetch_len;
1443 to+= prefetch_len;
1444 }
1445 }
1446 /* read rest of record from file */
1447 if (block_info.data_len)
1448 {
1449 if (info->opt_flag & WRITE_CACHE_USED &&
1450 info->rec_cache.pos_in_file < filepos + block_info.data_len &&
1451 info->rec_cache.flush())
1452 goto err;
1453 /*
1454 What a pity that this method is not called 'file_pread' and that
1455 there is no equivalent without seeking. We are at the right
1456 position already. :(
1457 */
1458 if (info->s->file_read(info, (unsigned char*) to, block_info.data_len,
1459 filepos, MYF(MY_NABP)))
1460 goto panic;
1461 left_length-=block_info.data_len;
1462 to+=block_info.data_len;
1463 }
1464 filepos= block_info.next_filepos;
1465 } while (left_length);
1466
1467 info->update|= HA_STATE_AKTIV; /* We have a aktive record */
1468 fast_mi_writeinfo(info);
1469 return(_mi_rec_unpack(info,buf,info->rec_buff,block_info.rec_len) !=
1470 MY_FILE_ERROR ? 0 : -1);
1471 }
1472 fast_mi_writeinfo(info);
1473 return(-1); /* Wrong data to read */
1474
1475panic:
1476 errno=HA_ERR_WRONG_IN_RECORD;
1477err:
1478 _mi_writeinfo(info,0);
1479 return(-1);
1480}
1481
1482 /* compare unique constraint between stored rows */
1483
1484int _mi_cmp_dynamic_unique(MI_INFO *info, MI_UNIQUEDEF *def,
1485 const unsigned char *record, internal::my_off_t pos)
1486{
1487 unsigned char *rec_buff,*old_record;
1488 int error;
1489
1490 if (!(old_record=(unsigned char *)malloc(info->s->base.reclength)))
1491 return(1);
1492
1493 /* Don't let the compare destroy blobs that may be in use */
1494 rec_buff=info->rec_buff;
1495 if (info->s->base.blobs)
1496 info->rec_buff=0;
1497 error=_mi_read_dynamic_record(info,pos,old_record);
1498 if (!error)
1499 error=mi_unique_comp(def, record, old_record, def->null_are_equal);
1500 if (info->s->base.blobs)
1501 {
1502 void * rec_buff_ptr= mi_get_rec_buff_ptr(info, info->rec_buff);
1503 free(rec_buff_ptr);
1504 info->rec_buff=rec_buff;
1505 }
1506 free(old_record);
1507 return(error);
1508}
1509
1510
1511 /* Compare of record one disk with packed record in memory */
1512
1513int _mi_cmp_dynamic_record(MI_INFO *info, const unsigned char *record)
1514{
1515 uint32_t flag,reclength,b_type;
1516 internal::my_off_t filepos;
1517 unsigned char *buffer;
1518 MI_BLOCK_INFO block_info;
1519
1520 if (info->opt_flag & WRITE_CACHE_USED)
1521 {
1522 info->update&= ~(HA_STATE_WRITE_AT_END | HA_STATE_EXTEND_BLOCK);
1523 if (info->rec_cache.flush())
1524 return(-1);
1525 }
1526 info->rec_cache.seek_not_done=1;
1527
1528 /* If nobody have touched the database we don't have to test rec */
1529
1530 buffer=info->rec_buff;
1531 if ((info->opt_flag & READ_CHECK_USED))
1532 { /* If check isn't disabled */
1533 if (info->s->base.blobs)
1534 {
1535 if (!(buffer=(unsigned char*) malloc(info->s->base.pack_reclength+
1536 _my_calc_total_blob_length(info,record))))
1537 return(-1);
1538 }
1539 reclength=_mi_rec_pack(info,buffer,record);
1540 record= buffer;
1541
1542 filepos=info->lastpos;
1543 flag=block_info.second_read=0;
1544 block_info.next_filepos=filepos;
1545 while (reclength > 0)
1546 {
1547 if ((b_type=_mi_get_block_info(&block_info,info->dfile,
1548 block_info.next_filepos))
1549 & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
1550 BLOCK_FATAL_ERROR))
1551 {
1552 if (b_type & (BLOCK_SYNC_ERROR | BLOCK_DELETED))
1553 errno=HA_ERR_RECORD_CHANGED;
1554 goto err;
1555 }
1556 if (flag == 0) /* First block */
1557 {
1558 flag=1;
1559 if (reclength != block_info.rec_len)
1560 {
1561 errno=HA_ERR_RECORD_CHANGED;
1562 goto err;
1563 }
1564 } else if (reclength < block_info.data_len)
1565 {
1566 errno=HA_ERR_WRONG_IN_RECORD;
1567 goto err;
1568 }
1569 reclength-=block_info.data_len;
1570 if (_mi_cmp_buffer(info->dfile,record,block_info.filepos,
1571 block_info.data_len))
1572 {
1573 errno=HA_ERR_RECORD_CHANGED;
1574 goto err;
1575 }
1576 flag=1;
1577 record+=block_info.data_len;
1578 }
1579 }
1580 errno=0;
1581err:
1582 if (buffer != info->rec_buff)
1583 free((unsigned char*) buffer);
1584 return(errno);
1585}
1586
1587
1588 /* Compare file to buffert */
1589
1590static int _mi_cmp_buffer(int file, const unsigned char *buff, internal::my_off_t filepos,
1591 uint32_t length)
1592{
1593 uint32_t next_length;
1594 unsigned char temp_buff[IO_SIZE*2];
1595
1596 next_length= IO_SIZE*2 - (uint) (filepos & (IO_SIZE-1));
1597
1598 while (length > IO_SIZE*2)
1599 {
1600 if (my_pread(file,temp_buff,next_length,filepos, MYF(MY_NABP)) ||
1601 memcmp(buff, temp_buff, next_length))
1602 goto err;
1603 filepos+=next_length;
1604 buff+=next_length;
1605 length-= next_length;
1606 next_length=IO_SIZE*2;
1607 }
1608 if (my_pread(file,temp_buff,length,filepos,MYF(MY_NABP)))
1609 goto err;
1610 return(memcmp(buff,temp_buff,length));
1611err:
1612 return(1);
1613}
1614
1615
1616/*
1617 Read record from datafile.
1618
1619 SYNOPSIS
1620 _mi_read_rnd_dynamic_record()
1621 info MI_INFO pointer to table.
1622 buf Destination for record.
1623 filepos From where to read the record.
1624 skip_deleted_blocks If to repeat reading until a non-deleted
1625 record is found.
1626
1627 NOTE
1628
1629 If a write buffer is active, it needs to be flushed if its contents
1630 intersects with the record to read. We always check if the position
1631 of the first byte of the write buffer is lower than the position
1632 past the last byte to read. In theory this is also true if the write
1633 buffer is completely below the read segment. That is, if there is no
1634 intersection. But this case is unusual. We flush anyway. Only if the
1635 first byte in the write buffer is above the last byte to read, we do
1636 not flush.
1637
1638 A dynamic record may need several reads. So this check must be done
1639 before every read. Reading a dynamic record starts with reading the
1640 block header. If the record does not fit into the free space of the
1641 header, the block may be longer than the header. In this case a
1642 second read is necessary. These one or two reads repeat for every
1643 part of the record.
1644
1645 RETURN
1646 0 OK
1647 != 0 Error
1648*/
1649
1650int _mi_read_rnd_dynamic_record(MI_INFO *info, unsigned char *buf,
1651 internal::my_off_t filepos,
1652 bool skip_deleted_blocks)
1653{
1654 int block_of_record, info_read, save_errno;
1655 uint32_t left_len,b_type;
1656 unsigned char *to= NULL;
1657 MI_BLOCK_INFO block_info;
1658 MYISAM_SHARE *share=info->s;
1659
1660 info_read=0;
1661
1662 if (info->lock_type == F_UNLCK)
1663 {
1664 info->tmp_lock_type=F_RDLCK;
1665 }
1666 else
1667 info_read=1; /* memory-keyinfoblock is ok */
1668
1669 block_of_record= 0; /* First block of record is numbered as zero. */
1670 block_info.second_read= 0;
1671 left_len=1;
1672 do
1673 {
1674 if (filepos >= info->state->data_file_length)
1675 {
1676 if (!info_read)
1677 { /* Check if changed */
1678 info_read=1;
1679 info->rec_cache.seek_not_done=1;
1680 if (mi_state_info_read_dsk(share->kfile,&share->state,1))
1681 goto panic;
1682 }
1683 if (filepos >= info->state->data_file_length)
1684 {
1685 errno= HA_ERR_END_OF_FILE;
1686 goto err;
1687 }
1688 }
1689 if (info->opt_flag & READ_CACHE_USED)
1690 {
1691 if (_mi_read_cache(&info->rec_cache,(unsigned char*) block_info.header,filepos,
1692 sizeof(block_info.header),
1693 (!block_of_record && skip_deleted_blocks ?
1694 READING_NEXT : 0) | READING_HEADER))
1695 goto panic;
1696 b_type=_mi_get_block_info(&block_info,-1,filepos);
1697 }
1698 else
1699 {
1700 if (info->opt_flag & WRITE_CACHE_USED &&
1701 info->rec_cache.pos_in_file < filepos + MI_BLOCK_INFO_HEADER_LENGTH &&
1702 info->rec_cache.flush())
1703 return(errno);
1704 info->rec_cache.seek_not_done=1;
1705 b_type=_mi_get_block_info(&block_info,info->dfile,filepos);
1706 }
1707
1708 if (b_type & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
1709 BLOCK_FATAL_ERROR))
1710 {
1711 if ((b_type & (BLOCK_DELETED | BLOCK_SYNC_ERROR))
1712 && skip_deleted_blocks)
1713 {
1714 filepos=block_info.filepos+block_info.block_len;
1715 block_info.second_read=0;
1716 continue; /* Search after next_record */
1717 }
1718 if (b_type & (BLOCK_DELETED | BLOCK_SYNC_ERROR))
1719 {
1720 errno=HA_ERR_RECORD_DELETED;
1721 info->lastpos=block_info.filepos;
1722 info->nextpos=block_info.filepos+block_info.block_len;
1723 }
1724 goto err;
1725 }
1726 if (block_of_record == 0) /* First block */
1727 {
1728 if (block_info.rec_len > (uint) share->base.max_pack_length)
1729 goto panic;
1730 info->lastpos=filepos;
1731 if (share->base.blobs)
1732 {
1733 if (!(to= mi_alloc_rec_buff(info, block_info.rec_len,
1734 &info->rec_buff)))
1735 goto err;
1736 }
1737 else
1738 to= info->rec_buff;
1739 left_len=block_info.rec_len;
1740 }
1741 if (left_len < block_info.data_len)
1742 goto panic; /* Wrong linked record */
1743
1744 /* copy information that is already read */
1745 {
1746 uint32_t offset=(uint) (block_info.filepos - filepos);
1747 uint32_t tmp_length= (sizeof(block_info.header) - offset);
1748 filepos=block_info.filepos;
1749
1750 if (tmp_length > block_info.data_len)
1751 tmp_length= block_info.data_len;
1752 if (tmp_length)
1753 {
1754 memcpy(to, block_info.header+offset,tmp_length);
1755 block_info.data_len-=tmp_length;
1756 left_len-=tmp_length;
1757 to+=tmp_length;
1758 filepos+=tmp_length;
1759 }
1760 }
1761 /* read rest of record from file */
1762 if (block_info.data_len)
1763 {
1764 if (info->opt_flag & READ_CACHE_USED)
1765 {
1766 if (_mi_read_cache(&info->rec_cache,(unsigned char*) to,filepos,
1767 block_info.data_len,
1768 (!block_of_record && skip_deleted_blocks) ?
1769 READING_NEXT : 0))
1770 goto panic;
1771 }
1772 else
1773 {
1774 if (info->opt_flag & WRITE_CACHE_USED &&
1775 info->rec_cache.pos_in_file <
1776 block_info.filepos + block_info.data_len &&
1777 info->rec_cache.flush())
1778 goto err;
1779 /* lseek(info->dfile,filepos,SEEK_SET); */
1780 if (internal::my_read(info->dfile,(unsigned char*) to,block_info.data_len,MYF(MY_NABP)))
1781 {
1782 if (errno == -1)
1783 errno= HA_ERR_WRONG_IN_RECORD; /* Unexpected end of file */
1784 goto err;
1785 }
1786 }
1787 }
1788 /*
1789 Increment block-of-record counter. If it was the first block,
1790 remember the position behind the block for the next call.
1791 */
1792 if (block_of_record++ == 0)
1793 {
1794 info->nextpos= block_info.filepos + block_info.block_len;
1795 skip_deleted_blocks= 0;
1796 }
1797 left_len-=block_info.data_len;
1798 to+=block_info.data_len;
1799 filepos=block_info.next_filepos;
1800 } while (left_len);
1801
1802 info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
1803 fast_mi_writeinfo(info);
1804 if (_mi_rec_unpack(info,buf,info->rec_buff,block_info.rec_len) !=
1805 MY_FILE_ERROR)
1806 return(0);
1807 return(errno); /* Wrong record */
1808
1809panic:
1810 errno=HA_ERR_WRONG_IN_RECORD; /* Something is fatal wrong */
1811err:
1812 save_errno=errno;
1813 _mi_writeinfo(info,0);
1814 return(errno=save_errno);
1815}
1816
1817
1818 /* Read and process header from a dynamic-record-file */
1819
1820uint32_t _mi_get_block_info(MI_BLOCK_INFO *info, int file, internal::my_off_t filepos)
1821{
1822 uint32_t return_val=0;
1823 unsigned char *header=info->header;
1824
1825 if (file >= 0)
1826 {
1827 /*
1828 We do not use my_pread() here because we want to have the file
1829 pointer set to the end of the header after this function.
1830 my_pread() may leave the file pointer untouched.
1831 */
1832 lseek(file,filepos,SEEK_SET);
1833 if (internal::my_read(file, header, sizeof(info->header),MYF(0)) !=
1834 sizeof(info->header))
1835 goto err;
1836 }
1837 if (info->second_read)
1838 {
1839 if (info->header[0] <= 6 || info->header[0] == 13)
1840 return_val=BLOCK_SYNC_ERROR;
1841 }
1842 else
1843 {
1844 if (info->header[0] > 6 && info->header[0] != 13)
1845 return_val=BLOCK_SYNC_ERROR;
1846 }
1847 info->next_filepos= HA_OFFSET_ERROR; /* Dummy if no next block */
1848
1849 switch (info->header[0]) {
1850 case 0:
1851 if ((info->block_len=(uint) mi_uint3korr(header+1)) <
1852 MI_MIN_BLOCK_LENGTH ||
1853 (info->block_len & (MI_DYN_ALIGN_SIZE -1)))
1854 goto err;
1855 info->filepos=filepos;
1856 info->next_filepos=mi_sizekorr(header+4);
1857 info->prev_filepos=mi_sizekorr(header+12);
1858#if SIZEOF_OFF_T == 4
1859 if ((mi_uint4korr(header+4) != 0 &&
1860 (mi_uint4korr(header+4) != UINT32_MAX ||
1861 info->next_filepos != UINT32_MAX) ||
1862 (mi_uint4korr(header+12) != 0 &&
1863 (mi_uint4korr(header+12) != UINT32_MAX ||
1864 info->prev_filepos != UINT32_MAX))
1865 goto err;
1866#endif
1867 return return_val | BLOCK_DELETED; /* Deleted block */
1868
1869 case 1:
1870 info->rec_len=info->data_len=info->block_len=mi_uint2korr(header+1);
1871 info->filepos=filepos+3;
1872 return return_val | BLOCK_FIRST | BLOCK_LAST;
1873 case 2:
1874 info->rec_len=info->data_len=info->block_len=mi_uint3korr(header+1);
1875 info->filepos=filepos+4;
1876 return return_val | BLOCK_FIRST | BLOCK_LAST;
1877
1878 case 13:
1879 info->rec_len=mi_uint4korr(header+1);
1880 info->block_len=info->data_len=mi_uint3korr(header+5);
1881 info->next_filepos=mi_sizekorr(header+8);
1882 info->second_read=1;
1883 info->filepos=filepos+16;
1884 return return_val | BLOCK_FIRST;
1885
1886 case 3:
1887 info->rec_len=info->data_len=mi_uint2korr(header+1);
1888 info->block_len=info->rec_len+ (uint) header[3];
1889 info->filepos=filepos+4;
1890 return return_val | BLOCK_FIRST | BLOCK_LAST;
1891 case 4:
1892 info->rec_len=info->data_len=mi_uint3korr(header+1);
1893 info->block_len=info->rec_len+ (uint) header[4];
1894 info->filepos=filepos+5;
1895 return return_val | BLOCK_FIRST | BLOCK_LAST;
1896
1897 case 5:
1898 info->rec_len=mi_uint2korr(header+1);
1899 info->block_len=info->data_len=mi_uint2korr(header+3);
1900 info->next_filepos=mi_sizekorr(header+5);
1901 info->second_read=1;
1902 info->filepos=filepos+13;
1903 return return_val | BLOCK_FIRST;
1904 case 6:
1905 info->rec_len=mi_uint3korr(header+1);
1906 info->block_len=info->data_len=mi_uint3korr(header+4);
1907 info->next_filepos=mi_sizekorr(header+7);
1908 info->second_read=1;
1909 info->filepos=filepos+15;
1910 return return_val | BLOCK_FIRST;
1911
1912 /* The following blocks are identical to 1-6 without rec_len */
1913 case 7:
1914 info->data_len=info->block_len=mi_uint2korr(header+1);
1915 info->filepos=filepos+3;
1916 return return_val | BLOCK_LAST;
1917 case 8:
1918 info->data_len=info->block_len=mi_uint3korr(header+1);
1919 info->filepos=filepos+4;
1920 return return_val | BLOCK_LAST;
1921
1922 case 9:
1923 info->data_len=mi_uint2korr(header+1);
1924 info->block_len=info->data_len+ (uint) header[3];
1925 info->filepos=filepos+4;
1926 return return_val | BLOCK_LAST;
1927 case 10:
1928 info->data_len=mi_uint3korr(header+1);
1929 info->block_len=info->data_len+ (uint) header[4];
1930 info->filepos=filepos+5;
1931 return return_val | BLOCK_LAST;
1932
1933 case 11:
1934 info->data_len=info->block_len=mi_uint2korr(header+1);
1935 info->next_filepos=mi_sizekorr(header+3);
1936 info->second_read=1;
1937 info->filepos=filepos+11;
1938 return return_val;
1939 case 12:
1940 info->data_len=info->block_len=mi_uint3korr(header+1);
1941 info->next_filepos=mi_sizekorr(header+4);
1942 info->second_read=1;
1943 info->filepos=filepos+12;
1944 return return_val;
1945 }
1946
1947err:
1948 errno=HA_ERR_WRONG_IN_RECORD; /* Garbage */
1949 return BLOCK_ERROR;
1950}
Convert from decimal to binary representation.
Definition common_fwd.h:215
TODO: Rename this file - func.h is stupid.
STL namespace.