~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_delete.c

  • Committer: Brian Aker
  • Date: 2008-07-07 14:25:25 UTC
  • mto: (77.1.25 codestyle)
  • mto: This revision was merged to the branch mainline in revision 82.
  • Revision ID: brian@tangent.org-20080707142525-xzy2nl3ie2ebwfln
LL() cleanup

Show diffs side-by-side

added added

removed removed

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