Skip to main content

mysql_handler/hton/
xa_state_list_collector.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//! Writer handle the engine uses to push prepared-in-TC XA transactions
24//! into MySQL's `Xa_state_list` during `recover_prepared_in_tc`.
25
26#![allow(unsafe_code)]
27
28use core::ffi::c_void;
29
30use crate::hton::RecoverXaState;
31
32unsafe extern "C" {
33    fn mysql__xa_state_list__add(
34        xa_list: *mut c_void,
35        format_id: i64,
36        gtrid_ptr: *const u8,
37        gtrid_len: usize,
38        bqual_ptr: *const u8,
39        bqual_len: usize,
40        state: i32,
41    );
42}
43
44/// Writeable handle into MySQL's `Xa_state_list`. The engine constructs
45/// XIDs from its persistent storage and pushes each (XID, state) pair via
46/// [`Self::add`]; the shim copies the data into a `XID` instance and calls
47/// `Xa_state_list::add` on the underlying map.
48#[derive(Debug)]
49pub struct XaStateListCollector {
50    xa_list: *mut c_void,
51}
52
53impl XaStateListCollector {
54    /// Construct a collector that writes into the MySQL-owned
55    /// `Xa_state_list` pointed to by `xa_list`. The pointer must outlive
56    /// every call to [`Self::add`] (it always does — the shim only hands
57    /// it to the engine for the duration of a single
58    /// `recover_prepared_in_tc` callback).
59    pub(crate) fn new(xa_list: *mut c_void) -> Self {
60        Self { xa_list }
61    }
62
63    /// Push a single prepared XA transaction into the list. `gtrid` and
64    /// `bqual` are the X/Open distributed transaction identifier and
65    /// branch qualifier; together they must fit within `XIDDATASIZE`
66    /// (128 bytes) and each individually within 64 bytes per the X/Open
67    /// spec. The shim copies the data into a fresh `XID` and forwards
68    /// it to `Xa_state_list::add`.
69    pub fn add(&mut self, format_id: i64, gtrid: &[u8], bqual: &[u8], state: RecoverXaState) {
70        // SAFETY: xa_list is the MySQL-owned pointer the shim handed us
71        // for this callback only; the slice pointers point into Rust
72        // memory the C++ side only reads (and copies) before returning.
73        unsafe {
74            mysql__xa_state_list__add(
75                self.xa_list,
76                format_id,
77                gtrid.as_ptr(),
78                gtrid.len(),
79                bqual.as_ptr(),
80                bqual.len(),
81                state.to_raw(),
82            );
83        }
84    }
85}