MySQL 8.0.32
Source Code Documentation
merge_many_buff.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2022, Oracle and/or its affiliates.
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, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef MERGE_MANY_BUFF_INCLUDED
24#define MERGE_MANY_BUFF_INCLUDED
25
26#include <algorithm>
27
28#include "my_dbug.h"
29#include "my_sys.h" // open_cached_file
30#include "sql/mysqld.h" // mysql_tmpdir
31#include "sql/sql_base.h" // TEMP_PREFIX
32#include "sql/sql_sort.h" // Sort_buffer
33
34class THD;
35
36/**
37 Merges buffers to make < MERGEBUFF2 buffers.
38
39 @param thd thread context
40 @param param Sort parameters.
41 @param sort_buffer The main memory buffer.
42 @param chunk_array Array of chunk descriptors to merge.
43 @param [out] p_num_chunks The number of chunks left in the output file.
44 @param [out] t_file Where to store the result.
45
46 @returns false if success, true if error
47*/
48
49template <typename Merge_param>
50bool merge_many_buff(THD *thd, Merge_param *param, Sort_buffer sort_buffer,
51 Merge_chunk_array chunk_array, size_t *p_num_chunks,
52 IO_CACHE *t_file) {
53 IO_CACHE t_file2;
55
56 size_t num_chunks = chunk_array.size();
57 *p_num_chunks = num_chunks;
58
59 if (num_chunks <= MERGEBUFF2) return false; /* purecov: inspected */
60
61 if (flush_io_cache(t_file) ||
63 MYF(MY_WME)))
64 return true; /* purecov: inspected */
65
66 IO_CACHE *from_file = t_file;
67 IO_CACHE *to_file = &t_file2;
68
69 while (num_chunks > MERGEBUFF2) {
70 if (reinit_io_cache(from_file, READ_CACHE, 0L, false, false)) goto cleanup;
71 if (reinit_io_cache(to_file, WRITE_CACHE, 0L, false, false)) goto cleanup;
72 Merge_chunk *last_chunk = chunk_array.begin();
73 uint i;
74 for (i = 0; i < num_chunks - MERGEBUFF * 3U / 2U; i += MERGEBUFF) {
75 if (merge_buffers(thd, param, from_file, to_file, sort_buffer,
76 last_chunk++,
77 Merge_chunk_array(&chunk_array[i], MERGEBUFF),
78 /*include_keys=*/true))
79 goto cleanup;
80 }
81 if (merge_buffers(thd, param, from_file, to_file, sort_buffer, last_chunk++,
82 Merge_chunk_array(&chunk_array[i], num_chunks - i),
83 /*include_keys=*/true))
84 break; /* purecov: inspected */
85 if (flush_io_cache(to_file)) break; /* purecov: inspected */
86
87 std::swap(from_file, to_file);
88 setup_io_cache(from_file);
89 setup_io_cache(to_file);
90 num_chunks = last_chunk - chunk_array.begin();
91 }
93 close_cached_file(to_file); // This holds old result
94 if (to_file == t_file) {
95 *t_file = t_file2; // Copy result file
96 setup_io_cache(t_file);
97 }
98
99 *p_num_chunks = num_chunks;
100 return num_chunks > MERGEBUFF2; /* Return true if interrupted */
101} /* merge_many_buff */
102
103#endif // MERGE_MANY_BUFF_INCLUDED
A wrapper class which provides array bounds checking.
Definition: sql_array.h:46
iterator begin()
begin : Returns a pointer to the first element in the array.
Definition: sql_array.h:120
size_t size() const
Definition: sql_array.h:135
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:33
void cleanup(void)
Definition: sql_class.cc:1214
#define L
Definition: ctype-tis620.cc:75
static int merge_buffers(THD *thd, Sort_param *param, IO_CACHE *from_file, IO_CACHE *to_file, Sort_buffer sort_buffer, Merge_chunk *last_chunk, Merge_chunk_array chunk_array, bool include_keys)
Merge buffers to one buffer.
Definition: filesort.cc:1900
void close_cached_file(IO_CACHE *cache)
Definition: mf_cache.cc:86
#define MY_WME
Definition: my_sys.h:122
void setup_io_cache(IO_CACHE *info)
Definition: mf_iocache.cc:116
#define flush_io_cache(info)
Definition: my_sys.h:757
bool reinit_io_cache(IO_CACHE *info, enum cache_type type, my_off_t seek_offset, bool use_async_io, bool clear_cache)
Definition: mf_iocache.cc:327
bool open_cached_file(IO_CACHE *cache, const char *dir, const char *prefix, size_t cache_size, myf cache_myflags)
Definition: mf_cache.cc:52
@ WRITE_CACHE
Definition: my_sys.h:284
@ READ_CACHE
Definition: my_sys.h:283
bool merge_many_buff(THD *thd, Merge_param *param, Sort_buffer sort_buffer, Merge_chunk_array chunk_array, size_t *p_num_chunks, IO_CACHE *t_file)
Merges buffers to make < MERGEBUFF2 buffers.
Definition: merge_many_buff.h:50
#define DBUG_TRACE
Definition: my_dbug.h:145
#define MYF(v)
Definition: my_inttypes.h:96
constexpr const unsigned int DISK_BUFFER_SIZE
Definition: my_io.h:163
Common header for many mysys elements.
#define mysql_tmpdir
Definition: mysqld.h:698
#define TEMP_PREFIX
Definition: sql_base.h:77
constexpr size_t MERGEBUFF2
Definition: sql_sort.h:40
Bounds_checked_array< Merge_chunk > Merge_chunk_array
Definition: sql_sort.h:142
constexpr size_t MERGEBUFF
Definition: sql_sort.h:39
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:611
Definition: my_sys.h:340
Descriptor for a merge chunk to be sort-merged.
Definition: sql_sort.h:56
unsigned int uint
Definition: uca-dump.cc:29