18 #include "myisam_priv.h"
19 #include <drizzled/internal/m_string.h>
20 #include <drizzled/util/test.h>
22 using namespace drizzled;
25 unsigned char *key,uint32_t key_length,internal::my_off_t page,
unsigned char *anc_buff);
26 static int del(
MI_INFO *info,
MI_KEYDEF *keyinfo,
unsigned char *key,
unsigned char *anc_buff,
27 internal::my_off_t leaf_page,
unsigned char *leaf_buff,
unsigned char *keypos,
28 internal::my_off_t next_block,
unsigned char *ret_key);
29 static int underflow(
MI_INFO *info,
MI_KEYDEF *keyinfo,
unsigned char *anc_buff,
30 internal::my_off_t leaf_page,
unsigned char *leaf_buff,
unsigned char *keypos);
31 static uint32_t remove_key(
MI_KEYDEF *keyinfo,uint32_t nod_flag,
unsigned char *keypos,
32 unsigned char *lastkey,
unsigned char *page_end,
33 internal::my_off_t *next_block);
35 unsigned char *key, uint32_t key_length, internal::my_off_t *root);
38 int mi_delete(
MI_INFO *info,
const unsigned char *record)
41 unsigned char *old_key;
48 if (!(info->update & HA_STATE_AKTIV))
50 return(errno=HA_ERR_KEY_NOT_FOUND);
52 if (share->options & HA_OPTION_READ_ONLY_DATA)
56 if (_mi_readinfo(info,F_WRLCK,1))
58 if (info->s->calc_checksum)
59 info->checksum=(*info->s->calc_checksum)(info,record);
60 if ((*share->compare_record)(info,record))
63 if (_mi_mark_file_changed(info))
68 old_key=info->lastkey2;
69 for (i=0 ; i < share->base.keys ; i++ )
71 if (mi_is_key_active(info->s->state.key_map, i))
73 info->s->keyinfo[i].version++;
75 if (info->s->keyinfo[i].ck_delete(info,i,old_key,
76 _mi_make_key(info,i,old_key,record,info->lastpos)))
80 info->update&= ~HA_STATE_RNEXT_SAME;
84 if ((*share->delete_record)(info))
86 info->state->checksum-=info->checksum;
88 info->update= HA_STATE_CHANGED+HA_STATE_DELETED+HA_STATE_ROW_CHANGED;
89 info->state->records--;
91 mi_sizestore(lastpos,info->lastpos);
92 _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
97 mi_sizestore(lastpos,info->lastpos);
98 if (save_errno != HA_ERR_RECORD_CHANGED)
100 mi_print_error(info->s, HA_ERR_CRASHED);
101 mi_mark_crashed(info);
103 _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
104 info->update|=HA_STATE_WRITTEN;
106 if (save_errno == HA_ERR_KEY_NOT_FOUND)
108 mi_print_error(info->s, HA_ERR_CRASHED);
109 errno=HA_ERR_CRASHED;
118 int _mi_ck_delete(
register MI_INFO *info, uint32_t keynr,
unsigned char *key,
121 return _mi_ck_real_delete(info, info->s->keyinfo+keynr, key, key_length,
122 &info->s->state.key_root[keynr]);
127 unsigned char *key, uint32_t key_length, internal::my_off_t *root)
131 internal::my_off_t old_root;
132 unsigned char *root_buff;
134 if ((old_root=*root) == HA_OFFSET_ERROR)
136 mi_print_error(info->s, HA_ERR_CRASHED);
137 return(errno=HA_ERR_CRASHED);
139 if (!(root_buff= (
unsigned char*) malloc(keyinfo->block_length+
142 return(errno=ENOMEM);
144 if (!_mi_fetch_keypage(info,keyinfo,old_root,DFLT_INIT_HITS,root_buff,0))
149 if ((error=d_search(info,keyinfo, (SEARCH_SAME), key,key_length,old_root,root_buff)) > 0)
153 error=_mi_enlarge_root(info,keyinfo,key,root);
157 if (mi_getint(root_buff) <= (nod_flag=mi_test_if_nod(root_buff))+3)
161 *root=_mi_kpos(nod_flag,root_buff+2+nod_flag);
163 *root=HA_OFFSET_ERROR;
164 if (_mi_dispose(info,keyinfo,old_root,DFLT_INIT_HITS))
168 error=_mi_write_keypage(info,keyinfo,old_root,
169 DFLT_INIT_HITS,root_buff);
187 uint32_t comp_flag,
unsigned char *key, uint32_t key_length,
188 internal::my_off_t page,
unsigned char *anc_buff)
190 int flag,ret_value,save_flag;
191 uint32_t length,nod_flag,search_key_length;
193 unsigned char *leaf_buff,*keypos;
194 internal::my_off_t leaf_page= 0, next_block;
195 unsigned char lastkey[MI_MAX_KEY_BUFF];
197 search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
198 flag=(*keyinfo->bin_search)(info,keyinfo,anc_buff,key, search_key_length,
199 comp_flag, &keypos, lastkey, &last_key);
200 if (flag == MI_FOUND_WRONG_KEY)
204 nod_flag=mi_test_if_nod(anc_buff);
209 leaf_page=_mi_kpos(nod_flag,keypos);
210 if (!(leaf_buff= (
unsigned char*) malloc(keyinfo->block_length+
216 if (!_mi_fetch_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff,0))
224 mi_print_error(info->s, HA_ERR_CRASHED);
225 errno=HA_ERR_CRASHED;
229 ret_value=d_search(info,keyinfo,comp_flag,key,key_length,
230 leaf_page,leaf_buff);
235 length=mi_getint(anc_buff);
236 if (!(tmp= remove_key(keyinfo,nod_flag,keypos,lastkey,anc_buff+length,
242 mi_putint(anc_buff,length,nod_flag);
245 if (_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,anc_buff))
250 return(test(length <= (info->quick_mode ? MI_MIN_KEYBLOCK_LENGTH :
251 (uint) keyinfo->underflow_block_length)));
254 ret_value=del(info,keyinfo,key,anc_buff,leaf_page,leaf_buff,keypos,
261 ret_value= underflow(info,keyinfo,anc_buff,leaf_page,leaf_buff,keypos);
264 if (!_mi_get_last_key(info,keyinfo,anc_buff,lastkey,keypos,&length))
268 ret_value=_mi_insert(info,keyinfo,key,anc_buff,keypos,lastkey,
269 (
unsigned char*) 0,(
unsigned char*) 0,(internal::my_off_t) 0,(
bool) 0);
272 if (ret_value == 0 && mi_getint(anc_buff) > keyinfo->block_length)
275 ret_value=_mi_split_page(info,keyinfo,key,anc_buff,lastkey,0) | 2;
277 if (save_flag && ret_value != 1)
278 ret_value|=_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,anc_buff);
290 static int del(
register MI_INFO *info,
register MI_KEYDEF *keyinfo,
unsigned char *key,
291 unsigned char *anc_buff, internal::my_off_t leaf_page,
unsigned char *leaf_buff,
292 unsigned char *keypos,
293 internal::my_off_t next_block,
294 unsigned char *ret_key)
296 int ret_value,length;
297 uint32_t a_length,nod_flag,tmp;
298 internal::my_off_t next_page;
299 unsigned char keybuff[MI_MAX_KEY_BUFF],*endpos,*next_buff,*key_start, *prev_key;
303 endpos=leaf_buff+mi_getint(leaf_buff);
304 if (!(key_start=_mi_get_last_key(info,keyinfo,leaf_buff,keybuff,endpos,
308 if ((nod_flag=mi_test_if_nod(leaf_buff)))
310 next_page= _mi_kpos(nod_flag,endpos);
311 if (!(next_buff= (
unsigned char*) malloc(keyinfo->block_length+
314 if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,next_buff,0))
318 if ((ret_value=del(info,keyinfo,key,anc_buff,next_page,next_buff,
319 keypos,next_block,ret_key)) >0)
321 endpos=leaf_buff+mi_getint(leaf_buff);
324 ret_value=underflow(info,keyinfo,leaf_buff,next_page,
326 if (ret_value == 0 && mi_getint(leaf_buff) > keyinfo->block_length)
328 ret_value=_mi_split_page(info,keyinfo,key,leaf_buff,ret_key,0) | 2;
333 if (!_mi_get_last_key(info,keyinfo,leaf_buff,keybuff,endpos,
336 ret_value=_mi_insert(info,keyinfo,key,leaf_buff,endpos,keybuff,
337 (
unsigned char*) 0,(
unsigned char*) 0,(internal::my_off_t) 0,0);
340 if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
349 mi_putint(leaf_buff,key_start-leaf_buff,nod_flag);
350 if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
355 a_length=mi_getint(anc_buff);
356 endpos=anc_buff+a_length;
357 if (keypos != anc_buff+2+share->base.key_reflength &&
358 !_mi_get_last_key(info,keyinfo,anc_buff,ret_key,keypos,&tmp))
360 prev_key=(keypos == anc_buff+2+share->base.key_reflength ?
362 length=(*keyinfo->pack_key)(keyinfo,share->base.key_reflength,
363 keypos == endpos ? (
unsigned char*) 0 : keypos,
367 internal::bmove_upp((
unsigned char*) endpos+length,(
unsigned char*) endpos,(uint) (endpos-keypos));
369 memmove(keypos,keypos-length, (
int) (endpos-keypos)+length);
370 (*keyinfo->store_key)(keyinfo,keypos,&s_temp);
372 if (!(*keyinfo->get_key)(keyinfo,share->base.key_reflength,&keypos,ret_key))
374 _mi_kpointer(info,keypos - share->base.key_reflength,next_block);
375 mi_putint(anc_buff,a_length+length,share->base.key_reflength);
377 return( mi_getint(leaf_buff) <=
378 (info->quick_mode ? MI_MIN_KEYBLOCK_LENGTH :
379 (uint) keyinfo->underflow_block_length));
388 unsigned char *anc_buff,
389 internal::my_off_t leaf_page,
390 unsigned char *leaf_buff,
391 unsigned char *keypos)
394 uint32_t length,anc_length,buff_length,leaf_length,p_length,s_length,nod_flag,
395 key_reflength,key_length;
396 internal::my_off_t next_page;
397 unsigned char anc_key[MI_MAX_KEY_BUFF],leaf_key[MI_MAX_KEY_BUFF],
398 *buff,*endpos,*next_keypos,*anc_pos,*half_pos,*temp_pos,*prev_key,
406 nod_flag=mi_test_if_nod(leaf_buff);
408 anc_length=mi_getint(anc_buff);
409 leaf_length=mi_getint(leaf_buff);
410 key_reflength=share->base.key_reflength;
411 if (info->s->keyinfo+info->lastinx == keyinfo)
412 info->page_changed=1;
414 if ((keypos < anc_buff+anc_length && (info->state->records & 1)) ||
415 keypos == anc_buff+2+key_reflength)
417 if (keyinfo->flag & HA_BINARY_PACK_KEY)
419 if (!(next_keypos=_mi_get_key(info, keyinfo,
420 anc_buff, buff, keypos, &length)))
427 if (!(*keyinfo->get_key)(keyinfo,key_reflength,&next_keypos,
431 next_page= _mi_kpos(key_reflength,next_keypos);
432 if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,buff,0))
434 buff_length=mi_getint(buff);
437 memmove(next_keypos - key_reflength, buff + 2, key_reflength);
438 if (!_mi_get_last_key(info,keyinfo,anc_buff,anc_key,next_keypos,&length)
439 || !_mi_get_last_key(info,keyinfo,leaf_buff,leaf_key,
440 leaf_buff+leaf_length,&length))
444 prev_key=(leaf_length == p_length ? (
unsigned char*) 0 : leaf_key);
445 t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,buff+p_length,
448 length=buff_length-p_length;
449 endpos=buff+length+leaf_length+t_length;
451 internal::bmove_upp((
unsigned char*) endpos, (
unsigned char*) buff+buff_length,length);
452 memcpy(buff, leaf_buff, leaf_length);
453 (*keyinfo->store_key)(keyinfo,buff+leaf_length,&s_temp);
454 buff_length=(uint) (endpos-buff);
455 mi_putint(buff,buff_length,nod_flag);
459 if (!(s_length=remove_key(keyinfo,key_reflength,keypos,anc_key,
460 anc_buff+anc_length,(internal::my_off_t *) 0)))
463 anc_length-=s_length;
464 mi_putint(anc_buff,anc_length,key_reflength);
466 if (buff_length <= keyinfo->block_length)
468 memcpy(leaf_buff, buff, buff_length);
469 if (_mi_dispose(info,keyinfo,next_page,DFLT_INIT_HITS))
474 endpos=anc_buff+anc_length;
475 if (keypos != anc_buff+2+key_reflength &&
476 !_mi_get_last_key(info,keyinfo,anc_buff,anc_key,keypos,&length))
478 if (!(half_pos=_mi_find_half_pos(nod_flag, keyinfo, buff, leaf_key,
479 &key_length, &after_key)))
481 length=(uint) (half_pos-buff);
482 memcpy(leaf_buff, buff, length);
483 mi_putint(leaf_buff,length,nod_flag);
487 _mi_kpointer(info,leaf_key+key_length,next_page);
489 prev_key=(keypos == anc_buff+2+key_reflength ? (
unsigned char*) 0 : anc_key),
490 t_length=(*keyinfo->pack_key)(keyinfo,key_reflength,
491 (keypos == endpos ? (
unsigned char*) 0 :
496 internal::bmove_upp((
unsigned char*) endpos+t_length,(
unsigned char*) endpos,
497 (uint) (endpos-keypos));
499 memmove(keypos,keypos-t_length,(uint) (endpos-keypos)+t_length);
500 (*keyinfo->store_key)(keyinfo,keypos,&s_temp);
501 mi_putint(anc_buff,(anc_length+=t_length),key_reflength);
505 memmove(buff + 2, half_pos - nod_flag, nod_flag);
506 if (!(*keyinfo->get_key)(keyinfo,nod_flag,&half_pos,leaf_key))
508 t_length=(int) (*keyinfo->pack_key)(keyinfo, nod_flag, (
unsigned char*) 0,
509 (
unsigned char*) 0, (
unsigned char *) 0,
512 length=(uint) ((buff+mi_getint(buff))-half_pos);
513 memmove(buff + p_length + t_length, half_pos, length);
514 (*keyinfo->store_key)(keyinfo,buff+p_length,&s_temp);
515 mi_putint(buff,length+t_length+p_length,nod_flag);
517 if (_mi_write_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,buff))
520 if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
522 return(anc_length <= ((info->quick_mode ? MI_MIN_BLOCK_LENGTH :
523 (uint) keyinfo->underflow_block_length)));
526 keypos=_mi_get_last_key(info,keyinfo,anc_buff,anc_key,keypos,&length);
529 next_page= _mi_kpos(key_reflength,keypos);
530 if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,buff,0))
532 buff_length=mi_getint(buff);
533 endpos=buff+buff_length;
536 memmove(next_keypos - key_reflength, leaf_buff+2, key_reflength);
538 if (!(*keyinfo->get_key)(keyinfo,key_reflength,&next_keypos,
541 if (!_mi_get_last_key(info,keyinfo,buff,leaf_key,endpos,&length))
545 prev_key=(leaf_length == p_length ? (
unsigned char*) 0 : leaf_key);
546 t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,
547 (leaf_length == p_length ?
548 (
unsigned char*) 0 : leaf_buff+p_length),
552 memmove(endpos+t_length,leaf_buff+p_length, leaf_length-p_length);
554 memmove(endpos, leaf_buff+((
int) p_length-t_length),
555 leaf_length - p_length + t_length);
557 (*keyinfo->store_key)(keyinfo,endpos,&s_temp);
558 buff_length=buff_length+leaf_length-p_length+t_length;
559 mi_putint(buff,buff_length,nod_flag);
562 if (!(s_length= remove_key(keyinfo,key_reflength,keypos,anc_key,
563 anc_buff+anc_length,(internal::my_off_t *) 0)))
566 anc_length-=s_length;
567 mi_putint(anc_buff,anc_length,key_reflength);
569 if (buff_length <= keyinfo->block_length)
571 if (_mi_dispose(info,keyinfo,leaf_page,DFLT_INIT_HITS))
576 if (keypos == anc_buff+2+key_reflength)
578 else if (!_mi_get_last_key(info,keyinfo,anc_buff,anc_pos=anc_key,keypos,
581 endpos=_mi_find_half_pos(nod_flag,keyinfo,buff,leaf_key,
582 &key_length, &half_pos);
585 _mi_kpointer(info,leaf_key+key_length,leaf_page);
588 temp_pos=anc_buff+anc_length;
589 t_length=(*keyinfo->pack_key)(keyinfo,key_reflength,
590 keypos == temp_pos ? (
unsigned char*) 0
595 internal::bmove_upp((
unsigned char*) temp_pos+t_length,(
unsigned char*) temp_pos,
596 (uint) (temp_pos-keypos));
598 memmove(keypos,keypos-t_length,(uint) (temp_pos-keypos)+t_length);
599 (*keyinfo->store_key)(keyinfo,keypos,&s_temp);
600 mi_putint(anc_buff,(anc_length+=t_length),key_reflength);
604 memmove(leaf_buff+2, half_pos - nod_flag, nod_flag);
605 if (!(length=(*keyinfo->get_key)(keyinfo,nod_flag,&half_pos,leaf_key)))
607 t_length=(*keyinfo->pack_key)(keyinfo,nod_flag, (
unsigned char*) 0,
608 (
unsigned char*) 0, (
unsigned char*) 0, leaf_key, &s_temp);
609 length=(uint) ((buff+buff_length)-half_pos);
610 memmove(leaf_buff + p_length + t_length, half_pos, length);
611 (*keyinfo->store_key)(keyinfo,leaf_buff+p_length,&s_temp);
612 mi_putint(leaf_buff,length+t_length+p_length,nod_flag);
613 if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
615 mi_putint(buff,endpos-buff,nod_flag);
617 if (_mi_write_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,buff))
619 return(anc_length <= (uint) keyinfo->block_length/2);
633 static uint32_t remove_key(
MI_KEYDEF *keyinfo, uint32_t nod_flag,
634 unsigned char *keypos,
635 unsigned char *lastkey,
636 unsigned char *page_end,
637 internal::my_off_t *next_block)
640 unsigned char *start;
643 if (!(keyinfo->flag &
644 (HA_PACK_KEY | HA_SPACE_PACK_USED | HA_VAR_LENGTH_KEY |
645 HA_BINARY_PACK_KEY)))
647 s_length=(int) (keyinfo->keylength+nod_flag);
648 if (next_block && nod_flag)
649 *next_block= _mi_kpos(nod_flag,keypos+s_length);
654 if (!(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,lastkey))
657 if (next_block && nod_flag)
658 *next_block= _mi_kpos(nod_flag,keypos);
659 s_length=(int) (keypos-start);
660 if (keypos != page_end)
662 if (keyinfo->flag & HA_BINARY_PACK_KEY)
664 unsigned char *old_key=start;
665 uint32_t next_length,prev_length,prev_pack_length;
666 get_key_length(next_length,keypos);
667 get_key_pack_length(prev_length,prev_pack_length,old_key);
668 if (next_length > prev_length)
671 internal::bmove_upp(keypos, (lastkey+next_length),
672 (next_length-prev_length));
673 keypos-=(next_length-prev_length)+prev_pack_length;
674 store_key_length(keypos,prev_length);
675 s_length=(int) (keypos-start);
681 if ((keyinfo->seg->flag & HA_PACK_KEY) && *keypos & 128)
684 uint32_t next_length,prev_length,prev_pack_length,lastkey_length,
686 if (keyinfo->seg[0].length >= 127)
688 if (!(prev_length=mi_uint2korr(start) & 32767))
690 next_length=mi_uint2korr(keypos) & 32767;
696 if (!(prev_length= *start & 127))
698 next_length= *keypos & 127;
704 if (keyinfo->seg[0].flag & HA_NULL_PART)
706 get_key_length(lastkey_length,lastkey);
709 next_length=lastkey_length;
713 get_key_length(rest_length,keypos);
715 if (next_length >= prev_length)
717 uint32_t pack_length,tmp;
718 internal::bmove_upp(keypos, (lastkey+next_length),
719 tmp=(next_length-prev_length));
721 pack_length= prev_length ? get_pack_length(rest_length): 0;
722 keypos-=tmp+pack_length+prev_pack_length;
723 s_length=(int) (keypos-start);
727 if (prev_pack_length == 2)
729 store_key_length(keypos,rest_length);
734 if (keyinfo->seg[0].flag & HA_NULL_PART)
738 if (prev_pack_length == 2)
740 mi_int2store(keypos,rest_length);
743 *keypos= rest_length;
751 assert(page_end-start >= s_length);
752 memmove(start, start + s_length, page_end-start-s_length);