lora_database/wal/
admin.rs1use std::path::Path;
9
10use lora_store::{InMemoryGraph, SnapshotMeta};
11use lora_wal::Lsn;
12
13use crate::database::Database;
14use crate::error::{LoraError, LoraErrorCode};
15
16pub trait WalAdmin: Send + Sync + 'static {
21 fn checkpoint(&self, path: &Path) -> Result<SnapshotMeta, LoraError>;
25
26 fn wal_status(&self) -> Result<WalStatus, LoraError>;
29
30 fn wal_truncate(&self, fence_lsn: u64) -> Result<(), LoraError>;
32}
33
34#[derive(Debug, Clone)]
41pub struct WalStatus {
42 pub durable_lsn: u64,
43 pub next_lsn: u64,
44 pub active_segment_id: u64,
45 pub oldest_segment_id: u64,
46 pub bg_failure: Option<String>,
47}
48
49impl WalAdmin for Database<InMemoryGraph> {
50 fn checkpoint(&self, path: &Path) -> Result<SnapshotMeta, LoraError> {
51 self.checkpoint_to(path)
52 }
53
54 fn wal_status(&self) -> Result<WalStatus, LoraError> {
55 let recorder = self
56 .wal
57 .as_ref()
58 .ok_or_else(|| LoraError::new(LoraErrorCode::Internal, "WAL not enabled"))?;
59 let wal = recorder.wal();
60 Ok(WalStatus {
61 durable_lsn: wal.durable_lsn().raw(),
62 next_lsn: wal.next_lsn().raw(),
63 active_segment_id: wal.active_segment_id(),
64 oldest_segment_id: wal.oldest_segment_id(),
65 bg_failure: wal.bg_failure(),
66 })
67 }
68
69 fn wal_truncate(&self, fence_lsn: u64) -> Result<(), LoraError> {
70 let recorder = self
71 .wal
72 .as_ref()
73 .ok_or_else(|| LoraError::new(LoraErrorCode::Internal, "WAL not enabled"))?;
74 recorder.truncate_up_to(Lsn::new(fence_lsn))?;
75 Ok(())
76 }
77}