mysql_handler/engine/sampling_method.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//! Sampling algorithm selector, mirroring MySQL's `enum_sampling_method`.
24
25/// Sampling algorithm requested for [`StorageEngine::sample_init`], mirroring
26/// MySQL's `enum_sampling_method`.
27///
28/// [`StorageEngine::sample_init`]: crate::engine::StorageEngine::sample_init
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30#[non_exhaustive]
31pub enum SamplingMethod {
32 /// `SYSTEM` sampling: page/block-level, the only method MySQL supports today
33 System,
34 /// No sampling method selected
35 None,
36}
37
38impl SamplingMethod {
39 /// Map the raw `enum_sampling_method` integer supplied at the FFI boundary
40 /// to a variant. Any value other than `SYSTEM` (`0`) becomes
41 /// [`SamplingMethod::None`] so the engine never observes an undefined value.
42 pub(crate) fn from_raw(raw: i32) -> Self {
43 match raw {
44 0 => Self::System,
45 _ => Self::None,
46 }
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn system_maps_from_zero() {
56 assert_eq!(SamplingMethod::from_raw(0), SamplingMethod::System);
57 }
58
59 #[test]
60 fn non_system_codes_become_none() {
61 assert_eq!(SamplingMethod::from_raw(1), SamplingMethod::None);
62 assert_eq!(SamplingMethod::from_raw(-1), SamplingMethod::None);
63 assert_eq!(SamplingMethod::from_raw(42), SamplingMethod::None);
64 }
65}