use std::sync::Arc;
use crate::control::security::catalog::SystemCatalog;
pub use crate::control::security::catalog::surrogate_hwm::SURROGATE_HWM;
pub trait SurrogateHwmPersist: Send + Sync {
fn checkpoint(&self, hwm: u32) -> crate::Result<()>;
fn load(&self) -> crate::Result<u32>;
}
pub struct SystemCatalogHwm {
catalog: Arc<SystemCatalog>,
}
impl SystemCatalogHwm {
pub fn new(catalog: Arc<SystemCatalog>) -> Self {
Self { catalog }
}
}
impl SurrogateHwmPersist for SystemCatalogHwm {
fn checkpoint(&self, hwm: u32) -> crate::Result<()> {
self.catalog.put_surrogate_hwm(hwm)
}
fn load(&self) -> crate::Result<u32> {
self.catalog.get_surrogate_hwm()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn handle_roundtrip_via_catalog() {
let dir = tempfile::tempdir().unwrap();
let catalog = Arc::new(SystemCatalog::open(&dir.path().join("system.redb")).unwrap());
let p = SystemCatalogHwm::new(catalog);
assert_eq!(p.load().unwrap(), 0);
p.checkpoint(123).unwrap();
assert_eq!(p.load().unwrap(), 123);
}
}