1
/* Copyright (C) 2000-2002, 2004-200 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 current record in heap-database */
20
int heap_delete(HP_INFO *info, const uchar *record)
23
HP_SHARE *share=info->s;
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));
30
if (info->opt_flag & READ_CHECK_USED && hp_rectest(info,record))
31
DBUG_RETURN(my_errno); /* Record changed */
34
if ( --(share->records) < share->blength >> 1) share->blength>>=1;
35
pos=info->current_ptr;
37
p_lastinx = share->keydef + info->lastinx;
38
for (keydef = share->keydef, end = keydef + share->keys; keydef < end;
41
if ((*keydef->delete_key)(info, keydef, record, pos, keydef == p_lastinx))
45
info->update=HA_STATE_DELETED;
46
*((uchar**) pos)=share->del_link;
48
pos[share->reclength]=0; /* Record deleted */
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););
57
if (++(share->records) == share->blength)
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);
89
Remove one key from hash-table
94
keyinfo key definition of key that we want to delete
95
record row data to be deleted
96
recpos Pointer to heap record in memory
97
flag Is set if we want's to correct info->current_ptr
104
int hp_delete_key(HP_INFO *info, register HP_KEYDEF *keyinfo,
105
const uchar *record, uchar *recpos, int flag)
107
ulong blength,pos2,pos_hashnr,lastpos_hashnr;
108
HASH_INFO *lastpos,*gpos,*pos,*pos3,*empty,*last_ptr;
109
HP_SHARE *share=info->s;
110
DBUG_ENTER("hp_delete_key");
112
blength=share->blength;
113
if (share->records+1 == blength)
115
lastpos=hp_find_hash(&keyinfo->block,share->records);
118
/* Search after record with key */
119
pos= hp_find_hash(&keyinfo->block,
120
hp_mask(hp_rec_hashnr(keyinfo, record), blength,
121
share->records + 1));
124
while (pos->ptr_to_rec != recpos)
126
if (flag && !hp_rec_key_cmp(keyinfo, record, pos->ptr_to_rec, 0))
127
last_ptr=pos; /* Previous same key */
129
if (!(pos=pos->next_key))
131
DBUG_RETURN(my_errno=HA_ERR_CRASHED); /* This shouldn't happend */
135
/* Remove link to record */
139
/* Save for heap_rnext/heap_rprev */
140
info->current_hash_ptr=last_ptr;
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));
147
gpos->next_key=pos->next_key; /* unlink current ptr */
148
else if (pos->next_key)
151
pos->ptr_to_rec=empty->ptr_to_rec;
152
pos->next_key=empty->next_key;
155
keyinfo->hash_buckets--;
157
if (empty == lastpos) /* deleted last hash key */
160
/* Move the last key (lastpos) */
161
lastpos_hashnr = hp_rec_hashnr(keyinfo, lastpos->ptr_to_rec);
162
/* pos is where lastpos should be */
163
pos=hp_find_hash(&keyinfo->block, hp_mask(lastpos_hashnr, share->blength,
165
if (pos == empty) /* Move to empty position. */
170
pos_hashnr = hp_rec_hashnr(keyinfo, pos->ptr_to_rec);
171
/* pos3 is where the pos should be */
172
pos3= hp_find_hash(&keyinfo->block,
173
hp_mask(pos_hashnr, share->blength, share->records));
175
{ /* pos is on wrong posit */
176
empty[0]=pos[0]; /* Save it here */
177
pos[0]=lastpos[0]; /* This shold be here */
178
hp_movelink(pos, pos3, empty); /* Fix link to pos */
181
pos2= hp_mask(lastpos_hashnr, blength, share->records + 1);
182
if (pos2 == hp_mask(pos_hashnr, blength, share->records + 1))
183
{ /* Identical key-positions */
184
if (pos2 != share->records)
187
hp_movelink(lastpos, pos, empty);
190
pos3= pos; /* Link pos->next after lastpos */
194
pos3= 0; /* Different positions merge */
195
keyinfo->hash_buckets--;
199
hp_movelink(pos3, empty, pos->next_key);