16
16
/* Remove a row from a MyISAM table */
18
#include "myisamdef.h"
18
#include "myisam_priv.h"
19
#include "drizzled/internal/m_string.h"
20
#include <drizzled/util/test.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,
22
static int d_search(MI_INFO *info,MI_KEYDEF *keyinfo,uint32_t comp_flag,
23
unsigned char *key,uint32_t key_length,my_off_t page,unsigned char *anc_buff);
24
static int del(MI_INFO *info,MI_KEYDEF *keyinfo,unsigned char *key,unsigned char *anc_buff,
25
my_off_t leaf_page,unsigned char *leaf_buff,unsigned char *keypos,
26
my_off_t next_block,unsigned char *ret_key);
27
static int underflow(MI_INFO *info,MI_KEYDEF *keyinfo,unsigned char *anc_buff,
28
my_off_t leaf_page,unsigned char *leaf_buff,unsigned char *keypos);
29
static uint32_t remove_key(MI_KEYDEF *keyinfo,uint32_t nod_flag,unsigned char *keypos,
30
unsigned char *lastkey,unsigned char *page_end,
29
31
my_off_t *next_block);
30
32
static int _mi_ck_real_delete(register MI_INFO *info,MI_KEYDEF *keyinfo,
31
uchar *key, uint key_length, my_off_t *root);
34
int mi_delete(MI_INFO *info,const uchar *record)
33
unsigned char *key, uint32_t key_length, my_off_t *root);
36
int mi_delete(MI_INFO *info,const unsigned char *record)
39
unsigned char *old_key;
43
45
/* Test if record is in datafile */
44
46
if (!(info->update & HA_STATE_AKTIV))
46
return(my_errno=HA_ERR_KEY_NOT_FOUND); /* No database read */
48
return(errno=HA_ERR_KEY_NOT_FOUND); /* No database read */
48
50
if (share->options & HA_OPTION_READ_ONLY_DATA)
50
return(my_errno=EACCES);
52
54
if (_mi_readinfo(info,F_WRLCK,1))
54
56
if (info->s->calc_checksum)
55
57
info->checksum=(*info->s->calc_checksum)(info,record);
56
58
if ((*share->compare_record)(info,record))
85
87
info->state->records--;
87
89
mi_sizestore(lastpos,info->lastpos);
88
VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
89
if (info->invalidator != 0)
91
(*info->invalidator)(info->filename);
90
_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
98
95
mi_sizestore(lastpos,info->lastpos);
99
96
if (save_errno != HA_ERR_RECORD_CHANGED)
101
98
mi_print_error(info->s, HA_ERR_CRASHED);
102
99
mi_mark_crashed(info); /* mark table crashed */
104
VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
101
_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
105
102
info->update|=HA_STATE_WRITTEN; /* Buffer changed */
107
104
if (save_errno == HA_ERR_KEY_NOT_FOUND)
109
106
mi_print_error(info->s, HA_ERR_CRASHED);
110
my_errno=HA_ERR_CRASHED;
107
errno=HA_ERR_CRASHED;
114
111
} /* mi_delete */
117
114
/* Remove a key from the btree index */
119
int _mi_ck_delete(register MI_INFO *info, uint keynr, uchar *key,
116
int _mi_ck_delete(register MI_INFO *info, uint32_t keynr, unsigned char *key,
122
119
return _mi_ck_real_delete(info, info->s->keyinfo+keynr, key, key_length,
123
120
&info->s->state.key_root[keynr]);
127
124
static int _mi_ck_real_delete(register MI_INFO *info, MI_KEYDEF *keyinfo,
128
uchar *key, uint key_length, my_off_t *root)
125
unsigned char *key, uint32_t key_length, my_off_t *root)
132
129
my_off_t old_root;
130
unsigned char *root_buff;
135
132
if ((old_root=*root) == HA_OFFSET_ERROR)
137
134
mi_print_error(info->s, HA_ERR_CRASHED);
138
return(my_errno=HA_ERR_CRASHED);
135
return(errno=HA_ERR_CRASHED);
140
if (!(root_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
137
if (!(root_buff= (unsigned char*) malloc(keyinfo->block_length+
141
138
MI_MAX_KEY_BUFF*2)))
143
return(my_errno=ENOMEM);
140
return(errno=ENOMEM);
145
142
if (!_mi_fetch_keypage(info,keyinfo,old_root,DFLT_INIT_HITS,root_buff,0))
187
184
static int d_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
188
uint comp_flag, uchar *key, uint key_length,
189
my_off_t page, uchar *anc_buff)
185
uint32_t comp_flag, unsigned char *key, uint32_t key_length,
186
my_off_t page, unsigned char *anc_buff)
191
188
int flag,ret_value,save_flag;
192
uint length,nod_flag,search_key_length;
189
uint32_t length,nod_flag,search_key_length;
194
uchar *leaf_buff,*keypos;
191
unsigned char *leaf_buff,*keypos;
195
192
my_off_t leaf_page= 0, next_block;
196
uchar lastkey[MI_MAX_KEY_BUFF];
193
unsigned char lastkey[MI_MAX_KEY_BUFF];
198
195
search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
199
196
flag=(*keyinfo->bin_search)(info,keyinfo,anc_buff,key, search_key_length,
269
266
ret_value=_mi_insert(info,keyinfo,key,anc_buff,keypos,lastkey,
270
(uchar*) 0,(uchar*) 0,(my_off_t) 0,(bool) 0);
267
(unsigned char*) 0,(unsigned char*) 0,(my_off_t) 0,(bool) 0);
273
270
if (ret_value == 0 && mi_getint(anc_buff) > keyinfo->block_length)
278
275
if (save_flag && ret_value != 1)
279
276
ret_value|=_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,anc_buff);
280
my_afree((uchar*) leaf_buff);
281
278
return(ret_value);
284
my_afree((uchar*) leaf_buff);
289
286
/* Remove a key that has a page-reference */
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 */
288
static int del(register MI_INFO *info, register MI_KEYDEF *keyinfo, unsigned char *key,
289
unsigned char *anc_buff, my_off_t leaf_page, unsigned char *leaf_buff,
290
unsigned char *keypos, /* Pos to where deleted key was */
294
291
my_off_t next_block,
295
uchar *ret_key) /* key before keypos in anc_buff */
292
unsigned char *ret_key) /* key before keypos in anc_buff */
297
294
int ret_value,length;
298
uint a_length,nod_flag,tmp;
295
uint32_t a_length,nod_flag,tmp;
299
296
my_off_t next_page;
300
uchar keybuff[MI_MAX_KEY_BUFF],*endpos,*next_buff,*key_start, *prev_key;
297
unsigned char keybuff[MI_MAX_KEY_BUFF],*endpos,*next_buff,*key_start, *prev_key;
301
298
MYISAM_SHARE *share=info->s;
302
299
MI_KEY_PARAM s_temp;
309
306
if ((nod_flag=mi_test_if_nod(leaf_buff)))
311
308
next_page= _mi_kpos(nod_flag,endpos);
312
if (!(next_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
309
if (!(next_buff= (unsigned char*) malloc(keyinfo->block_length+
313
310
MI_MAX_KEY_BUFF*2)))
315
312
if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,next_buff,0))
337
334
ret_value=_mi_insert(info,keyinfo,key,leaf_buff,endpos,keybuff,
338
(uchar*) 0,(uchar*) 0,(my_off_t) 0,0);
335
(unsigned char*) 0,(unsigned char*) 0,(my_off_t) 0,0);
341
338
if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
344
my_afree((uchar*) next_buff);
345
342
return(ret_value);
361
358
prev_key=(keypos == anc_buff+2+share->base.key_reflength ?
363
360
length=(*keyinfo->pack_key)(keyinfo,share->base.key_reflength,
364
keypos == endpos ? (uchar*) 0 : keypos,
361
keypos == endpos ? (unsigned char*) 0 : keypos,
365
362
prev_key, prev_key,
366
363
keybuff,&s_temp);
368
bmove_upp((uchar*) endpos+length,(uchar*) endpos,(uint) (endpos-keypos));
365
bmove_upp((unsigned char*) endpos+length,(unsigned char*) endpos,(uint) (endpos-keypos));
370
memcpy(keypos,keypos-length, (int) (endpos-keypos)+length);
367
memmove(keypos,keypos-length, (int) (endpos-keypos)+length);
371
368
(*keyinfo->store_key)(keyinfo,keypos,&s_temp);
372
369
/* Save pointer to next leaf */
373
370
if (!(*keyinfo->get_key)(keyinfo,share->base.key_reflength,&keypos,ret_key))
386
383
/* Balances adjacent pages if underflow occours */
388
385
static int underflow(register MI_INFO *info, register MI_KEYDEF *keyinfo,
386
unsigned char *anc_buff,
390
387
my_off_t leaf_page,/* Ancestor page and underflow page */
392
uchar *keypos) /* Position to pos after key */
388
unsigned char *leaf_buff,
389
unsigned char *keypos) /* Position to pos after key */
395
uint length,anc_length,buff_length,leaf_length,p_length,s_length,nod_flag,
392
uint32_t length,anc_length,buff_length,leaf_length,p_length,s_length,nod_flag,
396
393
key_reflength,key_length;
397
394
my_off_t next_page;
398
uchar anc_key[MI_MAX_KEY_BUFF],leaf_key[MI_MAX_KEY_BUFF],
395
unsigned char anc_key[MI_MAX_KEY_BUFF],leaf_key[MI_MAX_KEY_BUFF],
399
396
*buff,*endpos,*next_keypos,*anc_pos,*half_pos,*temp_pos,*prev_key,
401
398
MI_KEY_PARAM s_temp;
435
432
buff_length=mi_getint(buff);
437
434
/* find keys to make a big key-page */
438
memcpy(next_keypos - key_reflength, buff + 2, key_reflength);
435
memmove(next_keypos - key_reflength, buff + 2, key_reflength);
439
436
if (!_mi_get_last_key(info,keyinfo,anc_buff,anc_key,next_keypos,&length)
440
437
|| !_mi_get_last_key(info,keyinfo,leaf_buff,leaf_key,
441
438
leaf_buff+leaf_length,&length))
444
441
/* merge pages and put parting key from anc_buff between */
445
prev_key=(leaf_length == p_length ? (uchar*) 0 : leaf_key);
442
prev_key=(leaf_length == p_length ? (unsigned char*) 0 : leaf_key);
446
443
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,buff+p_length,
447
444
prev_key, prev_key,
448
445
anc_key, &s_temp);
449
446
length=buff_length-p_length;
450
447
endpos=buff+length+leaf_length+t_length;
451
448
/* buff will always be larger than before !*/
452
bmove_upp((uchar*) endpos, (uchar*) buff+buff_length,length);
449
bmove_upp((unsigned char*) endpos, (unsigned char*) buff+buff_length,length);
453
450
memcpy(buff, leaf_buff, leaf_length);
454
451
(*keyinfo->store_key)(keyinfo,buff+leaf_length,&s_temp);
455
452
buff_length=(uint) (endpos-buff);
487
484
half_pos=after_key;
488
485
_mi_kpointer(info,leaf_key+key_length,next_page);
489
486
/* Save key in anc_buff */
490
prev_key=(keypos == anc_buff+2+key_reflength ? (uchar*) 0 : anc_key),
487
prev_key=(keypos == anc_buff+2+key_reflength ? (unsigned char*) 0 : anc_key),
491
488
t_length=(*keyinfo->pack_key)(keyinfo,key_reflength,
492
(keypos == endpos ? (uchar*) 0 :
489
(keypos == endpos ? (unsigned char*) 0 :
494
491
prev_key, prev_key,
495
492
leaf_key, &s_temp);
496
493
if (t_length >= 0)
497
bmove_upp((uchar*) endpos+t_length,(uchar*) endpos,
494
bmove_upp((unsigned char*) endpos+t_length,(unsigned char*) endpos,
498
495
(uint) (endpos-keypos));
500
memcpy(keypos,keypos-t_length,(uint) (endpos-keypos)+t_length);
497
memmove(keypos,keypos-t_length,(uint) (endpos-keypos)+t_length);
501
498
(*keyinfo->store_key)(keyinfo,keypos,&s_temp);
502
499
mi_putint(anc_buff,(anc_length+=t_length),key_reflength);
504
501
/* Store key first in new page */
506
memcpy(buff + 2, half_pos - nod_flag, nod_flag);
503
memmove(buff + 2, half_pos - nod_flag, nod_flag);
507
504
if (!(*keyinfo->get_key)(keyinfo,nod_flag,&half_pos,leaf_key))
509
t_length=(int) (*keyinfo->pack_key)(keyinfo, nod_flag, (uchar*) 0,
510
(uchar*) 0, (uchar *) 0,
506
t_length=(int) (*keyinfo->pack_key)(keyinfo, nod_flag, (unsigned char*) 0,
507
(unsigned char*) 0, (unsigned char *) 0,
511
508
leaf_key, &s_temp);
512
509
/* t_length will always be > 0 for a new page !*/
513
510
length=(uint) ((buff+mi_getint(buff))-half_pos);
514
memcpy(buff + p_length + t_length, half_pos, length);
511
memmove(buff + p_length + t_length, half_pos, length);
515
512
(*keyinfo->store_key)(keyinfo,buff+p_length,&s_temp);
516
513
mi_putint(buff,length+t_length+p_length,nod_flag);
534
531
endpos=buff+buff_length;
536
533
/* find keys to make a big key-page */
537
memcpy(next_keypos - key_reflength, leaf_buff+2, key_reflength);
534
memmove(next_keypos - key_reflength, leaf_buff+2, key_reflength);
538
535
next_keypos=keypos;
539
536
if (!(*keyinfo->get_key)(keyinfo,key_reflength,&next_keypos,
545
542
/* merge pages and put parting key from anc_buff between */
546
prev_key=(leaf_length == p_length ? (uchar*) 0 : leaf_key);
543
prev_key=(leaf_length == p_length ? (unsigned char*) 0 : leaf_key);
547
544
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,
548
545
(leaf_length == p_length ?
549
(uchar*) 0 : leaf_buff+p_length),
546
(unsigned char*) 0 : leaf_buff+p_length),
550
547
prev_key, prev_key,
551
548
anc_key, &s_temp);
552
549
if (t_length >= 0)
553
memcpy(endpos+t_length,leaf_buff+p_length, leaf_length-p_length);
550
memmove(endpos+t_length,leaf_buff+p_length, leaf_length-p_length);
554
551
else /* We gained space */
555
memcpy(endpos, leaf_buff+((int) p_length-t_length),
556
leaf_length - p_length + t_length);
552
memmove(endpos, leaf_buff+((int) p_length-t_length),
553
leaf_length - p_length + t_length);
558
555
(*keyinfo->store_key)(keyinfo,endpos,&s_temp);
559
556
buff_length=buff_length+leaf_length-p_length+t_length;
589
586
temp_pos=anc_buff+anc_length;
590
587
t_length=(*keyinfo->pack_key)(keyinfo,key_reflength,
591
keypos == temp_pos ? (uchar*) 0
588
keypos == temp_pos ? (unsigned char*) 0
593
590
anc_pos, anc_pos,
594
591
leaf_key,&s_temp);
595
592
if (t_length > 0)
596
bmove_upp((uchar*) temp_pos+t_length,(uchar*) temp_pos,
593
bmove_upp((unsigned char*) temp_pos+t_length,(unsigned char*) temp_pos,
597
594
(uint) (temp_pos-keypos));
599
memcpy(keypos,keypos-t_length,(uint) (temp_pos-keypos)+t_length);
596
memmove(keypos,keypos-t_length,(uint) (temp_pos-keypos)+t_length);
600
597
(*keyinfo->store_key)(keyinfo,keypos,&s_temp);
601
598
mi_putint(anc_buff,(anc_length+=t_length),key_reflength);
603
600
/* Store first key on new page */
605
memcpy(leaf_buff+2, half_pos - nod_flag, nod_flag);
602
memmove(leaf_buff+2, half_pos - nod_flag, nod_flag);
606
603
if (!(length=(*keyinfo->get_key)(keyinfo,nod_flag,&half_pos,leaf_key)))
608
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag, (uchar*) 0,
609
(uchar*) 0, (uchar*) 0, leaf_key, &s_temp);
605
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag, (unsigned char*) 0,
606
(unsigned char*) 0, (unsigned char*) 0, leaf_key, &s_temp);
610
607
length=(uint) ((buff+buff_length)-half_pos);
611
memcpy(leaf_buff + p_length + t_length, half_pos, length);
608
memmove(leaf_buff + p_length + t_length, half_pos, length);
612
609
(*keyinfo->store_key)(keyinfo,leaf_buff+p_length,&s_temp);
613
610
mi_putint(leaf_buff,length+t_length+p_length,nod_flag);
614
611
if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
631
628
returns how many chars was removed or 0 on error
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 */
631
static uint32_t remove_key(MI_KEYDEF *keyinfo, uint32_t nod_flag,
632
unsigned char *keypos, /* Where key starts */
633
unsigned char *lastkey, /* key to be removed */
634
unsigned char *page_end, /* End of page */
638
635
my_off_t *next_block) /* ptr to next block */
638
unsigned char *start;
644
641
if (!(keyinfo->flag &
663
660
if (keyinfo->flag & HA_BINARY_PACK_KEY)
665
uchar *old_key=start;
666
uint next_length,prev_length,prev_pack_length;
662
unsigned char *old_key=start;
663
uint32_t next_length,prev_length,prev_pack_length;
667
664
get_key_length(next_length,keypos);
668
665
get_key_pack_length(prev_length,prev_pack_length,old_key);
669
666
if (next_length > prev_length)
682
679
if ((keyinfo->seg->flag & HA_PACK_KEY) && *keypos & 128)
684
681
/* Next key is packed against the current one */
685
uint next_length,prev_length,prev_pack_length,lastkey_length,
682
uint32_t next_length,prev_length,prev_pack_length,lastkey_length,
687
684
if (keyinfo->seg[0].length >= 127)