use std::path::Path;
use lora_store::{InMemoryGraph, SnapshotMeta};
use lora_wal::Lsn;
use crate::database::Database;
use crate::error::{LoraError, LoraErrorCode};
pub trait WalAdmin: Send + Sync + 'static {
fn checkpoint(&self, path: &Path) -> Result<SnapshotMeta, LoraError>;
fn wal_status(&self) -> Result<WalStatus, LoraError>;
fn wal_truncate(&self, fence_lsn: u64) -> Result<(), LoraError>;
}
#[derive(Debug, Clone)]
pub struct WalStatus {
pub durable_lsn: u64,
pub next_lsn: u64,
pub active_segment_id: u64,
pub oldest_segment_id: u64,
pub bg_failure: Option<String>,
}
impl WalAdmin for Database<InMemoryGraph> {
fn checkpoint(&self, path: &Path) -> Result<SnapshotMeta, LoraError> {
self.checkpoint_to(path)
}
fn wal_status(&self) -> Result<WalStatus, LoraError> {
let recorder = self
.wal
.as_ref()
.ok_or_else(|| LoraError::new(LoraErrorCode::Internal, "WAL not enabled"))?;
let wal = recorder.wal();
Ok(WalStatus {
durable_lsn: wal.durable_lsn().raw(),
next_lsn: wal.next_lsn().raw(),
active_segment_id: wal.active_segment_id(),
oldest_segment_id: wal.oldest_segment_id(),
bg_failure: wal.bg_failure(),
})
}
fn wal_truncate(&self, fence_lsn: u64) -> Result<(), LoraError> {
let recorder = self
.wal
.as_ref()
.ok_or_else(|| LoraError::new(LoraErrorCode::Internal, "WAL not enabled"))?;
recorder.truncate_up_to(Lsn::new(fence_lsn))?;
Ok(())
}
}