12
12
You should have received a copy of the GNU General Public License
13
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 */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
16
/* Remove a row from a MyISAM table */
18
#include "myisam_priv.h"
19
#include "drizzled/internal/m_string.h"
20
#include <drizzled/util/test.h>
22
using namespace drizzled;
24
static int d_search(MI_INFO *info,MI_KEYDEF *keyinfo,uint32_t comp_flag,
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);
18
#include "myisamdef.h"
20
static int d_search(MI_INFO *info,MI_KEYDEF *keyinfo,uint comp_flag,
21
uchar *key,uint key_length,my_off_t page,uchar *anc_buff);
22
static int del(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,uchar *anc_buff,
23
my_off_t leaf_page,uchar *leaf_buff,uchar *keypos,
24
my_off_t next_block,uchar *ret_key);
25
static int underflow(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *anc_buff,
26
my_off_t leaf_page,uchar *leaf_buff,uchar *keypos);
27
static uint remove_key(MI_KEYDEF *keyinfo,uint nod_flag,uchar *keypos,
28
uchar *lastkey,uchar *page_end,
29
my_off_t *next_block);
34
30
static int _mi_ck_real_delete(register MI_INFO *info,MI_KEYDEF *keyinfo,
35
unsigned char *key, uint32_t key_length, internal::my_off_t *root);
38
int mi_delete(MI_INFO *info,const unsigned char *record)
31
uchar *key, uint key_length, my_off_t *root);
34
int mi_delete(MI_INFO *info,const uchar *record)
41
unsigned char *old_key;
47
43
/* Test if record is in datafile */
48
44
if (!(info->update & HA_STATE_AKTIV))
50
return(errno=HA_ERR_KEY_NOT_FOUND); /* No database read */
46
return(my_errno=HA_ERR_KEY_NOT_FOUND); /* No database read */
52
48
if (share->options & HA_OPTION_READ_ONLY_DATA)
50
return(my_errno=EACCES);
56
52
if (_mi_readinfo(info,F_WRLCK,1))
58
54
if (info->s->calc_checksum)
59
55
info->checksum=(*info->s->calc_checksum)(info,record);
60
56
if ((*share->compare_record)(info,record))
89
85
info->state->records--;
91
87
mi_sizestore(lastpos,info->lastpos);
92
_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
88
VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
89
if (info->invalidator != 0)
91
(*info->invalidator)(info->filename);
97
98
mi_sizestore(lastpos,info->lastpos);
98
99
if (save_errno != HA_ERR_RECORD_CHANGED)
100
101
mi_print_error(info->s, HA_ERR_CRASHED);
101
102
mi_mark_crashed(info); /* mark table crashed */
103
_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
104
VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
104
105
info->update|=HA_STATE_WRITTEN; /* Buffer changed */
106
107
if (save_errno == HA_ERR_KEY_NOT_FOUND)
108
109
mi_print_error(info->s, HA_ERR_CRASHED);
109
errno=HA_ERR_CRASHED;
110
my_errno=HA_ERR_CRASHED;
113
114
} /* mi_delete */
116
117
/* Remove a key from the btree index */
118
int _mi_ck_delete(register MI_INFO *info, uint32_t keynr, unsigned char *key,
119
int _mi_ck_delete(register MI_INFO *info, uint keynr, uchar *key,
121
122
return _mi_ck_real_delete(info, info->s->keyinfo+keynr, key, key_length,
122
123
&info->s->state.key_root[keynr]);
126
127
static int _mi_ck_real_delete(register MI_INFO *info, MI_KEYDEF *keyinfo,
127
unsigned char *key, uint32_t key_length, internal::my_off_t *root)
128
uchar *key, uint key_length, my_off_t *root)
131
internal::my_off_t old_root;
132
unsigned char *root_buff;
134
135
if ((old_root=*root) == HA_OFFSET_ERROR)
136
137
mi_print_error(info->s, HA_ERR_CRASHED);
137
return(errno=HA_ERR_CRASHED);
138
return(my_errno=HA_ERR_CRASHED);
139
if (!(root_buff= (unsigned char*) malloc(keyinfo->block_length+
140
if (!(root_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
140
141
MI_MAX_KEY_BUFF*2)))
142
return(errno=ENOMEM);
143
return(my_errno=ENOMEM);
144
145
if (!_mi_fetch_keypage(info,keyinfo,old_root,DFLT_INIT_HITS,root_buff,0))
186
187
static int d_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
187
uint32_t comp_flag, unsigned char *key, uint32_t key_length,
188
internal::my_off_t page, unsigned char *anc_buff)
188
uint comp_flag, uchar *key, uint key_length,
189
my_off_t page, uchar *anc_buff)
190
191
int flag,ret_value,save_flag;
191
uint32_t length,nod_flag,search_key_length;
192
uint 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];
194
uchar *leaf_buff,*keypos;
195
my_off_t leaf_page= 0, next_block;
196
uchar lastkey[MI_MAX_KEY_BUFF];
197
198
search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
198
199
flag=(*keyinfo->bin_search)(info,keyinfo,anc_buff,key, search_key_length,
268
269
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);
270
(uchar*) 0,(uchar*) 0,(my_off_t) 0,(bool) 0);
272
273
if (ret_value == 0 && mi_getint(anc_buff) > keyinfo->block_length)
277
278
if (save_flag && ret_value != 1)
278
279
ret_value|=_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,anc_buff);
280
my_afree((uchar*) leaf_buff);
280
281
return(ret_value);
284
my_afree((uchar*) leaf_buff);
288
289
/* Remove a key that has a page-reference */
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, /* Pos to where deleted key was */
293
internal::my_off_t next_block,
294
unsigned char *ret_key) /* key before keypos in anc_buff */
291
static int del(register MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *key,
292
uchar *anc_buff, my_off_t leaf_page, uchar *leaf_buff,
293
uchar *keypos, /* Pos to where deleted key was */
295
uchar *ret_key) /* key before keypos in anc_buff */
296
297
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;
298
uint a_length,nod_flag,tmp;
300
uchar keybuff[MI_MAX_KEY_BUFF],*endpos,*next_buff,*key_start, *prev_key;
300
301
MYISAM_SHARE *share=info->s;
301
302
MI_KEY_PARAM s_temp;
336
337
ret_value=_mi_insert(info,keyinfo,key,leaf_buff,endpos,keybuff,
337
(unsigned char*) 0,(unsigned char*) 0,(internal::my_off_t) 0,0);
338
(uchar*) 0,(uchar*) 0,(my_off_t) 0,0);
340
341
if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
344
my_afree((uchar*) next_buff);
344
345
return(ret_value);
360
361
prev_key=(keypos == anc_buff+2+share->base.key_reflength ?
362
363
length=(*keyinfo->pack_key)(keyinfo,share->base.key_reflength,
363
keypos == endpos ? (unsigned char*) 0 : keypos,
364
keypos == endpos ? (uchar*) 0 : keypos,
364
365
prev_key, prev_key,
365
366
keybuff,&s_temp);
367
internal::bmove_upp((unsigned char*) endpos+length,(unsigned char*) endpos,(uint) (endpos-keypos));
368
bmove_upp((uchar*) endpos+length,(uchar*) endpos,(uint) (endpos-keypos));
369
memmove(keypos,keypos-length, (int) (endpos-keypos)+length);
370
memcpy(keypos,keypos-length, (int) (endpos-keypos)+length);
370
371
(*keyinfo->store_key)(keyinfo,keypos,&s_temp);
371
372
/* Save pointer to next leaf */
372
373
if (!(*keyinfo->get_key)(keyinfo,share->base.key_reflength,&keypos,ret_key))
385
386
/* Balances adjacent pages if underflow occours */
387
388
static int underflow(register MI_INFO *info, register MI_KEYDEF *keyinfo,
388
unsigned char *anc_buff,
389
internal::my_off_t leaf_page,/* Ancestor page and underflow page */
390
unsigned char *leaf_buff,
391
unsigned char *keypos) /* Position to pos after key */
390
my_off_t leaf_page,/* Ancestor page and underflow page */
392
uchar *keypos) /* Position to pos after key */
394
uint32_t length,anc_length,buff_length,leaf_length,p_length,s_length,nod_flag,
395
uint length,anc_length,buff_length,leaf_length,p_length,s_length,nod_flag,
395
396
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
uchar anc_key[MI_MAX_KEY_BUFF],leaf_key[MI_MAX_KEY_BUFF],
398
399
*buff,*endpos,*next_keypos,*anc_pos,*half_pos,*temp_pos,*prev_key,
400
401
MI_KEY_PARAM s_temp;
434
435
buff_length=mi_getint(buff);
436
437
/* find keys to make a big key-page */
437
memmove(next_keypos - key_reflength, buff + 2, key_reflength);
438
memcpy(next_keypos - key_reflength, buff + 2, key_reflength);
438
439
if (!_mi_get_last_key(info,keyinfo,anc_buff,anc_key,next_keypos,&length)
439
440
|| !_mi_get_last_key(info,keyinfo,leaf_buff,leaf_key,
440
441
leaf_buff+leaf_length,&length))
443
444
/* merge pages and put parting key from anc_buff between */
444
prev_key=(leaf_length == p_length ? (unsigned char*) 0 : leaf_key);
445
prev_key=(leaf_length == p_length ? (uchar*) 0 : leaf_key);
445
446
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,buff+p_length,
446
447
prev_key, prev_key,
447
448
anc_key, &s_temp);
448
449
length=buff_length-p_length;
449
450
endpos=buff+length+leaf_length+t_length;
450
451
/* buff will always be larger than before !*/
451
internal::bmove_upp((unsigned char*) endpos, (unsigned char*) buff+buff_length,length);
452
bmove_upp((uchar*) endpos, (uchar*) buff+buff_length,length);
452
453
memcpy(buff, leaf_buff, leaf_length);
453
454
(*keyinfo->store_key)(keyinfo,buff+leaf_length,&s_temp);
454
455
buff_length=(uint) (endpos-buff);
486
487
half_pos=after_key;
487
488
_mi_kpointer(info,leaf_key+key_length,next_page);
488
489
/* Save key in anc_buff */
489
prev_key=(keypos == anc_buff+2+key_reflength ? (unsigned char*) 0 : anc_key),
490
prev_key=(keypos == anc_buff+2+key_reflength ? (uchar*) 0 : anc_key),
490
491
t_length=(*keyinfo->pack_key)(keyinfo,key_reflength,
491
(keypos == endpos ? (unsigned char*) 0 :
492
(keypos == endpos ? (uchar*) 0 :
493
494
prev_key, prev_key,
494
495
leaf_key, &s_temp);
495
496
if (t_length >= 0)
496
internal::bmove_upp((unsigned char*) endpos+t_length,(unsigned char*) endpos,
497
bmove_upp((uchar*) endpos+t_length,(uchar*) endpos,
497
498
(uint) (endpos-keypos));
499
memmove(keypos,keypos-t_length,(uint) (endpos-keypos)+t_length);
500
memcpy(keypos,keypos-t_length,(uint) (endpos-keypos)+t_length);
500
501
(*keyinfo->store_key)(keyinfo,keypos,&s_temp);
501
502
mi_putint(anc_buff,(anc_length+=t_length),key_reflength);
503
504
/* Store key first in new page */
505
memmove(buff + 2, half_pos - nod_flag, nod_flag);
506
memcpy(buff + 2, half_pos - nod_flag, nod_flag);
506
507
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,
509
t_length=(int) (*keyinfo->pack_key)(keyinfo, nod_flag, (uchar*) 0,
510
(uchar*) 0, (uchar *) 0,
510
511
leaf_key, &s_temp);
511
512
/* t_length will always be > 0 for a new page !*/
512
513
length=(uint) ((buff+mi_getint(buff))-half_pos);
513
memmove(buff + p_length + t_length, half_pos, length);
514
memcpy(buff + p_length + t_length, half_pos, length);
514
515
(*keyinfo->store_key)(keyinfo,buff+p_length,&s_temp);
515
516
mi_putint(buff,length+t_length+p_length,nod_flag);
544
545
/* merge pages and put parting key from anc_buff between */
545
prev_key=(leaf_length == p_length ? (unsigned char*) 0 : leaf_key);
546
prev_key=(leaf_length == p_length ? (uchar*) 0 : leaf_key);
546
547
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,
547
548
(leaf_length == p_length ?
548
(unsigned char*) 0 : leaf_buff+p_length),
549
(uchar*) 0 : leaf_buff+p_length),
549
550
prev_key, prev_key,
550
551
anc_key, &s_temp);
551
552
if (t_length >= 0)
552
memmove(endpos+t_length,leaf_buff+p_length, leaf_length-p_length);
553
memcpy(endpos+t_length,leaf_buff+p_length, leaf_length-p_length);
553
554
else /* We gained space */
554
memmove(endpos, leaf_buff+((int) p_length-t_length),
555
leaf_length - p_length + t_length);
555
memcpy(endpos, leaf_buff+((int) p_length-t_length),
556
leaf_length - p_length + t_length);
557
558
(*keyinfo->store_key)(keyinfo,endpos,&s_temp);
558
559
buff_length=buff_length+leaf_length-p_length+t_length;
588
589
temp_pos=anc_buff+anc_length;
589
590
t_length=(*keyinfo->pack_key)(keyinfo,key_reflength,
590
keypos == temp_pos ? (unsigned char*) 0
591
keypos == temp_pos ? (uchar*) 0
592
593
anc_pos, anc_pos,
593
594
leaf_key,&s_temp);
594
595
if (t_length > 0)
595
internal::bmove_upp((unsigned char*) temp_pos+t_length,(unsigned char*) temp_pos,
596
bmove_upp((uchar*) temp_pos+t_length,(uchar*) temp_pos,
596
597
(uint) (temp_pos-keypos));
598
memmove(keypos,keypos-t_length,(uint) (temp_pos-keypos)+t_length);
599
memcpy(keypos,keypos-t_length,(uint) (temp_pos-keypos)+t_length);
599
600
(*keyinfo->store_key)(keyinfo,keypos,&s_temp);
600
601
mi_putint(anc_buff,(anc_length+=t_length),key_reflength);
602
603
/* Store first key on new page */
604
memmove(leaf_buff+2, half_pos - nod_flag, nod_flag);
605
memcpy(leaf_buff+2, half_pos - nod_flag, nod_flag);
605
606
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);
608
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag, (uchar*) 0,
609
(uchar*) 0, (uchar*) 0, leaf_key, &s_temp);
609
610
length=(uint) ((buff+buff_length)-half_pos);
610
memmove(leaf_buff + p_length + t_length, half_pos, length);
611
memcpy(leaf_buff + p_length + t_length, half_pos, length);
611
612
(*keyinfo->store_key)(keyinfo,leaf_buff+p_length,&s_temp);
612
613
mi_putint(leaf_buff,length+t_length+p_length,nod_flag);
613
614
if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
630
631
returns how many chars was removed or 0 on error
633
static uint32_t remove_key(MI_KEYDEF *keyinfo, uint32_t nod_flag,
634
unsigned char *keypos, /* Where key starts */
635
unsigned char *lastkey, /* key to be removed */
636
unsigned char *page_end, /* End of page */
637
internal::my_off_t *next_block) /* ptr to next block */
634
static uint remove_key(MI_KEYDEF *keyinfo, uint nod_flag,
635
uchar *keypos, /* Where key starts */
636
uchar *lastkey, /* key to be removed */
637
uchar *page_end, /* End of page */
638
my_off_t *next_block) /* ptr to next block */
640
unsigned char *start;
643
644
if (!(keyinfo->flag &
662
663
if (keyinfo->flag & HA_BINARY_PACK_KEY)
664
unsigned char *old_key=start;
665
uint32_t next_length,prev_length,prev_pack_length;
665
uchar *old_key=start;
666
uint next_length,prev_length,prev_pack_length;
666
667
get_key_length(next_length,keypos);
667
668
get_key_pack_length(prev_length,prev_pack_length,old_key);
668
669
if (next_length > prev_length)
670
671
/* We have to copy data from the current key to the next key */
671
internal::bmove_upp(keypos, (lastkey+next_length),
672
bmove_upp(keypos, (lastkey+next_length),
672
673
(next_length-prev_length));
673
674
keypos-=(next_length-prev_length)+prev_pack_length;
674
675
store_key_length(keypos,prev_length);
715
716
if (next_length >= prev_length)
716
717
{ /* Key after is based on deleted key */
717
uint32_t pack_length,tmp;
718
internal::bmove_upp(keypos, (lastkey+next_length),
719
tmp=(next_length-prev_length));
718
uint pack_length,tmp;
719
bmove_upp(keypos, (lastkey+next_length),
720
tmp=(next_length-prev_length));
720
721
rest_length+=tmp;
721
722
pack_length= prev_length ? get_pack_length(rest_length): 0;
722
723
keypos-=tmp+pack_length+prev_pack_length;