1 #ifndef SQL_SORT_INCLUDED
2 #define SQL_SORT_INCLUDED
3 
4 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; version 2 of the License.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
18 
19 #include "my_base.h"                            /* ha_rows */
20 #include <my_sys.h>                             /* qsort2_cmp */
21 #include "queues.h"
22 
23 typedef struct st_buffpek BUFFPEK;
24 
25 struct SORT_FIELD;
26 class Field;
27 struct TABLE;
28 
29 /* Defines used by filesort and uniques */
30 
31 #define MERGEBUFF		7
32 #define MERGEBUFF2		15
33 
34 /*
35    The structure SORT_ADDON_FIELD describes a fixed layout
36    for field values appended to sorted values in records to be sorted
37    in the sort buffer.
38    Only fixed layout is supported now.
39    Null bit maps for the appended values is placed before the values
40    themselves. Offsets are from the last sorted field, that is from the
41    record referefence, which is still last component of sorted records.
42    It is preserved for backward compatiblility.
43    The structure is used tp store values of the additional fields
44    in the sort buffer. It is used also when these values are read
45    from a temporary file/buffer. As the reading procedures are beyond the
46    scope of the 'filesort' code the values have to be retrieved via
47    the callback function 'unpack_addon_fields'.
48 */
49 
50 typedef struct st_sort_addon_field
51 {
52   /* Sort addon packed field */
53   Field *field;          /* Original field */
54   uint   offset;         /* Offset from the last sorted field */
55   uint   null_offset;    /* Offset to to null bit from the last sorted field */
56   uint   length;         /* Length in the sort buffer */
57   uint8  null_bit;       /* Null bit mask for the field */
58 } SORT_ADDON_FIELD;
59 
60 struct BUFFPEK_COMPARE_CONTEXT
61 {
62   qsort_cmp2 key_compare;
63   void *key_compare_arg;
64 };
65 
66 
67 class Sort_param {
68 public:
69   uint rec_length;            // Length of sorted records.
70   uint sort_length;           // Length of sorted columns.
71   uint ref_length;            // Length of record ref.
72   uint res_length;            // Length of records in final sorted file/buffer.
73   uint max_keys_per_buffer;   // Max keys / buffer.
74   uint min_dupl_count;
75   ha_rows max_rows;           // Select limit, or HA_POS_ERROR if unlimited.
76   ha_rows examined_rows;      // Number of examined rows.
77   TABLE *sort_form;           // For quicker make_sortkey.
78   SORT_FIELD *local_sortorder;
79   SORT_FIELD *end;
80   SORT_ADDON_FIELD *addon_field; // Descriptors for companion fields.
81   LEX_STRING addon_buf;          // Buffer & length of added packed fields.
82 
83   uchar *unique_buff;
84   bool not_killable;
85   char* tmp_buffer;
86   // The fields below are used only by Unique class.
87   qsort2_cmp compare;
88   BUFFPEK_COMPARE_CONTEXT cmp_context;
89 
Sort_param()90   Sort_param()
91   {
92     memset(this, 0, sizeof(*this));
93   }
94   void init_for_filesort(uint sortlen, TABLE *table,
95                          ulong max_length_for_sort_data,
96                          ha_rows maxrows, bool sort_positions);
97 };
98 
99 
100 int merge_many_buff(Sort_param *param, uchar *sort_buffer,
101 		    BUFFPEK *buffpek,
102 		    uint *maxbuffer, IO_CACHE *t_file);
103 ulong read_to_buffer(IO_CACHE *fromfile,BUFFPEK *buffpek,
104                      uint sort_length);
105 bool merge_buffers(Sort_param *param,IO_CACHE *from_file,
106                    IO_CACHE *to_file, uchar *sort_buffer,
107                    BUFFPEK *lastbuff,BUFFPEK *Fb,
108                    BUFFPEK *Tb,int flag);
109 int merge_index(Sort_param *param, uchar *sort_buffer,
110 		BUFFPEK *buffpek, uint maxbuffer,
111 		IO_CACHE *tempfile, IO_CACHE *outfile);
112 void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint key_length);
113 
114 #endif /* SQL_SORT_INCLUDED */
115