Skip to main content

RedbBackend

Struct RedbBackend 

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

On-disk EventStore backed by a per-run [redb] file.

One backend instance owns at most one open run at a time. Opening a fresh run creates <base>/<instance_id>/<run_id>.redb and writes the manifest with status RunStatus::Running before returning. Reopening a path whose manifest is still RunStatus::Running returns EventStoreError::CrashedPredecessor; the caller seals it as RunStatus::CrashedRecovered (or RunStatus::Quarantined) and then opens a new run, mirroring the in-memory backend’s contract.

Implementations§

Source§

impl RedbBackend

Source

pub fn new(base_dir: impl Into<PathBuf>) -> Self

Creates a new RedbBackend rooted at base_dir.

The backend creates <base_dir>/<instance_id>/ lazily on the first EventStore::open_run call.

Source

pub fn run_dir(&self, instance_id: &str) -> PathBuf

Returns the directory the backend writes run files to for instance_id.

Source

pub fn run_path(&self, instance_id: &str, run_id: &str) -> PathBuf

Returns the on-disk path the backend uses for (instance_id, run_id).

Source

pub fn current_path(&self) -> Result<&Path, EventStoreError>

Returns the path of the currently open run file.

§Errors

Returns EventStoreError::Backend when no run is open.

Source

pub fn open_sealed( base_dir: impl Into<PathBuf>, instance_id: &str, run_id: &str, ) -> Result<Self, EventStoreError>

Opens the sealed run file at <base>/<instance_id>/<run_id>.redb for read-only replay.

§Design

The standard EventStore::open_run path rejects sealed files: that is the crash-recovery guard, a successor must not silently reopen a predecessor’s log without going through seal. Event-store replay is the legitimate case for touching a sealed file, so the reader uses this constructor instead.

The shared EventStore trait is held intentionally narrow and is locked by design; adding a sealed-open method to it would force the in-memory backend (whose sealed runs stay readable in place without a reopen step) to carry a useless second entry point, and would conflate the writer’s open-or-recover lifecycle with the reader’s pure read-only path. The sealed-open path therefore lives as a backend-specific constructor: each backend adds the entry points it actually needs. The resulting RedbBackend still implements EventStore, so the reader composes over the locked trait without pulling in writer-only methods. crate::backend::MemoryBackend has no equivalent constructor: a sealed in-memory run keeps its state accessible to any reader holding the backend instance, and the reader receives that instance directly.

The returned backend holds a read-only database handle, rejects EventStore::append_batch with EventStoreError::Closed (the manifest is already sealed), and exposes every read path: EventStore::scan_range, EventStore::scan_seq, EventStore::lookup, and EventStore::manifest.

§Errors

Returns EventStoreError::Backend when the run file does not exist or its status is not a sealed terminal state (use EventStore::open_run for that path); EventStoreError::Corrupted when the run file lacks a manifest or fails to decode.

Source

pub fn open_sealed_file( path: impl Into<PathBuf>, ) -> Result<Self, EventStoreError>

Opens a sealed redb run file directly by path for read-only replay or verification.

§Errors

Returns EventStoreError::Backend when the run file does not exist or its status is not a sealed terminal state (use EventStore::open_run for that path); EventStoreError::Corrupted when the run file lacks a manifest or fails to decode.

Source

pub fn list_runs( base_dir: &Path, instance_id: &str, ) -> Result<Vec<RunManifest>, EventStoreError>

Lists the manifests of every run file under <base_dir>/<instance_id>/*.redb.

Used by the reader for forensics navigation across runs without requiring an active backend instance per run. The result is sorted by start_ts_init so chronologically-newer runs appear last.

Opens each run file with a read-only database handle. A run file whose process died hard (kill, OOM, power loss) lacks redb’s allocator-state table and refuses the read-only open; the listing falls back to a writable open, which performs redb’s repair pass and leaves the file readable again. Files that still cannot be opened or that lack a manifest are skipped with a logged error so one current-format damaged file cannot block recovery or retention over the healthy runs; such files never become recovery parents or reclaim candidates and are left in place for manual inspection. Unsupported store formats are returned as errors rather than skipped because they require operator action.

§Errors

Returns EventStoreError::Backend when the directory iterator fails, or EventStoreError::Corrupted when a run file uses an unsupported store format.

Trait Implementations§

Source§

impl Debug for RedbBackend

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl EventStore for RedbBackend

Source§

fn open_run(&mut self, manifest: RunManifest) -> Result<(), EventStoreError>

Opens an existing run or creates a new one with the supplied manifest. Read more
Source§

fn append_batch( &mut self, entries: &[AppendEntry], ) -> Result<u64, EventStoreError>

Appends a batch of (entry, index_keys) pairs in a single backend transaction. Read more
Source§

fn scan_range( &self, from: u64, to: u64, direction: ScanDirection, ) -> Result<Vec<EventStoreEntry>, EventStoreError>

Scans entries by seq over the inclusive range [from, to]. Read more
Source§

fn scan_seq(&self, seq: u64) -> Result<Option<EventStoreEntry>, EventStoreError>

Reads a single entry by seq. Read more
Source§

fn lookup( &self, kind: IndexKind, key: &str, ) -> Result<Option<u64>, EventStoreError>

Looks up the first seq recorded under the given index key. Read more
Source§

fn iter_index_keys( &self, kind: IndexKind, ) -> Result<Vec<(String, u64)>, EventStoreError>

Enumerates every (key, seq) pair stored under the given secondary index. Read more
Source§

fn record_snapshot_anchor( &mut self, anchor: SnapshotAnchor, ) -> Result<(), EventStoreError>

Records the latest cache snapshot anchor for the open run. Read more
Source§

fn latest_snapshot_anchor( &self, ) -> Result<Option<SnapshotAnchor>, EventStoreError>

Returns the latest recorded snapshot anchor for the open run. Read more
Source§

fn seal(&mut self, status: RunStatus) -> Result<(), EventStoreError>

Seals the open run with the given final status and persists the manifest update. Read more
Source§

fn manifest(&self) -> Result<RunManifest, EventStoreError>

Returns the current manifest snapshot. Read more
Source§

fn high_watermark(&self) -> Result<u64, EventStoreError>

Returns the largest seq durably acknowledged for the open run. Read more

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<T> Ungil for T
where T: Send,

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more