My Project
filesort_utils.h
00001 /* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
00015 
00016 #ifndef FILESORT_UTILS_INCLUDED
00017 #define FILESORT_UTILS_INCLUDED
00018 
00019 #include "my_global.h"
00020 #include "my_base.h"
00021 #include "sql_array.h"
00022 
00023 #include <utility>
00024 
00025 class Sort_param;
00026 /*
00027   Calculate cost of merge sort
00028 
00029     @param num_rows            Total number of rows.
00030     @param num_keys_per_buffer Number of keys per buffer.
00031     @param elem_size           Size of each element.
00032 
00033     Calculates cost of merge sort by simulating call to merge_many_buff().
00034 
00035   @retval
00036     Computed cost of merge sort in disk seeks.
00037 
00038   @note
00039     Declared here in order to be able to unit test it,
00040     since library dependencies have not been sorted out yet.
00041 
00042     See also comments get_merge_many_buffs_cost().
00043 */
00044 
00045 double get_merge_many_buffs_cost_fast(ha_rows num_rows,
00046                                       ha_rows num_keys_per_buffer,
00047                                       uint    elem_size);
00048 
00049 
00062 class Filesort_buffer
00063 {
00064 public:
00065   Filesort_buffer() :
00066     m_idx_array(), m_record_length(0), m_start_of_data(NULL)
00067   {}
00068 
00070   void sort_buffer(const Sort_param *param, uint count);
00071 
00073   uchar *get_record_buffer(uint idx)
00074   {
00075     m_idx_array[idx]= m_start_of_data + (idx * m_record_length);
00076     return m_idx_array[idx];
00077   }
00078 
00080   void init_record_pointers()
00081   {
00082     for (uint ix= 0; ix < m_idx_array.size(); ++ix)
00083       (void) get_record_buffer(ix);
00084   }
00085 
00087   size_t sort_buffer_size() const
00088   {
00089     return m_idx_array.size() * (m_record_length + sizeof(uchar*));
00090   }
00091 
00093   uchar **alloc_sort_buffer(uint num_records, uint record_length);
00094 
00096   void free_sort_buffer();
00097 
00099   uchar **get_sort_keys() { return m_idx_array.array(); }
00100 
00107   Filesort_buffer &operator=(const Filesort_buffer &rhs)
00108   {
00109     m_idx_array= rhs.m_idx_array;
00110     m_record_length= rhs.m_record_length;
00111     m_start_of_data= rhs.m_start_of_data;
00112     return *this;
00113   }
00114 
00115 private:
00116   typedef Bounds_checked_array<uchar*> Idx_array;
00117 
00118   Idx_array  m_idx_array;
00119   uint       m_record_length;
00120   uchar     *m_start_of_data;
00121 };
00122 
00123 #endif  // FILESORT_UTILS_INCLUDED
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines