~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/hash.c

  • Committer: Jay Pipes
  • Date: 2008-07-17 19:43:08 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717194308-l9i4ti57gikm2qbv
Phase 1 removal of DBUG in mysys

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
           hash_get_key get_key,
52
52
           void (*free_element)(void*),uint flags CALLER_INFO_PROTO)
53
53
{
54
 
  DBUG_ENTER("hash_init");
55
 
  DBUG_PRINT("enter",("hash: 0x%lx  size: %u", (long) hash, (uint) size));
56
 
 
57
54
  hash->records=0;
58
55
  if (my_init_dynamic_array_ci(&hash->array, sizeof(HASH_LINK), size,
59
56
                               growth_size))
60
57
  {
61
58
    hash->free=0;                               /* Allow call to hash_free */
62
 
    DBUG_RETURN(1);
 
59
    return(1);
63
60
  }
64
61
  hash->key_offset=key_offset;
65
62
  hash->key_length=key_length;
68
65
  hash->free=free_element;
69
66
  hash->flags=flags;
70
67
  hash->charset=charset;
71
 
  DBUG_RETURN(0);
 
68
  return(0);
72
69
}
73
70
 
74
71
 
108
105
 
109
106
void hash_free(HASH *hash)
110
107
{
111
 
  DBUG_ENTER("hash_free");
112
 
  DBUG_PRINT("enter",("hash: 0x%lx", (long) hash));
113
 
 
114
108
  hash_free_elements(hash);
115
109
  hash->free= 0;
116
110
  delete_dynamic(&hash->array);
117
 
  DBUG_VOID_RETURN;
 
111
  return;
118
112
}
119
113
 
120
114
 
128
122
 
129
123
void my_hash_reset(HASH *hash)
130
124
{
131
 
  DBUG_ENTER("my_hash_reset");
132
 
  DBUG_PRINT("enter",("hash: 0x%lxd", (long) hash));
133
 
 
134
125
  hash_free_elements(hash);
135
126
  reset_dynamic(&hash->array);
136
127
  /* Set row pointers so that the hash can be reused at once */
137
128
  hash->blength= 1;
138
 
  DBUG_VOID_RETURN;
 
129
  return;
139
130
}
140
131
 
141
132
/* some helper functions */
204
195
{
205
196
  HASH_LINK *pos;
206
197
  uint flag,idx;
207
 
  DBUG_ENTER("hash_first");
208
198
 
209
199
  flag=1;
210
200
  if (hash->records)
216
206
      pos= dynamic_element(&hash->array,idx,HASH_LINK*);
217
207
      if (!hashcmp(hash,pos,key,length))
218
208
      {
219
 
        DBUG_PRINT("exit",("found key at %d",idx));
220
209
        *current_record= idx;
221
 
        DBUG_RETURN (pos->data);
 
210
        return (pos->data);
222
211
      }
223
212
      if (flag)
224
213
      {
230
219
    while ((idx=pos->next) != NO_RECORD);
231
220
  }
232
221
  *current_record= NO_RECORD;
233
 
  DBUG_RETURN(0);
 
222
  return(0);
234
223
}
235
224
 
236
225
        /* Get next record with identical key */
448
437
{
449
438
  uint blength,pos2,pos_hashnr,lastpos_hashnr,idx,empty_index;
450
439
  HASH_LINK *data,*lastpos,*gpos,*pos,*pos3,*empty;
451
 
  DBUG_ENTER("hash_delete");
452
440
  if (!hash->records)
453
 
    DBUG_RETURN(1);
 
441
    return(1);
454
442
 
455
443
  blength=hash->blength;
456
444
  data=dynamic_element(&hash->array,0,HASH_LINK*);
462
450
  {
463
451
    gpos=pos;
464
452
    if (pos->next == NO_RECORD)
465
 
      DBUG_RETURN(1);                   /* Key not found */
 
453
      return(1);                        /* Key not found */
466
454
    pos=data+pos->next;
467
455
  }
468
456
 
523
511
  VOID(pop_dynamic(&hash->array));
524
512
  if (hash->free)
525
513
    (*hash->free)((uchar*) record);
526
 
  DBUG_RETURN(0);
 
514
  return(0);
527
515
}
528
516
 
529
517
        /*
537
525
  uint new_index,new_pos_index,blength,records,empty;
538
526
  size_t idx;
539
527
  HASH_LINK org_link,*data,*previous,*pos;
540
 
  DBUG_ENTER("hash_update");
541
528
  
542
529
  if (HASH_UNIQUE & hash->flags)
543
530
  {
548
535
      do 
549
536
      {
550
537
        if (found != record)
551
 
          DBUG_RETURN(1);               /* Duplicate entry */
 
538
          return(1);            /* Duplicate entry */
552
539
      } 
553
540
      while ((found= hash_next(hash, new_key, idx, &state)));
554
541
    }
565
552
                  blength,records);
566
553
  new_index=hash_mask(rec_hashnr(hash,record),blength,records);
567
554
  if (idx == new_index)
568
 
    DBUG_RETURN(0);                     /* Nothing to do (No record check) */
 
555
    return(0);                  /* Nothing to do (No record check) */
569
556
  previous=0;
570
557
  for (;;)
571
558
  {
574
561
      break;
575
562
    previous=pos;
576
563
    if ((idx=pos->next) == NO_RECORD)
577
 
      DBUG_RETURN(1);                   /* Not found in links */
 
564
      return(1);                        /* Not found in links */
578
565
  }
579
566
  org_link= *pos;
580
567
  empty=idx;
610
597
      data[empty]= org_link;
611
598
    }
612
599
    data[empty].next= NO_RECORD;
613
 
    DBUG_RETURN(0);
 
600
    return(0);
614
601
  }
615
602
  pos=data+new_index;
616
603
  new_pos_index=hash_rec_mask(hash,pos,blength,records);
627
614
    data[empty]=org_link;
628
615
    data[new_index].next=empty;
629
616
  }
630
 
  DBUG_RETURN(0);
 
617
  return(0);
631
618
}
632
619
 
633
620
 
649
636
  if (*current_record != NO_RECORD)            /* Safety */
650
637
    dynamic_element(&hash->array, *current_record, HASH_LINK*)->data= new_row;
651
638
}
652
 
 
653
 
 
654
 
#ifndef DBUG_OFF
655
 
 
656
 
bool hash_check(HASH *hash)
657
 
{
658
 
  int error;
659
 
  uint i,rec_link,found,max_links,seek,links,idx;
660
 
  uint records,blength;
661
 
  HASH_LINK *data,*hash_info;
662
 
 
663
 
  records=hash->records; blength=hash->blength;
664
 
  data=dynamic_element(&hash->array,0,HASH_LINK*);
665
 
  error=0;
666
 
 
667
 
  for (i=found=max_links=seek=0 ; i < records ; i++)
668
 
  {
669
 
    if (hash_rec_mask(hash,data+i,blength,records) == i)
670
 
    {
671
 
      found++; seek++; links=1;
672
 
      for (idx=data[i].next ;
673
 
           idx != NO_RECORD && found < records + 1;
674
 
           idx=hash_info->next)
675
 
      {
676
 
        if (idx >= records)
677
 
        {
678
 
          DBUG_PRINT("error",
679
 
                     ("Found pointer outside array to %d from link starting at %d",
680
 
                      idx,i));
681
 
          error=1;
682
 
        }
683
 
        hash_info=data+idx;
684
 
        seek+= ++links;
685
 
        if ((rec_link=hash_rec_mask(hash,hash_info,blength,records)) != i)
686
 
        {
687
 
          DBUG_PRINT("error",
688
 
                     ("Record in wrong link at %d: Start %d  Record: 0x%lx  Record-link %d",
689
 
                      idx, i, (long) hash_info->data, rec_link));
690
 
          error=1;
691
 
        }
692
 
        else
693
 
          found++;
694
 
      }
695
 
      if (links > max_links) max_links=links;
696
 
    }
697
 
  }
698
 
  if (found != records)
699
 
  {
700
 
    DBUG_PRINT("error",("Found %u of %u records", found, records));
701
 
    error=1;
702
 
  }
703
 
  if (records)
704
 
    DBUG_PRINT("info",
705
 
               ("records: %u   seeks: %d   max links: %d   hitrate: %.2f",
706
 
                records,seek,max_links,(float) seek / (float) records));
707
 
  return error;
708
 
}
709
 
#endif