~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/mi_delete.cc

  • Committer: Brian Aker
  • Date: 2009-11-18 06:24:48 UTC
  • mfrom: (1220.1.15 staging)
  • Revision ID: brian@gaz-20091118062448-o36lo3yv81sc6u9z
Merge Brian + Stewart

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
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 */
15
15
 
16
16
/* Remove a row from a MyISAM table */
17
17
 
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>
21
21
 
22
 
using namespace drizzled;
23
 
 
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);
36
34
 
37
35
 
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))
49
47
  {
50
 
    return(errno=HA_ERR_KEY_NOT_FOUND); /* No database read */
 
48
    return(my_errno=HA_ERR_KEY_NOT_FOUND);      /* No database read */
51
49
  }
52
50
  if (share->options & HA_OPTION_READ_ONLY_DATA)
53
51
  {
54
 
    return(errno=EACCES);
 
52
    return(my_errno=EACCES);
55
53
  }
56
54
  if (_mi_readinfo(info,F_WRLCK,1))
57
 
    return(errno);
 
55
    return(my_errno);
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))
93
91
  return(0);
94
92
 
95
93
err:
96
 
  save_errno=errno;
 
94
  save_errno=my_errno;
97
95
  mi_sizestore(lastpos,info->lastpos);
98
96
  if (save_errno != HA_ERR_RECORD_CHANGED)
99
97
  {
102
100
  }
103
101
  _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
104
102
  info->update|=HA_STATE_WRITTEN;       /* Buffer changed */
105
 
  errno=save_errno;
 
103
  my_errno=save_errno;
106
104
  if (save_errno == HA_ERR_KEY_NOT_FOUND)
107
105
  {
108
106
    mi_print_error(info->s, HA_ERR_CRASHED);
109
 
    errno=HA_ERR_CRASHED;
 
107
    my_errno=HA_ERR_CRASHED;
110
108
  }
111
109
 
112
 
  return(errno);
 
110
  return(my_errno);
113
111
} /* mi_delete */
114
112
 
115
113
 
124
122
 
125
123
 
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)
128
126
{
129
127
  int error;
130
128
  uint32_t nod_flag;
131
 
  internal::my_off_t old_root;
 
129
  my_off_t old_root;
132
130
  unsigned char *root_buff;
133
131
 
134
132
  if ((old_root=*root) == HA_OFFSET_ERROR)
135
133
  {
136
134
    mi_print_error(info->s, HA_ERR_CRASHED);
137
 
    return(errno=HA_ERR_CRASHED);
 
135
    return(my_errno=HA_ERR_CRASHED);
138
136
  }
139
137
  if (!(root_buff= (unsigned char*) malloc(keyinfo->block_length+
140
138
                                      MI_MAX_KEY_BUFF*2)))
141
139
  {
142
 
    return(errno=ENOMEM);
 
140
    return(my_errno=ENOMEM);
143
141
  }
144
142
  if (!_mi_fetch_keypage(info,keyinfo,old_root,DFLT_INIT_HITS,root_buff,0))
145
143
  {
185
183
 
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)
189
187
{
190
188
  int flag,ret_value,save_flag;
191
189
  uint32_t length,nod_flag,search_key_length;
192
190
  bool last_key;
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];
196
194
 
197
195
  search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
210
208
    if (!(leaf_buff= (unsigned char*) malloc(keyinfo->block_length+
211
209
                                        MI_MAX_KEY_BUFF*2)))
212
210
    {
213
 
      errno=ENOMEM;
 
211
      my_errno=ENOMEM;
214
212
      return(-1);
215
213
    }
216
214
    if (!_mi_fetch_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff,0))
222
220
    if (!nod_flag)
223
221
    {
224
222
      mi_print_error(info->s, HA_ERR_CRASHED);
225
 
      errno=HA_ERR_CRASHED;             /* This should newer happend */
 
223
      my_errno=HA_ERR_CRASHED;          /* This should newer happend */
226
224
      goto err;
227
225
    }
228
226
    save_flag=0;
266
264
        goto err;
267
265
      }
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);
270
268
    }
271
269
  }
272
270
  if (ret_value == 0 && mi_getint(anc_buff) > keyinfo->block_length)
288
286
        /* Remove a key that has a page-reference */
289
287
 
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,
 
291
               my_off_t next_block,
294
292
               unsigned char *ret_key)          /* key before keypos in anc_buff */
295
293
{
296
294
  int ret_value,length;
297
295
  uint32_t a_length,nod_flag,tmp;
298
 
  internal::my_off_t next_page;
 
296
  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;
334
332
                                &tmp))
335
333
            goto err;
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);
338
336
        }
339
337
      }
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);
366
364
  if (length > 0)
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));
368
366
  else
369
367
    memmove(keypos,keypos-length, (int) (endpos-keypos)+length);
370
368
  (*keyinfo->store_key)(keyinfo,keypos,&s_temp);
386
384
 
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 */
392
390
{
393
391
  int t_length;
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;
 
394
  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,
399
397
        *after_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);
457
455
    /* remove key from anc_buff */
458
456
 
459
457
    if (!(s_length=remove_key(keyinfo,key_reflength,keypos,anc_key,
460
 
                              anc_buff+anc_length,(internal::my_off_t *) 0)))
 
458
                              anc_buff+anc_length,(my_off_t *) 0)))
461
459
      goto err;
462
460
 
463
461
    anc_length-=s_length;
493
491
                                    prev_key, prev_key,
494
492
                                    leaf_key, &s_temp);
495
493
      if (t_length >= 0)
496
 
        internal::bmove_upp((unsigned char*) endpos+t_length,(unsigned char*) endpos,
 
494
        bmove_upp((unsigned char*) endpos+t_length,(unsigned char*) endpos,
497
495
                  (uint) (endpos-keypos));
498
496
      else
499
497
        memmove(keypos,keypos-t_length,(uint) (endpos-keypos)+t_length);
560
558
 
561
559
  /* remove key from anc_buff */
562
560
  if (!(s_length= remove_key(keyinfo,key_reflength,keypos,anc_key,
563
 
                             anc_buff+anc_length,(internal::my_off_t *) 0)))
 
561
                             anc_buff+anc_length,(my_off_t *) 0)))
564
562
    goto err;
565
563
 
566
564
  anc_length-=s_length;
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));
597
595
    else
598
596
      memmove(keypos,keypos-t_length,(uint) (temp_pos-keypos)+t_length);
634
632
                       unsigned char *keypos,   /* Where key starts */
635
633
                       unsigned char *lastkey,  /* key to be removed */
636
634
                       unsigned char *page_end, /* End of page */
637
 
                       internal::my_off_t *next_block)  /* ptr to next block */
 
635
                       my_off_t *next_block)    /* ptr to next block */
638
636
{
639
637
  int s_length;
640
638
  unsigned char *start;
668
666
        if (next_length > prev_length)
669
667
        {
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;