~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.cc

  • Committer: Brian Aker
  • Date: 2009-02-09 20:00:19 UTC
  • mfrom: (859.1.1 nofrm)
  • Revision ID: brian@tangent.org-20090209200019-fhik9eshxp0q08ii
Merge Stewart.

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
                           unsigned char *head, File file);
52
52
static void fix_type_pointers(const char ***array, TYPELIB *point_to_type,
53
53
                              uint32_t types, char **names);
54
 
static uint32_t find_field(Field **fields, unsigned char *record,
55
 
                           uint32_t start, uint32_t length);
56
54
 
57
55
/*************************************************************************/
58
56
 
402
400
    for(int indx= 0; indx < table.indexes_size(); indx++)
403
401
      share->key_parts+= table.indexes(indx).index_part_size();
404
402
 
 
403
    share->key_info= (KEY*) alloc_root(&share->mem_root,
 
404
                                       table.indexes_size() * sizeof(KEY)
 
405
                                       +share->key_parts*sizeof(KEY_PART_INFO));
 
406
 
 
407
    KEY_PART_INFO *key_part;
 
408
 
 
409
    key_part= reinterpret_cast<KEY_PART_INFO*>
 
410
      (share->key_info+table.indexes_size());
 
411
 
 
412
 
 
413
    ulong *rec_per_key= (ulong*) alloc_root(&share->mem_root,
 
414
                                            sizeof(ulong*)*share->key_parts);
 
415
 
 
416
    share->keynames.count= table.indexes_size();
 
417
    share->keynames.name= NULL;
 
418
    share->keynames.type_names= (const char**)
 
419
      alloc_root(&share->mem_root, sizeof(char*) * (table.indexes_size()+1));
 
420
 
 
421
    share->keynames.type_lengths= (unsigned int*)
 
422
      alloc_root(&share->mem_root,
 
423
                 sizeof(unsigned int) * (table.indexes_size()+1));
 
424
 
 
425
    share->keynames.type_names[share->keynames.count]= NULL;
 
426
    share->keynames.type_lengths[share->keynames.count]= 0;
 
427
 
 
428
    KEY* keyinfo= share->key_info;
 
429
    for (int keynr=0; keynr < table.indexes_size(); keynr++, keyinfo++)
 
430
    {
 
431
      drizzle::Table::Index indx= table.indexes(keynr);
 
432
 
 
433
      keyinfo->table= 0;
 
434
      keyinfo->flags= 0;
 
435
 
 
436
      if(indx.is_unique())
 
437
        keyinfo->flags|= HA_NOSAME;
 
438
 
 
439
      if(indx.has_options())
 
440
      {
 
441
        drizzle::Table::Index::IndexOptions indx_options= indx.options();
 
442
        if(indx_options.pack_key())
 
443
          keyinfo->flags|= HA_PACK_KEY;
 
444
 
 
445
        if(indx_options.var_length_key())
 
446
          keyinfo->flags|= HA_VAR_LENGTH_PART;
 
447
 
 
448
        if(indx_options.null_part_key())
 
449
          keyinfo->flags|= HA_NULL_PART_KEY;
 
450
 
 
451
        if(indx_options.binary_pack_key())
 
452
          keyinfo->flags|= HA_BINARY_PACK_KEY;
 
453
 
 
454
        if(indx_options.has_partial_segments())
 
455
          keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
 
456
 
 
457
        if(indx_options.auto_generated_key())
 
458
          keyinfo->flags|= HA_GENERATED_KEY;
 
459
 
 
460
        if(indx_options.has_key_block_size())
 
461
        {
 
462
          keyinfo->flags|= HA_USES_BLOCK_SIZE;
 
463
          keyinfo->block_size= indx_options.key_block_size();
 
464
        }
 
465
        else
 
466
        {
 
467
          keyinfo->block_size= 0;
 
468
        }
 
469
 
 
470
      }
 
471
 
 
472
      switch(indx.type())
 
473
      {
 
474
      case drizzle::Table::Index::UNKNOWN_INDEX:
 
475
        keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
476
        break;
 
477
      case drizzle::Table::Index::BTREE:
 
478
        keyinfo->algorithm= HA_KEY_ALG_BTREE;
 
479
        break;
 
480
      case drizzle::Table::Index::RTREE:
 
481
        keyinfo->algorithm= HA_KEY_ALG_RTREE;
 
482
        break;
 
483
      case drizzle::Table::Index::HASH:
 
484
        keyinfo->algorithm= HA_KEY_ALG_HASH;
 
485
        break;
 
486
      case drizzle::Table::Index::FULLTEXT:
 
487
        keyinfo->algorithm= HA_KEY_ALG_FULLTEXT;
 
488
 
 
489
      default:
 
490
        /* TODO: suitable warning ? */
 
491
        keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
492
        break;
 
493
      }
 
494
      
 
495
      keyinfo->key_length= indx.key_length();
 
496
 
 
497
      keyinfo->key_parts= indx.index_part_size();
 
498
 
 
499
      keyinfo->key_part= key_part;
 
500
      keyinfo->rec_per_key= rec_per_key;
 
501
 
 
502
      for(unsigned int partnr= 0;
 
503
          partnr < keyinfo->key_parts;
 
504
          partnr++, key_part++)
 
505
      {
 
506
        drizzle::Table::Index::IndexPart part;
 
507
        part= indx.index_part(partnr);
 
508
 
 
509
        *rec_per_key++=0;
 
510
 
 
511
        key_part->field= NULL;
 
512
        key_part->fieldnr= part.fieldnr();
 
513
        key_part->null_bit= 0;
 
514
        /* key_part->offset= ASS ASS ASS. Set later in the frm code */
 
515
        /* key_part->null_offset is only set if null_bit (see later) */
 
516
        /* key_part->key_type= */ /* I *THINK* this may be okay.... */
 
517
        /* key_part->type ???? */
 
518
        key_part->key_part_flag= 0;
 
519
        if(part.has_in_reverse_order())
 
520
          key_part->key_part_flag= part.in_reverse_order()? HA_REVERSE_SORT : 0;
 
521
 
 
522
        key_part->length= part.compare_length();
 
523
 
 
524
        key_part->store_length= key_part->length;
 
525
      }
 
526
 
 
527
      if(!indx.has_comment())
 
528
      {
 
529
        keyinfo->comment.length= 0;
 
530
        keyinfo->comment.str= NULL;
 
531
      }
 
532
      else
 
533
      {
 
534
        keyinfo->flags|= HA_USES_COMMENT;
 
535
        keyinfo->comment.length= indx.comment().length();
 
536
        keyinfo->comment.str= strmake_root(&share->mem_root,
 
537
                                           indx.comment().c_str(),
 
538
                                           keyinfo->comment.length);
 
539
      }
 
540
 
 
541
      keyinfo->name= strmake_root(&share->mem_root,
 
542
                                  indx.name().c_str(),
 
543
                                  indx.name().length());
 
544
 
 
545
      share->keynames.type_names[keynr]= keyinfo->name;
 
546
      share->keynames.type_lengths[keynr]= indx.name().length();
 
547
    }
 
548
 
405
549
    share->keys_for_keyread.init(0);
406
550
    share->keys_in_use.init(share->keys);
407
551
 
426
570
    share->key_block_size= table_options.has_key_block_size() ?
427
571
      table_options.key_block_size() : 0;
428
572
 
 
573
 
 
574
 
429
575
//    return 0;
430
576
  }
431
577
 
549
695
  uint32_t i,j;
550
696
  bool use_hash;
551
697
  unsigned char forminfo[288];
552
 
  char *keynames, *names, *comment_pos, *vcol_screen_pos;
 
698
  char *names, *comment_pos, *vcol_screen_pos;
553
699
  unsigned char *record;
554
700
  unsigned char *disk_buff, *strpos, *null_flags=NULL, *null_pos=NULL;
555
 
  ulong pos, record_offset, *rec_per_key, rec_buff_length;
 
701
  ulong pos, record_offset, rec_buff_length;
556
702
  handler *handler_file= 0;
557
703
  KEY   *keyinfo;
558
704
  KEY_PART_INFO *key_part;
586
732
    goto err;                                   /* purecov: inspected */
587
733
  if (disk_buff[0] & 0x80)
588
734
  {
589
 
    share->keys=      keys=      (disk_buff[1] << 7) | (disk_buff[0] & 0x7f);
590
 
    share->key_parts= key_parts= uint2korr(disk_buff+2);
 
735
    /*share->keys=*/      keys=      (disk_buff[1] << 7) | (disk_buff[0] & 0x7f);
 
736
    /*share->key_parts=*/ key_parts= uint2korr(disk_buff+2);
591
737
  }
592
738
  else
593
739
  {
594
 
    share->keys=      keys=      disk_buff[0];
595
 
    share->key_parts= key_parts= disk_buff[1];
 
740
    /*share->keys=      */keys=      disk_buff[0];
 
741
    /*share->key_parts=*/ key_parts= disk_buff[1];
596
742
  }
597
 
  share->keys_for_keyread.init(0);
598
 
  share->keys_in_use.init(keys);
599
 
 
600
 
  n_length=keys*sizeof(KEY)+key_parts*sizeof(KEY_PART_INFO);
601
 
  if (!(keyinfo = (KEY*) alloc_root(&share->mem_root,
602
 
                                    n_length + uint2korr(disk_buff+4))))
603
 
    goto err;                                   /* purecov: inspected */
604
 
  memset(keyinfo, 0, n_length);
605
 
  share->key_info= keyinfo;
 
743
//  share->keys_for_keyread.init(0);
 
744
//  share->keys_in_use.init(keys);
 
745
 
 
746
//  n_length=keys*sizeof(KEY)+key_parts*sizeof(KEY_PART_INFO);
 
747
//  if (!(keyinfo = (KEY*) alloc_root(&share->mem_root,
 
748
//                                  n_length + uint2korr(disk_buff+4))))
 
749
 
 
750
    /* the magic uint2korr(disk_buff+4) is  the key names size */
 
751
 
 
752
//    goto err;                                   /* purecov: inspected */
 
753
//  memset(keyinfo, 0, n_length);
 
754
//  share->key_info= keyinfo;
 
755
 
 
756
  keyinfo= share->key_info;
606
757
  key_part= reinterpret_cast<KEY_PART_INFO*> (keyinfo+keys);
607
758
  strpos=disk_buff+6;
608
759
 
609
 
  if (!(rec_per_key= (ulong*) alloc_root(&share->mem_root,
610
 
                                         sizeof(ulong*)*key_parts)))
611
 
    goto err;
 
760
//  if (!(rec_per_key= (ulong*) alloc_root(&share->mem_root,
 
761
//                                       sizeof(ulong*)*key_parts)))
 
762
//    goto err;
612
763
 
613
764
  for (i=0 ; i < keys ; i++, keyinfo++)
614
765
  {
615
 
    keyinfo->table= 0;                           // Updated in open_frm
 
766
//    keyinfo->table= 0;                           // Updated in open_frm
616
767
    if (new_frm_ver >= 3)
617
768
    {
618
 
      keyinfo->flags=      (uint) uint2korr(strpos) ^ HA_NOSAME;
619
 
      keyinfo->key_length= (uint) uint2korr(strpos+2);
620
 
      keyinfo->key_parts=  (uint) strpos[4];
621
 
      keyinfo->algorithm=  (enum ha_key_alg) strpos[5];
622
 
      keyinfo->block_size= uint2korr(strpos+6);
 
769
      /*keyinfo->flags=*/          (uint) uint2korr(strpos) ^ HA_NOSAME;
 
770
      /*keyinfo->key_length=*/ (uint) uint2korr(strpos+2);
 
771
      /*keyinfo->key_parts=*/  (uint) strpos[4];
 
772
//      /*keyinfo->algorithm=*/  (enum ha_key_alg) strpos[5];
 
773
//      /*keyinfo->block_size=*/ uint2korr(strpos+6);
623
774
      strpos+=8;
624
775
    }
625
776
 
626
 
    keyinfo->key_part=   key_part;
627
 
    keyinfo->rec_per_key= rec_per_key;
 
777
//    keyinfo->key_part=         key_part;
 
778
//    keyinfo->rec_per_key= rec_per_key;
628
779
    for (j=keyinfo->key_parts ; j-- ; key_part++)
629
780
    {
630
 
      *rec_per_key++=0;
631
 
      key_part->fieldnr=        (uint16_t) (uint2korr(strpos) & FIELD_NR_MASK);
 
781
//      *rec_per_key++=0;
 
782
      //key_part->fieldnr=      (uint16_t) (uint2korr(strpos) & FIELD_NR_MASK);
632
783
      key_part->offset= (uint) uint2korr(strpos+2)-1;
633
784
      key_part->key_type=       (uint) uint2korr(strpos+5);
634
785
      // key_part->field=       (Field*) 0;     // Will be fixed later
635
786
      if (new_frm_ver >= 1)
636
787
      {
637
 
        key_part->key_part_flag= *(strpos+4);
638
 
        key_part->length=       (uint) uint2korr(strpos+7);
 
788
        //key_part->key_part_flag= *(strpos+4);
 
789
        //key_part->length=     (uint) uint2korr(strpos+7);
639
790
        strpos+=9;
640
791
      }
641
792
      else
642
793
      {
643
 
        key_part->length=       *(strpos+4);
644
 
        key_part->key_part_flag=0;
645
 
        if (key_part->length > 128)
646
 
        {
647
 
          key_part->length&=127;                /* purecov: inspected */
648
 
          key_part->key_part_flag=HA_REVERSE_SORT; /* purecov: inspected */
649
 
        }
650
 
        strpos+=7;
 
794
        abort(); // Old FRM version, we abort as we should never see it.
651
795
      }
652
 
      key_part->store_length=key_part->length;
 
796
      //key_part->store_length=key_part->length;
653
797
    }
654
798
  }
655
 
  keynames=(char*) key_part;
656
 
  strpos+= (strcpy(keynames, (char*)strpos)+strlen((char*)strpos)-keynames)+1;
 
799
 
 
800
//  keynames=(char*) key_part;
 
801
//  strpos+= (strcpy(keynames, (char*)strpos)+strlen((char*)strpos)-keynames)+1;
657
802
 
658
803
  //reading index comments
659
804
  for (keyinfo= share->key_info, i=0; i < keys; i++, keyinfo++)
660
805
  {
661
806
    if (keyinfo->flags & HA_USES_COMMENT)
662
807
    {
663
 
      keyinfo->comment.length= uint2korr(strpos);
664
 
      keyinfo->comment.str= strmake_root(&share->mem_root, (char*) strpos+2,
665
 
                                         keyinfo->comment.length);
666
 
      strpos+= 2 + keyinfo->comment.length;
 
808
//      keyinfo->comment.length= uint2korr(strpos);
 
809
//      keyinfo->comment.str= strmake_root(&share->mem_root, (char*) strpos+2,
 
810
//                                         keyinfo->comment.length);
 
811
      strpos+= 2 + uint2korr(strpos);//keyinfo->comment.length;
667
812
    }
668
 
    assert(test(keyinfo->flags & HA_USES_COMMENT) ==
669
 
               (keyinfo->comment.length > 0));
 
813
//    assert(test(keyinfo->flags & HA_USES_COMMENT) ==
 
814
//               (keyinfo->comment.length > 0));
670
815
  }
671
816
 
672
817
  share->reclength = uint2korr((head+16));
833
978
    }
834
979
  }
835
980
 
836
 
  if (keynames)
837
 
    fix_type_pointers(&interval_array, &share->keynames, 1, &keynames);
 
981
/*  if (keynames)
 
982
    fix_type_pointers(&interval_array, &share->keynames, 1, &keynames);*/
838
983
 
839
984
 /* Allocate handler */
840
985
  if (!(handler_file= get_new_handler(share, session->mem_root,
1050
1195
    for (uint32_t key=0 ; key < share->keys ; key++,keyinfo++)
1051
1196
    {
1052
1197
      uint32_t usable_parts= 0;
1053
 
      keyinfo->name=(char*) share->keynames.type_names[key];
 
1198
//      keyinfo->name=(char*) share->keynames.type_names[key];
1054
1199
 
1055
1200
      if (primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1056
1201
      {
1067
1212
              share->field[fieldnr-1]->key_length() !=
1068
1213
              key_part[i].length)
1069
1214
          {
1070
 
            primary_key=MAX_KEY;                // Can't be used
 
1215
            primary_key=MAX_KEY;                // Can't be used
1071
1216
            break;
1072
1217
          }
1073
1218
        }
1076
1221
      for (i=0 ; i < keyinfo->key_parts ; key_part++,i++)
1077
1222
      {
1078
1223
        Field *field;
1079
 
        if (new_field_pack_flag <= 1)
1080
 
          key_part->fieldnr= (uint16_t) find_field(share->field,
1081
 
                                                 share->default_values,
1082
 
                                                 (uint) key_part->offset,
1083
 
                                                 (uint) key_part->length);
1084
1224
        if (!key_part->fieldnr)
1085
1225
        {
1086
1226
          error= 4;                             // Wrong file
2180
2320
  return result;
2181
2321
}
2182
2322
 
2183
 
 
2184
 
/*
2185
 
 Search after a field with given start & length
2186
 
 If an exact field isn't found, return longest field with starts
2187
 
 at right position.
2188
 
 
2189
 
 NOTES
2190
 
   This is needed because in some .frm fields 'fieldnr' was saved wrong
2191
 
 
2192
 
 RETURN
2193
 
   0  error
2194
 
   #  field number +1
2195
 
*/
2196
 
 
2197
 
static uint32_t find_field(Field **fields, unsigned char *record, uint32_t start, uint32_t length)
2198
 
{
2199
 
  Field **field;
2200
 
  uint32_t i, pos;
2201
 
 
2202
 
  pos= 0;
2203
 
  for (field= fields, i=1 ; *field ; i++,field++)
2204
 
  {
2205
 
    if ((*field)->offset(record) == start)
2206
 
    {
2207
 
      if ((*field)->key_length() == length)
2208
 
        return (i);
2209
 
      if (!pos || fields[pos-1]->pack_length() <
2210
 
          (*field)->pack_length())
2211
 
        pos= i;
2212
 
    }
2213
 
  }
2214
 
  return (pos);
2215
 
}
2216
 
 
2217
 
 
2218
2323
        /* Check that the integer is in the internal */
2219
2324
 
2220
2325
int set_zone(register int nr, int min_zone, int max_zone)