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 */
18
#include "heap_priv.h"
20
int heap_delete(HP_INFO *info, const unsigned char *record)
23
HP_SHARE *share=info->s;
24
HP_KEYDEF *keydef, *end, *p_lastinx;
25
uint32_t rec_length, chunk_count;
29
if (info->opt_flag & READ_CHECK_USED)
30
return(errno); /* Record changed */
33
rec_length = hp_get_encoded_data_length(share, record, &chunk_count);
35
if ( --(share->records) < share->blength >> 1) share->blength>>=1;
36
pos=info->current_ptr;
38
p_lastinx = share->keydef + info->lastinx;
39
for (keydef = share->keydef, end = keydef + share->keys; keydef < end;
42
if ((*keydef->delete_key)(info, keydef, record, pos, keydef == p_lastinx))
46
info->update=HA_STATE_DELETED;
47
hp_free_chunks(&share->recordspace, pos);
48
info->current_hash_ptr=0;
52
if (++(share->records) == share->blength)
53
share->blength+= share->blength;
59
Remove one key from rb-tree
62
int hp_rb_delete_key(HP_INFO *info, register HP_KEYDEF *keyinfo,
63
const unsigned char *record, unsigned char *recpos, int flag)
65
heap_rb_param custom_arg;
66
uint32_t old_allocated;
70
info->last_pos= NULL; /* For heap_rnext/heap_rprev */
72
custom_arg.keyseg= keyinfo->seg;
73
custom_arg.key_length= hp_rb_make_key(keyinfo, info->recbuf, record, recpos);
74
custom_arg.search_flag= SEARCH_SAME;
75
old_allocated= keyinfo->rb_tree.allocated;
76
res= tree_delete(&keyinfo->rb_tree, info->recbuf, custom_arg.key_length,
78
info->s->index_length-= (old_allocated - keyinfo->rb_tree.allocated);
84
Remove one key from hash-table
89
keyinfo key definition of key that we want to delete
90
record row data to be deleted
91
recpos Pointer to heap record in memory
92
flag Is set if we want's to correct info->current_ptr
99
int hp_delete_key(HP_INFO *info, register HP_KEYDEF *keyinfo,
100
const unsigned char *record, unsigned char *recpos, int flag)
102
uint32_t blength,pos2,pos_hashnr,lastpos_hashnr;
103
HASH_INFO *lastpos,*gpos,*pos,*pos3,*empty,*last_ptr;
104
HP_SHARE *share=info->s;
106
blength=share->blength;
107
if (share->records+1 == blength)
110
/* find the very last HASH_INFO pointer in the index */
111
/* note that records has already been decremented */
112
lastpos=hp_find_hash(&keyinfo->block,share->records);
115
/* Search after record with key */
116
pos= hp_find_hash(&keyinfo->block,
117
hp_mask(hp_rec_hashnr(keyinfo, record), blength,
118
share->records + 1));
121
while (pos->ptr_to_rec != recpos)
123
if (flag && !hp_rec_key_cmp(keyinfo, record, pos->ptr_to_rec, 0))
124
last_ptr=pos; /* Previous same key */
126
if (!(pos=pos->next_key))
128
return(errno=HA_ERR_CRASHED); /* This shouldn't happend */
132
/* Remove link to record */
136
/* Save for heap_rnext/heap_rprev */
137
info->current_hash_ptr=last_ptr;
138
info->current_ptr = last_ptr ? last_ptr->ptr_to_rec : 0;
142
/* gpos says we have previous HASH_INFO, change previous to point to next, this way unlinking "empty" */
143
gpos->next_key=pos->next_key;
145
else if (pos->next_key)
147
/* no previous gpos, this pos is the first in the list and it has pointer to "next" */
148
/* move next HASH_INFO data to our pos, to free up space at the next position */
149
/* remember next pos as "empty", nobody refers to "empty" at this point */
151
pos->ptr_to_rec=empty->ptr_to_rec;
152
pos->next_key=empty->next_key;
156
/* this was the only HASH_INFO at this position */
157
keyinfo->hash_buckets--;
160
if (empty == lastpos) /* deleted last hash key */
163
/* Move the last key (lastpos) */
164
lastpos_hashnr = hp_rec_hashnr(keyinfo, lastpos->ptr_to_rec);
165
/* pos is where lastpos should be */
166
pos=hp_find_hash(&keyinfo->block, hp_mask(lastpos_hashnr, share->blength,
168
if (pos == empty) /* Move to empty position. */
173
pos_hashnr = hp_rec_hashnr(keyinfo, pos->ptr_to_rec);
174
/* pos3 is where the pos should be */
175
pos3= hp_find_hash(&keyinfo->block,
176
hp_mask(pos_hashnr, share->blength, share->records));
178
{ /* pos is on wrong posit */
179
empty[0]=pos[0]; /* Save it here */
180
pos[0]=lastpos[0]; /* This shold be here */
181
hp_movelink(pos, pos3, empty); /* Fix link to pos */
184
pos2= hp_mask(lastpos_hashnr, blength, share->records + 1);
185
if (pos2 == hp_mask(pos_hashnr, blength, share->records + 1))
186
{ /* Identical key-positions */
187
if (pos2 != share->records)
190
hp_movelink(lastpos, pos, empty);
193
pos3= pos; /* Link pos->next after lastpos */
197
pos3= 0; /* Different positions merge */
198
keyinfo->hash_buckets--;
202
hp_movelink(pos3, empty, pos->next_key);