~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_delete.c

  • Committer: Monty Taylor
  • Date: 2008-07-01 14:33:36 UTC
  • mto: (28.1.12 backport_patch)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: monty@inaugust.com-20080701143336-8uihm7dhpu92rt0q
Somehow missed moving password.c. Duh.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* Remove a row from a MyISAM table */
17
17
 
18
 
#include "myisamdef.h"
19
 
#include <drizzled/util/test.h>
 
18
#include "fulltext.h"
 
19
#include "rt_index.h"
20
20
 
21
 
static int d_search(MI_INFO *info,MI_KEYDEF *keyinfo,uint32_t comp_flag,
22
 
                    unsigned char *key,uint32_t key_length,my_off_t page,unsigned char *anc_buff);
23
 
static int del(MI_INFO *info,MI_KEYDEF *keyinfo,unsigned char *key,unsigned char *anc_buff,
24
 
               my_off_t leaf_page,unsigned char *leaf_buff,unsigned char *keypos,
25
 
               my_off_t next_block,unsigned char *ret_key);
26
 
static int underflow(MI_INFO *info,MI_KEYDEF *keyinfo,unsigned char *anc_buff,
27
 
                     my_off_t leaf_page,unsigned char *leaf_buff,unsigned char *keypos);
28
 
static uint32_t remove_key(MI_KEYDEF *keyinfo,uint32_t nod_flag,unsigned char *keypos,
29
 
                       unsigned char *lastkey,unsigned char *page_end,
 
21
static int d_search(MI_INFO *info,MI_KEYDEF *keyinfo,uint comp_flag,
 
22
                    uchar *key,uint key_length,my_off_t page,uchar *anc_buff);
 
23
static int del(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,uchar *anc_buff,
 
24
               my_off_t leaf_page,uchar *leaf_buff,uchar *keypos,
 
25
               my_off_t next_block,uchar *ret_key);
 
26
static int underflow(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *anc_buff,
 
27
                     my_off_t leaf_page,uchar *leaf_buff,uchar *keypos);
 
28
static uint remove_key(MI_KEYDEF *keyinfo,uint nod_flag,uchar *keypos,
 
29
                       uchar *lastkey,uchar *page_end,
30
30
                       my_off_t *next_block);
31
31
static int _mi_ck_real_delete(register MI_INFO *info,MI_KEYDEF *keyinfo,
32
 
                              unsigned char *key, uint32_t key_length, my_off_t *root);
33
 
 
34
 
 
35
 
int mi_delete(MI_INFO *info,const unsigned char *record)
 
32
                              uchar *key, uint key_length, my_off_t *root);
 
33
 
 
34
 
 
35
int mi_delete(MI_INFO *info,const uchar *record)
36
36
{
37
 
  uint32_t i;
38
 
  unsigned char *old_key;
 
37
  uint i;
 
38
  uchar *old_key;
39
39
  int save_errno;
40
40
  char lastpos[8];
41
41
 
42
42
  MYISAM_SHARE *share=info->s;
 
43
  DBUG_ENTER("mi_delete");
43
44
 
44
45
        /* Test if record is in datafile */
 
46
 
 
47
  DBUG_EXECUTE_IF("myisam_pretend_crashed_table_on_usage",
 
48
                  mi_print_error(info->s, HA_ERR_CRASHED);
 
49
                  DBUG_RETURN(my_errno= HA_ERR_CRASHED););
 
50
  DBUG_EXECUTE_IF("my_error_test_undefined_error",
 
51
                  mi_print_error(info->s, INT_MAX);
 
52
                  DBUG_RETURN(my_errno= INT_MAX););
45
53
  if (!(info->update & HA_STATE_AKTIV))
46
54
  {
47
 
    return(my_errno=HA_ERR_KEY_NOT_FOUND);      /* No database read */
 
55
    DBUG_RETURN(my_errno=HA_ERR_KEY_NOT_FOUND); /* No database read */
48
56
  }
49
57
  if (share->options & HA_OPTION_READ_ONLY_DATA)
50
58
  {
51
 
    return(my_errno=EACCES);
 
59
    DBUG_RETURN(my_errno=EACCES);
52
60
  }
53
61
  if (_mi_readinfo(info,F_WRLCK,1))
54
 
    return(my_errno);
 
62
    DBUG_RETURN(my_errno);
55
63
  if (info->s->calc_checksum)
56
64
    info->checksum=(*info->s->calc_checksum)(info,record);
57
65
  if ((*share->compare_record)(info,record))
68
76
    if (mi_is_key_active(info->s->state.key_map, i))
69
77
    {
70
78
      info->s->keyinfo[i].version++;
 
79
      if (info->s->keyinfo[i].flag & HA_FULLTEXT )
 
80
      {
 
81
        if (_mi_ft_del(info,i, old_key,record,info->lastpos))
 
82
          goto err;
 
83
      }
 
84
      else
71
85
      {
72
86
        if (info->s->keyinfo[i].ck_delete(info,i,old_key,
73
87
                _mi_make_key(info,i,old_key,record,info->lastpos)))
86
100
  info->state->records--;
87
101
 
88
102
  mi_sizestore(lastpos,info->lastpos);
89
 
  _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
 
103
  myisam_log_command(MI_LOG_DELETE,info,(uchar*) lastpos,sizeof(lastpos),0);
 
104
  VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
 
105
  allow_break();                        /* Allow SIGHUP & SIGINT */
90
106
  if (info->invalidator != 0)
91
107
  {
 
108
    DBUG_PRINT("info", ("invalidator... '%s' (delete)", info->filename));
92
109
    (*info->invalidator)(info->filename);
93
110
    info->invalidator=0;
94
111
  }
95
 
  return(0);
 
112
  DBUG_RETURN(0);
96
113
 
97
114
err:
98
115
  save_errno=my_errno;
99
116
  mi_sizestore(lastpos,info->lastpos);
 
117
  myisam_log_command(MI_LOG_DELETE,info,(uchar*) lastpos, sizeof(lastpos),0);
100
118
  if (save_errno != HA_ERR_RECORD_CHANGED)
101
119
  {
102
120
    mi_print_error(info->s, HA_ERR_CRASHED);
103
121
    mi_mark_crashed(info);              /* mark table crashed */
104
122
  }
105
 
  _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
 
123
  VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
106
124
  info->update|=HA_STATE_WRITTEN;       /* Buffer changed */
 
125
  allow_break();                        /* Allow SIGHUP & SIGINT */
107
126
  my_errno=save_errno;
108
127
  if (save_errno == HA_ERR_KEY_NOT_FOUND)
109
128
  {
111
130
    my_errno=HA_ERR_CRASHED;
112
131
  }
113
132
 
114
 
  return(my_errno);
 
133
  DBUG_RETURN(my_errno);
115
134
} /* mi_delete */
116
135
 
117
136
 
118
137
        /* Remove a key from the btree index */
119
138
 
120
 
int _mi_ck_delete(register MI_INFO *info, uint32_t keynr, unsigned char *key,
121
 
                  uint32_t key_length)
 
139
int _mi_ck_delete(register MI_INFO *info, uint keynr, uchar *key,
 
140
                  uint key_length)
122
141
{
123
142
  return _mi_ck_real_delete(info, info->s->keyinfo+keynr, key, key_length,
124
143
                            &info->s->state.key_root[keynr]);
126
145
 
127
146
 
128
147
static int _mi_ck_real_delete(register MI_INFO *info, MI_KEYDEF *keyinfo,
129
 
                              unsigned char *key, uint32_t key_length, my_off_t *root)
 
148
                              uchar *key, uint key_length, my_off_t *root)
130
149
{
131
150
  int error;
132
 
  uint32_t nod_flag;
 
151
  uint nod_flag;
133
152
  my_off_t old_root;
134
 
  unsigned char *root_buff;
 
153
  uchar *root_buff;
 
154
  DBUG_ENTER("_mi_ck_real_delete");
135
155
 
136
156
  if ((old_root=*root) == HA_OFFSET_ERROR)
137
157
  {
138
158
    mi_print_error(info->s, HA_ERR_CRASHED);
139
 
    return(my_errno=HA_ERR_CRASHED);
 
159
    DBUG_RETURN(my_errno=HA_ERR_CRASHED);
140
160
  }
141
 
  if (!(root_buff= (unsigned char*) my_alloca((uint) keyinfo->block_length+
 
161
  if (!(root_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
142
162
                                      MI_MAX_KEY_BUFF*2)))
143
163
  {
144
 
    return(my_errno=ENOMEM);
 
164
    DBUG_PRINT("error",("Couldn't allocate memory"));
 
165
    DBUG_RETURN(my_errno=ENOMEM);
145
166
  }
 
167
  DBUG_PRINT("info",("root_page: %ld", (long) old_root));
146
168
  if (!_mi_fetch_keypage(info,keyinfo,old_root,DFLT_INIT_HITS,root_buff,0))
147
169
  {
148
170
    error= -1;
149
171
    goto err;
150
172
  }
151
 
  if ((error=d_search(info,keyinfo, (SEARCH_SAME), key,key_length,old_root,root_buff)) > 0)
 
173
  if ((error=d_search(info,keyinfo,
 
174
                      (keyinfo->flag & HA_FULLTEXT ? SEARCH_FIND | SEARCH_UPDATE
 
175
                                                   : SEARCH_SAME),
 
176
                       key,key_length,old_root,root_buff)) >0)
152
177
  {
153
178
    if (error == 2)
154
179
    {
 
180
      DBUG_PRINT("test",("Enlarging of root when deleting"));
155
181
      error=_mi_enlarge_root(info,keyinfo,key,root);
156
182
    }
157
183
    else /* error == 1 */
172
198
    }
173
199
  }
174
200
err:
175
 
  my_afree((unsigned char*) root_buff);
176
 
  return(error);
 
201
  my_afree((uchar*) root_buff);
 
202
  DBUG_PRINT("exit",("Return: %d",error));
 
203
  DBUG_RETURN(error);
177
204
} /* _mi_ck_real_delete */
178
205
 
179
206
 
186
213
        */
187
214
 
188
215
static int d_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
189
 
                    uint32_t comp_flag, unsigned char *key, uint32_t key_length,
190
 
                    my_off_t page, unsigned char *anc_buff)
 
216
                    uint comp_flag, uchar *key, uint key_length,
 
217
                    my_off_t page, uchar *anc_buff)
191
218
{
192
219
  int flag,ret_value,save_flag;
193
 
  uint32_t length,nod_flag,search_key_length;
194
 
  bool last_key;
195
 
  unsigned char *leaf_buff,*keypos;
 
220
  uint length,nod_flag,search_key_length;
 
221
  my_bool last_key;
 
222
  uchar *leaf_buff,*keypos;
196
223
  my_off_t leaf_page= 0, next_block;
197
 
  unsigned char lastkey[MI_MAX_KEY_BUFF];
 
224
  uchar lastkey[MI_MAX_KEY_BUFF];
 
225
  DBUG_ENTER("d_search");
 
226
  DBUG_DUMP("page",(uchar*) anc_buff,mi_getint(anc_buff));
198
227
 
199
228
  search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
200
229
  flag=(*keyinfo->bin_search)(info,keyinfo,anc_buff,key, search_key_length,
201
230
                              comp_flag, &keypos, lastkey, &last_key);
202
231
  if (flag == MI_FOUND_WRONG_KEY)
203
232
  {
204
 
    return(-1);
 
233
    DBUG_PRINT("error",("Found wrong key"));
 
234
    DBUG_RETURN(-1);
205
235
  }
206
236
  nod_flag=mi_test_if_nod(anc_buff);
207
237
 
 
238
  if (!flag && keyinfo->flag & HA_FULLTEXT)
 
239
  {
 
240
    uint off;
 
241
    int  subkeys;
 
242
 
 
243
    get_key_full_length_rdonly(off, lastkey);
 
244
    subkeys=ft_sintXkorr(lastkey+off);
 
245
    DBUG_ASSERT(info->ft1_to_ft2==0 || subkeys >=0);
 
246
    comp_flag=SEARCH_SAME;
 
247
    if (subkeys >= 0)
 
248
    {
 
249
      /* normal word, one-level tree structure */
 
250
      if (info->ft1_to_ft2)
 
251
      {
 
252
        /* we're in ft1->ft2 conversion mode. Saving key data */
 
253
        insert_dynamic(info->ft1_to_ft2, (lastkey+off));
 
254
      }
 
255
      else
 
256
      {
 
257
        /* we need exact match only if not in ft1->ft2 conversion mode */
 
258
        flag=(*keyinfo->bin_search)(info,keyinfo,anc_buff,key,USE_WHOLE_KEY,
 
259
                                    comp_flag, &keypos, lastkey, &last_key);
 
260
      }
 
261
      /* fall through to normal delete */
 
262
    }
 
263
    else
 
264
    {
 
265
      /* popular word. two-level tree. going down */
 
266
      uint tmp_key_length;
 
267
      my_off_t root;
 
268
      uchar *kpos=keypos;
 
269
 
 
270
      if (!(tmp_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&kpos,lastkey)))
 
271
      {
 
272
        mi_print_error(info->s, HA_ERR_CRASHED);
 
273
        my_errno= HA_ERR_CRASHED;
 
274
        DBUG_RETURN(-1);
 
275
      }
 
276
      root=_mi_dpos(info,nod_flag,kpos);
 
277
      if (subkeys == -1)
 
278
      {
 
279
        /* the last entry in sub-tree */
 
280
        if (_mi_dispose(info, keyinfo, root,DFLT_INIT_HITS))
 
281
          DBUG_RETURN(-1);
 
282
        /* fall through to normal delete */
 
283
      }
 
284
      else
 
285
      {
 
286
        keyinfo=&info->s->ft2_keyinfo;
 
287
        kpos-=keyinfo->keylength+nod_flag; /* we'll modify key entry 'in vivo' */
 
288
        get_key_full_length_rdonly(off, key);
 
289
        key+=off;
 
290
        ret_value=_mi_ck_real_delete(info, &info->s->ft2_keyinfo,
 
291
            key, HA_FT_WLEN, &root);
 
292
        _mi_dpointer(info, kpos+HA_FT_WLEN, root);
 
293
        subkeys++;
 
294
        ft_intXstore(kpos, subkeys);
 
295
        if (!ret_value)
 
296
          ret_value=_mi_write_keypage(info,keyinfo,page,
 
297
                                      DFLT_INIT_HITS,anc_buff);
 
298
        DBUG_PRINT("exit",("Return: %d",ret_value));
 
299
        DBUG_RETURN(ret_value);
 
300
      }
 
301
    }
 
302
  }
208
303
  leaf_buff= 0;
209
304
  if (nod_flag)
210
305
  {
211
306
    leaf_page=_mi_kpos(nod_flag,keypos);
212
 
    if (!(leaf_buff= (unsigned char*) my_alloca((uint) keyinfo->block_length+
 
307
    if (!(leaf_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
213
308
                                        MI_MAX_KEY_BUFF*2)))
214
309
    {
 
310
      DBUG_PRINT("error",("Couldn't allocate memory"));
215
311
      my_errno=ENOMEM;
216
 
      return(-1);
 
312
      DBUG_PRINT("exit",("Return: %d",-1));
 
313
      DBUG_RETURN(-1);
217
314
    }
218
315
    if (!_mi_fetch_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff,0))
219
316
      goto err;
223
320
  {
224
321
    if (!nod_flag)
225
322
    {
 
323
      DBUG_PRINT("error",("Didn't find key"));
226
324
      mi_print_error(info->s, HA_ERR_CRASHED);
227
325
      my_errno=HA_ERR_CRASHED;          /* This should newer happend */
228
326
      goto err;
233
331
  }
234
332
  else
235
333
  {                                             /* Found key */
236
 
    uint32_t tmp;
 
334
    uint tmp;
237
335
    length=mi_getint(anc_buff);
238
336
    if (!(tmp= remove_key(keyinfo,nod_flag,keypos,lastkey,anc_buff+length,
239
337
                          &next_block)))
246
344
    {                                           /* On leaf page */
247
345
      if (_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,anc_buff))
248
346
      {
249
 
        return(-1);
 
347
        DBUG_PRINT("exit",("Return: %d",-1));
 
348
        DBUG_RETURN(-1);
250
349
      }
251
350
      /* Page will be update later if we return 1 */
252
 
      return(test(length <= (info->quick_mode ? MI_MIN_KEYBLOCK_LENGTH :
 
351
      DBUG_RETURN(test(length <= (info->quick_mode ? MI_MIN_KEYBLOCK_LENGTH :
253
352
                                  (uint) keyinfo->underflow_block_length)));
254
353
    }
255
354
    save_flag=1;
263
362
      ret_value= underflow(info,keyinfo,anc_buff,leaf_page,leaf_buff,keypos);
264
363
    else
265
364
    {                           /* This happens only with packed keys */
 
365
      DBUG_PRINT("test",("Enlarging of key when deleting"));
266
366
      if (!_mi_get_last_key(info,keyinfo,anc_buff,lastkey,keypos,&length))
267
367
      {
268
368
        goto err;
269
369
      }
270
370
      ret_value=_mi_insert(info,keyinfo,key,anc_buff,keypos,lastkey,
271
 
                           (unsigned char*) 0,(unsigned char*) 0,(my_off_t) 0,(bool) 0);
 
371
                           (uchar*) 0,(uchar*) 0,(my_off_t) 0,(my_bool) 0);
272
372
    }
273
373
  }
274
374
  if (ret_value == 0 && mi_getint(anc_buff) > keyinfo->block_length)
278
378
  }
279
379
  if (save_flag && ret_value != 1)
280
380
    ret_value|=_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,anc_buff);
281
 
  my_afree((unsigned char*) leaf_buff);
282
 
  return(ret_value);
 
381
  else
 
382
  {
 
383
    DBUG_DUMP("page",(uchar*) anc_buff,mi_getint(anc_buff));
 
384
  }
 
385
  my_afree((uchar*) leaf_buff);
 
386
  DBUG_PRINT("exit",("Return: %d",ret_value));
 
387
  DBUG_RETURN(ret_value);
283
388
 
284
389
err:
285
 
  my_afree((unsigned char*) leaf_buff);
286
 
  return (-1);
 
390
  my_afree((uchar*) leaf_buff);
 
391
  DBUG_PRINT("exit",("Error: %d",my_errno));
 
392
  DBUG_RETURN (-1);
287
393
} /* d_search */
288
394
 
289
395
 
290
396
        /* Remove a key that has a page-reference */
291
397
 
292
 
static int del(register MI_INFO *info, register MI_KEYDEF *keyinfo, unsigned char *key,
293
 
               unsigned char *anc_buff, my_off_t leaf_page, unsigned char *leaf_buff,
294
 
               unsigned char *keypos,           /* Pos to where deleted key was */
 
398
static int del(register MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *key,
 
399
               uchar *anc_buff, my_off_t leaf_page, uchar *leaf_buff,
 
400
               uchar *keypos,           /* Pos to where deleted key was */
295
401
               my_off_t next_block,
296
 
               unsigned char *ret_key)          /* key before keypos in anc_buff */
 
402
               uchar *ret_key)          /* key before keypos in anc_buff */
297
403
{
298
404
  int ret_value,length;
299
 
  uint32_t a_length,nod_flag,tmp;
 
405
  uint a_length,nod_flag,tmp;
300
406
  my_off_t next_page;
301
 
  unsigned char keybuff[MI_MAX_KEY_BUFF],*endpos,*next_buff,*key_start, *prev_key;
 
407
  uchar keybuff[MI_MAX_KEY_BUFF],*endpos,*next_buff,*key_start, *prev_key;
302
408
  MYISAM_SHARE *share=info->s;
303
409
  MI_KEY_PARAM s_temp;
 
410
  DBUG_ENTER("del");
 
411
  DBUG_PRINT("enter",("leaf_page: %ld  keypos: 0x%lx", (long) leaf_page,
 
412
                      (ulong) keypos));
 
413
  DBUG_DUMP("leaf_buff",(uchar*) leaf_buff,mi_getint(leaf_buff));
304
414
 
305
415
  endpos=leaf_buff+mi_getint(leaf_buff);
306
416
  if (!(key_start=_mi_get_last_key(info,keyinfo,leaf_buff,keybuff,endpos,
307
417
                                   &tmp)))
308
 
    return(-1);
 
418
    DBUG_RETURN(-1);
309
419
 
310
420
  if ((nod_flag=mi_test_if_nod(leaf_buff)))
311
421
  {
312
422
    next_page= _mi_kpos(nod_flag,endpos);
313
 
    if (!(next_buff= (unsigned char*) my_alloca((uint) keyinfo->block_length+
 
423
    if (!(next_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
314
424
                                        MI_MAX_KEY_BUFF*2)))
315
 
      return(-1);
 
425
      DBUG_RETURN(-1);
316
426
    if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,next_buff,0))
317
427
      ret_value= -1;
318
428
    else
319
429
    {
 
430
      DBUG_DUMP("next_page",(uchar*) next_buff,mi_getint(next_buff));
320
431
      if ((ret_value=del(info,keyinfo,key,anc_buff,next_page,next_buff,
321
432
                         keypos,next_block,ret_key)) >0)
322
433
      {
332
443
        }
333
444
        else
334
445
        {
 
446
          DBUG_PRINT("test",("Inserting of key when deleting"));
335
447
          if (!_mi_get_last_key(info,keyinfo,leaf_buff,keybuff,endpos,
336
448
                                &tmp))
337
449
            goto err;
338
450
          ret_value=_mi_insert(info,keyinfo,key,leaf_buff,endpos,keybuff,
339
 
                               (unsigned char*) 0,(unsigned char*) 0,(my_off_t) 0,0);
 
451
                               (uchar*) 0,(uchar*) 0,(my_off_t) 0,0);
340
452
        }
341
453
      }
342
454
      if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
343
455
        goto err;
344
456
    }
345
 
    my_afree((unsigned char*) next_buff);
346
 
    return(ret_value);
 
457
    my_afree((uchar*) next_buff);
 
458
    DBUG_RETURN(ret_value);
347
459
  }
348
460
 
349
461
        /* Remove last key from leaf page */
362
474
  prev_key=(keypos == anc_buff+2+share->base.key_reflength ?
363
475
            0 : ret_key);
364
476
  length=(*keyinfo->pack_key)(keyinfo,share->base.key_reflength,
365
 
                              keypos == endpos ? (unsigned char*) 0 : keypos,
 
477
                              keypos == endpos ? (uchar*) 0 : keypos,
366
478
                              prev_key, prev_key,
367
479
                              keybuff,&s_temp);
368
480
  if (length > 0)
369
 
    bmove_upp((unsigned char*) endpos+length,(unsigned char*) endpos,(uint) (endpos-keypos));
 
481
    bmove_upp((uchar*) endpos+length,(uchar*) endpos,(uint) (endpos-keypos));
370
482
  else
371
 
    memcpy(keypos,keypos-length, (int) (endpos-keypos)+length);
 
483
    bmove(keypos,keypos-length, (int) (endpos-keypos)+length);
372
484
  (*keyinfo->store_key)(keyinfo,keypos,&s_temp);
373
485
  /* Save pointer to next leaf */
374
486
  if (!(*keyinfo->get_key)(keyinfo,share->base.key_reflength,&keypos,ret_key))
376
488
  _mi_kpointer(info,keypos - share->base.key_reflength,next_block);
377
489
  mi_putint(anc_buff,a_length+length,share->base.key_reflength);
378
490
 
379
 
  return( mi_getint(leaf_buff) <=
 
491
  DBUG_RETURN( mi_getint(leaf_buff) <=
380
492
               (info->quick_mode ? MI_MIN_KEYBLOCK_LENGTH :
381
493
                (uint) keyinfo->underflow_block_length));
382
494
err:
383
 
  return(-1);
 
495
  DBUG_RETURN(-1);
384
496
} /* del */
385
497
 
386
498
 
387
499
        /* Balances adjacent pages if underflow occours */
388
500
 
389
501
static int underflow(register MI_INFO *info, register MI_KEYDEF *keyinfo,
390
 
                     unsigned char *anc_buff,
 
502
                     uchar *anc_buff,
391
503
                     my_off_t leaf_page,/* Ancestor page and underflow page */
392
 
                     unsigned char *leaf_buff,
393
 
                     unsigned char *keypos)     /* Position to pos after key */
 
504
                     uchar *leaf_buff,
 
505
                     uchar *keypos)     /* Position to pos after key */
394
506
{
395
507
  int t_length;
396
 
  uint32_t length,anc_length,buff_length,leaf_length,p_length,s_length,nod_flag,
 
508
  uint length,anc_length,buff_length,leaf_length,p_length,s_length,nod_flag,
397
509
       key_reflength,key_length;
398
510
  my_off_t next_page;
399
 
  unsigned char anc_key[MI_MAX_KEY_BUFF],leaf_key[MI_MAX_KEY_BUFF],
 
511
  uchar anc_key[MI_MAX_KEY_BUFF],leaf_key[MI_MAX_KEY_BUFF],
400
512
        *buff,*endpos,*next_keypos,*anc_pos,*half_pos,*temp_pos,*prev_key,
401
513
        *after_key;
402
514
  MI_KEY_PARAM s_temp;
403
515
  MYISAM_SHARE *share=info->s;
 
516
  DBUG_ENTER("underflow");
 
517
  DBUG_PRINT("enter",("leaf_page: %ld  keypos: 0x%lx",(long) leaf_page,
 
518
                      (ulong) keypos));
 
519
  DBUG_DUMP("anc_buff",(uchar*) anc_buff,mi_getint(anc_buff));
 
520
  DBUG_DUMP("leaf_buff",(uchar*) leaf_buff,mi_getint(leaf_buff));
404
521
 
405
522
  buff=info->buff;
406
523
  info->buff_used=1;
416
533
  if ((keypos < anc_buff+anc_length && (info->state->records & 1)) ||
417
534
      keypos == anc_buff+2+key_reflength)
418
535
  {                                     /* Use page right of anc-page */
 
536
    DBUG_PRINT("test",("use right page"));
 
537
 
419
538
    if (keyinfo->flag & HA_BINARY_PACK_KEY)
420
539
    {
421
540
      if (!(next_keypos=_mi_get_key(info, keyinfo,
434
553
    if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,buff,0))
435
554
      goto err;
436
555
    buff_length=mi_getint(buff);
 
556
    DBUG_DUMP("next",(uchar*) buff,buff_length);
437
557
 
438
558
    /* find keys to make a big key-page */
439
 
    memcpy(next_keypos - key_reflength, buff + 2, key_reflength);
 
559
    bmove((uchar*) next_keypos-key_reflength,(uchar*) buff+2,
 
560
          key_reflength);
440
561
    if (!_mi_get_last_key(info,keyinfo,anc_buff,anc_key,next_keypos,&length)
441
562
        || !_mi_get_last_key(info,keyinfo,leaf_buff,leaf_key,
442
563
                             leaf_buff+leaf_length,&length))
443
564
      goto err;
444
565
 
445
566
    /* merge pages and put parting key from anc_buff between */
446
 
    prev_key=(leaf_length == p_length ? (unsigned char*) 0 : leaf_key);
 
567
    prev_key=(leaf_length == p_length ? (uchar*) 0 : leaf_key);
447
568
    t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,buff+p_length,
448
569
                                  prev_key, prev_key,
449
570
                                  anc_key, &s_temp);
450
571
    length=buff_length-p_length;
451
572
    endpos=buff+length+leaf_length+t_length;
452
573
    /* buff will always be larger than before !*/
453
 
    bmove_upp((unsigned char*) endpos, (unsigned char*) buff+buff_length,length);
454
 
    memcpy(buff, leaf_buff, leaf_length);
 
574
    bmove_upp((uchar*) endpos, (uchar*) buff+buff_length,length);
 
575
    memcpy((uchar*) buff, (uchar*) leaf_buff,(size_t) leaf_length);
455
576
    (*keyinfo->store_key)(keyinfo,buff+leaf_length,&s_temp);
456
577
    buff_length=(uint) (endpos-buff);
457
578
    mi_putint(buff,buff_length,nod_flag);
467
588
 
468
589
    if (buff_length <= keyinfo->block_length)
469
590
    {                                           /* Keys in one page */
470
 
      memcpy(leaf_buff, buff, buff_length);
 
591
      memcpy((uchar*) leaf_buff,(uchar*) buff,(size_t) buff_length);
471
592
      if (_mi_dispose(info,keyinfo,next_page,DFLT_INIT_HITS))
472
593
       goto err;
473
594
    }
474
595
    else
475
596
    {                                           /* Page is full */
476
597
      endpos=anc_buff+anc_length;
 
598
      DBUG_PRINT("test",("anc_buff: 0x%lx  endpos: 0x%lx",
 
599
                         (long) anc_buff, (long) endpos));
477
600
      if (keypos != anc_buff+2+key_reflength &&
478
601
          !_mi_get_last_key(info,keyinfo,anc_buff,anc_key,keypos,&length))
479
602
        goto err;
481
604
                                       &key_length, &after_key)))
482
605
        goto err;
483
606
      length=(uint) (half_pos-buff);
484
 
      memcpy(leaf_buff, buff, length);
 
607
      memcpy((uchar*) leaf_buff,(uchar*) buff,(size_t) length);
485
608
      mi_putint(leaf_buff,length,nod_flag);
486
609
 
487
610
      /* Correct new keypointer to leaf_page */
488
611
      half_pos=after_key;
489
612
      _mi_kpointer(info,leaf_key+key_length,next_page);
490
613
      /* Save key in anc_buff */
491
 
      prev_key=(keypos == anc_buff+2+key_reflength ? (unsigned char*) 0 : anc_key),
 
614
      prev_key=(keypos == anc_buff+2+key_reflength ? (uchar*) 0 : anc_key),
492
615
      t_length=(*keyinfo->pack_key)(keyinfo,key_reflength,
493
 
                                    (keypos == endpos ? (unsigned char*) 0 :
 
616
                                    (keypos == endpos ? (uchar*) 0 :
494
617
                                     keypos),
495
618
                                    prev_key, prev_key,
496
619
                                    leaf_key, &s_temp);
497
620
      if (t_length >= 0)
498
 
        bmove_upp((unsigned char*) endpos+t_length,(unsigned char*) endpos,
 
621
        bmove_upp((uchar*) endpos+t_length,(uchar*) endpos,
499
622
                  (uint) (endpos-keypos));
500
623
      else
501
 
        memcpy(keypos,keypos-t_length,(uint) (endpos-keypos)+t_length);
 
624
        bmove(keypos,keypos-t_length,(uint) (endpos-keypos)+t_length);
502
625
      (*keyinfo->store_key)(keyinfo,keypos,&s_temp);
503
626
      mi_putint(anc_buff,(anc_length+=t_length),key_reflength);
504
627
 
505
628
        /* Store key first in new page */
506
629
      if (nod_flag)
507
 
        memcpy(buff + 2, half_pos - nod_flag, nod_flag);
 
630
        bmove((uchar*) buff+2,(uchar*) half_pos-nod_flag,(size_t) nod_flag);
508
631
      if (!(*keyinfo->get_key)(keyinfo,nod_flag,&half_pos,leaf_key))
509
632
        goto err;
510
 
      t_length=(int) (*keyinfo->pack_key)(keyinfo, nod_flag, (unsigned char*) 0,
511
 
                                          (unsigned char*) 0, (unsigned char *) 0,
 
633
      t_length=(int) (*keyinfo->pack_key)(keyinfo, nod_flag, (uchar*) 0,
 
634
                                          (uchar*) 0, (uchar *) 0,
512
635
                                          leaf_key, &s_temp);
513
636
      /* t_length will always be > 0 for a new page !*/
514
637
      length=(uint) ((buff+mi_getint(buff))-half_pos);
515
 
      memcpy(buff + p_length + t_length, half_pos, length);
 
638
      bmove((uchar*) buff+p_length+t_length,(uchar*) half_pos,(size_t) length);
516
639
      (*keyinfo->store_key)(keyinfo,buff+p_length,&s_temp);
517
640
      mi_putint(buff,length+t_length+p_length,nod_flag);
518
641
 
521
644
    }
522
645
    if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
523
646
      goto err;
524
 
    return(anc_length <= ((info->quick_mode ? MI_MIN_BLOCK_LENGTH :
 
647
    DBUG_RETURN(anc_length <= ((info->quick_mode ? MI_MIN_BLOCK_LENGTH :
525
648
                                (uint) keyinfo->underflow_block_length)));
526
649
  }
527
650
 
 
651
  DBUG_PRINT("test",("use left page"));
 
652
 
528
653
  keypos=_mi_get_last_key(info,keyinfo,anc_buff,anc_key,keypos,&length);
529
654
  if (!keypos)
530
655
    goto err;
533
658
      goto err;
534
659
  buff_length=mi_getint(buff);
535
660
  endpos=buff+buff_length;
 
661
  DBUG_DUMP("prev",(uchar*) buff,buff_length);
536
662
 
537
663
  /* find keys to make a big key-page */
538
 
  memcpy(next_keypos - key_reflength, leaf_buff+2, key_reflength);
 
664
  bmove((uchar*) next_keypos - key_reflength,(uchar*) leaf_buff+2,
 
665
        key_reflength);
539
666
  next_keypos=keypos;
540
667
  if (!(*keyinfo->get_key)(keyinfo,key_reflength,&next_keypos,
541
668
                           anc_key))
544
671
    goto err;
545
672
 
546
673
  /* merge pages and put parting key from anc_buff between */
547
 
  prev_key=(leaf_length == p_length ? (unsigned char*) 0 : leaf_key);
 
674
  prev_key=(leaf_length == p_length ? (uchar*) 0 : leaf_key);
548
675
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,
549
676
                                (leaf_length == p_length ?
550
 
                                 (unsigned char*) 0 : leaf_buff+p_length),
 
677
                                 (uchar*) 0 : leaf_buff+p_length),
551
678
                                prev_key, prev_key,
552
679
                                anc_key, &s_temp);
553
680
  if (t_length >= 0)
554
 
    memcpy(endpos+t_length,leaf_buff+p_length, leaf_length-p_length);
 
681
    bmove((uchar*) endpos+t_length,(uchar*) leaf_buff+p_length,
 
682
            (size_t) (leaf_length-p_length));
555
683
  else                                          /* We gained space */
556
 
    memcpy(endpos, leaf_buff+((int) p_length-t_length),
557
 
           leaf_length - p_length + t_length);
 
684
    bmove((uchar*) endpos,(uchar*) leaf_buff+((int) p_length-t_length),
 
685
          (size_t) (leaf_length-p_length+t_length));
558
686
 
559
687
  (*keyinfo->store_key)(keyinfo,endpos,&s_temp);
560
688
  buff_length=buff_length+leaf_length-p_length+t_length;
586
714
      goto err;
587
715
    _mi_kpointer(info,leaf_key+key_length,leaf_page);
588
716
    /* Save key in anc_buff */
 
717
    DBUG_DUMP("anc_buff",(uchar*) anc_buff,anc_length);
 
718
    DBUG_DUMP("key_to_anc",(uchar*) leaf_key,key_length);
589
719
 
590
720
    temp_pos=anc_buff+anc_length;
591
721
    t_length=(*keyinfo->pack_key)(keyinfo,key_reflength,
592
 
                                  keypos == temp_pos ? (unsigned char*) 0
 
722
                                  keypos == temp_pos ? (uchar*) 0
593
723
                                  : keypos,
594
724
                                  anc_pos, anc_pos,
595
725
                                  leaf_key,&s_temp);
596
726
    if (t_length > 0)
597
 
      bmove_upp((unsigned char*) temp_pos+t_length,(unsigned char*) temp_pos,
 
727
      bmove_upp((uchar*) temp_pos+t_length,(uchar*) temp_pos,
598
728
                (uint) (temp_pos-keypos));
599
729
    else
600
 
      memcpy(keypos,keypos-t_length,(uint) (temp_pos-keypos)+t_length);
 
730
      bmove(keypos,keypos-t_length,(uint) (temp_pos-keypos)+t_length);
601
731
    (*keyinfo->store_key)(keyinfo,keypos,&s_temp);
602
732
    mi_putint(anc_buff,(anc_length+=t_length),key_reflength);
603
733
 
604
734
    /* Store first key on new page */
605
735
    if (nod_flag)
606
 
      memcpy(leaf_buff+2, half_pos - nod_flag, nod_flag);
 
736
      bmove((uchar*) leaf_buff+2,(uchar*) half_pos-nod_flag,(size_t) nod_flag);
607
737
    if (!(length=(*keyinfo->get_key)(keyinfo,nod_flag,&half_pos,leaf_key)))
608
738
      goto err;
609
 
    t_length=(*keyinfo->pack_key)(keyinfo,nod_flag, (unsigned char*) 0,
610
 
                                  (unsigned char*) 0, (unsigned char*) 0, leaf_key, &s_temp);
 
739
    DBUG_DUMP("key_to_leaf",(uchar*) leaf_key,length);
 
740
    t_length=(*keyinfo->pack_key)(keyinfo,nod_flag, (uchar*) 0,
 
741
                                  (uchar*) 0, (uchar*) 0, leaf_key, &s_temp);
611
742
    length=(uint) ((buff+buff_length)-half_pos);
612
 
    memcpy(leaf_buff + p_length + t_length, half_pos, length);
 
743
    DBUG_PRINT("info",("t_length: %d  length: %d",t_length,(int) length));
 
744
    bmove((uchar*) leaf_buff+p_length+t_length,(uchar*) half_pos,
 
745
          (size_t) length);
613
746
    (*keyinfo->store_key)(keyinfo,leaf_buff+p_length,&s_temp);
614
747
    mi_putint(leaf_buff,length+t_length+p_length,nod_flag);
615
748
    if (_mi_write_keypage(info,keyinfo,leaf_page,DFLT_INIT_HITS,leaf_buff))
618
751
  }
619
752
  if (_mi_write_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,buff))
620
753
    goto err;
621
 
  return(anc_length <= (uint) keyinfo->block_length/2);
 
754
  DBUG_RETURN(anc_length <= (uint) keyinfo->block_length/2);
622
755
 
623
756
err:
624
 
  return(-1);
 
757
  DBUG_RETURN(-1);
625
758
} /* underflow */
626
759
 
627
760
 
632
765
          returns how many chars was removed or 0 on error
633
766
        */
634
767
 
635
 
static uint32_t remove_key(MI_KEYDEF *keyinfo, uint32_t nod_flag,
636
 
                       unsigned char *keypos,   /* Where key starts */
637
 
                       unsigned char *lastkey,  /* key to be removed */
638
 
                       unsigned char *page_end, /* End of page */
 
768
static uint remove_key(MI_KEYDEF *keyinfo, uint nod_flag,
 
769
                       uchar *keypos,   /* Where key starts */
 
770
                       uchar *lastkey,  /* key to be removed */
 
771
                       uchar *page_end, /* End of page */
639
772
                       my_off_t *next_block)    /* ptr to next block */
640
773
{
641
774
  int s_length;
642
 
  unsigned char *start;
 
775
  uchar *start;
 
776
  DBUG_ENTER("remove_key");
 
777
  DBUG_PRINT("enter",("keypos: 0x%lx  page_end: 0x%lx",(long) keypos, (long) page_end));
643
778
 
644
779
  start=keypos;
645
780
  if (!(keyinfo->flag &
654
789
  {                                      /* Let keypos point at next key */
655
790
    /* Calculate length of key */
656
791
    if (!(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,lastkey))
657
 
      return(0);                                /* Error */
 
792
      DBUG_RETURN(0);                           /* Error */
658
793
 
659
794
    if (next_block && nod_flag)
660
795
      *next_block= _mi_kpos(nod_flag,keypos);
663
798
    {
664
799
      if (keyinfo->flag & HA_BINARY_PACK_KEY)
665
800
      {
666
 
        unsigned char *old_key=start;
667
 
        uint32_t next_length,prev_length,prev_pack_length;
 
801
        uchar *old_key=start;
 
802
        uint next_length,prev_length,prev_pack_length;
668
803
        get_key_length(next_length,keypos);
669
804
        get_key_pack_length(prev_length,prev_pack_length,old_key);
670
805
        if (next_length > prev_length)
683
818
        if ((keyinfo->seg->flag & HA_PACK_KEY) && *keypos & 128)
684
819
        {
685
820
          /* Next key is packed against the current one */
686
 
          uint32_t next_length,prev_length,prev_pack_length,lastkey_length,
 
821
          uint next_length,prev_length,prev_pack_length,lastkey_length,
687
822
            rest_length;
688
823
          if (keyinfo->seg[0].length >= 127)
689
824
          {
716
851
 
717
852
          if (next_length >= prev_length)
718
853
          {             /* Key after is based on deleted key */
719
 
            uint32_t pack_length,tmp;
 
854
            uint pack_length,tmp;
720
855
            bmove_upp(keypos, (lastkey+next_length),
721
856
                      tmp=(next_length-prev_length));
722
857
            rest_length+=tmp;
749
884
      }
750
885
    }
751
886
  }
752
 
end:
753
 
  assert(page_end-start >= s_length);
754
 
  memcpy(start, start + s_length, page_end-start-s_length);
755
 
  return s_length;
 
887
  end:
 
888
  bmove((uchar*) start,(uchar*) start+s_length,
 
889
        (uint) (page_end-start-s_length));
 
890
  DBUG_RETURN((uint) s_length);
756
891
} /* remove_key */