16
16
/* remove current record in heap-database */
18
#include "heap_priv.h"
20
int heap_delete(HP_INFO *info, const unsigned char *record)
20
int heap_delete(HP_INFO *info, const uchar *record)
23
HP_SHARE *share=info->getShare();
23
HP_SHARE *share=info->s;
24
24
HP_KEYDEF *keydef, *end, *p_lastinx;
25
DBUG_ENTER("heap_delete");
26
DBUG_PRINT("enter",("info: 0x%lx record: 0x%lx", (long) info, (long) record));
28
if (info->opt_flag & READ_CHECK_USED)
29
return(errno); /* Record changed */
30
if (info->opt_flag & READ_CHECK_USED && hp_rectest(info,record))
31
DBUG_RETURN(my_errno); /* Record changed */
32
34
if ( --(share->records) < share->blength >> 1) share->blength>>=1;
33
35
pos=info->current_ptr;
35
37
p_lastinx = share->keydef + info->lastinx;
36
for (keydef = share->keydef, end = keydef + share->keys; keydef < end;
38
for (keydef = share->keydef, end = keydef + share->keys; keydef < end;
39
if (hp_delete_key(info, keydef, record, pos, keydef == p_lastinx))
41
if ((*keydef->delete_key)(info, keydef, record, pos, keydef == p_lastinx))
43
45
info->update=HA_STATE_DELETED;
44
hp_free_chunks(&share->recordspace, pos);
46
*((uchar**) pos)=share->del_link;
48
pos[share->reclength]=0; /* Record deleted */
45
50
info->current_hash_ptr=0;
51
#if !defined(DBUG_OFF) && defined(EXTRA_HEAP_DEBUG)
52
DBUG_EXECUTE("check_heap",heap_check_heap(info, 0););
49
57
if (++(share->records) == share->blength)
50
58
share->blength+= share->blength;
59
DBUG_RETURN(my_errno);
64
Remove one key from rb-tree
67
int hp_rb_delete_key(HP_INFO *info, register HP_KEYDEF *keyinfo,
68
const uchar *record, uchar *recpos, int flag)
70
heap_rb_param custom_arg;
75
info->last_pos= NULL; /* For heap_rnext/heap_rprev */
77
custom_arg.keyseg= keyinfo->seg;
78
custom_arg.key_length= hp_rb_make_key(keyinfo, info->recbuf, record, recpos);
79
custom_arg.search_flag= SEARCH_SAME;
80
old_allocated= keyinfo->rb_tree.allocated;
81
res= tree_delete(&keyinfo->rb_tree, info->recbuf, custom_arg.key_length,
83
info->s->index_length-= (old_allocated - keyinfo->rb_tree.allocated);
71
104
int hp_delete_key(HP_INFO *info, register HP_KEYDEF *keyinfo,
72
const unsigned char *record, unsigned char *recpos, int flag)
105
const uchar *record, uchar *recpos, int flag)
74
uint32_t blength,pos2,pos_hashnr,lastpos_hashnr;
107
ulong blength,pos2,pos_hashnr,lastpos_hashnr;
75
108
HASH_INFO *lastpos,*gpos,*pos,*pos3,*empty,*last_ptr;
76
HP_SHARE *share=info->getShare();
109
HP_SHARE *share=info->s;
110
DBUG_ENTER("hp_delete_key");
78
112
blength=share->blength;
79
113
if (share->records+1 == blength)
80
114
blength+= blength;
82
/* find the very last HASH_INFO pointer in the index */
83
/* note that records has already been decremented */
84
115
lastpos=hp_find_hash(&keyinfo->block,share->records);
108
139
/* Save for heap_rnext/heap_rprev */
109
140
info->current_hash_ptr=last_ptr;
110
141
info->current_ptr = last_ptr ? last_ptr->ptr_to_rec : 0;
142
DBUG_PRINT("info",("Corrected current_ptr to point at: 0x%lx",
143
(long) info->current_ptr));
114
/* gpos says we have previous HASH_INFO, change previous to point to next, this way unlinking "empty" */
115
gpos->next_key=pos->next_key;
147
gpos->next_key=pos->next_key; /* unlink current ptr */
117
148
else if (pos->next_key)
119
/* no previous gpos, this pos is the first in the list and it has pointer to "next" */
120
/* move next HASH_INFO data to our pos, to free up space at the next position */
121
/* remember next pos as "empty", nobody refers to "empty" at this point */
122
150
empty=pos->next_key;
123
151
pos->ptr_to_rec=empty->ptr_to_rec;
124
152
pos->next_key=empty->next_key;
128
/* this was the only HASH_INFO at this position */
129
155
keyinfo->hash_buckets--;
132
157
if (empty == lastpos) /* deleted last hash key */
135
160
/* Move the last key (lastpos) */
136
161
lastpos_hashnr = hp_rec_hashnr(keyinfo, lastpos->ptr_to_rec);
151
176
empty[0]=pos[0]; /* Save it here */
152
177
pos[0]=lastpos[0]; /* This shold be here */
153
178
hp_movelink(pos, pos3, empty); /* Fix link to pos */
156
181
pos2= hp_mask(lastpos_hashnr, blength, share->records + 1);
157
182
if (pos2 == hp_mask(pos_hashnr, blength, share->records + 1))