MySQL 9.1.0
Source Code Documentation
merge_many_buff.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MERGE_MANY_BUFF_INCLUDED
25#define MERGE_MANY_BUFF_INCLUDED
26
27#include <algorithm>
28
29#include "my_dbug.h"
30#include "my_sys.h" // open_cached_file
31#include "sql/mysqld.h" // mysql_tmpdir
32#include "sql/sql_base.h" // TEMP_PREFIX
33#include "sql/sql_sort.h" // Sort_buffer
34
35class THD;
36
37/**
38 Merges buffers to make < MERGEBUFF2 buffers.
39
40 @param thd thread context
41 @param param Sort parameters.
42 @param sort_buffer The main memory buffer.
43 @param chunk_array Array of chunk descriptors to merge.
44 @param [out] p_num_chunks The number of chunks left in the output file.
45 @param [out] t_file Where to store the result.
46
47 @returns false if success, true if error
48*/
49
50template <typename Merge_param>
51bool merge_many_buff(THD *thd, Merge_param *param, Sort_buffer sort_buffer,
52 Merge_chunk_array chunk_array, size_t *p_num_chunks,
53 IO_CACHE *t_file) {
54 IO_CACHE t_file2;
56
57 size_t num_chunks = chunk_array.size();
58 *p_num_chunks = num_chunks;
59
60 if (num_chunks <= MERGEBUFF2) return false; /* purecov: inspected */
61
62 if (flush_io_cache(t_file) ||
64 MYF(MY_WME)))
65 return true; /* purecov: inspected */
66
67 IO_CACHE *from_file = t_file;
68 IO_CACHE *to_file = &t_file2;
69
70 while (num_chunks > MERGEBUFF2) {
71 if (reinit_io_cache(from_file, READ_CACHE, 0L, false, false)) goto cleanup;
72 if (reinit_io_cache(to_file, WRITE_CACHE, 0L, false, false)) goto cleanup;
73 Merge_chunk *last_chunk = chunk_array.begin();
74 uint i;
75 for (i = 0; i < num_chunks - MERGEBUFF * 3U / 2U; i += MERGEBUFF) {
76 if (merge_buffers(thd, param, from_file, to_file, sort_buffer,
77 last_chunk++,
78 Merge_chunk_array(&chunk_array[i], MERGEBUFF),
79 /*include_keys=*/true))
80 goto cleanup;
81 }
82 if (merge_buffers(thd, param, from_file, to_file, sort_buffer, last_chunk++,
83 Merge_chunk_array(&chunk_array[i], num_chunks - i),
84 /*include_keys=*/true))
85 break; /* purecov: inspected */
86 if (flush_io_cache(to_file)) break; /* purecov: inspected */
87
88 std::swap(from_file, to_file);
89 setup_io_cache(from_file);
90 setup_io_cache(to_file);
91 num_chunks = last_chunk - chunk_array.begin();
92 }
94 close_cached_file(to_file); // This holds old result
95 if (to_file == t_file) {
96 *t_file = t_file2; // Copy result file
97 setup_io_cache(t_file);
98 }
99
100 *p_num_chunks = num_chunks;
101 return num_chunks > MERGEBUFF2; /* Return true if interrupted */
102} /* merge_many_buff */
103
104#endif // MERGE_MANY_BUFF_INCLUDED
A wrapper class which provides array bounds checking.
Definition: sql_array.h:47
iterator begin()
begin : Returns a pointer to the first element in the array.
Definition: sql_array.h:134
size_t size() const
Definition: sql_array.h:154
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
void cleanup(void)
Definition: sql_class.cc:1283
#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:1911
void close_cached_file(IO_CACHE *cache)
Definition: mf_cache.cc:87
#define MY_WME
Definition: my_sys.h:130
void setup_io_cache(IO_CACHE *info)
Definition: mf_iocache.cc:117
#define flush_io_cache(info)
Definition: my_sys.h:763
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:328
bool open_cached_file(IO_CACHE *cache, const char *dir, const char *prefix, size_t cache_size, myf cache_myflags)
Definition: mf_cache.cc:53
@ WRITE_CACHE
Definition: my_sys.h:286
@ READ_CACHE
Definition: my_sys.h:285
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:51
#define DBUG_TRACE
Definition: my_dbug.h:146
#define MYF(v)
Definition: my_inttypes.h:97
constexpr const unsigned int DISK_BUFFER_SIZE
Definition: my_io.h:164
Common header for many mysys elements.
#define mysql_tmpdir
Definition: mysqld.h:689
#define TEMP_PREFIX
Definition: sql_base.h:79
constexpr size_t MERGEBUFF2
Definition: sql_sort.h:41
Bounds_checked_array< Merge_chunk > Merge_chunk_array
Definition: sql_sort.h:143
constexpr size_t MERGEBUFF
Definition: sql_sort.h:40
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:663
Definition: my_sys.h:334
Descriptor for a merge chunk to be sort-merged.
Definition: sql_sort.h:57