Debian Codegraph @

unittest/gunit/filesort_buffer-t.cc

raw symbols SCIP
1 /* Copyright (c) 2011, 2026, 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 #include <gtest/gtest.h>
25 #include <stddef.h>
26 #include <sys/types.h>
27 #include <utility>
28
29 #include "my_inttypes.h"
30 #include "my_pointer_arithmetic.h"
31 #include "sql/filesort_utils.h"
32 #include "sql/table.h"
33
34 namespace filesort_buffer_unittest {
35
36 class FileSortBufferTest : public ::testing::Test {
37 protected:
38 void TearDown() override {
39 fs_info.free_sort_buffer();
40 EXPECT_TRUE(nullptr == fs_info.get_sort_keys());
41 }
42
43 Filesort_buffer fs_info;
44 };
45
46 TEST_F(FileSortBufferTest, Basic) {
47 const char letters[10] = "abcdefghi"; // Zero-terminated.
48
49 fs_info.set_max_size(32768, 10);
50 for (uint ix = 0; ix < 10; ++ix) {
51 Bounds_checked_array<uchar> buf = fs_info.get_next_record_pointer(10);
52 ASSERT_GE(buf.size(), 10);
53 memcpy(buf.array(), letters, 10);
54 fs_info.commit_used_memory(10);
55 }
56 uchar **data = fs_info.get_sort_keys();
57 for (uint ix = 0; ix < 10; ++ix) {
58 const char *str = reinterpret_cast<const char *>(data[ix]);
59 EXPECT_STREQ(letters, str);
60 }
61
62 EXPECT_GE(fs_info.peak_memory_used(), 100);
63 }
64
65 TEST_F(FileSortBufferTest, SizeNotGivenUpFront) {
66 const char letters[10] = "abcdefghi"; // Zero-terminated.
67 bool ever_given_too_little = false;
68
69 fs_info.set_max_size(10485760, 10);
70 for (uint ix = 0; ix < 10000; ++ix) {
71 Bounds_checked_array<uchar> buf;
72 size_t min_size = 1;
73 for (;;) // Termination condition within loop.
74 {
75 buf = fs_info.get_next_record_pointer(min_size);
76 ASSERT_GE(buf.size(), min_size);
77 if (buf.size() >= 10) break;
78 ever_given_too_little = true;
79 min_size = buf.size() + 1;
80 }
81 memcpy(buf.array(), letters, 10);
82 fs_info.commit_used_memory(10);
83 }
84
85 /*
86 Since MIN_SORT_MEMORY (32768) is not divisible by 10, we should get a
87 record that's too small (8 bytes long, actually) at least once.
88 */
89 EXPECT_TRUE(ever_given_too_little);
90
91 uchar **data = fs_info.get_sort_keys();
92 for (uint ix = 0; ix < 10000; ++ix) {
93 const char *str = reinterpret_cast<const char *>(data[ix]);
94 EXPECT_STREQ(letters, str);
95 }
96
97 EXPECT_GE(fs_info.peak_memory_used(), 100000);
98 EXPECT_LT(fs_info.peak_memory_used(), 1000000);
99 }
100
101 TEST_F(FileSortBufferTest, OneBigRecordFits) {
102 /*
103 Set the record size to maximum possible to ensure we don't
104 estimate any space for record pointers. This is depending on an
105 implementation detail; if the record pointer allocation gets
106 smarter in the future, the test will have to be adjusted.
107 */
108 fs_info.set_max_size(10485760, 0xFFFFFFFFu);
109 Bounds_checked_array<uchar> buf;
110 size_t min_size = 1;
111 for (;;) // Termination condition within loop.
112 {
113 buf = fs_info.get_next_record_pointer(min_size);
114
115 // Should increase by _more_ than just one byte each reallocation.
116 ASSERT_GE(buf.size(), min_size + 1);
117
118 if (buf.size() >= 10485760) break;
119 min_size = buf.size() + 1;
120 }
121
122 EXPECT_EQ(10485760, buf.size());
123 }
124
125 TEST_F(FileSortBufferTest, RecordPointersGetReclaimed) {
126 // Allocate tiny records until there is no more room.
127 fs_info.set_max_size(300000, 1);
128 Bounds_checked_array<uchar> buf;
129 size_t num_records = 0;
130 for (;;) { // Termination condition within loop.
131 buf = fs_info.get_next_record_pointer(1);
132 if (buf.array() == nullptr) break;
133 fs_info.commit_used_memory(1);
134 ++num_records;
135 }
136
137 // Verify that we took record pointers into account.
138 EXPECT_LT(num_records, 100000);
139
140 fs_info.reset();
141
142 /*
143 Now we will have tons of record pointers that need to be reclaimed
144 if we want to be able to to store 250 kB of large rows.
145 */
146 fs_info.set_max_size(300000, 10000);
147 for (uint ix = 0; ix < 25; ++ix) {
148 buf = fs_info.get_next_record_pointer(10000);
149 EXPECT_NE(nullptr, buf.array());
150 fs_info.commit_used_memory(10000);
151 }
152 }
153
154 TEST_F(FileSortBufferTest, PreallocateRecords) {
155 fs_info.set_max_size(32768, sizeof(char));
156 fs_info.preallocate_records(10);
157 uchar **ptr = fs_info.get_sort_keys();
158 EXPECT_NE(nullptr, ptr);
159 for (uint ix = 0; ix < 10 - 1; ++ix) {
160 uchar **nxt = ptr + 1;
161 EXPECT_EQ(1, *nxt - *ptr) << "index:" << ix;
162 ++ptr;
163 }
164 }
165
166 } // namespace filesort_buffer_unittest