~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/filesort.cc

Merge Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
#include "plugin/myisam/myisam.h"
43
43
 
44
44
using namespace std;
45
 
using namespace drizzled;
 
45
 
 
46
namespace drizzled
 
47
{
46
48
 
47
49
/* functions defined in this file */
48
50
 
49
51
static char **make_char_array(char **old_pos, register uint32_t fields,
50
52
                              uint32_t length);
51
53
 
52
 
static unsigned char *read_buffpek_from_file(IO_CACHE *buffer_file, uint32_t count,
53
 
                                     unsigned char *buf);
 
54
static unsigned char *read_buffpek_from_file(internal::IO_CACHE *buffer_file,
 
55
                                             uint32_t count,
 
56
                                             unsigned char *buf);
54
57
 
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,
59
 
                             IO_CACHE *tempfile,
60
 
                             IO_CACHE *indexfile);
61
 
 
62
 
static int write_keys(SORTPARAM *param,unsigned char * *sort_keys,
63
 
                      uint32_t count, IO_CACHE *buffer_file, IO_CACHE *tempfile);
64
 
 
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);
 
64
 
 
65
static int write_keys(SORTPARAM *param,
 
66
                      unsigned char * *sort_keys,
 
67
                      uint32_t count,
 
68
                      internal::IO_CACHE *buffer_file,
 
69
                      internal::IO_CACHE *tempfile);
 
70
 
 
71
static void make_sortkey(SORTPARAM *param,
 
72
                         unsigned char *to,
 
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,
68
77
                       BUFFPEK *buffpek,
69
 
                       uint32_t maxbuffer,IO_CACHE *tempfile,
70
 
                       IO_CACHE *outfile);
71
 
static bool save_index(SORTPARAM *param,unsigned char **sort_keys, uint32_t count,
 
78
                       uint32_t maxbuffer,
 
79
                       internal::IO_CACHE *tempfile,
 
80
                       internal::IO_CACHE *outfile);
 
81
static bool save_index(SORTPARAM *param,
 
82
                       unsigned char **sort_keys,
 
83
                       uint32_t count,
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,
 
88
                           uint32_t s_length,
 
89
                           bool *multi_byte_charset);
 
90
static SORT_ADDON_FIELD *get_addon_fields(Session *session,
 
91
                                          Field **ptabfield,
 
92
                                          uint32_t sortlength,
 
93
                                          uint32_t *plength);
78
94
static void unpack_addon_fields(struct st_sort_addon_field *addon_field,
79
95
                                unsigned char *buff);
80
96
/**
123
139
  BUFFPEK *buffpek;
124
140
  ha_rows records= HA_POS_ERROR;
125
141
  unsigned char **sort_keys= 0;
126
 
  IO_CACHE tempfile, buffpek_pointers, *selected_records_file, *outfile;
 
142
  internal::IO_CACHE tempfile, buffpek_pointers, *selected_records_file, *outfile;
127
143
  SORTPARAM param;
128
144
  bool multi_byte_charset;
129
145
 
281
297
        open_cached_file(outfile,drizzle_tmpdir,TEMP_PREFIX,READ_RECORD_BUFFER,
282
298
                          MYF(MY_WME)))
283
299
      goto err;
284
 
    if (reinit_io_cache(outfile,WRITE_CACHE,0L,0,0))
 
300
    if (reinit_io_cache(outfile,internal::WRITE_CACHE,0L,0,0))
285
301
      goto err;
286
302
 
287
303
    /*
295
311
                        &tempfile))
296
312
      goto err;
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))
299
315
      goto err;
300
316
    if (merge_index(&param,(unsigned char*) sort_keys,buffpek,maxbuffer,&tempfile,
301
317
                    outfile))
326
342
    if (flush_io_cache(outfile))
327
343
      error=1;
328
344
    {
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))
332
348
        error=1;
333
349
      outfile->end_of_file=save_pos;
334
350
    }
399
415
 
400
416
/** Read 'count' number of buffer pointers into memory. */
401
417
 
402
 
static unsigned char *read_buffpek_from_file(IO_CACHE *buffpek_pointers, uint32_t count,
 
418
static unsigned char *read_buffpek_from_file(internal::IO_CACHE *buffpek_pointers, uint32_t count,
403
419
                                     unsigned char *buf)
404
420
{
405
421
  uint32_t length= sizeof(BUFFPEK)*count;
410
426
    tmp= (unsigned char *)malloc(length);
411
427
  if (tmp)
412
428
  {
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))
415
431
    {
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)
466
482
{
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];
470
 
  my_off_t record;
 
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;
545
561
 
546
562
        if (!flag)
547
563
        {
548
 
          my_store_ptr(ref_pos,ref_length,record); // Position to row
 
564
          internal::my_store_ptr(ref_pos,ref_length,record); // Position to row
549
565
          record+= sort_form->s->db_record_offset;
550
566
        }
551
567
        else if (!error)
642
658
 
643
659
static int
644
660
write_keys(SORTPARAM *param, register unsigned char **sort_keys, uint32_t count,
645
 
           IO_CACHE *buffpek_pointers, IO_CACHE *tempfile)
 
661
           internal::IO_CACHE *buffpek_pointers, internal::IO_CACHE *tempfile)
646
662
{
647
663
  size_t sort_length, rec_length;
648
664
  unsigned char **end;
650
666
 
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,
656
672
                       MYF(MY_WME)))
976
992
  uint32_t offset,res_length;
977
993
  unsigned char *to;
978
994
 
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. */
997
1013
 
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)
1000
1016
{
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;
1004
1020
 
1005
1021
  if (*maxbuffer < MERGEBUFF2)
1012
1028
  from_file= t_file ; to_file= &t_file2;
1013
1029
  while (*maxbuffer >= MERGEBUFF2)
1014
1030
  {
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))
1016
1032
      goto cleanup;
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))
1018
1034
      goto cleanup;
1019
1035
    lastbuff=buffpek;
1020
1036
    for (i=0 ; i <= *maxbuffer-MERGEBUFF*3/2 ; i+=MERGEBUFF)
1052
1068
    (uint32_t)-1 if something goes wrong
1053
1069
*/
1054
1070
 
1055
 
uint32_t read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
 
1071
uint32_t read_to_buffer(internal::IO_CACHE *fromfile, BUFFPEK *buffpek,
1056
1072
                        uint32_t rec_length)
1057
1073
{
1058
1074
  register uint32_t count;
1106
1122
    other  error
1107
1123
*/
1108
1124
 
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,
1112
1128
                  int flag)
1113
1129
{
1116
1132
  size_t sort_length;
1117
1133
  uint32_t maxcount;
1118
1134
  ha_rows max_rows,org_max_rows;
1119
 
  my_off_t to_start_filepos;
 
1135
  internal::my_off_t to_start_filepos;
1120
1136
  unsigned char *strpos;
1121
1137
  BUFFPEK *buffpek;
1122
1138
  qsort2_cmp cmp;
1151
1167
  }
1152
1168
  else
1153
1169
  {
1154
 
    cmp= get_ptr_compare(sort_length);
 
1170
    cmp= internal::get_ptr_compare(sort_length);
1155
1171
    first_cmp_arg= (void*) &sort_length;
1156
1172
  }
1157
1173
  priority_queue<BUFFPEK *, vector<BUFFPEK *>, compare_functor > 
1315
1331
 
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)
1319
1335
{
1320
1336
  if (merge_buffers(param,tempfile,outfile,sort_buffer,buffpek,buffpek,
1321
1337
                    buffpek+maxbuffer,1))
1613
1629
    }
1614
1630
  }
1615
1631
}
 
1632
 
 
1633
} /* namespace drizzled */