MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_sort.h
1 #ifndef SQL_SORT_INCLUDED
2 #define SQL_SORT_INCLUDED
3 
4 /* Copyright (c) 2000, 2011, 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-1301 USA */
18 
19 #include "m_string.h" /* memset */
20 #include "my_global.h" /* uchar */
21 #include "my_base.h" /* ha_rows */
22 #include "my_sys.h" /* qsort2_cmp */
23 
24 typedef struct st_buffpek BUFFPEK;
25 typedef struct st_queue QUEUE;
26 typedef struct st_sort_field SORT_FIELD;
27 
28 class Field;
29 struct TABLE;
30 
31 /* Defines used by filesort and uniques */
32 
33 #define MERGEBUFF 7
34 #define MERGEBUFF2 15
35 
36 /*
37  The structure SORT_ADDON_FIELD describes a fixed layout
38  for field values appended to sorted values in records to be sorted
39  in the sort buffer.
40  Only fixed layout is supported now.
41  Null bit maps for the appended values is placed before the values
42  themselves. Offsets are from the last sorted field, that is from the
43  record referefence, which is still last component of sorted records.
44  It is preserved for backward compatiblility.
45  The structure is used tp store values of the additional fields
46  in the sort buffer. It is used also when these values are read
47  from a temporary file/buffer. As the reading procedures are beyond the
48  scope of the 'filesort' code the values have to be retrieved via
49  the callback function 'unpack_addon_fields'.
50 */
51 
52 typedef struct st_sort_addon_field { /* 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 */
59 
60 typedef struct st_buffpek { /* Struktur om sorteringsbuffrarna */
61  my_off_t file_pos; /* Where we are in the sort file */
62  uchar *base,*key; /* key pointers */
63  ha_rows count; /* Number of rows in table */
64  ulong mem_count; /* numbers of keys in memory */
65  ulong max_keys; /* Max keys in buffert */
66 } BUFFPEK;
67 
69 {
70  qsort_cmp2 key_compare;
71  const void *key_compare_arg;
72 };
73 
74 class Sort_param {
75 public:
76  uint rec_length; // Length of sorted records.
77  uint sort_length; // Length of sorted columns.
78  uint ref_length; // Length of record ref.
79  uint addon_length; // Length of added packed fields.
80  uint res_length; // Length of records in final sorted file/buffer.
81  uint max_keys_per_buffer; // Max keys / buffer.
82  ha_rows max_rows; // Select limit, or HA_POS_ERROR if unlimited.
83  ha_rows examined_rows; // Number of examined rows.
84  TABLE *sort_form; // For quicker make_sortkey.
85  SORT_FIELD *local_sortorder;
86  SORT_FIELD *end;
87  SORT_ADDON_FIELD *addon_field; // Descriptors for companion fields.
88  uchar *unique_buff;
89  bool not_killable;
90  char* tmp_buffer;
91  // The fields below are used only by Unique class.
92  qsort2_cmp compare;
93  BUFFPEK_COMPARE_CONTEXT cmp_context;
94 
95  Sort_param()
96  {
97  memset(this, 0, sizeof(*this));
98  }
99  void init_for_filesort(uint sortlen, TABLE *table,
100  ulong max_length_for_sort_data,
101  ha_rows maxrows, bool sort_positions);
102 };
103 
104 
105 int merge_many_buff(Sort_param *param, uchar *sort_buffer,
106  BUFFPEK *buffpek,
107  uint *maxbuffer, IO_CACHE *t_file);
108 uint read_to_buffer(IO_CACHE *fromfile,BUFFPEK *buffpek,
109  uint sort_length);
110 int merge_buffers(Sort_param *param,IO_CACHE *from_file,
111  IO_CACHE *to_file, uchar *sort_buffer,
112  BUFFPEK *lastbuff,BUFFPEK *Fb,
113  BUFFPEK *Tb,int flag);
114 void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint key_length);
115 
116 #endif /* SQL_SORT_INCLUDED */