Skip to main content

mysql_handler/hton/
clone_kind.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//! `Ha_clone_mode` and `Ha_clone_type` from `sql/handler.h`. Used as the
24//! `mode` / `type` arguments to the clone interface trait methods.
25
26/// Mode for starting a clone operation.
27///
28/// Mirrors `enum Ha_clone_mode` in `mysql-server/sql/handler.h`.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30#[non_exhaustive]
31pub enum HaCloneMode {
32    /// `HA_CLONE_MODE_START`: first start of a clone.
33    Start,
34    /// `HA_CLONE_MODE_RESTART`: restart of a previously paused clone.
35    Restart,
36    /// `HA_CLONE_MODE_ADD_TASK`: add a worker task to an existing clone.
37    AddTask,
38    /// `HA_CLONE_MODE_VERSION`: query supported version.
39    Version,
40    /// `HA_CLONE_MODE_MAX`: sentinel; treated as `Start` for forward-compat.
41    Max,
42}
43
44impl HaCloneMode {
45    /// Decode the C `enum Ha_clone_mode` value. Unknown values fall back to
46    /// [`HaCloneMode::Start`].
47    #[must_use]
48    pub const fn from_raw(value: u32) -> Self {
49        match value {
50            1 => Self::Restart,
51            2 => Self::AddTask,
52            3 => Self::Version,
53            4 => Self::Max,
54            _ => Self::Start,
55        }
56    }
57}
58
59/// Clone-transfer type the source engine should perform.
60///
61/// Mirrors `enum Ha_clone_type` in `mysql-server/sql/handler.h`.
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63#[non_exhaustive]
64pub enum HaCloneType {
65    /// `HA_CLONE_BLOCKING`: serialised copy (write operations must block).
66    Blocking,
67    /// `HA_CLONE_REDO`: archive redo log to support concurrent DML.
68    Redo,
69    /// `HA_CLONE_PAGE`: page-tracked incremental.
70    Page,
71    /// `HA_CLONE_HYBRID`: page tracking + redo (currently InnoDB).
72    Hybrid,
73    /// `HA_CLONE_MULTI_TASK`: multiple worker threads.
74    MultiTask,
75    /// `HA_CLONE_RESTART`: restart after network failure.
76    Restart,
77    /// Forward-compatible fallback.
78    Unknown,
79}
80
81impl HaCloneType {
82    /// Decode the C `enum Ha_clone_type` value (a `size_t` upstream).
83    #[must_use]
84    pub const fn from_raw(value: usize) -> Self {
85        match value {
86            0 => Self::Blocking,
87            1 => Self::Redo,
88            2 => Self::Page,
89            3 => Self::Hybrid,
90            4 => Self::MultiTask,
91            5 => Self::Restart,
92            _ => Self::Unknown,
93        }
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn clone_mode_known_values() {
103        assert_eq!(HaCloneMode::from_raw(0), HaCloneMode::Start);
104        assert_eq!(HaCloneMode::from_raw(1), HaCloneMode::Restart);
105        assert_eq!(HaCloneMode::from_raw(2), HaCloneMode::AddTask);
106        assert_eq!(HaCloneMode::from_raw(3), HaCloneMode::Version);
107        assert_eq!(HaCloneMode::from_raw(4), HaCloneMode::Max);
108        assert_eq!(HaCloneMode::from_raw(99), HaCloneMode::Start);
109    }
110
111    #[test]
112    fn clone_type_known_values() {
113        assert_eq!(HaCloneType::from_raw(0), HaCloneType::Blocking);
114        assert_eq!(HaCloneType::from_raw(1), HaCloneType::Redo);
115        assert_eq!(HaCloneType::from_raw(2), HaCloneType::Page);
116        assert_eq!(HaCloneType::from_raw(3), HaCloneType::Hybrid);
117        assert_eq!(HaCloneType::from_raw(4), HaCloneType::MultiTask);
118        assert_eq!(HaCloneType::from_raw(5), HaCloneType::Restart);
119        assert_eq!(HaCloneType::from_raw(99), HaCloneType::Unknown);
120    }
121}