MySQL 26.7.0
Source Code Documentation
fsp0space.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2013, 2026, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/fsp0space.h
29 General shared tablespace implementation.
30
31 Created 2013-7-26 by Kevin Lewis
32 *******************************************************/
33
34#ifndef fsp0space_h
35#define fsp0space_h
36
38#include "fsp0file.h"
39#include "fsp0fsp.h"
40#include "fsp0types.h"
41#include "univ.i"
42#include "ut0log.h"
43#include "ut0new.h"
44
45#include <vector>
46
47namespace ib::fsp {
48
49/** Stores information about a single tablespace node. */
51 public:
53 size_t order)
54 : m_name(name),
56 m_order(order) {}
57
58 [[nodiscard]] page_no_t expected_size_in_pages() const {
60 }
61
62 [[nodiscard]] const std::string &name() const { return m_name; }
63
64 [[nodiscard]] size_t order() const { return m_order; }
65
66 private:
67 /** It can be an absolute path to the node storage.
68 It can be a relative path (using `./` or `../`) to the process working
69 directory. Otherwise it is a name, that is to be concatenated with
70 `Tablespace::path`. */
71 const std::string m_name;
72
73 /** Expected size of the node in pages. */
75
76 /** Node's position in the list of nodes of the tablespace it resides in. */
77 const size_t m_order;
78};
79
80/** Data structure that contains the information about an InnoDB tablespace. */
81template <typename TNode = Tablespace_node>
83 protected:
84 /** Data nodes information used in this tablespace, in the correct order. */
86
87 public:
90
91 virtual ~Tablespace() {
92 if (m_name != nullptr) {
94 m_name = nullptr;
95 }
96 }
97
98 // Disable copying
99 Tablespace(const Tablespace &) = delete;
100
101 Tablespace &operator=(const Tablespace &) = delete;
102
103#ifdef UNIV_HOTBACKUP
104 virtual void reset() {
105 if (m_name != nullptr) {
107 m_name = nullptr;
108 }
109 m_nodes.clear();
110 m_path.clear();
111 m_flags = 0;
113 /* We are leaving space_id and space_type fields untouched. */
114 }
115#endif
116
117 const TNode &node(size_t order) const {
118 ut_a(order < m_nodes.size());
119 const auto &res = m_nodes[order];
120 ut_a(res.order() == order);
121 return res;
122 }
123
124 /** Get tablespace type
125 @return tablespace type */
127
128 /** Set tablespace name
129 @param[in] name tablespace name */
130 void set_name(const char *name) {
131 ut_ad(m_name == nullptr);
133 ut_ad(m_name != nullptr);
134 }
135
136 /** Get tablespace name
137 @return tablespace name */
138 const char *name() const { return (m_name); }
139
140 /** Set tablespace path.
141 @param[in] path where tablespace nodes reside. */
142 void set_path(const std::string &path) {
143 ut_ad(m_path.empty());
144 m_path = path;
146 }
147
148 /** Get tablespace path
149 @return tablespace path */
150 const std::string &path() const { return m_path; }
151
152 /** Set the space id of the tablespace
153 @param[in] space_id tablespace ID to set */
157 }
158
159 /** Get the space id of the tablespace
160 @return m_space_id space id of the tablespace */
161 space_id_t space_id() const { return (m_space_id); }
162
163 /** Set the tablespace flags
164 @param[in] fsp_flags tablespace flags */
165 void set_flags(uint32_t fsp_flags) {
166 ut_ad(fsp_flags_is_valid(fsp_flags));
167 m_flags = fsp_flags;
168 }
169
170 /** Get the tablespace flags
171 @return m_flags tablespace flags */
172 uint32_t flags() const { return (m_flags); }
173
174 /** @return the sum of all the node sizes, in pages. */
176 page_no_t sum = 0;
177
178 for (const auto &node : m_nodes) {
179 sum += node.expected_size_in_pages();
180 }
181
182 return sum;
183 }
184
185 /** Delete all the data files.
186 @return Order of all files for which deletion succeeded. */
187 std::vector<size_t> delete_files();
188
189 /** Check if two tablespaces have common data file names.
190 @param[in] other_space Tablespace to check against this.
191 @return true if they have the same data filenames and paths */
192 bool intersects(const Tablespace &other_space);
193
194 /** Use the ADD DATAFILE path to create a node object and add
195 it to the front of m_nodes. Parse the node path into a path
196 and a basename with extension 'ibd'. This datafile_path provided
197 may be an absolute or relative path, but it must end with the
198 extension .ibd and have a basename of at least 1 byte.
199
200 Set tablespace m_path member and add the first node with the path and filename
201 to be extracted from node_path specified.
202 @param[in] node_path full path of the tablespace node. */
203 void add_datafile(const char *node_path);
204
205 /** Determines if the current tablespace should be opened for read-only. */
206 bool is_read_only() const {
208 }
209
210 /** Returns the number of nodes the tablespace contains. */
211 size_t get_nodes_count() const { return m_nodes.size(); }
212
213 /** Returns a full path to the node.
214 @param[in] node_order Number of the node on the list of nodes in the
215 tablespace to query */
216 std::string get_node_full_path(size_t node_order = 0) const {
217 return get_node_full_path(node(node_order));
218 }
219
220 /** Returns a full path to the node.
221 @param[in] node Node from the tablespace to query */
222 std::string get_node_full_path(const Tablespace_node &node) const {
223 const auto node_path = Fil_path::make(path(), node.name(), NO_EXT);
224 const std::string res{node_path};
225 ut::free(node_path);
226 return res;
227 }
228
229 /* Set the autoextend size for the tablespace */
231
232 /* Get the autoextend size for the tablespace */
233 uint64_t get_autoextend_size() const { return m_autoextend_size; }
234
235 private:
236 /**
237 @param[in] node_name Name to lookup in the data nodes.
238 @return true if the node name exists in the data nodes */
239 bool find(std::string_view node_name);
240
241 /** Name of the tablespace. */
242 char *m_name = nullptr;
243
244 /** Tablespace ID */
246
247 /** Path where tablespace files will reside, not including a filename.*/
248 std::string m_path;
249
250 /** Tablespace flags */
251 uint32_t m_flags;
252
253 /** Autoextend size */
255
256 /** Type of the tablespace */
258};
259
260template <typename TNode>
262 for (const auto &node : other_space.m_nodes) {
263 if (find(node.name())) {
264 return (true);
265 }
266 }
267
268 return (false);
269}
270
271template <typename TNode>
272bool Tablespace<TNode>::find(std::string_view node_name) {
273 for (const auto &node : m_nodes) {
274 if (innobase_strcasecmp(node_name.data(), node.name().c_str()) == 0) {
275 return (true);
276 }
277 }
278
279 return (false);
280}
281
282template <typename TNode>
283std::vector<size_t> Tablespace<TNode>::delete_files() {
284 std::vector<size_t> res;
285 for (const auto &node : m_nodes) {
287 .m_path = get_node_full_path(node)};
288 const auto node_info =
289 tablespaces_nodes->get_node_info(space_id(), node.order(), hints, 0);
290 if (node_info) {
291 const auto delete_status =
292 tablespaces_nodes->remove(space_id(), node.order(), hints);
293
294 if (delete_status ==
296 res.push_back(node.order());
297 }
298 }
299 }
300 return res;
301}
302
303template <typename TNode>
304void Tablespace<TNode>::add_datafile(const char *node_path) {
305 /* This method works only for adding first and only node. */
306 ut_a(m_nodes.size() == 0);
307 /* The path provided ends in ".ibd". This was assured by
308 validate_create_tablespace_info() */
309 ut_d(const char *dot = strrchr(node_path, '.'));
310 ut_ad(dot != nullptr && Fil_path::has_suffix(IBD, dot));
311
312 std::string filepath{node_path};
313
315
316 /* If the path is an absolute path, separate it into m_path and a basename.
317 For relative paths, make the whole thing a basename so that
318 it can be appended to the datadir. */
319 const auto dirlen = Fil_path::is_absolute_path(filepath)
320 ? dirname_length(filepath.c_str())
321 : 0;
322
323 /* If the pathname contains a directory separator, fill the
324 m_path member which is the default directory for files in this
325 tablespace. Leave it null otherwise. */
326 if (dirlen > 0) {
327 set_path(filepath.substr(0, dirlen));
328 }
329
330 /* Now add a new node and set the file name removing the prefix path that we
331 extracted and have set above. */
332 m_nodes.push_back({filepath.substr(dirlen), FIL_IBD_FILE_INITIAL_SIZE, 0});
333}
334
335} // namespace ib::fsp
336
337#endif /* fsp0space_h */
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:49
uint32_t page_no_t
Page number.
Definition: api0api.h:47
static char * make(const std::string &path_in, const std::string &name_in, ib_file_suffix ext, bool trim=false)
Allocate and build a file name from a path, a table or tablespace name and a suffix.
Definition: fil0fil.cc:4573
bool is_absolute_path() const
Definition: fil0fil.h:1186
static void normalize(std::string &path)
Normalize a directory path for the current OS: On Windows, we convert '/' to '\', else we convert '\'...
Definition: fil0fil.h:1244
static bool has_suffix(ib_file_suffix sfx, const std::string &path)
Check if the file has the specified suffix.
Definition: fil0fil.h:1310
Data structure that contains the information about an InnoDB tablespace.
Definition: fsp0space.h:82
Tablespace(const Tablespace &)=delete
fil_type_t space_type() const
Get tablespace type.
Definition: fsp0space.h:126
space_id_t space_id() const
Get the space id of the tablespace.
Definition: fsp0space.h:161
char * m_name
Name of the tablespace.
Definition: fsp0space.h:242
virtual ~Tablespace()
Definition: fsp0space.h:91
const char * name() const
Get tablespace name.
Definition: fsp0space.h:138
const std::string & path() const
Get tablespace path.
Definition: fsp0space.h:150
space_id_t m_space_id
Tablespace ID.
Definition: fsp0space.h:245
std::vector< size_t > delete_files()
Delete all the data files.
Definition: fsp0space.h:283
const TNode & node(size_t order) const
Definition: fsp0space.h:117
Tablespace(space_id_t space_id, fil_type_t space_type)
Definition: fsp0space.h:88
uint32_t m_flags
Tablespace flags.
Definition: fsp0space.h:251
size_t get_nodes_count() const
Returns the number of nodes the tablespace contains.
Definition: fsp0space.h:211
bool is_read_only() const
Determines if the current tablespace should be opened for read-only.
Definition: fsp0space.h:206
std::string get_node_full_path(size_t node_order=0) const
Returns a full path to the node.
Definition: fsp0space.h:216
uint64_t m_autoextend_size
Autoextend size.
Definition: fsp0space.h:254
ut::vector< TNode > m_nodes
Data nodes information used in this tablespace, in the correct order.
Definition: fsp0space.h:85
std::string get_node_full_path(const Tablespace_node &node) const
Returns a full path to the node.
Definition: fsp0space.h:222
void set_path(const std::string &path)
Set tablespace path.
Definition: fsp0space.h:142
std::string m_path
Path where tablespace files will reside, not including a filename.
Definition: fsp0space.h:248
void set_flags(uint32_t fsp_flags)
Set the tablespace flags.
Definition: fsp0space.h:165
void add_datafile(const char *node_path)
Use the ADD DATAFILE path to create a node object and add it to the front of m_nodes.
Definition: fsp0space.h:304
void set_autoextend_size(uint64_t size)
Definition: fsp0space.h:230
Tablespace & operator=(const Tablespace &)=delete
bool find(std::string_view node_name)
Definition: fsp0space.h:272
void set_name(const char *name)
Set tablespace name.
Definition: fsp0space.h:130
page_no_t get_sum_of_expected_sizes_in_pages() const
Definition: fsp0space.h:175
bool intersects(const Tablespace &other_space)
Check if two tablespaces have common data file names.
Definition: fsp0space.h:261
void set_space_id(space_id_t space_id)
Set the space id of the tablespace.
Definition: fsp0space.h:154
const fil_type_t m_space_type
Type of the tablespace.
Definition: fsp0space.h:257
uint32_t flags() const
Get the tablespace flags.
Definition: fsp0space.h:172
uint64_t get_autoextend_size() const
Definition: fsp0space.h:233
ut::unique_ptr< ib::fil::Tablespaces_nodes_interface > tablespaces_nodes
The low-level tablespaces' nodes' storage implementation.
Definition: fil0fil.cc:1388
constexpr size_t FIL_IBD_FILE_INITIAL_SIZE
Initial size of a single-table tablespace in pages.
Definition: fil0fil.h:1493
@ IBD
Definition: fil0fil.h:945
@ NO_EXT
Definition: fil0fil.h:944
fil_type_t
File types.
Definition: fil0types.h:180
Tablespace data file implementation.
bool fsp_is_system_temporary(space_id_t space_id)
Check if tablespace is system temporary.
Definition: fsp0fsp.cc:294
File space management.
bool fsp_flags_is_valid(uint32_t flags)
Validate the tablespace flags.
Definition: fsp0fsp.ic:317
size_t dirname_length(const char *name)
Get the string length of the directory part of name, including the last FN_LIBCHAR.
Definition: mf_dirname.cc:62
int innobase_strcasecmp(const char *a, const char *b)
Compares NUL-terminated UTF-8 strings case insensitively.
Definition: ha_innodb.cc:2391
static char * mem_strdup(const char *str)
Duplicates a NUL-terminated string.
static const char * filepath
Definition: myisamlog.cc:97
std::string HARNESS_EXPORT reset()
get 'reset attributes' ESC sequence.
Definition: vt100.cc:37
Definition: fsp0sysspace.cc:74
Container::const_iterator find(const Container &c, Value &&value)
Definition: generic.h:40
size_t size(const char *const c)
Definition: base64.h:46
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2724
void free(void *ptr) noexcept
Releases storage which has been dynamically allocated through any of the ut::malloc*(),...
Definition: ut0new.h:559
bool srv_read_only_mode
Set if InnoDB must operate in read-only mode.
Definition: srv0srv.cc:196
Definition: fil0tablespaces_nodes_interface.h:76
const std::string m_path
Path and filename of the node to process.
Definition: fil0tablespaces_nodes_interface.h:78
Stores information about a single tablespace node.
Definition: fsp0space.h:50
const std::string m_name
It can be an absolute path to the node storage.
Definition: fsp0space.h:71
const page_no_t m_expected_size_in_pages
Expected size of the node in pages.
Definition: fsp0space.h:74
const std::string & name() const
Definition: fsp0space.h:62
Tablespace_node(const std::string &name, page_no_t expected_size_in_pages, size_t order)
Definition: fsp0space.h:52
size_t order() const
Definition: fsp0space.h:64
const size_t m_order
Node's position in the list of nodes of the tablespace it resides in.
Definition: fsp0space.h:77
page_no_t expected_size_in_pages() const
Definition: fsp0space.h:58
Version control for database, common definitions, and include files.
constexpr space_id_t SPACE_UNKNOWN
Unknown space id.
Definition: univ.i:452
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:109
#define ut_d(EXPR)
Debug statement.
Definition: ut0dbg.h:111
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:97
Base of InnoDB utilities.
Dynamic memory allocation routines and custom allocators specifically crafted to support memory instr...