MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
filesort_utils.h
1 /* Copyright (c) 2010, 2012 Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #ifndef FILESORT_UTILS_INCLUDED
17 #define FILESORT_UTILS_INCLUDED
18 
19 #include "my_global.h"
20 #include "my_base.h"
21 #include "sql_array.h"
22 
23 #include <utility>
24 
25 class Sort_param;
26 /*
27  Calculate cost of merge sort
28 
29  @param num_rows Total number of rows.
30  @param num_keys_per_buffer Number of keys per buffer.
31  @param elem_size Size of each element.
32 
33  Calculates cost of merge sort by simulating call to merge_many_buff().
34 
35  @retval
36  Computed cost of merge sort in disk seeks.
37 
38  @note
39  Declared here in order to be able to unit test it,
40  since library dependencies have not been sorted out yet.
41 
42  See also comments get_merge_many_buffs_cost().
43 */
44 
45 double get_merge_many_buffs_cost_fast(ha_rows num_rows,
46  ha_rows num_keys_per_buffer,
47  uint elem_size);
48 
49 
63 {
64 public:
65  Filesort_buffer() :
66  m_idx_array(), m_record_length(0), m_start_of_data(NULL)
67  {}
68 
70  void sort_buffer(const Sort_param *param, uint count);
71 
73  uchar *get_record_buffer(uint idx)
74  {
75  m_idx_array[idx]= m_start_of_data + (idx * m_record_length);
76  return m_idx_array[idx];
77  }
78 
81  {
82  for (uint ix= 0; ix < m_idx_array.size(); ++ix)
83  (void) get_record_buffer(ix);
84  }
85 
87  size_t sort_buffer_size() const
88  {
89  return m_idx_array.size() * (m_record_length + sizeof(uchar*));
90  }
91 
93  uchar **alloc_sort_buffer(uint num_records, uint record_length);
94 
96  std::pair<uint, uint> sort_buffer_properties() const
97  {
98  return std::make_pair(static_cast<uint>(m_idx_array.size()),
99  m_record_length);
100  }
101 
103  void free_sort_buffer();
104 
106  uchar **get_sort_keys() { return m_idx_array.array(); }
107 
115  {
116  m_idx_array= rhs.m_idx_array;
117  m_record_length= rhs.m_record_length;
118  m_start_of_data= rhs.m_start_of_data;
119  return *this;
120  }
121 
122 private:
123  typedef Bounds_checked_array<uchar*> Idx_array;
124 
125  Idx_array m_idx_array;
126  uint m_record_length;
127  uchar *m_start_of_data;
128 };
129 
130 #endif // FILESORT_UTILS_INCLUDED