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
18
#include "myisam_priv.h"
19
#include "drizzled/internal/m_string.h"
19
#include <mystrings/m_string.h>
20
20
#include <drizzled/util/test.h>
22
using namespace drizzled;
24
22
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);
23
unsigned char *key,uint32_t key_length,my_off_t page,unsigned char *anc_buff);
26
24
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);
25
my_off_t leaf_page,unsigned char *leaf_buff,unsigned char *keypos,
26
my_off_t next_block,unsigned char *ret_key);
29
27
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);
28
my_off_t leaf_page,unsigned char *leaf_buff,unsigned char *keypos);
31
29
static uint32_t remove_key(MI_KEYDEF *keyinfo,uint32_t nod_flag,unsigned char *keypos,
32
30
unsigned char *lastkey,unsigned char *page_end,
33
internal::my_off_t *next_block);
31
my_off_t *next_block);
34
32
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);
33
unsigned char *key, uint32_t key_length, my_off_t *root);
38
36
int mi_delete(MI_INFO *info,const unsigned char *record)
47
45
/* Test if record is in datafile */
48
46
if (!(info->update & HA_STATE_AKTIV))
50
return(errno=HA_ERR_KEY_NOT_FOUND); /* No database read */
48
return(my_errno=HA_ERR_KEY_NOT_FOUND); /* No database read */
52
50
if (share->options & HA_OPTION_READ_ONLY_DATA)
52
return(my_errno=EACCES);
56
54
if (_mi_readinfo(info,F_WRLCK,1))
58
56
if (info->s->calc_checksum)
59
57
info->checksum=(*info->s->calc_checksum)(info,record);
60
58
if ((*share->compare_record)(info,record))
126
124
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)
125
unsigned char *key, uint32_t key_length, my_off_t *root)
130
128
uint32_t nod_flag;
131
internal::my_off_t old_root;
132
130
unsigned char *root_buff;
134
132
if ((old_root=*root) == HA_OFFSET_ERROR)
136
134
mi_print_error(info->s, HA_ERR_CRASHED);
137
return(errno=HA_ERR_CRASHED);
135
return(my_errno=HA_ERR_CRASHED);
139
137
if (!(root_buff= (unsigned char*) malloc(keyinfo->block_length+
140
138
MI_MAX_KEY_BUFF*2)))
142
return(errno=ENOMEM);
140
return(my_errno=ENOMEM);
144
142
if (!_mi_fetch_keypage(info,keyinfo,old_root,DFLT_INIT_HITS,root_buff,0))
186
184
static int d_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
187
185
uint32_t comp_flag, unsigned char *key, uint32_t key_length,
188
internal::my_off_t page, unsigned char *anc_buff)
186
my_off_t page, unsigned char *anc_buff)
190
188
int flag,ret_value,save_flag;
191
189
uint32_t length,nod_flag,search_key_length;
193
191
unsigned char *leaf_buff,*keypos;
194
internal::my_off_t leaf_page= 0, next_block;
192
my_off_t leaf_page= 0, next_block;
195
193
unsigned char lastkey[MI_MAX_KEY_BUFF];
197
195
search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
268
266
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);
267
(unsigned char*) 0,(unsigned char*) 0,(my_off_t) 0,(bool) 0);
272
270
if (ret_value == 0 && mi_getint(anc_buff) > keyinfo->block_length)
288
286
/* Remove a key that has a page-reference */
290
288
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,
289
unsigned char *anc_buff, my_off_t leaf_page, unsigned char *leaf_buff,
292
290
unsigned char *keypos, /* Pos to where deleted key was */
293
internal::my_off_t next_block,
294
292
unsigned char *ret_key) /* key before keypos in anc_buff */
296
294
int ret_value,length;
297
295
uint32_t a_length,nod_flag,tmp;
298
internal::my_off_t next_page;
299
297
unsigned char keybuff[MI_MAX_KEY_BUFF],*endpos,*next_buff,*key_start, *prev_key;
300
298
MYISAM_SHARE *share=info->s;
301
299
MI_KEY_PARAM s_temp;
336
334
ret_value=_mi_insert(info,keyinfo,key,leaf_buff,endpos,keybuff,
337
(unsigned char*) 0,(unsigned char*) 0,(internal::my_off_t) 0,0);
335
(unsigned char*) 0,(unsigned char*) 0,(my_off_t) 0,0);
340
338
if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
364
362
prev_key, prev_key,
365
363
keybuff,&s_temp);
367
internal::bmove_upp((unsigned char*) endpos+length,(unsigned char*) endpos,(uint) (endpos-keypos));
365
bmove_upp((unsigned char*) endpos+length,(unsigned char*) endpos,(uint) (endpos-keypos));
369
367
memmove(keypos,keypos-length, (int) (endpos-keypos)+length);
370
368
(*keyinfo->store_key)(keyinfo,keypos,&s_temp);
387
385
static int underflow(register MI_INFO *info, register MI_KEYDEF *keyinfo,
388
386
unsigned char *anc_buff,
389
internal::my_off_t leaf_page,/* Ancestor page and underflow page */
387
my_off_t leaf_page,/* Ancestor page and underflow page */
390
388
unsigned char *leaf_buff,
391
389
unsigned char *keypos) /* Position to pos after key */
394
392
uint32_t length,anc_length,buff_length,leaf_length,p_length,s_length,nod_flag,
395
393
key_reflength,key_length;
396
internal::my_off_t next_page;
397
395
unsigned char anc_key[MI_MAX_KEY_BUFF],leaf_key[MI_MAX_KEY_BUFF],
398
396
*buff,*endpos,*next_keypos,*anc_pos,*half_pos,*temp_pos,*prev_key,
448
446
length=buff_length-p_length;
449
447
endpos=buff+length+leaf_length+t_length;
450
448
/* buff will always be larger than before !*/
451
internal::bmove_upp((unsigned char*) endpos, (unsigned char*) buff+buff_length,length);
449
bmove_upp((unsigned char*) endpos, (unsigned char*) buff+buff_length,length);
452
450
memcpy(buff, leaf_buff, leaf_length);
453
451
(*keyinfo->store_key)(keyinfo,buff+leaf_length,&s_temp);
454
452
buff_length=(uint) (endpos-buff);
592
590
anc_pos, anc_pos,
593
591
leaf_key,&s_temp);
594
592
if (t_length > 0)
595
internal::bmove_upp((unsigned char*) temp_pos+t_length,(unsigned char*) temp_pos,
593
bmove_upp((unsigned char*) temp_pos+t_length,(unsigned char*) temp_pos,
596
594
(uint) (temp_pos-keypos));
598
596
memmove(keypos,keypos-t_length,(uint) (temp_pos-keypos)+t_length);
668
666
if (next_length > prev_length)
670
668
/* We have to copy data from the current key to the next key */
671
internal::bmove_upp(keypos, (lastkey+next_length),
669
bmove_upp(keypos, (lastkey+next_length),
672
670
(next_length-prev_length));
673
671
keypos-=(next_length-prev_length)+prev_pack_length;
674
672
store_key_length(keypos,prev_length);
715
713
if (next_length >= prev_length)
716
714
{ /* Key after is based on deleted key */
717
715
uint32_t pack_length,tmp;
718
internal::bmove_upp(keypos, (lastkey+next_length),
719
tmp=(next_length-prev_length));
716
bmove_upp(keypos, (lastkey+next_length),
717
tmp=(next_length-prev_length));
720
718
rest_length+=tmp;
721
719
pack_length= prev_length ? get_pack_length(rest_length): 0;
722
720
keypos-=tmp+pack_length+prev_pack_length;