26#include "myisam_priv.h"
32#include <drizzled/util/test.h>
33#include <drizzled/error.h>
42static char zero_string[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
44static int write_dynamic_record(MI_INFO *info,
const unsigned char *record,
46static int _mi_find_writepos(MI_INFO *info,ulong reclength,internal::my_off_t *filepos,
48static int update_dynamic_record(MI_INFO *info,internal::my_off_t filepos,
unsigned char *record,
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,
70bool mi_dynmap_file(MI_INFO *info, internal::my_off_t size)
72 if (size > (internal::my_off_t) (~((
size_t) 0)) - MEMMAP_EXTRA_MARGIN)
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,
90 if (info->s->file_map == (
unsigned char*) MAP_FAILED)
92 info->s->file_map= NULL;
96 madvise((
char*) info->s->file_map, size, MADV_RANDOM);
97 info->s->mmaped_length= size;
112void mi_remap_file(MI_INFO *info, internal::my_off_t size)
114 if (info->s->file_map)
116 munmap((
char*) info->s->file_map,
117 (
size_t) info->s->mmaped_length + MEMMAP_EXTRA_MARGIN);
118 mi_dynmap_file(info, size);
138size_t mi_mmap_pread(MI_INFO *info,
unsigned char *Buffer,
139 size_t Count, internal::my_off_t offset, myf MyFlags)
148 if (info->s->mmaped_length >= offset + Count)
150 memcpy(Buffer, info->s->file_map + offset, Count);
155 return my_pread(info->dfile, Buffer, Count, offset, MyFlags);
162size_t mi_nommap_pread(MI_INFO *info,
unsigned char *Buffer,
163 size_t Count, internal::my_off_t offset, myf MyFlags)
165 return my_pread(info->dfile, Buffer, Count, offset, MyFlags);
185size_t mi_mmap_pwrite(MI_INFO *info,
const unsigned char *Buffer,
186 size_t Count, internal::my_off_t offset, myf MyFlags)
196 if (info->s->mmaped_length >= offset + Count)
198 memcpy(info->s->file_map + offset, Buffer, Count);
203 info->s->nonmmaped_inserts++;
204 return my_pwrite(info->dfile, Buffer, Count, offset, MyFlags);
212size_t mi_nommap_pwrite(MI_INFO *info,
const unsigned char *Buffer,
213 size_t Count, internal::my_off_t offset, myf MyFlags)
215 return my_pwrite(info->dfile, Buffer, Count, offset, MyFlags);
219int _mi_write_dynamic_record(MI_INFO *info,
const unsigned char *record)
221 ulong reclength=_mi_rec_pack(info,info->rec_buff,record);
222 return (write_dynamic_record(info,info->rec_buff,reclength));
225int _mi_update_dynamic_record(MI_INFO *info, internal::my_off_t pos,
const unsigned char *record)
227 uint32_t length=_mi_rec_pack(info,info->rec_buff,record);
228 return (update_dynamic_record(info,pos,info->rec_buff,length));
231int _mi_write_blob_record(MI_INFO *info,
const unsigned char *record)
233 unsigned char *rec_buff;
235 ulong reclength,reclength2,extra;
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);
242 if (reclength > MI_DYN_MAX_ROW_LENGTH)
244 errno=HA_ERR_TO_BIG_ROW;
248 if (!(rec_buff=(
unsigned char*) malloc(reclength)))
250 errno= HA_ERR_OUT_OF_MEM;
253 reclength2= _mi_rec_pack(info,rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
255 assert(reclength2 <= reclength);
256 error=write_dynamic_record(info,rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
263int _mi_update_blob_record(MI_INFO *info, internal::my_off_t pos,
const unsigned char *record)
265 unsigned char *rec_buff;
267 ulong reclength,extra;
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);
274 if (reclength > MI_DYN_MAX_ROW_LENGTH)
276 errno=HA_ERR_TO_BIG_ROW;
280 if (!(rec_buff=(
unsigned char*) malloc(reclength)))
282 errno= HA_ERR_OUT_OF_MEM;
285 reclength=_mi_rec_pack(info,rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
287 error=update_dynamic_record(info,pos,
288 rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
295int _mi_delete_dynamic_record(MI_INFO *info)
297 return delete_dynamic_record(info,info->lastpos,0);
303static int write_dynamic_record(MI_INFO *info,
const unsigned char *record,
308 internal::my_off_t filepos;
321 if (DRIZZLE_UNLIKELY(info->s->base.max_data_file_length -
322 info->state->data_file_length <
323 reclength + MI_MAX_DYN_BLOCK_HEADER))
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)
329 errno=HA_ERR_RECORD_FILE_FULL;
336 if (_mi_find_writepos(info,reclength,&filepos,&length))
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))
353static int _mi_find_writepos(MI_INFO *info,
355 internal::my_off_t *filepos,
358 MI_BLOCK_INFO block_info;
361 if (info->s->state.dellink != HA_OFFSET_ERROR &&
362 !info->append_insert_at_end)
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) &
371 errno=HA_ERR_WRONG_IN_RECORD;
374 info->s->state.dellink=block_info.next_filepos;
376 info->state->empty-= block_info.block_len;
377 *length= block_info.block_len;
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;
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))
392 errno=HA_ERR_RECORD_FILE_FULL;
395 if (tmp > MI_MAX_BLOCK_LENGTH)
396 tmp=MI_MAX_BLOCK_LENGTH;
398 info->state->data_file_length+= tmp;
399 info->s->state.split++;
400 info->update|=HA_STATE_WRITE_AT_END;
413static bool unlink_deleted_block(MI_INFO *info, MI_BLOCK_INFO *block_info)
415 if (block_info->filepos == info->s->state.dellink)
418 info->s->state.dellink=block_info->next_filepos;
425 if (!(_mi_get_block_info(&tmp,info->dfile,block_info->prev_filepos)
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)))
433 if (block_info->next_filepos != HA_OFFSET_ERROR)
435 if (!(_mi_get_block_info(&tmp,info->dfile,block_info->next_filepos)
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,
447 info->state->empty-= block_info->block_len;
448 info->s->state.split--;
455 if (info->nextpos == block_info->filepos)
456 info->nextpos+=block_info->block_len;
476static int update_backward_delete_link(MI_INFO *info, internal::my_off_t delete_block,
477 internal::my_off_t filepos)
479 MI_BLOCK_INFO block_info;
481 if (delete_block != HA_OFFSET_ERROR)
483 block_info.second_read=0;
484 if (_mi_get_block_info(&block_info,info->dfile,delete_block)
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)))
494 errno=HA_ERR_WRONG_IN_RECORD;
504static int delete_dynamic_record(MI_INFO *info, internal::my_off_t filepos,
505 uint32_t second_read)
507 uint32_t length,b_type;
508 MI_BLOCK_INFO block_info,del_block;
510 bool remove_next_block;
513 error= update_backward_delete_link(info, info->s->state.dellink, filepos);
515 block_info.second_read=second_read;
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) <
525 errno=HA_ERR_WRONG_IN_RECORD;
529 del_block.second_read=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)
536 length+=del_block.block_len;
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);
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,
549 info->s->state.dellink = filepos;
551 info->state->empty+=length;
552 filepos=block_info.next_filepos;
555 if (remove_next_block && unlink_deleted_block(info,&del_block))
557 }
while (!(b_type & BLOCK_LAST));
565int _mi_write_part_record(MI_INFO *info,
566 internal::my_off_t filepos,
568 internal::my_off_t next_filepos,
569 unsigned char **record,
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];
578 next_delete_block=HA_OFFSET_ERROR;
580 res_length=extra_length=0;
581 if (length > *reclength + MI_SPLIT_LENGTH)
583 res_length=MY_ALIGN(length- *reclength - MI_EXTEND_BLOCK_LENGTH,
587 long_block= (length < 65520L && *reclength < 65520L) ? 0 : 1;
588 if (length == *reclength+ 3 + long_block)
591 temp[0]=(
unsigned char) (1+ *flag)+(
unsigned char) long_block;
594 mi_int3store(temp+1,*reclength);
599 mi_int2store(temp+1,*reclength);
603 else if (length-long_block < *reclength+4)
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);
611 if (*reclength > MI_MAX_BLOCK_LENGTH)
615 mi_int4store(temp+1,*reclength);
616 mi_int3store(temp+5,length-head_length);
617 mi_sizestore((
unsigned char*) temp+8,next_filepos);
621 head_length=5+8+long_block*2;
622 temp[0]=5+(
unsigned char) long_block;
625 mi_int3store(temp+1,*reclength);
626 mi_int3store(temp+4,length-head_length);
627 mi_sizestore((
unsigned char*) temp+7,next_filepos);
631 mi_int2store(temp+1,*reclength);
632 mi_int2store(temp+3,length-head_length);
633 mi_sizestore((
unsigned char*) temp+5,next_filepos);
639 head_length=3+8+long_block;
640 temp[0]=11+(
unsigned char) long_block;
643 mi_int3store(temp+1,length-head_length);
644 mi_sizestore((
unsigned char*) temp+4,next_filepos);
648 mi_int2store(temp+1,length-head_length);
649 mi_sizestore((
unsigned char*) temp+3,next_filepos);
655 head_length=4+long_block;
656 extra_length= length- *reclength-head_length;
657 temp[0]= (
unsigned char) (3+ *flag)+(
unsigned char) long_block;
660 mi_int3store(temp+1,*reclength);
661 temp[4]= (
unsigned char) (extra_length);
665 mi_int2store(temp+1,*reclength);
666 temp[3]= (
unsigned char) (extra_length);
668 length= *reclength+head_length;
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);
681 MI_BLOCK_INFO del_block;
682 internal::my_off_t next_block=filepos+length+extra_length+res_length;
684 del_block.second_read=0;
685 if (next_block < info->state->data_file_length &&
686 info->s->state.dellink != HA_OFFSET_ERROR)
688 if ((_mi_get_block_info(&del_block,info->dfile,next_block)
690 res_length + del_block.block_len < MI_DYN_MAX_BLOCK_LENGTH)
692 if (unlink_deleted_block(info,&del_block))
694 res_length+=del_block.block_len;
699 pos=record_end+extra_length;
701 mi_int3store(pos+1,res_length);
702 mi_sizestore(pos+4,info->s->state.dellink);
703 memset(pos+12, 255, 8);
704 next_delete_block=info->s->state.dellink;
705 info->s->state.dellink= filepos+length+extra_length;
707 info->state->empty+=res_length;
708 info->s->state.split++;
710 if (info->opt_flag & WRITE_CACHE_USED &&
711 info->update & HA_STATE_WRITE_AT_END)
713 if (info->update & HA_STATE_EXTEND_BLOCK)
715 info->update&= ~HA_STATE_EXTEND_BLOCK;
716 if (info->rec_cache.block_write(*record - head_length,
717 length+extra_length+del_length,filepos))
720 else if (info->rec_cache.write(*record-head_length, length+extra_length+del_length))
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))
730 memcpy(record_end, temp, extra_length + del_length);
732 *reclength-=(length-head_length);
738 if (update_backward_delete_link(info, next_delete_block,
739 info->s->state.dellink))
751static int update_dynamic_record(MI_INFO *info, internal::my_off_t filepos,
unsigned char *record,
757 MI_BLOCK_INFO block_info;
759 flag=block_info.second_read=0;
773 if (DRIZZLE_UNLIKELY(info->s->base.max_data_file_length -
774 info->state->data_file_length < reclength))
780 if ((error=_mi_get_block_info(&block_info,info->dfile,filepos))
781 & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR | BLOCK_FATAL_ERROR))
783 if (!(error & BLOCK_FATAL_ERROR))
784 errno=HA_ERR_WRONG_IN_RECORD;
791 if (block_info.rec_len < reclength)
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)
797 errno=HA_ERR_RECORD_FILE_FULL;
801 block_info.second_read=0;
804 while (reclength > 0)
806 if (filepos != info->s->state.dellink)
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 |
813 if (!(error & BLOCK_FATAL_ERROR))
814 errno=HA_ERR_WRONG_IN_RECORD;
817 length=(ulong) (block_info.filepos-filepos) + block_info.block_len;
818 if (length < reclength)
820 uint32_t tmp=MY_ALIGN(reclength - length + 3 +
821 test(reclength >= 65520L),MI_DYN_ALIGN_SIZE);
823 tmp= min(length+tmp, MI_MAX_BLOCK_LENGTH)-length;
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)
831 if (info->nextpos == info->state->data_file_length)
833 info->state->data_file_length+= tmp;
834 info->update|= HA_STATE_WRITE_AT_END | HA_STATE_EXTEND_BLOCK;
837 else if (length < MI_MAX_BLOCK_LENGTH - MI_MIN_BLOCK_LENGTH)
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) &
853 if (unlink_deleted_block(info,&del_block))
855 if ((length+=del_block.block_len) > MI_MAX_BLOCK_LENGTH)
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;
866 if (update_backward_delete_link(info, info->s->state.dellink,
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,
878 info->s->state.dellink= next_pos;
879 info->s->state.split++;
881 info->state->empty+= rest_length;
882 length-= rest_length;
890 if (_mi_find_writepos(info,reclength,&filepos,&length))
893 if (_mi_write_part_record(info,filepos,length,block_info.next_filepos,
894 &record,&reclength,&flag))
896 if ((filepos=block_info.next_filepos) == HA_OFFSET_ERROR)
899 filepos=info->s->state.dellink;
903 if (block_info.next_filepos != HA_OFFSET_ERROR)
904 if (delete_dynamic_record(info,block_info.next_filepos,1))
914uint32_t _mi_rec_pack(MI_INFO *info,
unsigned char *to,
915 const unsigned char *from)
917 uint length,new_length,flag,bit,i;
918 unsigned char *pos,*end,*startpos,*packpos;
919 enum en_fieldtype
type;
924 startpos=packpos=to; to+= info->s->base.pack_bits; blob=info->blobs;
927 for (i=info->s->base.fields ; i-- > 0; from+= length,rec++)
929 length=(uint) rec->length;
930 if ((
type = (
enum en_fieldtype) rec->type) != FIELD_NORMAL)
932 if (
type == FIELD_BLOB)
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;
947 else if (
type == FIELD_SKIP_ZERO)
949 if (memcmp(from,zero_string,length) == 0)
953 memcpy(to, from, length);
957 else if (
type == FIELD_SKIP_ENDSPACE ||
958 type == FIELD_SKIP_PRESPACE)
960 pos= (
unsigned char*) from; end= (
unsigned char*) from + length;
961 if (
type == FIELD_SKIP_ENDSPACE)
963 while (end > from && *(end-1) ==
' ')
968 while (pos < end && *pos ==
' ')
971 new_length=(uint) (end-pos);
972 if (new_length +1 + test(rec->length > 255 && new_length > 127)
975 if (rec->length > 255 && new_length > 127)
977 to[0]= (
unsigned char) ((new_length & 127) + 128);
978 to[1]= (
unsigned char) (new_length >> 7);
982 *to++= (
unsigned char) new_length;
983 memcpy(to, pos, new_length);
989 memcpy(to, from, length);
993 else if (
type == FIELD_VARCHAR)
995 uint32_t pack_length= ha_varchar_packlength(rec->length -1);
997 if (pack_length == 1)
999 tmp_length= (uint) *(
unsigned char*) from;
1004 tmp_length= uint2korr(from);
1005 store_key_length_inc(to,tmp_length);
1007 memcpy(to, from+pack_length, tmp_length);
1013 memcpy(to, from, length);
1017 if ((bit= bit << 1) >= 256)
1019 *packpos++= (
unsigned char) flag;
1025 memcpy(to, from, length);
1030 *packpos= (
unsigned char) flag;
1031 if (info->s->calc_checksum)
1032 *to++= (
unsigned char) info->checksum;
1033 return((uint) (to-startpos));
1043bool _mi_rec_check(MI_INFO *info,
const unsigned char *record,
unsigned char *rec_buff,
1044 ulong packed_length,
bool with_checksum)
1046 uint length,new_length,flag,bit,i;
1047 unsigned char *pos,*end,*packpos,*to;
1048 enum en_fieldtype
type;
1051 packpos=rec_buff; to= rec_buff+info->s->base.pack_bits;
1053 flag= *packpos; bit=1;
1055 for (i=info->s->base.fields ; i-- > 0; record+= length, rec++)
1057 length=(uint) rec->length;
1058 if ((
type = (
enum en_fieldtype) rec->type) != FIELD_NORMAL)
1060 if (
type == FIELD_BLOB)
1062 uint32_t blob_length=
1063 _mi_calc_blob_length(length-portable_sizeof_char_ptr,record);
1064 if (!blob_length && !(flag & bit))
1067 to+=length - portable_sizeof_char_ptr+ blob_length;
1069 else if (
type == FIELD_SKIP_ZERO)
1071 if (memcmp(record,zero_string,length) == 0)
1079 else if (
type == FIELD_SKIP_ENDSPACE ||
1080 type == FIELD_SKIP_PRESPACE)
1082 pos= (
unsigned char*) record; end= (
unsigned char*) record + length;
1083 if (
type == FIELD_SKIP_ENDSPACE)
1085 while (end > record && *(end-1) ==
' ')
1090 while (pos < end && *pos ==
' ')
1093 new_length=(uint) (end-pos);
1094 if (new_length +1 + test(rec->length > 255 && new_length > 127)
1099 if (rec->length > 255 && new_length > 127)
1101 if (to[0] != (
unsigned char) ((new_length & 127) + 128) ||
1102 to[1] != (
unsigned char) (new_length >> 7))
1106 else if (*to++ != (
unsigned char) new_length)
1113 else if (
type == FIELD_VARCHAR)
1115 uint32_t pack_length= ha_varchar_packlength(rec->length -1);
1116 uint32_t tmp_length;
1117 if (pack_length == 1)
1119 tmp_length= (uint) *(
unsigned char*) record;
1125 tmp_length= uint2korr(record);
1126 to+= get_pack_length(tmp_length)+tmp_length;
1135 if ((bit= bit << 1) >= 256)
1144 if (packed_length != (uint) (to - rec_buff) + test(info->s->calc_checksum) ||
1145 (bit != 1 && (flag & ~(bit - 1))))
1147 if (with_checksum && ((
unsigned char) info->checksum != (
unsigned char) *to))
1163ulong _mi_rec_unpack(MI_INFO *info,
unsigned char *to,
unsigned char *from,
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;
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)
1176 from+= info->s->base.pack_bits;
1177 min_pack_length=info->s->base.min_pack_length - info->s->base.pack_bits;
1179 for (rec=info->s->rec , end_field=rec+info->s->base.fields ;
1180 rec < end_field ; to+= rec_length, rec++)
1182 rec_length=rec->length;
1183 if ((
type = (
enum en_fieldtype) rec->type) != FIELD_NORMAL &&
1184 (
type != FIELD_CHECK))
1186 if (
type == FIELD_VARCHAR)
1188 uint32_t pack_length= ha_varchar_packlength(rec_length-1);
1189 if (pack_length == 1)
1191 length= (uint) *(
unsigned char*) from;
1192 if (length > rec_length-1)
1198 get_key_length(length, from);
1199 if (length > rec_length-2)
1201 int2store(to,length);
1203 if (from+length > from_end)
1205 memcpy(to+pack_length, from, length);
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)
1217 if (rec->length > 255 && *from & 128)
1219 if (from + 1 >= from_end)
1221 length= (*from & 127)+ ((uint) (
unsigned char) *(from+1) << 7); from+=2;
1225 if (from == from_end)
1227 length= (
unsigned char) *from++;
1230 if (length >= rec_length ||
1231 min_pack_length + length > (uint) (from_end - from))
1233 if (
type == FIELD_SKIP_ENDSPACE)
1235 memcpy(to, from, length);
1236 memset(to+length,
' ', rec_length-length);
1240 memset(to,
' ', rec_length-length);
1241 memcpy(to + rec_length - length, from, length);
1246 else if (
type == FIELD_BLOB)
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)
1255 memcpy(to, from, size_length);
1257 memcpy(to+size_length, &from,
sizeof(
char*));
1262 if (
type == FIELD_SKIP_ENDSPACE ||
type == FIELD_SKIP_PRESPACE)
1264 if (min_pack_length + rec_length > (uint) (from_end - from))
1266 memcpy(to, from, rec_length);
1269 if ((bit= bit << 1) >= 256)
1271 flag= (
unsigned char) *++packpos; bit=1;
1276 if (min_pack_length > (uint) (from_end - from))
1278 min_pack_length-=rec_length;
1279 memcpy(to, from, rec_length);
1283 if (info->s->calc_checksum)
1285 if (to == to_end && from == from_end && (bit == 1 || !(flag & ~(bit-1))))
1286 return(found_length);
1289 errno= HA_ERR_WRONG_IN_RECORD;
1290 return(MY_FILE_ERROR);
1296ulong _my_calc_total_blob_length(MI_INFO *info,
const unsigned char *record)
1301 for (length=0, blob= info->blobs, end=blob+info->s->base.blobs ;
1305 blob->length=_mi_calc_blob_length(blob->pack_length,record + blob->offset);
1306 length+=blob->length;
1312ulong _mi_calc_blob_length(uint32_t length,
const unsigned char *pos)
1316 return (uint) (
unsigned char) *pos;
1318 return (uint) uint2korr(pos);
1320 return uint3korr(pos);
1322 return uint4korr(pos);
1330void _my_store_blob_length(
unsigned char *pos,uint32_t pack_length,uint32_t length)
1332 switch (pack_length) {
1334 *pos= (
unsigned char) length;
1337 int2store(pos,length);
1340 int3store(pos,length);
1343 int4store(pos,length);
1383int _mi_read_dynamic_record(MI_INFO *info, internal::my_off_t filepos,
unsigned char *buf)
1385 int block_of_record;
1386 uint32_t b_type, left_length= 0;
1387 unsigned char *to= NULL;
1388 MI_BLOCK_INFO block_info;
1391 if (filepos != HA_OFFSET_ERROR)
1395 block_info.second_read= 0;
1399 if (filepos == HA_OFFSET_ERROR)
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())
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 |
1410 if (b_type & (BLOCK_SYNC_ERROR | BLOCK_DELETED))
1411 errno=HA_ERR_RECORD_DELETED;
1414 if (block_of_record++ == 0)
1416 if (block_info.rec_len > (uint) info->s->base.max_pack_length)
1418 if (info->s->base.blobs)
1420 if (!(to=mi_alloc_rec_buff(info, block_info.rec_len,
1426 left_length=block_info.rec_len;
1428 if (left_length < block_info.data_len || ! block_info.data_len)
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);
1436 if (prefetch_len > block_info.data_len)
1437 prefetch_len= block_info.data_len;
1440 memcpy(to, block_info.header + offset, prefetch_len);
1441 block_info.data_len-= prefetch_len;
1442 left_length-= prefetch_len;
1447 if (block_info.data_len)
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())
1458 if (info->s->file_read(info, (
unsigned char*) to, block_info.data_len,
1459 filepos, MYF(MY_NABP)))
1461 left_length-=block_info.data_len;
1462 to+=block_info.data_len;
1464 filepos= block_info.next_filepos;
1465 }
while (left_length);
1467 info->update|= HA_STATE_AKTIV;
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);
1472 fast_mi_writeinfo(info);
1476 errno=HA_ERR_WRONG_IN_RECORD;
1478 _mi_writeinfo(info,0);
1484int _mi_cmp_dynamic_unique(MI_INFO *info, MI_UNIQUEDEF *def,
1485 const unsigned char *record, internal::my_off_t pos)
1487 unsigned char *rec_buff,*old_record;
1490 if (!(old_record=(
unsigned char *)malloc(info->s->base.reclength)))
1494 rec_buff=info->rec_buff;
1495 if (info->s->base.blobs)
1497 error=_mi_read_dynamic_record(info,pos,old_record);
1499 error=mi_unique_comp(def, record, old_record, def->null_are_equal);
1500 if (info->s->base.blobs)
1502 void * rec_buff_ptr= mi_get_rec_buff_ptr(info, info->rec_buff);
1504 info->rec_buff=rec_buff;
1513int _mi_cmp_dynamic_record(MI_INFO *info,
const unsigned char *record)
1515 uint32_t flag,reclength,b_type;
1516 internal::my_off_t filepos;
1517 unsigned char *buffer;
1518 MI_BLOCK_INFO block_info;
1520 if (info->opt_flag & WRITE_CACHE_USED)
1522 info->update&= ~(HA_STATE_WRITE_AT_END | HA_STATE_EXTEND_BLOCK);
1523 if (info->rec_cache.flush())
1526 info->rec_cache.seek_not_done=1;
1530 buffer=info->rec_buff;
1531 if ((info->opt_flag & READ_CHECK_USED))
1533 if (info->s->base.blobs)
1535 if (!(buffer=(
unsigned char*) malloc(info->s->base.pack_reclength+
1536 _my_calc_total_blob_length(info,record))))
1539 reclength=_mi_rec_pack(info,buffer,record);
1542 filepos=info->lastpos;
1543 flag=block_info.second_read=0;
1544 block_info.next_filepos=filepos;
1545 while (reclength > 0)
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 |
1552 if (b_type & (BLOCK_SYNC_ERROR | BLOCK_DELETED))
1553 errno=HA_ERR_RECORD_CHANGED;
1559 if (reclength != block_info.rec_len)
1561 errno=HA_ERR_RECORD_CHANGED;
1564 }
else if (reclength < block_info.data_len)
1566 errno=HA_ERR_WRONG_IN_RECORD;
1569 reclength-=block_info.data_len;
1570 if (_mi_cmp_buffer(info->dfile,record,block_info.filepos,
1571 block_info.data_len))
1573 errno=HA_ERR_RECORD_CHANGED;
1577 record+=block_info.data_len;
1582 if (buffer != info->rec_buff)
1583 free((
unsigned char*) buffer);
1590static int _mi_cmp_buffer(
int file,
const unsigned char *buff, internal::my_off_t filepos,
1593 uint32_t next_length;
1594 unsigned char temp_buff[IO_SIZE*2];
1596 next_length= IO_SIZE*2 - (uint) (filepos & (IO_SIZE-1));
1598 while (length > IO_SIZE*2)
1600 if (my_pread(file,temp_buff,next_length,filepos, MYF(MY_NABP)) ||
1601 memcmp(buff, temp_buff, next_length))
1603 filepos+=next_length;
1605 length-= next_length;
1606 next_length=IO_SIZE*2;
1608 if (my_pread(file,temp_buff,length,filepos,MYF(MY_NABP)))
1610 return(memcmp(buff,temp_buff,length));
1650int _mi_read_rnd_dynamic_record(MI_INFO *info,
unsigned char *buf,
1651 internal::my_off_t filepos,
1652 bool skip_deleted_blocks)
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;
1662 if (info->lock_type == F_UNLCK)
1664 info->tmp_lock_type=F_RDLCK;
1670 block_info.second_read= 0;
1674 if (filepos >= info->state->data_file_length)
1679 info->rec_cache.seek_not_done=1;
1680 if (mi_state_info_read_dsk(share->kfile,&share->state,1))
1683 if (filepos >= info->state->data_file_length)
1685 errno= HA_ERR_END_OF_FILE;
1689 if (info->opt_flag & READ_CACHE_USED)
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))
1696 b_type=_mi_get_block_info(&block_info,-1,filepos);
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())
1704 info->rec_cache.seek_not_done=1;
1705 b_type=_mi_get_block_info(&block_info,info->dfile,filepos);
1708 if (b_type & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
1711 if ((b_type & (BLOCK_DELETED | BLOCK_SYNC_ERROR))
1712 && skip_deleted_blocks)
1714 filepos=block_info.filepos+block_info.block_len;
1715 block_info.second_read=0;
1718 if (b_type & (BLOCK_DELETED | BLOCK_SYNC_ERROR))
1720 errno=HA_ERR_RECORD_DELETED;
1721 info->lastpos=block_info.filepos;
1722 info->nextpos=block_info.filepos+block_info.block_len;
1726 if (block_of_record == 0)
1728 if (block_info.rec_len > (uint) share->base.max_pack_length)
1730 info->lastpos=filepos;
1731 if (share->base.blobs)
1733 if (!(to= mi_alloc_rec_buff(info, block_info.rec_len,
1739 left_len=block_info.rec_len;
1741 if (left_len < block_info.data_len)
1746 uint32_t offset=(uint) (block_info.filepos - filepos);
1747 uint32_t tmp_length= (
sizeof(block_info.header) - offset);
1748 filepos=block_info.filepos;
1750 if (tmp_length > block_info.data_len)
1751 tmp_length= block_info.data_len;
1754 memcpy(to, block_info.header+offset,tmp_length);
1755 block_info.data_len-=tmp_length;
1756 left_len-=tmp_length;
1758 filepos+=tmp_length;
1762 if (block_info.data_len)
1764 if (info->opt_flag & READ_CACHE_USED)
1766 if (_mi_read_cache(&info->rec_cache,(
unsigned char*) to,filepos,
1767 block_info.data_len,
1768 (!block_of_record && skip_deleted_blocks) ?
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())
1780 if (internal::my_read(info->dfile,(
unsigned char*) to,block_info.data_len,MYF(MY_NABP)))
1783 errno= HA_ERR_WRONG_IN_RECORD;
1792 if (block_of_record++ == 0)
1794 info->nextpos= block_info.filepos + block_info.block_len;
1795 skip_deleted_blocks= 0;
1797 left_len-=block_info.data_len;
1798 to+=block_info.data_len;
1799 filepos=block_info.next_filepos;
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) !=
1810 errno=HA_ERR_WRONG_IN_RECORD;
1813 _mi_writeinfo(info,0);
1814 return(errno=save_errno);
1820uint32_t _mi_get_block_info(MI_BLOCK_INFO *info,
int file, internal::my_off_t filepos)
1822 uint32_t return_val=0;
1823 unsigned char *header=info->header;
1832 lseek(file,filepos,SEEK_SET);
1833 if (internal::my_read(file, header,
sizeof(info->header),MYF(0)) !=
1834 sizeof(info->header))
1837 if (info->second_read)
1839 if (info->header[0] <= 6 || info->header[0] == 13)
1840 return_val=BLOCK_SYNC_ERROR;
1844 if (info->header[0] > 6 && info->header[0] != 13)
1845 return_val=BLOCK_SYNC_ERROR;
1847 info->next_filepos= HA_OFFSET_ERROR;
1849 switch (info->header[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)))
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))
1867 return return_val | BLOCK_DELETED;
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;
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;
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;
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;
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;
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;
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;
1914 info->data_len=info->block_len=mi_uint2korr(header+1);
1915 info->filepos=filepos+3;
1916 return return_val | BLOCK_LAST;
1918 info->data_len=info->block_len=mi_uint3korr(header+1);
1919 info->filepos=filepos+4;
1920 return return_val | BLOCK_LAST;
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;
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;
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;
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;
1948 errno=HA_ERR_WRONG_IN_RECORD;
Convert from decimal to binary representation.
TODO: Rename this file - func.h is stupid.