44
44
/* Functions defined in this file */
46
46
static ha_rows find_all_keys(MI_SORT_PARAM *info,uint keys,
47
unsigned char **sort_keys,
48
48
DYNAMIC_ARRAY *buffpek,int *maxbuffer,
49
49
IO_CACHE *tempfile,
50
50
IO_CACHE *tempfile_for_exceptions);
51
static int write_keys(MI_SORT_PARAM *info,uchar **sort_keys,
51
static int write_keys(MI_SORT_PARAM *info,unsigned char **sort_keys,
52
52
uint count, BUFFPEK *buffpek,IO_CACHE *tempfile);
53
static int write_key(MI_SORT_PARAM *info, uchar *key,
53
static int write_key(MI_SORT_PARAM *info, unsigned char *key,
54
54
IO_CACHE *tempfile);
55
static int write_index(MI_SORT_PARAM *info,uchar * *sort_keys,
55
static int write_index(MI_SORT_PARAM *info,unsigned char * *sort_keys,
57
57
static int merge_many_buff(MI_SORT_PARAM *info,uint keys,
58
unsigned char * *sort_keys,
59
59
BUFFPEK *buffpek,int *maxbuffer,
61
61
static uint read_to_buffer(IO_CACHE *fromfile,BUFFPEK *buffpek,
63
63
static int merge_buffers(MI_SORT_PARAM *info,uint keys,
64
64
IO_CACHE *from_file, IO_CACHE *to_file,
65
uchar * *sort_keys, BUFFPEK *lastbuff,
65
unsigned char * *sort_keys, BUFFPEK *lastbuff,
66
66
BUFFPEK *Fb, BUFFPEK *Tb);
67
static int merge_index(MI_SORT_PARAM *,uint,uchar **,BUFFPEK *, int,
67
static int merge_index(MI_SORT_PARAM *,uint,unsigned char **,BUFFPEK *, int,
70
static int write_keys_varlen(MI_SORT_PARAM *info,uchar **sort_keys,
70
static int write_keys_varlen(MI_SORT_PARAM *info,unsigned char **sort_keys,
71
71
uint count, BUFFPEK *buffpek,
72
72
IO_CACHE *tempfile);
73
73
static uint read_to_buffer_varlen(IO_CACHE *fromfile,BUFFPEK *buffpek,
75
75
static int write_merge_key(MI_SORT_PARAM *info, IO_CACHE *to_file,
76
uchar *key, uint sort_length, uint count);
76
unsigned char *key, uint sort_length, uint count);
77
77
static int write_merge_key_varlen(MI_SORT_PARAM *info,
79
uchar* key, uint sort_length,
79
unsigned char* key, uint sort_length,
82
my_var_write(MI_SORT_PARAM *info, IO_CACHE *to_file, uchar *bufs);
82
my_var_write(MI_SORT_PARAM *info, IO_CACHE *to_file, unsigned char *bufs);
85
85
Creates a index of sorted keys
222
222
reinit_io_cache(&tempfile_for_exceptions,READ_CACHE,0L,0,0))
225
while (!my_b_read(&tempfile_for_exceptions,(uchar*)&key_length,
225
while (!my_b_read(&tempfile_for_exceptions,(unsigned char*)&key_length,
226
226
sizeof(key_length))
227
&& !my_b_read(&tempfile_for_exceptions,(uchar*)sort_keys,
227
&& !my_b_read(&tempfile_for_exceptions,(unsigned char*)sort_keys,
228
228
(uint) key_length))
230
if (_mi_ck_write(idx,keyno,(uchar*) sort_keys,key_length-ref_length))
230
if (_mi_ck_write(idx,keyno,(unsigned char*) sort_keys,key_length-ref_length))
362
362
while ((maxbuffer= (int) (idx/(keys-1)+1)) != skr);
364
if ((sort_keys= (uchar**)
364
if ((sort_keys= (unsigned char**)
365
365
my_malloc(keys*(sort_length+sizeof(char*)), MYF(0))))
367
367
if (my_init_dynamic_array(&sort_param->buffpek, sizeof(BUFFPEK),
368
368
maxbuffer, maxbuffer/2))
370
free((uchar*) sort_keys);
371
sort_keys= (uchar **) NULL; /* for err: label */
370
free((unsigned char*) sort_keys);
371
sort_keys= (unsigned char **) NULL; /* for err: label */
562
562
if (param->testflag & T_VERBOSE)
563
563
printf("Key %d - Merging %u keys\n",sinfo->key+1, sinfo->keys);
564
if (merge_many_buff(sinfo, keys, (uchar **)mergebuf,
564
if (merge_many_buff(sinfo, keys, (unsigned char **)mergebuf,
565
565
dynamic_element(&sinfo->buffpek, 0, BUFFPEK *),
566
566
(int*) &maxbuffer, &sinfo->tempfile))
578
578
if (param->testflag & T_VERBOSE)
579
579
printf("Key %d - Last merge and dumping keys\n", sinfo->key+1);
580
if (merge_index(sinfo, keys, (uchar **)mergebuf,
580
if (merge_index(sinfo, keys, (unsigned char **)mergebuf,
581
581
dynamic_element(&sinfo->buffpek,0,BUFFPEK *),
582
582
maxbuffer,&sinfo->tempfile) ||
583
583
flush_pending_blocks(sinfo))
603
603
while (!got_error &&
604
!my_b_read(&sinfo->tempfile_for_exceptions,(uchar*)&key_length,
604
!my_b_read(&sinfo->tempfile_for_exceptions,(unsigned char*)&key_length,
605
605
sizeof(key_length)))
607
unsigned char ft_buf[10];
608
608
if (key_length > sizeof(ft_buf) ||
609
my_b_read(&sinfo->tempfile_for_exceptions, (uchar*)ft_buf,
609
my_b_read(&sinfo->tempfile_for_exceptions, (unsigned char*)ft_buf,
610
610
(uint)key_length) ||
611
_mi_ck_write(info, sinfo->key, (uchar*)ft_buf,
611
_mi_ck_write(info, sinfo->key, (unsigned char*)ft_buf,
612
612
key_length - info->s->rec_reflength))
617
free((uchar*) mergebuf);
617
free((unsigned char*) mergebuf);
618
618
return(got_error);
621
621
/* Write all keys in memory to file for later merge */
623
static int write_keys(MI_SORT_PARAM *info, register uchar **sort_keys,
623
static int write_keys(MI_SORT_PARAM *info, register unsigned char **sort_keys,
624
624
uint count, BUFFPEK *buffpek, IO_CACHE *tempfile)
627
627
uint sort_length=info->key_length;
629
my_qsort2((uchar*) sort_keys,count,sizeof(uchar*),(qsort2_cmp) info->key_cmp,
629
my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
631
631
if (!my_b_inited(tempfile) &&
632
632
open_cached_file(tempfile, my_tmpdir(info->tmpdir), "ST",
648
648
static inline int
649
my_var_write(MI_SORT_PARAM *info, IO_CACHE *to_file, uchar *bufs)
649
my_var_write(MI_SORT_PARAM *info, IO_CACHE *to_file, unsigned char *bufs)
652
uint16_t len = _mi_keylength(info->keyinfo, (uchar*) bufs);
652
uint16_t len = _mi_keylength(info->keyinfo, (unsigned char*) bufs);
654
654
/* The following is safe as this is a local file */
655
if ((err= my_b_write(to_file, (uchar*)&len, sizeof(len))))
655
if ((err= my_b_write(to_file, (unsigned char*)&len, sizeof(len))))
657
657
if ((err= my_b_write(to_file,bufs, (uint) len)))
663
663
static int write_keys_varlen(MI_SORT_PARAM *info,
664
register uchar **sort_keys,
664
register unsigned char **sort_keys,
665
665
uint count, BUFFPEK *buffpek,
666
666
IO_CACHE *tempfile)
671
my_qsort2((uchar*) sort_keys,count,sizeof(uchar*),(qsort2_cmp) info->key_cmp,
671
my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
673
673
if (!my_b_inited(tempfile) &&
674
674
open_cached_file(tempfile, my_tmpdir(info->tmpdir), "ST",
679
679
buffpek->count=count;
680
680
for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
682
if ((err= my_var_write(info,tempfile, (uchar*) *sort_keys)))
682
if ((err= my_var_write(info,tempfile, (unsigned char*) *sort_keys)))
686
686
} /* write_keys_varlen */
689
static int write_key(MI_SORT_PARAM *info, uchar *key,
689
static int write_key(MI_SORT_PARAM *info, unsigned char *key,
690
690
IO_CACHE *tempfile)
692
692
uint key_length=info->real_key_length;
706
706
/* Write index */
708
static int write_index(MI_SORT_PARAM *info, register uchar **sort_keys,
708
static int write_index(MI_SORT_PARAM *info, register unsigned char **sort_keys,
709
709
register uint count)
711
my_qsort2((uchar*) sort_keys,(size_t) count,sizeof(uchar*),
711
my_qsort2((unsigned char*) sort_keys,(size_t) count,sizeof(unsigned char*),
712
712
(qsort2_cmp) info->key_cmp,info);
812
812
for (idx=1;idx<=count;idx++)
814
if (my_pread(fromfile->file,(uchar*)&length_of_key,sizeof(length_of_key),
814
if (my_pread(fromfile->file,(unsigned char*)&length_of_key,sizeof(length_of_key),
815
815
buffpek->file_pos,MYF_RW))
816
816
return((uint) -1);
817
817
buffpek->file_pos+=sizeof(length_of_key);
818
if (my_pread(fromfile->file,(uchar*) buffp,length_of_key,
818
if (my_pread(fromfile->file,(unsigned char*) buffp,length_of_key,
819
819
buffpek->file_pos,MYF_RW))
820
820
return((uint) -1);
821
821
buffpek->file_pos+=length_of_key;
863
863
merge_buffers(MI_SORT_PARAM *info, uint keys, IO_CACHE *from_file,
864
IO_CACHE *to_file, uchar **sort_keys, BUFFPEK *lastbuff,
864
IO_CACHE *to_file, unsigned char **sort_keys, BUFFPEK *lastbuff,
865
865
BUFFPEK *Fb, BUFFPEK *Tb)
868
868
uint sort_length,maxcount;
870
870
my_off_t to_start_filepos= 0;
871
unsigned char *strpos;
872
872
BUFFPEK *buffpek,**refpek;
874
874
volatile int *killed= killed_ptr(info->sort_info->param);
878
878
assert(maxcount > 0);
880
880
to_start_filepos=my_b_tell(to_file);
881
strpos=(uchar*) sort_keys;
881
strpos=(unsigned char*) sort_keys;
882
882
sort_length=info->key_length;
884
884
if (init_queue(&queue,(uint) (Tb-Fb)+1,offsetof(BUFFPEK,key),0,
885
(int (*)(void*, uchar *,uchar*)) info->key_cmp,
885
(int (*)(void*, unsigned char *,unsigned char*)) info->key_cmp,
887
887
return(1); /* purecov: inspected */
961
961
buffpek=(BUFFPEK*) queue_top(&queue);
962
buffpek->base=(uchar *) sort_keys;
962
buffpek->base=(unsigned char *) sort_keys;
963
963
buffpek->max_keys=keys;
968
if (info->write_key(info,to_file,(uchar*) buffpek->key,
968
if (info->write_key(info,to_file,(unsigned char*) buffpek->key,
969
969
sort_length,buffpek->mem_count))
971
971
error=1; goto err; /* purecov: inspected */
1001
1001
/* Do a merge to output-file (save only positions) */
1004
merge_index(MI_SORT_PARAM *info, uint keys, uchar **sort_keys,
1004
merge_index(MI_SORT_PARAM *info, uint keys, unsigned char **sort_keys,
1005
1005
BUFFPEK *buffpek, int maxbuffer, IO_CACHE *tempfile)
1007
1007
if (merge_buffers(info,keys,tempfile,(IO_CACHE*) 0,sort_keys,buffpek,buffpek,