1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* Remove a row from a MyISAM table */
21
static int d_search(MI_INFO *info,MI_KEYDEF *keyinfo,uint comp_flag,
22
uchar *key,uint key_length,my_off_t page,uchar *anc_buff);
23
static int del(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,uchar *anc_buff,
24
my_off_t leaf_page,uchar *leaf_buff,uchar *keypos,
25
my_off_t next_block,uchar *ret_key);
26
static int underflow(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *anc_buff,
27
my_off_t leaf_page,uchar *leaf_buff,uchar *keypos);
28
static uint remove_key(MI_KEYDEF *keyinfo,uint nod_flag,uchar *keypos,
29
uchar *lastkey,uchar *page_end,
30
my_off_t *next_block);
31
static int _mi_ck_real_delete(register MI_INFO *info,MI_KEYDEF *keyinfo,
32
uchar *key, uint key_length, my_off_t *root);
35
int mi_delete(MI_INFO *info,const uchar *record)
42
MYISAM_SHARE *share=info->s;
43
DBUG_ENTER("mi_delete");
45
/* Test if record is in datafile */
47
DBUG_EXECUTE_IF("myisam_pretend_crashed_table_on_usage",
48
mi_print_error(info->s, HA_ERR_CRASHED);
49
DBUG_RETURN(my_errno= HA_ERR_CRASHED););
50
DBUG_EXECUTE_IF("my_error_test_undefined_error",
51
mi_print_error(info->s, INT_MAX);
52
DBUG_RETURN(my_errno= INT_MAX););
53
if (!(info->update & HA_STATE_AKTIV))
55
DBUG_RETURN(my_errno=HA_ERR_KEY_NOT_FOUND); /* No database read */
57
if (share->options & HA_OPTION_READ_ONLY_DATA)
59
DBUG_RETURN(my_errno=EACCES);
61
if (_mi_readinfo(info,F_WRLCK,1))
62
DBUG_RETURN(my_errno);
63
if (info->s->calc_checksum)
64
info->checksum=(*info->s->calc_checksum)(info,record);
65
if ((*share->compare_record)(info,record))
66
goto err; /* Error on read-check */
68
if (_mi_mark_file_changed(info))
71
/* Remove all keys from the .ISAM file */
73
old_key=info->lastkey2;
74
for (i=0 ; i < share->base.keys ; i++ )
76
if (mi_is_key_active(info->s->state.key_map, i))
78
info->s->keyinfo[i].version++;
79
if (info->s->keyinfo[i].flag & HA_FULLTEXT )
81
if (_mi_ft_del(info,i, old_key,record,info->lastpos))
86
if (info->s->keyinfo[i].ck_delete(info,i,old_key,
87
_mi_make_key(info,i,old_key,record,info->lastpos)))
90
/* The above changed info->lastkey2. Inform mi_rnext_same(). */
91
info->update&= ~HA_STATE_RNEXT_SAME;
95
if ((*share->delete_record)(info))
96
goto err; /* Remove record from database */
97
info->state->checksum-=info->checksum;
99
info->update= HA_STATE_CHANGED+HA_STATE_DELETED+HA_STATE_ROW_CHANGED;
100
info->state->records--;
102
mi_sizestore(lastpos,info->lastpos);
103
myisam_log_command(MI_LOG_DELETE,info,(uchar*) lastpos,sizeof(lastpos),0);
104
VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
105
allow_break(); /* Allow SIGHUP & SIGINT */
106
if (info->invalidator != 0)
108
DBUG_PRINT("info", ("invalidator... '%s' (delete)", info->filename));
109
(*info->invalidator)(info->filename);
116
mi_sizestore(lastpos,info->lastpos);
117
myisam_log_command(MI_LOG_DELETE,info,(uchar*) lastpos, sizeof(lastpos),0);
118
if (save_errno != HA_ERR_RECORD_CHANGED)
120
mi_print_error(info->s, HA_ERR_CRASHED);
121
mi_mark_crashed(info); /* mark table crashed */
123
VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
124
info->update|=HA_STATE_WRITTEN; /* Buffer changed */
125
allow_break(); /* Allow SIGHUP & SIGINT */
127
if (save_errno == HA_ERR_KEY_NOT_FOUND)
129
mi_print_error(info->s, HA_ERR_CRASHED);
130
my_errno=HA_ERR_CRASHED;
133
DBUG_RETURN(my_errno);
137
/* Remove a key from the btree index */
139
int _mi_ck_delete(register MI_INFO *info, uint keynr, uchar *key,
142
return _mi_ck_real_delete(info, info->s->keyinfo+keynr, key, key_length,
143
&info->s->state.key_root[keynr]);
144
} /* _mi_ck_delete */
147
static int _mi_ck_real_delete(register MI_INFO *info, MI_KEYDEF *keyinfo,
148
uchar *key, uint key_length, my_off_t *root)
154
DBUG_ENTER("_mi_ck_real_delete");
156
if ((old_root=*root) == HA_OFFSET_ERROR)
158
mi_print_error(info->s, HA_ERR_CRASHED);
159
DBUG_RETURN(my_errno=HA_ERR_CRASHED);
161
if (!(root_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
164
DBUG_PRINT("error",("Couldn't allocate memory"));
165
DBUG_RETURN(my_errno=ENOMEM);
167
DBUG_PRINT("info",("root_page: %ld", (long) old_root));
168
if (!_mi_fetch_keypage(info,keyinfo,old_root,DFLT_INIT_HITS,root_buff,0))
173
if ((error=d_search(info,keyinfo,
174
(keyinfo->flag & HA_FULLTEXT ? SEARCH_FIND | SEARCH_UPDATE
176
key,key_length,old_root,root_buff)) >0)
180
DBUG_PRINT("test",("Enlarging of root when deleting"));
181
error=_mi_enlarge_root(info,keyinfo,key,root);
183
else /* error == 1 */
185
if (mi_getint(root_buff) <= (nod_flag=mi_test_if_nod(root_buff))+3)
189
*root=_mi_kpos(nod_flag,root_buff+2+nod_flag);
191
*root=HA_OFFSET_ERROR;
192
if (_mi_dispose(info,keyinfo,old_root,DFLT_INIT_HITS))
196
error=_mi_write_keypage(info,keyinfo,old_root,
197
DFLT_INIT_HITS,root_buff);
201
my_afree((uchar*) root_buff);
202
DBUG_PRINT("exit",("Return: %d",error));
204
} /* _mi_ck_real_delete */
208
** Remove key below key root
210
** 1 if there are less buffers; In this case anc_buff is not saved
211
** 2 if there are more buffers
215
static int d_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
216
uint comp_flag, uchar *key, uint key_length,
217
my_off_t page, uchar *anc_buff)
219
int flag,ret_value,save_flag;
220
uint length,nod_flag,search_key_length;
222
uchar *leaf_buff,*keypos;
223
my_off_t leaf_page= 0, next_block;
224
uchar lastkey[MI_MAX_KEY_BUFF];
225
DBUG_ENTER("d_search");
226
DBUG_DUMP("page",(uchar*) anc_buff,mi_getint(anc_buff));
228
search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
229
flag=(*keyinfo->bin_search)(info,keyinfo,anc_buff,key, search_key_length,
230
comp_flag, &keypos, lastkey, &last_key);
231
if (flag == MI_FOUND_WRONG_KEY)
233
DBUG_PRINT("error",("Found wrong key"));
236
nod_flag=mi_test_if_nod(anc_buff);
238
if (!flag && keyinfo->flag & HA_FULLTEXT)
243
get_key_full_length_rdonly(off, lastkey);
244
subkeys=ft_sintXkorr(lastkey+off);
245
DBUG_ASSERT(info->ft1_to_ft2==0 || subkeys >=0);
246
comp_flag=SEARCH_SAME;
249
/* normal word, one-level tree structure */
250
if (info->ft1_to_ft2)
252
/* we're in ft1->ft2 conversion mode. Saving key data */
253
insert_dynamic(info->ft1_to_ft2, (lastkey+off));
257
/* we need exact match only if not in ft1->ft2 conversion mode */
258
flag=(*keyinfo->bin_search)(info,keyinfo,anc_buff,key,USE_WHOLE_KEY,
259
comp_flag, &keypos, lastkey, &last_key);
261
/* fall through to normal delete */
265
/* popular word. two-level tree. going down */
270
if (!(tmp_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&kpos,lastkey)))
272
mi_print_error(info->s, HA_ERR_CRASHED);
273
my_errno= HA_ERR_CRASHED;
276
root=_mi_dpos(info,nod_flag,kpos);
279
/* the last entry in sub-tree */
280
if (_mi_dispose(info, keyinfo, root,DFLT_INIT_HITS))
282
/* fall through to normal delete */
286
keyinfo=&info->s->ft2_keyinfo;
287
kpos-=keyinfo->keylength+nod_flag; /* we'll modify key entry 'in vivo' */
288
get_key_full_length_rdonly(off, key);
290
ret_value=_mi_ck_real_delete(info, &info->s->ft2_keyinfo,
291
key, HA_FT_WLEN, &root);
292
_mi_dpointer(info, kpos+HA_FT_WLEN, root);
294
ft_intXstore(kpos, subkeys);
296
ret_value=_mi_write_keypage(info,keyinfo,page,
297
DFLT_INIT_HITS,anc_buff);
298
DBUG_PRINT("exit",("Return: %d",ret_value));
299
DBUG_RETURN(ret_value);
306
leaf_page=_mi_kpos(nod_flag,keypos);
307
if (!(leaf_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
310
DBUG_PRINT("error",("Couldn't allocate memory"));
312
DBUG_PRINT("exit",("Return: %d",-1));
315
if (!_mi_fetch_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff,0))
323
DBUG_PRINT("error",("Didn't find key"));
324
mi_print_error(info->s, HA_ERR_CRASHED);
325
my_errno=HA_ERR_CRASHED; /* This should newer happend */
329
ret_value=d_search(info,keyinfo,comp_flag,key,key_length,
330
leaf_page,leaf_buff);
335
length=mi_getint(anc_buff);
336
if (!(tmp= remove_key(keyinfo,nod_flag,keypos,lastkey,anc_buff+length,
342
mi_putint(anc_buff,length,nod_flag);
345
if (_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,anc_buff))
347
DBUG_PRINT("exit",("Return: %d",-1));
350
/* Page will be update later if we return 1 */
351
DBUG_RETURN(test(length <= (info->quick_mode ? MI_MIN_KEYBLOCK_LENGTH :
352
(uint) keyinfo->underflow_block_length)));
355
ret_value=del(info,keyinfo,key,anc_buff,leaf_page,leaf_buff,keypos,
362
ret_value= underflow(info,keyinfo,anc_buff,leaf_page,leaf_buff,keypos);
364
{ /* This happens only with packed keys */
365
DBUG_PRINT("test",("Enlarging of key when deleting"));
366
if (!_mi_get_last_key(info,keyinfo,anc_buff,lastkey,keypos,&length))
370
ret_value=_mi_insert(info,keyinfo,key,anc_buff,keypos,lastkey,
371
(uchar*) 0,(uchar*) 0,(my_off_t) 0,(my_bool) 0);
374
if (ret_value == 0 && mi_getint(anc_buff) > keyinfo->block_length)
377
ret_value=_mi_split_page(info,keyinfo,key,anc_buff,lastkey,0) | 2;
379
if (save_flag && ret_value != 1)
380
ret_value|=_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,anc_buff);
383
DBUG_DUMP("page",(uchar*) anc_buff,mi_getint(anc_buff));
385
my_afree((uchar*) leaf_buff);
386
DBUG_PRINT("exit",("Return: %d",ret_value));
387
DBUG_RETURN(ret_value);
390
my_afree((uchar*) leaf_buff);
391
DBUG_PRINT("exit",("Error: %d",my_errno));
396
/* Remove a key that has a page-reference */
398
static int del(register MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *key,
399
uchar *anc_buff, my_off_t leaf_page, uchar *leaf_buff,
400
uchar *keypos, /* Pos to where deleted key was */
402
uchar *ret_key) /* key before keypos in anc_buff */
404
int ret_value,length;
405
uint a_length,nod_flag,tmp;
407
uchar keybuff[MI_MAX_KEY_BUFF],*endpos,*next_buff,*key_start, *prev_key;
408
MYISAM_SHARE *share=info->s;
411
DBUG_PRINT("enter",("leaf_page: %ld keypos: 0x%lx", (long) leaf_page,
413
DBUG_DUMP("leaf_buff",(uchar*) leaf_buff,mi_getint(leaf_buff));
415
endpos=leaf_buff+mi_getint(leaf_buff);
416
if (!(key_start=_mi_get_last_key(info,keyinfo,leaf_buff,keybuff,endpos,
420
if ((nod_flag=mi_test_if_nod(leaf_buff)))
422
next_page= _mi_kpos(nod_flag,endpos);
423
if (!(next_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
426
if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,next_buff,0))
430
DBUG_DUMP("next_page",(uchar*) next_buff,mi_getint(next_buff));
431
if ((ret_value=del(info,keyinfo,key,anc_buff,next_page,next_buff,
432
keypos,next_block,ret_key)) >0)
434
endpos=leaf_buff+mi_getint(leaf_buff);
437
ret_value=underflow(info,keyinfo,leaf_buff,next_page,
439
if (ret_value == 0 && mi_getint(leaf_buff) > keyinfo->block_length)
441
ret_value=_mi_split_page(info,keyinfo,key,leaf_buff,ret_key,0) | 2;
446
DBUG_PRINT("test",("Inserting of key when deleting"));
447
if (!_mi_get_last_key(info,keyinfo,leaf_buff,keybuff,endpos,
450
ret_value=_mi_insert(info,keyinfo,key,leaf_buff,endpos,keybuff,
451
(uchar*) 0,(uchar*) 0,(my_off_t) 0,0);
454
if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
457
my_afree((uchar*) next_buff);
458
DBUG_RETURN(ret_value);
461
/* Remove last key from leaf page */
463
mi_putint(leaf_buff,key_start-leaf_buff,nod_flag);
464
if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
467
/* Place last key in ancestor page on deleted key position */
469
a_length=mi_getint(anc_buff);
470
endpos=anc_buff+a_length;
471
if (keypos != anc_buff+2+share->base.key_reflength &&
472
!_mi_get_last_key(info,keyinfo,anc_buff,ret_key,keypos,&tmp))
474
prev_key=(keypos == anc_buff+2+share->base.key_reflength ?
476
length=(*keyinfo->pack_key)(keyinfo,share->base.key_reflength,
477
keypos == endpos ? (uchar*) 0 : keypos,
481
bmove_upp((uchar*) endpos+length,(uchar*) endpos,(uint) (endpos-keypos));
483
bmove(keypos,keypos-length, (int) (endpos-keypos)+length);
484
(*keyinfo->store_key)(keyinfo,keypos,&s_temp);
485
/* Save pointer to next leaf */
486
if (!(*keyinfo->get_key)(keyinfo,share->base.key_reflength,&keypos,ret_key))
488
_mi_kpointer(info,keypos - share->base.key_reflength,next_block);
489
mi_putint(anc_buff,a_length+length,share->base.key_reflength);
491
DBUG_RETURN( mi_getint(leaf_buff) <=
492
(info->quick_mode ? MI_MIN_KEYBLOCK_LENGTH :
493
(uint) keyinfo->underflow_block_length));
499
/* Balances adjacent pages if underflow occours */
501
static int underflow(register MI_INFO *info, register MI_KEYDEF *keyinfo,
503
my_off_t leaf_page,/* Ancestor page and underflow page */
505
uchar *keypos) /* Position to pos after key */
508
uint length,anc_length,buff_length,leaf_length,p_length,s_length,nod_flag,
509
key_reflength,key_length;
511
uchar anc_key[MI_MAX_KEY_BUFF],leaf_key[MI_MAX_KEY_BUFF],
512
*buff,*endpos,*next_keypos,*anc_pos,*half_pos,*temp_pos,*prev_key,
515
MYISAM_SHARE *share=info->s;
516
DBUG_ENTER("underflow");
517
DBUG_PRINT("enter",("leaf_page: %ld keypos: 0x%lx",(long) leaf_page,
519
DBUG_DUMP("anc_buff",(uchar*) anc_buff,mi_getint(anc_buff));
520
DBUG_DUMP("leaf_buff",(uchar*) leaf_buff,mi_getint(leaf_buff));
525
nod_flag=mi_test_if_nod(leaf_buff);
527
anc_length=mi_getint(anc_buff);
528
leaf_length=mi_getint(leaf_buff);
529
key_reflength=share->base.key_reflength;
530
if (info->s->keyinfo+info->lastinx == keyinfo)
531
info->page_changed=1;
533
if ((keypos < anc_buff+anc_length && (info->state->records & 1)) ||
534
keypos == anc_buff+2+key_reflength)
535
{ /* Use page right of anc-page */
536
DBUG_PRINT("test",("use right page"));
538
if (keyinfo->flag & HA_BINARY_PACK_KEY)
540
if (!(next_keypos=_mi_get_key(info, keyinfo,
541
anc_buff, buff, keypos, &length)))
546
/* Got to end of found key */
547
buff[0]=buff[1]=0; /* Avoid length error check if packed key */
548
if (!(*keyinfo->get_key)(keyinfo,key_reflength,&next_keypos,
552
next_page= _mi_kpos(key_reflength,next_keypos);
553
if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,buff,0))
555
buff_length=mi_getint(buff);
556
DBUG_DUMP("next",(uchar*) buff,buff_length);
558
/* find keys to make a big key-page */
559
bmove((uchar*) next_keypos-key_reflength,(uchar*) buff+2,
561
if (!_mi_get_last_key(info,keyinfo,anc_buff,anc_key,next_keypos,&length)
562
|| !_mi_get_last_key(info,keyinfo,leaf_buff,leaf_key,
563
leaf_buff+leaf_length,&length))
566
/* merge pages and put parting key from anc_buff between */
567
prev_key=(leaf_length == p_length ? (uchar*) 0 : leaf_key);
568
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,buff+p_length,
571
length=buff_length-p_length;
572
endpos=buff+length+leaf_length+t_length;
573
/* buff will always be larger than before !*/
574
bmove_upp((uchar*) endpos, (uchar*) buff+buff_length,length);
575
memcpy((uchar*) buff, (uchar*) leaf_buff,(size_t) leaf_length);
576
(*keyinfo->store_key)(keyinfo,buff+leaf_length,&s_temp);
577
buff_length=(uint) (endpos-buff);
578
mi_putint(buff,buff_length,nod_flag);
580
/* remove key from anc_buff */
582
if (!(s_length=remove_key(keyinfo,key_reflength,keypos,anc_key,
583
anc_buff+anc_length,(my_off_t *) 0)))
586
anc_length-=s_length;
587
mi_putint(anc_buff,anc_length,key_reflength);
589
if (buff_length <= keyinfo->block_length)
590
{ /* Keys in one page */
591
memcpy((uchar*) leaf_buff,(uchar*) buff,(size_t) buff_length);
592
if (_mi_dispose(info,keyinfo,next_page,DFLT_INIT_HITS))
597
endpos=anc_buff+anc_length;
598
DBUG_PRINT("test",("anc_buff: 0x%lx endpos: 0x%lx",
599
(long) anc_buff, (long) endpos));
600
if (keypos != anc_buff+2+key_reflength &&
601
!_mi_get_last_key(info,keyinfo,anc_buff,anc_key,keypos,&length))
603
if (!(half_pos=_mi_find_half_pos(nod_flag, keyinfo, buff, leaf_key,
604
&key_length, &after_key)))
606
length=(uint) (half_pos-buff);
607
memcpy((uchar*) leaf_buff,(uchar*) buff,(size_t) length);
608
mi_putint(leaf_buff,length,nod_flag);
610
/* Correct new keypointer to leaf_page */
612
_mi_kpointer(info,leaf_key+key_length,next_page);
613
/* Save key in anc_buff */
614
prev_key=(keypos == anc_buff+2+key_reflength ? (uchar*) 0 : anc_key),
615
t_length=(*keyinfo->pack_key)(keyinfo,key_reflength,
616
(keypos == endpos ? (uchar*) 0 :
621
bmove_upp((uchar*) endpos+t_length,(uchar*) endpos,
622
(uint) (endpos-keypos));
624
bmove(keypos,keypos-t_length,(uint) (endpos-keypos)+t_length);
625
(*keyinfo->store_key)(keyinfo,keypos,&s_temp);
626
mi_putint(anc_buff,(anc_length+=t_length),key_reflength);
628
/* Store key first in new page */
630
bmove((uchar*) buff+2,(uchar*) half_pos-nod_flag,(size_t) nod_flag);
631
if (!(*keyinfo->get_key)(keyinfo,nod_flag,&half_pos,leaf_key))
633
t_length=(int) (*keyinfo->pack_key)(keyinfo, nod_flag, (uchar*) 0,
634
(uchar*) 0, (uchar *) 0,
636
/* t_length will always be > 0 for a new page !*/
637
length=(uint) ((buff+mi_getint(buff))-half_pos);
638
bmove((uchar*) buff+p_length+t_length,(uchar*) half_pos,(size_t) length);
639
(*keyinfo->store_key)(keyinfo,buff+p_length,&s_temp);
640
mi_putint(buff,length+t_length+p_length,nod_flag);
642
if (_mi_write_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,buff))
645
if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
647
DBUG_RETURN(anc_length <= ((info->quick_mode ? MI_MIN_BLOCK_LENGTH :
648
(uint) keyinfo->underflow_block_length)));
651
DBUG_PRINT("test",("use left page"));
653
keypos=_mi_get_last_key(info,keyinfo,anc_buff,anc_key,keypos,&length);
656
next_page= _mi_kpos(key_reflength,keypos);
657
if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,buff,0))
659
buff_length=mi_getint(buff);
660
endpos=buff+buff_length;
661
DBUG_DUMP("prev",(uchar*) buff,buff_length);
663
/* find keys to make a big key-page */
664
bmove((uchar*) next_keypos - key_reflength,(uchar*) leaf_buff+2,
667
if (!(*keyinfo->get_key)(keyinfo,key_reflength,&next_keypos,
670
if (!_mi_get_last_key(info,keyinfo,buff,leaf_key,endpos,&length))
673
/* merge pages and put parting key from anc_buff between */
674
prev_key=(leaf_length == p_length ? (uchar*) 0 : leaf_key);
675
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,
676
(leaf_length == p_length ?
677
(uchar*) 0 : leaf_buff+p_length),
681
bmove((uchar*) endpos+t_length,(uchar*) leaf_buff+p_length,
682
(size_t) (leaf_length-p_length));
683
else /* We gained space */
684
bmove((uchar*) endpos,(uchar*) leaf_buff+((int) p_length-t_length),
685
(size_t) (leaf_length-p_length+t_length));
687
(*keyinfo->store_key)(keyinfo,endpos,&s_temp);
688
buff_length=buff_length+leaf_length-p_length+t_length;
689
mi_putint(buff,buff_length,nod_flag);
691
/* remove key from anc_buff */
692
if (!(s_length= remove_key(keyinfo,key_reflength,keypos,anc_key,
693
anc_buff+anc_length,(my_off_t *) 0)))
696
anc_length-=s_length;
697
mi_putint(anc_buff,anc_length,key_reflength);
699
if (buff_length <= keyinfo->block_length)
700
{ /* Keys in one page */
701
if (_mi_dispose(info,keyinfo,leaf_page,DFLT_INIT_HITS))
706
if (keypos == anc_buff+2+key_reflength)
707
anc_pos=0; /* First key */
708
else if (!_mi_get_last_key(info,keyinfo,anc_buff,anc_pos=anc_key,keypos,
711
endpos=_mi_find_half_pos(nod_flag,keyinfo,buff,leaf_key,
712
&key_length, &half_pos);
715
_mi_kpointer(info,leaf_key+key_length,leaf_page);
716
/* Save key in anc_buff */
717
DBUG_DUMP("anc_buff",(uchar*) anc_buff,anc_length);
718
DBUG_DUMP("key_to_anc",(uchar*) leaf_key,key_length);
720
temp_pos=anc_buff+anc_length;
721
t_length=(*keyinfo->pack_key)(keyinfo,key_reflength,
722
keypos == temp_pos ? (uchar*) 0
727
bmove_upp((uchar*) temp_pos+t_length,(uchar*) temp_pos,
728
(uint) (temp_pos-keypos));
730
bmove(keypos,keypos-t_length,(uint) (temp_pos-keypos)+t_length);
731
(*keyinfo->store_key)(keyinfo,keypos,&s_temp);
732
mi_putint(anc_buff,(anc_length+=t_length),key_reflength);
734
/* Store first key on new page */
736
bmove((uchar*) leaf_buff+2,(uchar*) half_pos-nod_flag,(size_t) nod_flag);
737
if (!(length=(*keyinfo->get_key)(keyinfo,nod_flag,&half_pos,leaf_key)))
739
DBUG_DUMP("key_to_leaf",(uchar*) leaf_key,length);
740
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag, (uchar*) 0,
741
(uchar*) 0, (uchar*) 0, leaf_key, &s_temp);
742
length=(uint) ((buff+buff_length)-half_pos);
743
DBUG_PRINT("info",("t_length: %d length: %d",t_length,(int) length));
744
bmove((uchar*) leaf_buff+p_length+t_length,(uchar*) half_pos,
746
(*keyinfo->store_key)(keyinfo,leaf_buff+p_length,&s_temp);
747
mi_putint(leaf_buff,length+t_length+p_length,nod_flag);
748
if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
750
mi_putint(buff,endpos-buff,nod_flag);
752
if (_mi_write_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,buff))
754
DBUG_RETURN(anc_length <= (uint) keyinfo->block_length/2);
762
remove a key from packed buffert
763
The current code doesn't handle the case that the next key may be
764
packed better against the previous key if there is a case difference
765
returns how many chars was removed or 0 on error
768
static uint remove_key(MI_KEYDEF *keyinfo, uint nod_flag,
769
uchar *keypos, /* Where key starts */
770
uchar *lastkey, /* key to be removed */
771
uchar *page_end, /* End of page */
772
my_off_t *next_block) /* ptr to next block */
776
DBUG_ENTER("remove_key");
777
DBUG_PRINT("enter",("keypos: 0x%lx page_end: 0x%lx",(long) keypos, (long) page_end));
780
if (!(keyinfo->flag &
781
(HA_PACK_KEY | HA_SPACE_PACK_USED | HA_VAR_LENGTH_KEY |
782
HA_BINARY_PACK_KEY)))
784
s_length=(int) (keyinfo->keylength+nod_flag);
785
if (next_block && nod_flag)
786
*next_block= _mi_kpos(nod_flag,keypos+s_length);
789
{ /* Let keypos point at next key */
790
/* Calculate length of key */
791
if (!(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,lastkey))
792
DBUG_RETURN(0); /* Error */
794
if (next_block && nod_flag)
795
*next_block= _mi_kpos(nod_flag,keypos);
796
s_length=(int) (keypos-start);
797
if (keypos != page_end)
799
if (keyinfo->flag & HA_BINARY_PACK_KEY)
801
uchar *old_key=start;
802
uint next_length,prev_length,prev_pack_length;
803
get_key_length(next_length,keypos);
804
get_key_pack_length(prev_length,prev_pack_length,old_key);
805
if (next_length > prev_length)
807
/* We have to copy data from the current key to the next key */
808
bmove_upp(keypos, (lastkey+next_length),
809
(next_length-prev_length));
810
keypos-=(next_length-prev_length)+prev_pack_length;
811
store_key_length(keypos,prev_length);
812
s_length=(int) (keypos-start);
817
/* Check if a variable length first key part */
818
if ((keyinfo->seg->flag & HA_PACK_KEY) && *keypos & 128)
820
/* Next key is packed against the current one */
821
uint next_length,prev_length,prev_pack_length,lastkey_length,
823
if (keyinfo->seg[0].length >= 127)
825
if (!(prev_length=mi_uint2korr(start) & 32767))
827
next_length=mi_uint2korr(keypos) & 32767;
833
if (!(prev_length= *start & 127))
834
goto end; /* Same key as previous*/
835
next_length= *keypos & 127;
840
prev_length=0; /* prev key not packed */
841
if (keyinfo->seg[0].flag & HA_NULL_PART)
842
lastkey++; /* Skip null marker */
843
get_key_length(lastkey_length,lastkey);
844
if (!next_length) /* Same key after */
846
next_length=lastkey_length;
850
get_key_length(rest_length,keypos);
852
if (next_length >= prev_length)
853
{ /* Key after is based on deleted key */
854
uint pack_length,tmp;
855
bmove_upp(keypos, (lastkey+next_length),
856
tmp=(next_length-prev_length));
858
pack_length= prev_length ? get_pack_length(rest_length): 0;
859
keypos-=tmp+pack_length+prev_pack_length;
860
s_length=(int) (keypos-start);
861
if (prev_length) /* Pack against prev key */
864
if (prev_pack_length == 2)
866
store_key_length(keypos,rest_length);
870
/* Next key is not packed anymore */
871
if (keyinfo->seg[0].flag & HA_NULL_PART)
873
rest_length++; /* Mark not null */
875
if (prev_pack_length == 2)
877
mi_int2store(keypos,rest_length);
880
*keypos= rest_length;
888
bmove((uchar*) start,(uchar*) start+s_length,
889
(uint) (page_end-start-s_length));
890
DBUG_RETURN((uint) s_length);