~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_delete.c

Renamed more stuff to drizzle.

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