My Project
|
00001 #ifndef SQL_SORT_INCLUDED 00002 #define SQL_SORT_INCLUDED 00003 00004 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; version 2 of the License. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 00018 00019 #include "m_string.h" /* memset */ 00020 #include "my_global.h" /* uchar */ 00021 #include "my_base.h" /* ha_rows */ 00022 #include "my_sys.h" /* qsort2_cmp */ 00023 00024 typedef struct st_buffpek BUFFPEK; 00025 typedef struct st_queue QUEUE; 00026 typedef struct st_sort_field SORT_FIELD; 00027 00028 class Field; 00029 struct TABLE; 00030 00031 /* Defines used by filesort and uniques */ 00032 00033 #define MERGEBUFF 7 00034 #define MERGEBUFF2 15 00035 00036 /* 00037 The structure SORT_ADDON_FIELD describes a fixed layout 00038 for field values appended to sorted values in records to be sorted 00039 in the sort buffer. 00040 Only fixed layout is supported now. 00041 Null bit maps for the appended values is placed before the values 00042 themselves. Offsets are from the last sorted field, that is from the 00043 record referefence, which is still last component of sorted records. 00044 It is preserved for backward compatiblility. 00045 The structure is used tp store values of the additional fields 00046 in the sort buffer. It is used also when these values are read 00047 from a temporary file/buffer. As the reading procedures are beyond the 00048 scope of the 'filesort' code the values have to be retrieved via 00049 the callback function 'unpack_addon_fields'. 00050 */ 00051 00052 typedef struct st_sort_addon_field { /* Sort addon packed field */ 00053 Field *field; /* Original field */ 00054 uint offset; /* Offset from the last sorted field */ 00055 uint null_offset; /* Offset to to null bit from the last sorted field */ 00056 uint length; /* Length in the sort buffer */ 00057 uint8 null_bit; /* Null bit mask for the field */ 00058 } SORT_ADDON_FIELD; 00059 00060 typedef struct st_buffpek { /* Struktur om sorteringsbuffrarna */ 00061 my_off_t file_pos; /* Where we are in the sort file */ 00062 uchar *base,*key; /* key pointers */ 00063 ha_rows count; /* Number of rows in table */ 00064 ulong mem_count; /* numbers of keys in memory */ 00065 ulong max_keys; /* Max keys in buffert */ 00066 } BUFFPEK; 00067 00068 struct BUFFPEK_COMPARE_CONTEXT 00069 { 00070 qsort_cmp2 key_compare; 00071 const void *key_compare_arg; 00072 }; 00073 00074 class Sort_param { 00075 public: 00076 uint rec_length; // Length of sorted records. 00077 uint sort_length; // Length of sorted columns. 00078 uint ref_length; // Length of record ref. 00079 uint addon_length; // Length of added packed fields. 00080 uint res_length; // Length of records in final sorted file/buffer. 00081 uint max_keys_per_buffer; // Max keys / buffer. 00082 ha_rows max_rows; // Select limit, or HA_POS_ERROR if unlimited. 00083 ha_rows examined_rows; // Number of examined rows. 00084 TABLE *sort_form; // For quicker make_sortkey. 00085 SORT_FIELD *local_sortorder; 00086 SORT_FIELD *end; 00087 SORT_ADDON_FIELD *addon_field; // Descriptors for companion fields. 00088 uchar *unique_buff; 00089 bool not_killable; 00090 char* tmp_buffer; 00091 // The fields below are used only by Unique class. 00092 qsort2_cmp compare; 00093 BUFFPEK_COMPARE_CONTEXT cmp_context; 00094 00095 Sort_param() 00096 { 00097 memset(this, 0, sizeof(*this)); 00098 } 00099 void init_for_filesort(uint sortlen, TABLE *table, 00100 ulong max_length_for_sort_data, 00101 ha_rows maxrows, bool sort_positions); 00102 }; 00103 00104 00105 int merge_many_buff(Sort_param *param, uchar *sort_buffer, 00106 BUFFPEK *buffpek, 00107 uint *maxbuffer, IO_CACHE *t_file); 00108 uint read_to_buffer(IO_CACHE *fromfile,BUFFPEK *buffpek, 00109 uint sort_length); 00110 int merge_buffers(Sort_param *param,IO_CACHE *from_file, 00111 IO_CACHE *to_file, uchar *sort_buffer, 00112 BUFFPEK *lastbuff,BUFFPEK *Fb, 00113 BUFFPEK *Tb,int flag); 00114 void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint key_length); 00115 00116 #endif /* SQL_SORT_INCLUDED */