use crate::custom_transaction_procedure::{CustomTxnProc, DefaultTxnProc, SetTxnProc};
pub type TxnProcFactory = fn() -> CustomTxnProc;
#[derive(Debug, Clone, Copy)]
pub struct StaticTxnProc {
pub name: &'static str,
pub arity: i32,
pub factory: TxnProcFactory,
}
pub mod txn_proc_slot {
pub const DEFAULT: u8 = 0;
pub const SET: u8 = 1;
}
pub const TXN_PROCS: [Option<StaticTxnProc>; 2] = [
Some(StaticTxnProc {
name: "default",
arity: 0,
factory: || {
CustomTxnProc::Default(DefaultTxnProc {
id: txn_proc_slot::DEFAULT,
})
},
}),
Some(StaticTxnProc {
name: "setx",
arity: 0,
factory: || {
CustomTxnProc::Set(SetTxnProc {
id: txn_proc_slot::SET,
args: Vec::new(),
})
},
}),
];
pub const fn txn_proc(id: u8) -> Option<&'static StaticTxnProc> {
if (id as usize) < TXN_PROCS.len() {
match &TXN_PROCS[id as usize] {
Some(entry) => Some(entry),
None => None,
}
} else {
None
}
}
#[cfg(test)]
mod tests {
use wtxn::TxnProcedure;
use super::*;
use crate::custom_transaction_procedure::CustomTransactionProcedure;
#[test]
fn table_indexes_by_slot_id() {
assert_eq!(txn_proc(txn_proc_slot::DEFAULT).unwrap().name, "default");
assert_eq!(txn_proc(txn_proc_slot::SET).unwrap().name, "setx");
assert!(txn_proc(9).is_none());
}
#[test]
fn factory_rebuilds_proc_with_slot_id() {
let built = (txn_proc(txn_proc_slot::SET).unwrap().factory)();
assert_eq!(built.id(), txn_proc_slot::SET);
let built = (txn_proc(txn_proc_slot::DEFAULT).unwrap().factory)();
assert_eq!(built.id(), txn_proc_slot::DEFAULT);
}
#[test]
fn set_proc_binds_args() {
let mut proc = (txn_proc(txn_proc_slot::SET).unwrap().factory)();
proc.bind_args(&[b"k".to_vec(), b"v".to_vec()]);
assert!(matches!(
&proc,
CustomTxnProc::Set(p) if p.args == vec![b"k".to_vec(), b"v".to_vec()]
));
}
}