42
42
#include "plugin/myisam/myisam.h"
44
44
using namespace std;
45
using namespace drizzled;
47
49
/* functions defined in this file */
49
51
static char **make_char_array(char **old_pos, register uint32_t fields,
52
static unsigned char *read_buffpek_from_file(IO_CACHE *buffer_file, uint32_t count,
54
static unsigned char *read_buffpek_from_file(internal::IO_CACHE *buffer_file,
55
58
static ha_rows find_all_keys(SORTPARAM *param,
56
59
optimizer::SqlSelect *select,
57
60
unsigned char * *sort_keys,
58
IO_CACHE *buffer_file,
62
static int write_keys(SORTPARAM *param,unsigned char * *sort_keys,
63
uint32_t count, IO_CACHE *buffer_file, IO_CACHE *tempfile);
65
static void make_sortkey(SORTPARAM *param,unsigned char *to, unsigned char *ref_pos);
61
internal::IO_CACHE *buffer_file,
62
internal::IO_CACHE *tempfile,
63
internal::IO_CACHE *indexfile);
65
static int write_keys(SORTPARAM *param,
66
unsigned char * *sort_keys,
68
internal::IO_CACHE *buffer_file,
69
internal::IO_CACHE *tempfile);
71
static void make_sortkey(SORTPARAM *param,
73
unsigned char *ref_pos);
66
74
static void register_used_fields(SORTPARAM *param);
67
static int merge_index(SORTPARAM *param,unsigned char *sort_buffer,
75
static int merge_index(SORTPARAM *param,
76
unsigned char *sort_buffer,
69
uint32_t maxbuffer,IO_CACHE *tempfile,
71
static bool save_index(SORTPARAM *param,unsigned char **sort_keys, uint32_t count,
79
internal::IO_CACHE *tempfile,
80
internal::IO_CACHE *outfile);
81
static bool save_index(SORTPARAM *param,
82
unsigned char **sort_keys,
72
84
filesort_info_st *table_sort);
73
85
static uint32_t suffix_length(uint32_t string_length);
74
static uint32_t sortlength(Session *session, SORT_FIELD *sortorder, uint32_t s_length,
75
bool *multi_byte_charset);
76
static SORT_ADDON_FIELD *get_addon_fields(Session *session, Field **ptabfield,
77
uint32_t sortlength, uint32_t *plength);
86
static uint32_t sortlength(Session *session,
87
SORT_FIELD *sortorder,
89
bool *multi_byte_charset);
90
static SORT_ADDON_FIELD *get_addon_fields(Session *session,
78
94
static void unpack_addon_fields(struct st_sort_addon_field *addon_field,
79
95
unsigned char *buff);
297
313
if (flush_io_cache(&tempfile) ||
298
reinit_io_cache(&tempfile,READ_CACHE,0L,0,0))
314
reinit_io_cache(&tempfile,internal::READ_CACHE,0L,0,0))
300
316
if (merge_index(¶m,(unsigned char*) sort_keys,buffpek,maxbuffer,&tempfile,
326
342
if (flush_io_cache(outfile))
329
my_off_t save_pos=outfile->pos_in_file;
345
internal::my_off_t save_pos=outfile->pos_in_file;
330
346
/* For following reads */
331
if (reinit_io_cache(outfile,READ_CACHE,0L,0,0))
347
if (reinit_io_cache(outfile,internal::READ_CACHE,0L,0,0))
333
349
outfile->end_of_file=save_pos;
410
426
tmp= (unsigned char *)malloc(length);
413
if (reinit_io_cache(buffpek_pointers,READ_CACHE,0L,0,0) ||
429
if (reinit_io_cache(buffpek_pointers,internal::READ_CACHE,0L,0,0) ||
414
430
my_b_read(buffpek_pointers, (unsigned char*) tmp, length))
416
432
free((char*) tmp);
461
477
static ha_rows find_all_keys(SORTPARAM *param,
462
478
optimizer::SqlSelect *select,
463
479
unsigned char **sort_keys,
464
IO_CACHE *buffpek_pointers,
465
IO_CACHE *tempfile, IO_CACHE *indexfile)
480
internal::IO_CACHE *buffpek_pointers,
481
internal::IO_CACHE *tempfile, internal::IO_CACHE *indexfile)
467
483
int error,flag,quick_select;
468
484
uint32_t idx,indexpos,ref_length;
469
485
unsigned char *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
486
internal::my_off_t record;
471
487
Table *sort_form;
472
488
Session *session= current_session;
473
489
volatile Session::killed_state *killed= &session->killed;
651
667
sort_length= param->sort_length;
652
668
rec_length= param->rec_length;
653
my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
669
internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
654
670
if (!my_b_inited(tempfile) &&
655
671
open_cached_file(tempfile, drizzle_tmpdir, TEMP_PREFIX, DISK_BUFFER_SIZE,
976
992
uint32_t offset,res_length;
977
993
unsigned char *to;
979
my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, param->sort_length);
995
internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, param->sort_length);
980
996
res_length= param->res_length;
981
997
offset= param->rec_length-res_length;
982
998
if ((ha_rows) count > param->max_rows)
996
1012
/** Merge buffers to make < MERGEBUFF2 buffers. */
998
1014
int merge_many_buff(SORTPARAM *param, unsigned char *sort_buffer,
999
BUFFPEK *buffpek, uint32_t *maxbuffer, IO_CACHE *t_file)
1015
BUFFPEK *buffpek, uint32_t *maxbuffer, internal::IO_CACHE *t_file)
1001
1017
register uint32_t i;
1002
IO_CACHE t_file2,*from_file,*to_file,*temp;
1018
internal::IO_CACHE t_file2,*from_file,*to_file,*temp;
1003
1019
BUFFPEK *lastbuff;
1005
1021
if (*maxbuffer < MERGEBUFF2)
1012
1028
from_file= t_file ; to_file= &t_file2;
1013
1029
while (*maxbuffer >= MERGEBUFF2)
1015
if (reinit_io_cache(from_file,READ_CACHE,0L,0,0))
1031
if (reinit_io_cache(from_file,internal::READ_CACHE,0L,0,0))
1017
if (reinit_io_cache(to_file,WRITE_CACHE,0L,0,0))
1033
if (reinit_io_cache(to_file,internal::WRITE_CACHE,0L,0,0))
1019
1035
lastbuff=buffpek;
1020
1036
for (i=0 ; i <= *maxbuffer-MERGEBUFF*3/2 ; i+=MERGEBUFF)
1109
int merge_buffers(SORTPARAM *param, IO_CACHE *from_file,
1110
IO_CACHE *to_file, unsigned char *sort_buffer,
1125
int merge_buffers(SORTPARAM *param, internal::IO_CACHE *from_file,
1126
internal::IO_CACHE *to_file, unsigned char *sort_buffer,
1111
1127
BUFFPEK *lastbuff, BUFFPEK *Fb, BUFFPEK *Tb,
1316
1332
static int merge_index(SORTPARAM *param, unsigned char *sort_buffer,
1317
1333
BUFFPEK *buffpek, uint32_t maxbuffer,
1318
IO_CACHE *tempfile, IO_CACHE *outfile)
1334
internal::IO_CACHE *tempfile, internal::IO_CACHE *outfile)
1320
1336
if (merge_buffers(param,tempfile,outfile,sort_buffer,buffpek,buffpek,
1321
1337
buffpek+maxbuffer,1))