mysql_handler/engine/parallel_scan_init.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//! Outcome of [`StorageEngine::parallel_scan_init`].
24
25use core::ffi::c_void;
26
27/// The two outputs of [`StorageEngine::parallel_scan_init`]: the engine-owned
28/// scan context that every later `parallel_scan*` call receives back, and the
29/// number of worker threads the engine will drive.
30///
31/// The context pointer is round-tripped through MySQL verbatim; the binding
32/// never dereferences it and the engine owns its lifetime (freed in
33/// [`StorageEngine::parallel_scan_end`]).
34///
35/// [`StorageEngine::parallel_scan_init`]: crate::engine::StorageEngine::parallel_scan_init
36/// [`StorageEngine::parallel_scan_end`]: crate::engine::StorageEngine::parallel_scan_end
37#[derive(Debug)]
38#[non_exhaustive]
39pub struct ParallelScanInit {
40 scan_ctx: *mut c_void,
41 num_threads: usize,
42}
43
44impl ParallelScanInit {
45 /// Build the outcome from the engine's scan context and worker-thread count.
46 /// A null `scan_ctx` with `num_threads == 0` signals "no parallel scan".
47 #[must_use]
48 pub fn new(scan_ctx: *mut c_void, num_threads: usize) -> Self {
49 Self {
50 scan_ctx,
51 num_threads,
52 }
53 }
54
55 /// Engine-owned scan-context pointer handed back to MySQL
56 #[must_use]
57 pub fn scan_ctx(&self) -> *mut c_void {
58 self.scan_ctx
59 }
60
61 /// Number of worker threads the engine will drive for the scan
62 #[must_use]
63 pub fn num_threads(&self) -> usize {
64 self.num_threads
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn exposes_context_and_thread_count() {
74 let mut sentinel = 0u8;
75 let ptr: *mut c_void = (&raw mut sentinel).cast();
76 let init = ParallelScanInit::new(ptr, 4);
77 assert_eq!(init.scan_ctx(), ptr);
78 assert_eq!(init.num_threads(), 4);
79 }
80
81 #[test]
82 fn null_context_signals_no_parallel_scan() {
83 let init = ParallelScanInit::new(core::ptr::null_mut(), 0);
84 assert!(init.scan_ctx().is_null());
85 assert_eq!(init.num_threads(), 0);
86 }
87}