~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_write.c

  • Committer: Brian Aker
  • Date: 2008-07-13 19:39:24 UTC
  • Revision ID: brian@tangent.org-20080713193924-tf5yc33skcq224uf
More my_bool conversion. This time the set_var class.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
  my_off_t filepos;
48
48
  uchar *buff;
49
49
  my_bool lock_tree= share->concurrent_insert;
 
50
  DBUG_ENTER("mi_write");
 
51
  DBUG_PRINT("enter",("isam: %d  data: %d",info->s->kfile,info->dfile));
50
52
 
 
53
  DBUG_EXECUTE_IF("myisam_pretend_crashed_table_on_usage",
 
54
                  mi_print_error(info->s, HA_ERR_CRASHED);
 
55
                  DBUG_RETURN(my_errno= HA_ERR_CRASHED););
51
56
  if (share->options & HA_OPTION_READ_ONLY_DATA)
52
57
  {
53
 
    return(my_errno=EACCES);
 
58
    DBUG_RETURN(my_errno=EACCES);
54
59
  }
55
60
  if (_mi_readinfo(info,F_WRLCK,1))
56
 
    return(my_errno);
 
61
    DBUG_RETURN(my_errno);
 
62
#if !defined(NO_LOCKING) && defined(USE_RECORD_LOCK)
 
63
  if (!info->locked && my_lock(info->dfile,F_WRLCK,0L,F_TO_EOF,
 
64
                               MYF(MY_SEEK_NOT_DONE) | info->lock_wait))
 
65
    goto err;
 
66
#endif
57
67
  filepos= ((share->state.dellink != HA_OFFSET_ERROR &&
58
68
             !info->append_insert_at_end) ?
59
69
            share->state.dellink :
104
114
        {
105
115
          if (local_lock_tree)
106
116
            rw_unlock(&share->key_root_lock[i]);
 
117
          DBUG_PRINT("error",("Got error: %d on write",my_errno));
107
118
          goto err;
108
119
        }
109
120
      }
134
145
  VOID(_mi_writeinfo(info, WRITEINFO_UPDATE_KEYFILE));
135
146
  if (info->invalidator != 0)
136
147
  {
 
148
    DBUG_PRINT("info", ("invalidator... '%s' (update)", info->filename));
137
149
    (*info->invalidator)(info->filename);
138
150
    info->invalidator=0;
139
151
  }
149
161
  if (share->is_log_table)
150
162
    mi_update_status((void*) info);
151
163
 
152
 
  return(0);
 
164
  DBUG_RETURN(0);
153
165
 
154
166
err:
155
167
  save_errno=my_errno;
197
209
  save_errno=my_errno;
198
210
  myisam_log_record(MI_LOG_WRITE,info,record,filepos,my_errno);
199
211
  VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
200
 
  return(my_errno=save_errno);
 
212
  DBUG_RETURN(my_errno=save_errno);
201
213
} /* mi_write */
202
214
 
203
215
 
205
217
 
206
218
int _mi_ck_write(MI_INFO *info, uint keynr, uchar *key, uint key_length)
207
219
{
 
220
  DBUG_ENTER("_mi_ck_write");
 
221
 
208
222
  if (info->bulk_insert && is_tree_inited(&info->bulk_insert[keynr]))
209
223
  {
210
 
    return(_mi_ck_write_tree(info, keynr, key, key_length));
 
224
    DBUG_RETURN(_mi_ck_write_tree(info, keynr, key, key_length));
211
225
  }
212
226
  else
213
227
  {
214
 
    return(_mi_ck_write_btree(info, keynr, key, key_length));
 
228
    DBUG_RETURN(_mi_ck_write_btree(info, keynr, key, key_length));
215
229
  }
216
230
} /* _mi_ck_write */
217
231
 
227
241
  uint comp_flag;
228
242
  MI_KEYDEF *keyinfo=info->s->keyinfo+keynr;
229
243
  my_off_t  *root=&info->s->state.key_root[keynr];
 
244
  DBUG_ENTER("_mi_ck_write_btree");
230
245
 
231
246
  if (keyinfo->flag & HA_SORT_ALLOWS_SAME)
232
247
    comp_flag=SEARCH_BIGGER;                    /* Put after same key */
241
256
 
242
257
  error=_mi_ck_real_write_btree(info, keyinfo, key, key_length,
243
258
                                root, comp_flag);
244
 
  return(error);
 
259
  DBUG_RETURN(error);
245
260
} /* _mi_ck_write_btree */
246
261
 
247
262
int _mi_ck_real_write_btree(MI_INFO *info, MI_KEYDEF *keyinfo,
248
263
    uchar *key, uint key_length, my_off_t *root, uint comp_flag)
249
264
{
250
265
  int error;
 
266
  DBUG_ENTER("_mi_ck_real_write_btree");
251
267
  /* key_length parameter is used only if comp_flag is SEARCH_FIND */
252
268
  if (*root == HA_OFFSET_ERROR ||
253
269
      (error=w_search(info, keyinfo, comp_flag, key, key_length,
254
270
                      *root, (uchar *) 0, (uchar*) 0,
255
271
                      (my_off_t) 0, 1)) > 0)
256
272
    error=_mi_enlarge_root(info,keyinfo,key,root);
257
 
  return(error);
 
273
  DBUG_RETURN(error);
258
274
} /* _mi_ck_real_write_btree */
259
275
 
260
276
 
266
282
  uint t_length,nod_flag;
267
283
  MI_KEY_PARAM s_temp;
268
284
  MYISAM_SHARE *share=info->s;
 
285
  DBUG_ENTER("_mi_enlarge_root");
269
286
 
270
287
  nod_flag= (*root != HA_OFFSET_ERROR) ?  share->base.key_reflength : 0;
271
288
  _mi_kpointer(info,info->buff+2,*root); /* if nod */
276
293
  info->buff_used=info->page_changed=1;         /* info->buff is used */
277
294
  if ((*root= _mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR ||
278
295
      _mi_write_keypage(info,keyinfo,*root,DFLT_INIT_HITS,info->buff))
279
 
    return(-1);
280
 
  return(0);
 
296
    DBUG_RETURN(-1);
 
297
  DBUG_RETURN(0);
281
298
} /* _mi_enlarge_root */
282
299
 
283
300
 
299
316
  uchar keybuff[MI_MAX_KEY_BUFF];
300
317
  my_bool was_last_key;
301
318
  my_off_t next_page, dupp_key_pos;
 
319
  DBUG_ENTER("w_search");
 
320
  DBUG_PRINT("enter",("page: %ld", (long) page));
302
321
 
303
322
  search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
304
323
  if (!(temp_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
305
324
                                      MI_MAX_KEY_BUFF*2)))
306
 
    return(-1);
 
325
    DBUG_RETURN(-1);
307
326
  if (!_mi_fetch_keypage(info,keyinfo,page,DFLT_INIT_HITS,temp_buff,0))
308
327
    goto err;
309
328
 
324
343
      info->dupp_key_pos= dupp_key_pos;
325
344
      my_afree((uchar*) temp_buff);
326
345
      my_errno=HA_ERR_FOUND_DUPP_KEY;
327
 
      return(-1);
 
346
      DBUG_RETURN(-1);
328
347
    }
329
348
  }
330
349
  if (flag == MI_FOUND_WRONG_KEY)
331
 
    return(-1);
 
350
    DBUG_RETURN(-1);
332
351
  if (!was_last_key)
333
352
    insert_last=0;
334
353
  next_page=_mi_kpos(nod_flag,keypos);
342
361
      goto err;
343
362
  }
344
363
  my_afree((uchar*) temp_buff);
345
 
  return(error);
 
364
  DBUG_RETURN(error);
346
365
err:
347
366
  my_afree((uchar*) temp_buff);
348
 
  return (-1);
 
367
  DBUG_PRINT("exit",("Error: %d",my_errno));
 
368
  DBUG_RETURN (-1);
349
369
} /* w_search */
350
370
 
351
371
 
383
403
  int t_length;
384
404
  uchar *endpos, *prev_key;
385
405
  MI_KEY_PARAM s_temp;
 
406
  DBUG_ENTER("_mi_insert");
 
407
  DBUG_PRINT("enter",("key_pos: 0x%lx", (long) key_pos));
 
408
  DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,key,USE_WHOLE_KEY););
386
409
 
387
410
  nod_flag=mi_test_if_nod(anc_buff);
388
411
  a_length=mi_getint(anc_buff);
392
415
                                (key_pos == endpos ? (uchar*) 0 : key_pos),
393
416
                                prev_key, prev_key,
394
417
                                key,&s_temp);
395
 
 
 
418
#ifndef DBUG_OFF
 
419
  if (key_pos != anc_buff+2+nod_flag && (keyinfo->flag &
 
420
                                         (HA_BINARY_PACK_KEY | HA_PACK_KEY)))
 
421
  {
 
422
    DBUG_DUMP("prev_key",(uchar*) key_buff,_mi_keylength(keyinfo,key_buff));
 
423
  }
 
424
  if (keyinfo->flag & HA_PACK_KEY)
 
425
  {
 
426
    DBUG_PRINT("test",("t_length: %d  ref_len: %d",
 
427
                       t_length,s_temp.ref_length));
 
428
    DBUG_PRINT("test",("n_ref_len: %d  n_length: %d  key_pos: 0x%lx",
 
429
                       s_temp.n_ref_length,s_temp.n_length, (long) s_temp.key));
 
430
  }
 
431
#endif
396
432
  if (t_length > 0)
397
433
  {
398
434
    if (t_length >= keyinfo->maxlength*2+MAX_POINTER_LENGTH)
399
435
    {
400
436
      mi_print_error(info->s, HA_ERR_CRASHED);
401
437
      my_errno=HA_ERR_CRASHED;
402
 
      return(-1);
 
438
      DBUG_RETURN(-1);
403
439
    }
404
440
    bmove_upp((uchar*) endpos+t_length,(uchar*) endpos,(uint) (endpos-key_pos));
405
441
  }
409
445
    {
410
446
      mi_print_error(info->s, HA_ERR_CRASHED);
411
447
      my_errno=HA_ERR_CRASHED;
412
 
      return(-1);
 
448
      DBUG_RETURN(-1);
413
449
    }
414
450
    bmove(key_pos,key_pos-t_length,(uint) (endpos-key_pos)+t_length);
415
451
  }
418
454
  mi_putint(anc_buff,a_length,nod_flag);
419
455
  if (a_length <= keyinfo->block_length)
420
456
  {
421
 
    return(0);                          /* There is room on page */
 
457
    DBUG_RETURN(0);                             /* There is room on page */
422
458
  }
423
459
  /* Page is full */
424
460
  if (nod_flag)
425
461
    insert_last=0;
426
462
  if (!(keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)) &&
427
463
      father_buff && !insert_last)
428
 
    return(_mi_balance_page(info,keyinfo,key,anc_buff,father_buff,
 
464
    DBUG_RETURN(_mi_balance_page(info,keyinfo,key,anc_buff,father_buff,
429
465
                                 father_key_pos,father_page));
430
 
  return(_mi_split_page(info,keyinfo,key,anc_buff,key_buff, insert_last));
 
466
  DBUG_RETURN(_mi_split_page(info,keyinfo,key,anc_buff,key_buff, insert_last));
431
467
} /* _mi_insert */
432
468
 
433
469
 
441
477
  uchar *key_pos,*pos, *after_key= NULL;
442
478
  my_off_t new_pos;
443
479
  MI_KEY_PARAM s_temp;
 
480
  DBUG_ENTER("mi_split_page");
 
481
  DBUG_DUMP("buff",(uchar*) buff,mi_getint(buff));
444
482
 
445
483
  if (info->s->keyinfo+info->lastinx == keyinfo)
446
484
    info->page_changed=1;                       /* Info->buff is used */
453
491
    key_pos=_mi_find_half_pos(nod_flag,keyinfo,buff,key_buff, &key_length,
454
492
                              &after_key);
455
493
  if (!key_pos)
456
 
    return(-1);
 
494
    DBUG_RETURN(-1);
457
495
 
458
496
  length=(uint) (key_pos-buff);
459
497
  a_length=mi_getint(buff);
462
500
  key_pos=after_key;
463
501
  if (nod_flag)
464
502
  {
 
503
    DBUG_PRINT("test",("Splitting nod"));
465
504
    pos=key_pos-nod_flag;
466
505
    memcpy((uchar*) info->buff+2,(uchar*) pos,(size_t) nod_flag);
467
506
  }
468
507
 
469
508
        /* Move middle item to key and pointer to new page */
470
509
  if ((new_pos=_mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR)
471
 
    return(-1);
 
510
    DBUG_RETURN(-1);
472
511
  _mi_kpointer(info,_mi_move_key(keyinfo,key,key_buff),new_pos);
473
512
 
474
513
        /* Store new page */
475
514
  if (!(*keyinfo->get_key)(keyinfo,nod_flag,&key_pos,key_buff))
476
 
    return(-1);
 
515
    DBUG_RETURN(-1);
477
516
 
478
517
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar *) 0,
479
518
                                (uchar*) 0, (uchar*) 0,
485
524
  mi_putint(info->buff,length+t_length+key_ref_length,nod_flag);
486
525
 
487
526
  if (_mi_write_keypage(info,keyinfo,new_pos,DFLT_INIT_HITS,info->buff))
488
 
    return(-1);
489
 
  return(2);                            /* Middle key up */
 
527
    DBUG_RETURN(-1);
 
528
  DBUG_DUMP("key",(uchar*) key,_mi_keylength(keyinfo,key));
 
529
  DBUG_RETURN(2);                               /* Middle key up */
490
530
} /* _mi_split_page */
491
531
 
492
532
 
504
544
{
505
545
  uint keys,length,key_ref_length;
506
546
  uchar *end,*lastpos;
 
547
  DBUG_ENTER("_mi_find_half_pos");
507
548
 
508
549
  key_ref_length=2+nod_flag;
509
550
  length=mi_getint(page)-key_ref_length;
518
559
    end=page+keys*key_ref_length;
519
560
    *after_key=end+key_ref_length;
520
561
    memcpy(key,end,key_ref_length);
521
 
    return(end);
 
562
    DBUG_RETURN(end);
522
563
  }
523
564
 
524
565
  end=page+length/2-key_ref_length;             /* This is aprox. half */
527
568
  {
528
569
    lastpos=page;
529
570
    if (!(length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,key)))
530
 
      return(0);
 
571
      DBUG_RETURN(0);
531
572
  } while (page < end);
532
573
  *return_key_length=length;
533
574
  *after_key=page;
534
 
  return(lastpos);
 
575
  DBUG_PRINT("exit",("returns: 0x%lx  page: 0x%lx  half: 0x%lx",
 
576
                     (long) lastpos, (long) page, (long) end));
 
577
  DBUG_RETURN(lastpos);
535
578
} /* _mi_find_half_pos */
536
579
 
537
580
 
542
585
        */
543
586
 
544
587
static uchar *_mi_find_last_pos(MI_KEYDEF *keyinfo, uchar *page,
545
 
                                uchar *key, uint *return_key_length,
546
 
                                uchar **after_key)
 
588
                                uchar *key, uint *return_key_length,
 
589
                                uchar **after_key)
547
590
{
548
 
  uint keys;
549
 
  uint length;
550
 
  uint last_length= 0;
551
 
  uint key_ref_length;
 
591
  uint keys, length, last_length=0, key_ref_length;
552
592
  uchar *end, *lastpos, *prevpos= NULL;
553
593
  uchar key_buff[MI_MAX_KEY_BUFF];
 
594
  DBUG_ENTER("_mi_find_last_pos");
554
595
 
555
596
  key_ref_length=2;
556
597
  length=mi_getint(page)-key_ref_length;
564
605
    end=page+keys*length;
565
606
    *after_key=end+length;
566
607
    memcpy(key,end,length);
567
 
    return(end);
 
608
    DBUG_RETURN(end);
568
609
  }
569
610
 
570
611
  end= page + length - key_ref_length;
580
621
    {
581
622
      mi_print_error(keyinfo->share, HA_ERR_CRASHED);
582
623
      my_errno=HA_ERR_CRASHED;
583
 
      return(0);
 
624
      DBUG_RETURN(0);
584
625
    }
585
626
  }
586
627
  *return_key_length=last_length;
587
628
  *after_key=lastpos;
588
 
  return(prevpos);
 
629
  DBUG_PRINT("exit",("returns: 0x%lx  page: 0x%lx  end: 0x%lx",
 
630
                     (long) prevpos,(long) page,(long) end));
 
631
  DBUG_RETURN(prevpos);
589
632
} /* _mi_find_last_pos */
590
633
 
591
634
 
603
646
  uchar *pos,*buff,*extra_buff;
604
647
  my_off_t next_page,new_pos;
605
648
  uchar tmp_part_key[MI_MAX_KEY_BUFF];
 
649
  DBUG_ENTER("_mi_balance_page");
606
650
 
607
651
  k_length=keyinfo->keylength;
608
652
  father_length=mi_getint(father_buff);
619
663
    next_page= _mi_kpos(info->s->base.key_reflength,
620
664
                        father_key_pos+father_keylength);
621
665
    buff=info->buff;
 
666
    DBUG_PRINT("test",("use right page: %lu", (ulong) next_page));
622
667
  }
623
668
  else
624
669
  {
627
672
    next_page= _mi_kpos(info->s->base.key_reflength,father_key_pos);
628
673
                                        /* Fix that curr_buff is to left */
629
674
    buff=curr_buff; curr_buff=info->buff;
 
675
    DBUG_PRINT("test",("use left page: %lu", (ulong) next_page));
630
676
  }                                     /* father_key_pos ptr to parting key */
631
677
 
632
678
  if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,info->buff,0))
633
679
    goto err;
 
680
  DBUG_DUMP("next",(uchar*) info->buff,mi_getint(info->buff));
634
681
 
635
682
        /* Test if there is room to share keys */
636
683
 
671
718
    if (_mi_write_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,info->buff) ||
672
719
        _mi_write_keypage(info,keyinfo,father_page,DFLT_INIT_HITS,father_buff))
673
720
      goto err;
674
 
    return(0);
 
721
    DBUG_RETURN(0);
675
722
  }
676
723
 
677
724
        /* curr_buff[] and buff[] are full, lets split and make new nod */
682
729
    new_left_length-=curr_keylength;
683
730
  extra_length=nod_flag+left_length+right_length-
684
731
    new_left_length-new_right_length-curr_keylength;
 
732
  DBUG_PRINT("info",("left_length: %d  right_length: %d  new_left_length: %d  new_right_length: %d  extra_length: %d",
 
733
                     left_length, right_length,
 
734
                     new_left_length, new_right_length,
 
735
                     extra_length));
685
736
  mi_putint(curr_buff,new_left_length,nod_flag);
686
737
  mi_putint(buff,new_right_length,nod_flag);
687
738
  mi_putint(extra_buff,extra_length+2,nod_flag);
714
765
                        DFLT_INIT_HITS,extra_buff))
715
766
    goto err;
716
767
 
717
 
  return(1);                            /* Middle key up */
 
768
  DBUG_RETURN(1);                               /* Middle key up */
718
769
 
719
770
err:
720
 
  return(-1);
 
771
  DBUG_RETURN(-1);
721
772
} /* _mi_balance_page */
722
773
 
723
774
/**********************************************************************
733
784
                      uint key_length)
734
785
{
735
786
  int error;
 
787
  DBUG_ENTER("_mi_ck_write_tree");
736
788
 
737
789
  error= tree_insert(&info->bulk_insert[keynr], key,
738
790
         key_length + info->s->rec_reflength,
739
791
         info->bulk_insert[keynr].custom_arg) ? 0 : HA_ERR_OUT_OF_MEM ;
740
792
 
741
 
  return(error);
 
793
  DBUG_RETURN(error);
742
794
} /* _mi_ck_write_tree */
743
795
 
744
796
 
792
844
  MI_KEYDEF *key=share->keyinfo;
793
845
  bulk_insert_param *params;
794
846
  uint i, num_keys, total_keylength;
795
 
  uint64_t key_map;
 
847
  ulonglong key_map;
 
848
  DBUG_ENTER("_mi_init_bulk_insert");
 
849
  DBUG_PRINT("enter",("cache_size: %lu", cache_size));
796
850
 
797
 
  assert(!info->bulk_insert &&
 
851
  DBUG_ASSERT(!info->bulk_insert &&
798
852
              (!rows || rows >= MI_MIN_ROWS_TO_USE_BULK_INSERT));
799
853
 
800
854
  mi_clear_all_keys_active(key_map);
811
865
 
812
866
  if (num_keys==0 ||
813
867
      num_keys * MI_MIN_SIZE_BULK_INSERT_TREE > cache_size)
814
 
    return(0);
 
868
    DBUG_RETURN(0);
815
869
 
816
870
  if (rows && rows*total_keylength < cache_size)
817
871
    cache_size= (ulong)rows;
823
877
               sizeof(bulk_insert_param)*num_keys),MYF(0));
824
878
 
825
879
  if (!info->bulk_insert)
826
 
    return(HA_ERR_OUT_OF_MEM);
 
880
    DBUG_RETURN(HA_ERR_OUT_OF_MEM);
827
881
 
828
882
  params=(bulk_insert_param *)(info->bulk_insert+share->base.keys);
829
883
  for (i=0 ; i < share->base.keys ; i++)
843
897
     info->bulk_insert[i].root=0;
844
898
  }
845
899
 
846
 
  return(0);
 
900
  DBUG_RETURN(0);
847
901
}
848
902
 
849
903
void mi_flush_bulk_insert(MI_INFO *info, uint inx)