51
51
unsigned char **sort_keys,
52
52
DYNAMIC_ARRAY *buffpek,
54
internal::IO_CACHE *tempfile,
55
internal::IO_CACHE *tempfile_for_exceptions);
54
internal::io_cache_st *tempfile,
55
internal::io_cache_st *tempfile_for_exceptions);
56
56
static int write_keys(MI_SORT_PARAM *info,unsigned char **sort_keys,
57
uint32_t count, BUFFPEK *buffpek,internal::IO_CACHE *tempfile);
57
uint32_t count, BUFFPEK *buffpek,internal::io_cache_st *tempfile);
58
58
static int write_key(MI_SORT_PARAM *info, unsigned char *key,
59
internal::IO_CACHE *tempfile);
59
internal::io_cache_st *tempfile);
60
60
static int write_index(MI_SORT_PARAM *info,unsigned char * *sort_keys,
62
62
static int merge_many_buff(MI_SORT_PARAM *info,uint32_t keys,
63
63
unsigned char * *sort_keys,
64
64
BUFFPEK *buffpek,size_t *maxbuffer,
65
internal::IO_CACHE *t_file);
66
static uint32_t read_to_buffer(internal::IO_CACHE *fromfile,BUFFPEK *buffpek,
65
internal::io_cache_st *t_file);
66
static uint32_t read_to_buffer(internal::io_cache_st *fromfile,BUFFPEK *buffpek,
67
67
uint32_t sort_length);
68
68
static int merge_buffers(MI_SORT_PARAM *info,uint32_t keys,
69
internal::IO_CACHE *from_file, internal::IO_CACHE *to_file,
69
internal::io_cache_st *from_file, internal::io_cache_st *to_file,
70
70
unsigned char * *sort_keys, BUFFPEK *lastbuff,
71
71
BUFFPEK *Fb, BUFFPEK *Tb);
72
72
static int merge_index(MI_SORT_PARAM *,uint,unsigned char **,BUFFPEK *, int,
73
internal::IO_CACHE *);
73
internal::io_cache_st *);
74
74
static int write_keys_varlen(MI_SORT_PARAM *info,unsigned char **sort_keys,
75
75
uint32_t count, BUFFPEK *buffpek,
76
internal::IO_CACHE *tempfile);
77
static uint32_t read_to_buffer_varlen(internal::IO_CACHE *fromfile,BUFFPEK *buffpek,
76
internal::io_cache_st *tempfile);
77
static uint32_t read_to_buffer_varlen(internal::io_cache_st *fromfile,BUFFPEK *buffpek,
78
78
uint32_t sort_length);
79
static int write_merge_key(MI_SORT_PARAM *info, internal::IO_CACHE *to_file,
79
static int write_merge_key(MI_SORT_PARAM *info, internal::io_cache_st *to_file,
80
80
unsigned char *key, uint32_t sort_length, uint32_t count);
81
81
static int write_merge_key_varlen(MI_SORT_PARAM *info,
82
internal::IO_CACHE *to_file,
82
internal::io_cache_st *to_file,
83
83
unsigned char* key, uint32_t sort_length,
87
my_var_write(MI_SORT_PARAM *info, internal::IO_CACHE *to_file, unsigned char *bufs);
87
my_var_write(MI_SORT_PARAM *info, internal::io_cache_st *to_file, unsigned char *bufs);
90
90
Creates a index of sorted keys
124
121
info->write_key=write_merge_key;
127
my_b_clear(&tempfile);
128
my_b_clear(&tempfile_for_exceptions);
125
tempfile_for_exceptions.clear();
129
126
memset(&buffpek, 0, sizeof(buffpek));
130
sort_keys= (unsigned char **) NULL; error= 1;
127
unsigned char** sort_keys= (unsigned char **) NULL;
133
131
memavl=max(sortbuff_size,(size_t)MIN_SORT_MEMORY);
134
records= info->sort_info->max_records;
135
sort_length= info->key_length;
132
ha_rows records= info->sort_info->max_records;
133
uint32_t sort_length= info->key_length;
137
135
while (memavl >= MIN_SORT_MEMORY)
147
if (memavl < sizeof(BUFFPEK)* maxbuffer ||
148
(keys=(memavl-sizeof(BUFFPEK)* maxbuffer)/
149
(sort_length+sizeof(char*))) <= 1 ||
152
mi_check_print_error(info->sort_info->param,
153
"myisam_sort_buffer_size is too small");
145
if (memavl < sizeof(BUFFPEK)* maxbuffer ||
146
(keys=(memavl-sizeof(BUFFPEK)* maxbuffer) / (sort_length+sizeof(char*))) <= 1 ||
149
mi_check_print_error(info->sort_info->param,
150
"myisam_sort_buffer_size is too small");
157
154
while ((maxbuffer= (size_t)(records/(keys-1)+1)) != skr);
159
if ((sort_keys=(unsigned char **)malloc(keys*(sort_length+sizeof(char*)))))
161
if (my_init_dynamic_array(&buffpek, sizeof(BUFFPEK), maxbuffer,
164
free((unsigned char*) sort_keys);
171
if ((memavl=memavl/4*3) < MIN_SORT_MEMORY && old_memavl > MIN_SORT_MEMORY)
172
memavl=MIN_SORT_MEMORY;
156
sort_keys=(unsigned char **)malloc(keys*(sort_length+sizeof(char*)));
157
buffpek.init(sizeof(BUFFPEK), maxbuffer, maxbuffer/2);
174
160
if (memavl < MIN_SORT_MEMORY)
223
209
if (not no_messages)
224
210
printf(" - Adding exceptions\n");
226
if (flush_io_cache(&tempfile_for_exceptions) || tempfile_for_exceptions.reinit_io_cache(internal::READ_CACHE,0L,0,0))
212
if (tempfile_for_exceptions.flush() || tempfile_for_exceptions.reinit_io_cache(internal::READ_CACHE,0L,0,0))
231
while (!my_b_read(&tempfile_for_exceptions,(unsigned char*)&key_length,
233
&& !my_b_read(&tempfile_for_exceptions,(unsigned char*)sort_keys,
217
while (not tempfile_for_exceptions.read(&key_length, sizeof(key_length))
218
&& not tempfile_for_exceptions.read(sort_keys, key_length))
236
if (_mi_ck_write(idx,keyno,(unsigned char*) sort_keys,key_length-ref_length))
220
if (_mi_ck_write(idx,keyno,(unsigned char*) sort_keys,key_length-ref_length))
245
free((unsigned char*) sort_keys);
246
delete_dynamic(&buffpek);
247
230
tempfile.close_cached_file();
248
231
tempfile_for_exceptions.close_cached_file();
250
return(error ? -1 : 0);
233
return error ? -1 : 0;
251
234
} /* _create_index_by_sort */
349
329
free((unsigned char*) sinfo->sort_keys);
350
330
void * rec_buff_ptr= mi_get_rec_buff_ptr(info, sinfo->rec_buff);
351
if (rec_buff_ptr != NULL)
353
332
sinfo->sort_keys=0;
356
for (i= 0, sinfo= sort_param ;
357
i < sort_info->total_keys ;
359
delete_dynamic(&sinfo->buffpek),
335
for (i= 0, sinfo= sort_param; i < sort_info->total_keys; i++,
336
sinfo->buffpek.free(),
360
337
sinfo->tempfile.close_cached_file(),
361
338
sinfo->tempfile_for_exceptions.close_cached_file(),
423
if (my_b_inited(&sinfo->tempfile_for_exceptions))
400
if (sinfo->tempfile_for_exceptions.inited())
425
402
uint32_t key_length;
427
404
if (param->testflag & T_VERBOSE)
428
405
printf("Key %d - Dumping 'long' keys\n", sinfo->key+1);
430
if (flush_io_cache(&sinfo->tempfile_for_exceptions) || sinfo->tempfile_for_exceptions.reinit_io_cache(internal::READ_CACHE,0L,0,0))
407
if (sinfo->tempfile_for_exceptions.flush() || sinfo->tempfile_for_exceptions.reinit_io_cache(internal::READ_CACHE,0L,0,0))
437
!my_b_read(&sinfo->tempfile_for_exceptions,(unsigned char*)&key_length,
413
while (!got_error && not sinfo->tempfile_for_exceptions.read(&key_length, sizeof(key_length)))
440
415
unsigned char ft_buf[10];
441
if (key_length > sizeof(ft_buf) ||
442
my_b_read(&sinfo->tempfile_for_exceptions, (unsigned char*)ft_buf,
444
_mi_ck_write(info, sinfo->key, (unsigned char*)ft_buf,
445
key_length - info->s->rec_reflength))
416
if (key_length > sizeof(ft_buf) || sinfo->tempfile_for_exceptions.read(ft_buf, (uint)key_length)
417
|| _mi_ck_write(info, sinfo->key, (unsigned char*)ft_buf, key_length - info->s->rec_reflength))
454
426
/* Write all keys in memory to file for later merge */
456
428
static int write_keys(MI_SORT_PARAM *info, register unsigned char **sort_keys,
457
uint32_t count, BUFFPEK *buffpek, internal::IO_CACHE *tempfile)
429
uint32_t count, BUFFPEK *buffpek, internal::io_cache_st *tempfile)
459
431
unsigned char **end;
460
432
uint32_t sort_length=info->key_length;
462
internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
464
if (!my_b_inited(tempfile) && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
434
internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp, info);
435
if (not tempfile->inited() && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
467
buffpek->file_pos=my_b_tell(tempfile);
438
buffpek->file_pos= tempfile->tell();
468
439
buffpek->count=count;
470
441
for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
472
if (my_b_write(tempfile,(unsigned char*) *sort_keys,(uint) sort_length))
443
if (tempfile->write(*sort_keys, sort_length))
480
my_var_write(MI_SORT_PARAM *info, internal::IO_CACHE *to_file, unsigned char *bufs)
451
my_var_write(MI_SORT_PARAM *info, internal::io_cache_st *to_file, unsigned char *bufs)
483
454
uint16_t len = _mi_keylength(info->keyinfo, (unsigned char*) bufs);
485
456
/* The following is safe as this is a local file */
486
if ((err= my_b_write(to_file, (unsigned char*)&len, sizeof(len))))
457
if ((err= to_file->write(&len, sizeof(len))))
488
if ((err= my_b_write(to_file,bufs, (uint) len)))
459
if ((err= to_file->write(bufs, len)))
494
465
static int write_keys_varlen(MI_SORT_PARAM *info,
495
466
register unsigned char **sort_keys,
496
467
uint32_t count, BUFFPEK *buffpek,
497
internal::IO_CACHE *tempfile)
468
internal::io_cache_st *tempfile)
499
470
unsigned char **end;
502
internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
504
if (!my_b_inited(tempfile) && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
473
internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp, info);
474
if (not tempfile->inited() && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
507
buffpek->file_pos=my_b_tell(tempfile);
477
buffpek->file_pos= tempfile->tell();
508
478
buffpek->count=count;
509
479
for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
518
488
static int write_key(MI_SORT_PARAM *info, unsigned char *key,
519
internal::IO_CACHE *tempfile)
489
internal::io_cache_st *tempfile)
521
491
uint32_t key_length=info->real_key_length;
523
if (!my_b_inited(tempfile) && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
493
if (not tempfile->inited() && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
526
if (my_b_write(tempfile,(unsigned char*)&key_length,sizeof(key_length)) ||
527
my_b_write(tempfile,(unsigned char*)key,(uint) key_length))
496
if (tempfile->write(&key_length, sizeof(key_length)) ||
497
tempfile->write(key, key_length))
530
500
} /* write_key */
551
521
static int merge_many_buff(MI_SORT_PARAM *info, uint32_t keys,
552
522
unsigned char **sort_keys, BUFFPEK *buffpek,
553
size_t *maxbuffer, internal::IO_CACHE *t_file)
523
size_t *maxbuffer, internal::io_cache_st *t_file)
556
internal::IO_CACHE t_file2, *from_file, *to_file, *temp;
526
internal::io_cache_st t_file2, *from_file, *to_file, *temp;
557
527
BUFFPEK *lastbuff;
559
529
if (*maxbuffer < MERGEBUFF2)
561
if (flush_io_cache(t_file) || t_file2.open_cached_file(P_tmpdir, "ST",
562
DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
531
if (t_file->flush() || t_file2.open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
565
534
from_file= t_file ; to_file= &t_file2;
676
static int write_merge_key(MI_SORT_PARAM *info,
677
internal::IO_CACHE *to_file, unsigned char *key,
645
static int write_merge_key(MI_SORT_PARAM*,
646
internal::io_cache_st *to_file, unsigned char *key,
678
647
uint32_t sort_length, uint32_t count)
681
return my_b_write(to_file, key, (size_t) sort_length*count);
649
return to_file->write(key, sort_length*count);
709
merge_buffers(MI_SORT_PARAM *info, uint32_t keys, internal::IO_CACHE *from_file,
710
internal::IO_CACHE *to_file, unsigned char **sort_keys, BUFFPEK *lastbuff,
677
merge_buffers(MI_SORT_PARAM *info, uint32_t keys, internal::io_cache_st *from_file,
678
internal::io_cache_st *to_file, unsigned char **sort_keys, BUFFPEK *lastbuff,
711
679
BUFFPEK *Fb, BUFFPEK *Tb)
825
793
merge_index(MI_SORT_PARAM *info, uint32_t keys, unsigned char **sort_keys,
826
BUFFPEK *buffpek, int maxbuffer, internal::IO_CACHE *tempfile)
794
BUFFPEK *buffpek, int maxbuffer, internal::io_cache_st *tempfile)
828
if (merge_buffers(info,keys,tempfile,(internal::IO_CACHE*) 0,sort_keys,buffpek,buffpek,
796
if (merge_buffers(info,keys,tempfile,(internal::io_cache_st*) 0,sort_keys,buffpek,buffpek,
829
797
buffpek+maxbuffer))