~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/sort.cc

Merge Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#define DISK_BUFFER_SIZE (IO_SIZE*16)
37
37
 
38
38
using namespace std;
 
39
using namespace drizzled;
39
40
 
40
41
 
41
42
/*
46
47
 
47
48
/* Functions defined in this file */
48
49
 
49
 
extern "C" 
50
 
{
51
50
static ha_rows  find_all_keys(MI_SORT_PARAM *info,uint32_t keys,
52
51
                              unsigned char **sort_keys,
53
52
                              DYNAMIC_ARRAY *buffpek,
54
53
                              size_t *maxbuffer,
55
 
                              IO_CACHE *tempfile,
56
 
                              IO_CACHE *tempfile_for_exceptions);
 
54
                              internal::IO_CACHE *tempfile,
 
55
                              internal::IO_CACHE *tempfile_for_exceptions);
57
56
static int  write_keys(MI_SORT_PARAM *info,unsigned char **sort_keys,
58
 
                             uint32_t count, BUFFPEK *buffpek,IO_CACHE *tempfile);
 
57
                             uint32_t count, BUFFPEK *buffpek,internal::IO_CACHE *tempfile);
59
58
static int  write_key(MI_SORT_PARAM *info, unsigned char *key,
60
 
                            IO_CACHE *tempfile);
 
59
                            internal::IO_CACHE *tempfile);
61
60
static int  write_index(MI_SORT_PARAM *info,unsigned char * *sort_keys,
62
61
                              uint32_t count);
63
62
static int  merge_many_buff(MI_SORT_PARAM *info,uint32_t keys,
64
63
                            unsigned char * *sort_keys,
65
64
                            BUFFPEK *buffpek,size_t *maxbuffer,
66
 
                            IO_CACHE *t_file);
67
 
static uint32_t  read_to_buffer(IO_CACHE *fromfile,BUFFPEK *buffpek,
 
65
                            internal::IO_CACHE *t_file);
 
66
static uint32_t  read_to_buffer(internal::IO_CACHE *fromfile,BUFFPEK *buffpek,
68
67
                                  uint32_t sort_length);
69
68
static int  merge_buffers(MI_SORT_PARAM *info,uint32_t keys,
70
 
                                IO_CACHE *from_file, IO_CACHE *to_file,
 
69
                                internal::IO_CACHE *from_file, internal::IO_CACHE *to_file,
71
70
                                unsigned char * *sort_keys, BUFFPEK *lastbuff,
72
71
                                BUFFPEK *Fb, BUFFPEK *Tb);
73
72
static int  merge_index(MI_SORT_PARAM *,uint,unsigned char **,BUFFPEK *, int,
74
 
                              IO_CACHE *);
 
73
                              internal::IO_CACHE *);
75
74
static int  write_keys_varlen(MI_SORT_PARAM *info,unsigned char **sort_keys,
76
75
                       uint32_t count, BUFFPEK *buffpek,
77
 
                       IO_CACHE *tempfile);
78
 
static uint32_t  read_to_buffer_varlen(IO_CACHE *fromfile,BUFFPEK *buffpek,
 
76
                       internal::IO_CACHE *tempfile);
 
77
static uint32_t  read_to_buffer_varlen(internal::IO_CACHE *fromfile,BUFFPEK *buffpek,
79
78
                                uint32_t sort_length);
80
 
static int  write_merge_key(MI_SORT_PARAM *info, IO_CACHE *to_file,
 
79
static int  write_merge_key(MI_SORT_PARAM *info, internal::IO_CACHE *to_file,
81
80
                     unsigned char *key, uint32_t sort_length, uint32_t count);
82
81
static int  write_merge_key_varlen(MI_SORT_PARAM *info,
83
 
                            IO_CACHE *to_file,
 
82
                            internal::IO_CACHE *to_file,
84
83
                            unsigned char* key, uint32_t sort_length,
85
84
                            uint32_t count);
86
 
}
 
85
 
87
86
inline int
88
 
my_var_write(MI_SORT_PARAM *info, IO_CACHE *to_file, unsigned char *bufs);
 
87
my_var_write(MI_SORT_PARAM *info, internal::IO_CACHE *to_file, unsigned char *bufs);
89
88
 
90
89
/*
91
90
  Creates a index of sorted keys
110
109
  DYNAMIC_ARRAY buffpek;
111
110
  ha_rows records;
112
111
  unsigned char **sort_keys;
113
 
  IO_CACHE tempfile, tempfile_for_exceptions;
 
112
  internal::IO_CACHE tempfile, tempfile_for_exceptions;
114
113
 
115
114
  if (info->keyinfo->flag & HA_VAR_LENGTH_KEY)
116
115
  {
138
137
  while (memavl >= MIN_SORT_MEMORY)
139
138
  {
140
139
    if ((records < UINT32_MAX) &&
141
 
       ((my_off_t) (records + 1) *
142
 
        (sort_length + sizeof(char*)) <= (my_off_t) memavl))
 
140
       ((internal::my_off_t) (records + 1) *
 
141
        (sort_length + sizeof(char*)) <= (internal::my_off_t) memavl))
143
142
      keys= (uint)records+1;
144
143
    else
145
144
      do
204
203
                  dynamic_element(&buffpek,0,BUFFPEK *),&maxbuffer,&tempfile))
205
204
        goto err;
206
205
    }
207
 
    if (flush_io_cache(&tempfile) ||
208
 
        reinit_io_cache(&tempfile,READ_CACHE,0L,0,0))
 
206
    if (internal::flush_io_cache(&tempfile) ||
 
207
        internal::reinit_io_cache(&tempfile,internal::READ_CACHE,0L,0,0))
209
208
      goto err;
210
209
    if (!no_messages)
211
210
      printf("  - Last merge and dumping keys\n");
226
225
    if (!no_messages)
227
226
      printf("  - Adding exceptions\n");
228
227
    if (flush_io_cache(&tempfile_for_exceptions) ||
229
 
        reinit_io_cache(&tempfile_for_exceptions,READ_CACHE,0L,0,0))
 
228
        reinit_io_cache(&tempfile_for_exceptions,internal::READ_CACHE,0L,0,0))
230
229
      goto err;
231
230
 
232
231
    while (!my_b_read(&tempfile_for_exceptions,(unsigned char*)&key_length,
257
256
static ha_rows  find_all_keys(MI_SORT_PARAM *info, uint32_t keys,
258
257
                              unsigned char **sort_keys,
259
258
                              DYNAMIC_ARRAY *buffpek,
260
 
                              size_t *maxbuffer, IO_CACHE *tempfile,
261
 
                              IO_CACHE *tempfile_for_exceptions)
 
259
                              size_t *maxbuffer, internal::IO_CACHE *tempfile,
 
260
                              internal::IO_CACHE *tempfile_for_exceptions)
262
261
{
263
262
  int error;
264
263
  uint32_t idx;
316
315
 
317
316
  error=1;
318
317
 
319
 
  if (my_thread_init())
 
318
  if (internal::my_thread_init())
320
319
    goto err;
321
320
 
322
321
  {
349
348
 
350
349
    while (memavl >= MIN_SORT_MEMORY)
351
350
    {
352
 
      if ((my_off_t) (idx+1)*(sort_length+sizeof(char*)) <=
353
 
          (my_off_t) memavl)
 
351
      if ((internal::my_off_t) (idx+1)*(sort_length+sizeof(char*)) <=
 
352
          (internal::my_off_t) memavl)
354
353
        keys= idx+1;
355
354
      else
356
355
      {
469
468
      pthread_cond_signal(&sort_param->sort_info->cond);
470
469
    pthread_mutex_unlock(&sort_param->sort_info->mutex);
471
470
  }
472
 
  my_thread_end();
 
471
  internal::my_thread_end();
473
472
  return NULL;
474
473
}
475
474
 
579
578
        }
580
579
      }
581
580
      if (flush_io_cache(&sinfo->tempfile) ||
582
 
          reinit_io_cache(&sinfo->tempfile,READ_CACHE,0L,0,0))
 
581
          reinit_io_cache(&sinfo->tempfile,internal::READ_CACHE,0L,0,0))
583
582
      {
584
583
        got_error=1;
585
584
        continue;
603
602
        printf("Key %d  - Dumping 'long' keys\n", sinfo->key+1);
604
603
 
605
604
      if (flush_io_cache(&sinfo->tempfile_for_exceptions) ||
606
 
          reinit_io_cache(&sinfo->tempfile_for_exceptions,READ_CACHE,0L,0,0))
 
605
          reinit_io_cache(&sinfo->tempfile_for_exceptions,internal::READ_CACHE,0L,0,0))
607
606
      {
608
607
        got_error=1;
609
608
        continue;
630
629
        /* Write all keys in memory to file for later merge */
631
630
 
632
631
static int  write_keys(MI_SORT_PARAM *info, register unsigned char **sort_keys,
633
 
                             uint32_t count, BUFFPEK *buffpek, IO_CACHE *tempfile)
 
632
                             uint32_t count, BUFFPEK *buffpek, internal::IO_CACHE *tempfile)
634
633
{
635
634
  unsigned char **end;
636
635
  uint32_t sort_length=info->key_length;
637
636
 
638
 
  my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
 
637
  internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
639
638
            info);
640
639
  if (!my_b_inited(tempfile) &&
641
640
      open_cached_file(tempfile, P_tmpdir, "ST",
655
654
 
656
655
 
657
656
inline int
658
 
my_var_write(MI_SORT_PARAM *info, IO_CACHE *to_file, unsigned char *bufs)
 
657
my_var_write(MI_SORT_PARAM *info, internal::IO_CACHE *to_file, unsigned char *bufs)
659
658
{
660
659
  int err;
661
660
  uint16_t len = _mi_keylength(info->keyinfo, (unsigned char*) bufs);
672
671
static int  write_keys_varlen(MI_SORT_PARAM *info,
673
672
                                    register unsigned char **sort_keys,
674
673
                                    uint32_t count, BUFFPEK *buffpek,
675
 
                                    IO_CACHE *tempfile)
 
674
                                    internal::IO_CACHE *tempfile)
676
675
{
677
676
  unsigned char **end;
678
677
  int err;
679
678
 
680
 
  my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
 
679
  internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
681
680
            info);
682
681
  if (!my_b_inited(tempfile) &&
683
682
      open_cached_file(tempfile, P_tmpdir, "ST",
696
695
 
697
696
 
698
697
static int  write_key(MI_SORT_PARAM *info, unsigned char *key,
699
 
                            IO_CACHE *tempfile)
 
698
                            internal::IO_CACHE *tempfile)
700
699
{
701
700
  uint32_t key_length=info->real_key_length;
702
701
 
717
716
static int  write_index(MI_SORT_PARAM *info, register unsigned char **sort_keys,
718
717
                              register uint32_t count)
719
718
{
720
 
  my_qsort2((unsigned char*) sort_keys,(size_t) count,sizeof(unsigned char*),
 
719
  internal::my_qsort2((unsigned char*) sort_keys,(size_t) count,sizeof(unsigned char*),
721
720
           (qsort2_cmp) info->key_cmp,info);
722
721
  while (count--)
723
722
  {
732
731
 
733
732
static int  merge_many_buff(MI_SORT_PARAM *info, uint32_t keys,
734
733
                            unsigned char **sort_keys, BUFFPEK *buffpek,
735
 
                            size_t *maxbuffer, IO_CACHE *t_file)
 
734
                            size_t *maxbuffer, internal::IO_CACHE *t_file)
736
735
{
737
736
  uint32_t i;
738
 
  IO_CACHE t_file2, *from_file, *to_file, *temp;
 
737
  internal::IO_CACHE t_file2, *from_file, *to_file, *temp;
739
738
  BUFFPEK *lastbuff;
740
739
 
741
740
  if (*maxbuffer < MERGEBUFF2)
748
747
  from_file= t_file ; to_file= &t_file2;
749
748
  while (*maxbuffer >= MERGEBUFF2)
750
749
  {
751
 
    reinit_io_cache(from_file,READ_CACHE,0L,0,0);
752
 
    reinit_io_cache(to_file,WRITE_CACHE,0L,0,0);
 
750
    reinit_io_cache(from_file,internal::READ_CACHE,0L,0,0);
 
751
    reinit_io_cache(to_file,internal::WRITE_CACHE,0L,0,0);
753
752
    lastbuff=buffpek;
754
753
    for (i=0 ; i <= *maxbuffer-MERGEBUFF*3/2 ; i+=MERGEBUFF)
755
754
    {
787
786
    -1  Error
788
787
*/
789
788
 
790
 
static uint32_t  read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
 
789
static uint32_t  read_to_buffer(internal::IO_CACHE *fromfile, BUFFPEK *buffpek,
791
790
                                  uint32_t sort_length)
792
791
{
793
792
  register uint32_t count;
806
805
  return (count*sort_length);
807
806
} /* read_to_buffer */
808
807
 
809
 
static uint32_t  read_to_buffer_varlen(IO_CACHE *fromfile, BUFFPEK *buffpek,
 
808
static uint32_t  read_to_buffer_varlen(internal::IO_CACHE *fromfile, BUFFPEK *buffpek,
810
809
                                         uint32_t sort_length)
811
810
{
812
811
  register uint32_t count;
839
838
 
840
839
 
841
840
static int  write_merge_key_varlen(MI_SORT_PARAM *info,
842
 
                                         IO_CACHE *to_file, unsigned char* key,
 
841
                                         internal::IO_CACHE *to_file, unsigned char* key,
843
842
                                         uint32_t sort_length, uint32_t count)
844
843
{
845
844
  uint32_t idx;
857
856
 
858
857
 
859
858
static int  write_merge_key(MI_SORT_PARAM *info,
860
 
                                  IO_CACHE *to_file, unsigned char *key,
 
859
                                  internal::IO_CACHE *to_file, unsigned char *key,
861
860
                                  uint32_t sort_length, uint32_t count)
862
861
{
863
862
  (void)info;
889
888
*/
890
889
 
891
890
static int
892
 
merge_buffers(MI_SORT_PARAM *info, uint32_t keys, IO_CACHE *from_file,
893
 
              IO_CACHE *to_file, unsigned char **sort_keys, BUFFPEK *lastbuff,
 
891
merge_buffers(MI_SORT_PARAM *info, uint32_t keys, internal::IO_CACHE *from_file,
 
892
              internal::IO_CACHE *to_file, unsigned char **sort_keys, BUFFPEK *lastbuff,
894
893
              BUFFPEK *Fb, BUFFPEK *Tb)
895
894
{
896
895
  int error;
897
896
  uint32_t sort_length,maxcount;
898
897
  ha_rows count;
899
 
  my_off_t to_start_filepos= 0;
 
898
  internal::my_off_t to_start_filepos= 0;
900
899
  unsigned char *strpos;
901
900
  BUFFPEK *buffpek;
902
901
  priority_queue<BUFFPEK *, vector<BUFFPEK *>, compare_functor > 
1006
1005
 
1007
1006
static int
1008
1007
merge_index(MI_SORT_PARAM *info, uint32_t keys, unsigned char **sort_keys,
1009
 
            BUFFPEK *buffpek, int maxbuffer, IO_CACHE *tempfile)
 
1008
            BUFFPEK *buffpek, int maxbuffer, internal::IO_CACHE *tempfile)
1010
1009
{
1011
 
  if (merge_buffers(info,keys,tempfile,(IO_CACHE*) 0,sort_keys,buffpek,buffpek,
 
1010
  if (merge_buffers(info,keys,tempfile,(internal::IO_CACHE*) 0,sort_keys,buffpek,buffpek,
1012
1011
                    buffpek+maxbuffer))
1013
1012
    return(1);
1014
1013
  return(0);