mysql_handler/hton/tablespace_statistics.rs
1// Copyright (C) 2026 ren-yamanashi
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 this program 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 for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, see <https://www.gnu.org/licenses/>.
22
23//! Engine-published per-tablespace statistics.
24
25/// Snapshot of the numeric fields of MySQL's `ha_tablespace_statistics`.
26///
27/// Returned from
28/// [`Handlerton::get_tablespace_statistics`](crate::hton::Handlerton::get_tablespace_statistics);
29/// the shim copies each field into the matching `ha_tablespace_statistics`
30/// slot. The five string fields (`m_type`, `m_logfile_group_name`,
31/// `m_row_format`, `m_status`, `m_extra`) stay at their default-empty
32/// values today — they need a separate dd::String_type setter that has
33/// not been wired yet.
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct TablespaceStatistics {
37 id: u64,
38 logfile_group_number: u64,
39 free_extents: u64,
40 total_extents: u64,
41 extent_size: u64,
42 initial_size: u64,
43 maximum_size: u64,
44 autoextend_size: u64,
45 version: u64,
46 data_free: u64,
47}
48
49impl TablespaceStatistics {
50 /// Construct a snapshot with every numeric field zeroed.
51 #[must_use]
52 pub const fn new() -> Self {
53 Self {
54 id: 0,
55 logfile_group_number: 0,
56 free_extents: 0,
57 total_extents: 0,
58 extent_size: 0,
59 initial_size: 0,
60 maximum_size: 0,
61 autoextend_size: 0,
62 version: 0,
63 data_free: 0,
64 }
65 }
66
67 /// Tablespace identifier (`m_id`).
68 #[must_use]
69 pub const fn with_id(mut self, value: u64) -> Self {
70 self.id = value;
71 self
72 }
73 /// Logfile group number — NDB only (`m_logfile_group_number`).
74 #[must_use]
75 pub const fn with_logfile_group_number(mut self, value: u64) -> Self {
76 self.logfile_group_number = value;
77 self
78 }
79 /// Number of free extents (`m_free_extents`).
80 #[must_use]
81 pub const fn with_free_extents(mut self, value: u64) -> Self {
82 self.free_extents = value;
83 self
84 }
85 /// Total number of extents (`m_total_extents`).
86 #[must_use]
87 pub const fn with_total_extents(mut self, value: u64) -> Self {
88 self.total_extents = value;
89 self
90 }
91 /// Extent size in bytes (`m_extent_size`).
92 #[must_use]
93 pub const fn with_extent_size(mut self, value: u64) -> Self {
94 self.extent_size = value;
95 self
96 }
97 /// Initial tablespace size in bytes (`m_initial_size`).
98 #[must_use]
99 pub const fn with_initial_size(mut self, value: u64) -> Self {
100 self.initial_size = value;
101 self
102 }
103 /// Maximum tablespace size in bytes (`m_maximum_size`).
104 #[must_use]
105 pub const fn with_maximum_size(mut self, value: u64) -> Self {
106 self.maximum_size = value;
107 self
108 }
109 /// Auto-extend increment in bytes (`m_autoextend_size`).
110 #[must_use]
111 pub const fn with_autoextend_size(mut self, value: u64) -> Self {
112 self.autoextend_size = value;
113 self
114 }
115 /// Tablespace version — NDB only (`m_version`).
116 #[must_use]
117 pub const fn with_version(mut self, value: u64) -> Self {
118 self.version = value;
119 self
120 }
121 /// Reclaimable bytes — primarily InnoDB (`m_data_free`).
122 #[must_use]
123 pub const fn with_data_free(mut self, value: u64) -> Self {
124 self.data_free = value;
125 self
126 }
127
128 /// Tablespace identifier.
129 pub const fn id(&self) -> u64 {
130 self.id
131 }
132 /// Logfile group number — NDB only.
133 pub const fn logfile_group_number(&self) -> u64 {
134 self.logfile_group_number
135 }
136 /// Free-extent count.
137 pub const fn free_extents(&self) -> u64 {
138 self.free_extents
139 }
140 /// Total-extent count.
141 pub const fn total_extents(&self) -> u64 {
142 self.total_extents
143 }
144 /// Extent size in bytes.
145 pub const fn extent_size(&self) -> u64 {
146 self.extent_size
147 }
148 /// Initial size in bytes.
149 pub const fn initial_size(&self) -> u64 {
150 self.initial_size
151 }
152 /// Maximum size in bytes.
153 pub const fn maximum_size(&self) -> u64 {
154 self.maximum_size
155 }
156 /// Auto-extend increment in bytes.
157 pub const fn autoextend_size(&self) -> u64 {
158 self.autoextend_size
159 }
160 /// Tablespace version (NDB only).
161 pub const fn version(&self) -> u64 {
162 self.version
163 }
164 /// Reclaimable bytes (InnoDB).
165 pub const fn data_free(&self) -> u64 {
166 self.data_free
167 }
168}
169
170impl Default for TablespaceStatistics {
171 fn default() -> Self {
172 Self::new()
173 }
174}
175
176#[cfg(test)]
177mod tests {
178 use super::*;
179
180 #[test]
181 fn new_zeros_every_field() {
182 let s = TablespaceStatistics::new();
183 assert_eq!(s.id(), 0);
184 assert_eq!(s.data_free(), 0);
185 }
186
187 #[test]
188 fn builder_preserves_each_setter() {
189 let s = TablespaceStatistics::new()
190 .with_id(7)
191 .with_data_free(4096)
192 .with_extent_size(64);
193 assert_eq!(s.id(), 7);
194 assert_eq!(s.data_free(), 4096);
195 assert_eq!(s.extent_size(), 64);
196 }
197}