~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-05 18:10:38 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: monty@inaugust.com-20080705181038-0ih0nnamu5qrut0y
Fixed prototypes. Cleaned define a little bit.

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