Skip to main content

WalWriter

Struct WalWriter 

Source
pub struct WalWriter { /* private fields */ }
Expand description

Append-only WAL writer.

Opens (or creates) a file in append mode and writes one JSONL line per event, using file locking for concurrent access safety and fsync after each write for durability.

§Example

use runtimo_core::{WalWriter, WalEvent, WalEventType};
use std::path::Path;

let mut wal = WalWriter::create(Path::new("/tmp/app.wal")).unwrap();
wal.append(WalEvent {
    seq: 0, ts: 1715800000,
    event_type: WalEventType::JobStarted,
    job_id: "job1".into(),
    capability: None, output: None, error: None,
}).unwrap();

Implementations§

Source§

impl WalWriter

Source

pub fn create(path: &Path) -> Result<Self>

Creates or opens a WAL file at the given path.

The file is opened in append mode. Existing content is preserved.

§Errors

Returns Error::WalError if the file cannot be created or opened.

Source

pub fn append(&mut self, event: WalEvent) -> Result<()>

Appends an event to the WAL using true append mode (P0 FIX).

Opens the file in append mode, acquires an exclusive lock, writes the JSONL line, fsyncs, then releases the lock. This is O(1) per write instead of O(N) read-rewrite, and the lock is held during the entire write operation preventing concurrent write loss.

Increments the internal sequence counter after a successful write.

§Errors

Returns Error::WalError on serialization or I/O failure.

Source

pub fn seq(&self) -> u64

Returns the current sequence number (next event will use this value).

Source§

impl WalWriter

WAL cleanup and rotation utilities.

Source

pub fn rotate( path: &Path, max_size_bytes: u64, max_rotations: usize, ) -> Result<()>

Rotates the WAL when it exceeds max_size_bytes.

Moves the current WAL to {path}.1 (shifting older rotations), then creates a fresh empty WAL. Keeps at most max_rotations old files. FINDING #15: basic WAL rotation to prevent unbounded growth.

Source

pub fn cleanup(path: &Path, max_age_secs: u64) -> Result<usize>

Cleans up WAL entries older than max_age_secs.

Writes retained events to a temporary file, then atomically renames it over the original WAL. This prevents event loss if another writer appends during cleanup.

§Arguments
  • path - Path to WAL file
  • max_age_secs - Maximum age in seconds
§Returns
  • Ok(usize) - Number of entries removed
  • Err(Error) - Cleanup failure

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.