Durable

Struct Durable 

Source
pub struct Durable<T> { /* private fields */ }
Expand description

A wrapper that checkpoints its inner value to block storage.

Durable<T> owns both the inner value and a BlockLease used for persistence. Calling checkpoint() serializes the inner value and writes it to the block lease. restore() reads a checkpoint and reconstructs the value.

Implements Deref<Target = T> and DerefMut, so you can access the inner value’s fields and methods directly through the wrapper.

§Example

use grafos_collections::durable::Durable;
use grafos_std::block::BlockBuilder;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Data { values: Vec<u32> }

let lease = BlockBuilder::new().acquire()?;
let mut d = Durable::new(Data { values: vec![1, 2, 3] }, lease);

// Deref access
assert_eq!(d.values.len(), 3);

// Mutate and checkpoint
d.inner_mut().values.push(4);
d.checkpoint()?;

Implementations§

Source§

impl<T: Serialize + DeserializeOwned> Durable<T>

Source

pub fn new(inner: T, block_lease: BlockLease) -> Self

Wrap a value with a block lease for checkpointing.

Does not write anything to the block lease. Call checkpoint() to persist the value.

Source

pub fn inner(&self) -> &T

Returns a reference to the inner value.

Source

pub fn inner_mut(&mut self) -> &mut T

Returns a mutable reference to the inner value.

This does not increment the mutation counter. Use mutate() if you want auto-checkpoint tracking.

Source

pub fn set_auto_checkpoint(&mut self, batch_size: Option<u64>)

Enable auto-checkpoint: after every batch_size calls to mutate(), the state is automatically checkpointed to block storage.

Pass 0 or call with None to disable auto-checkpoint.

Source

pub fn mutate<F>(&mut self, f: F) -> Result<()>
where F: FnOnce(&mut T),

Apply a mutation to the inner value and optionally auto-checkpoint.

The closure f receives &mut T and can modify it. After the closure returns, the mutation counter is incremented. If auto-checkpoint is enabled and the counter reaches the batch size, checkpoint() is called automatically and the counter resets to zero.

§Errors

Returns an error only if auto-checkpoint triggers and the checkpoint itself fails.

Source

pub fn mutation_count(&self) -> u64

Returns the number of mutations since the last checkpoint (or since auto-checkpoint was enabled).

Source

pub fn lease_id(&self) -> u128

Returns the lease ID of the block lease for external renewal management (e.g. via [grafos_leasekit::RenewalManager]).

Source

pub fn expires_at_unix_secs(&self) -> u64

Returns the expiry time (unix seconds) of the block lease for external renewal management.

Source

pub fn into_block_lease(self) -> BlockLease

Consume the Durable and return the block lease.

Useful for passing the same lease to restore() after a checkpoint, simulating a process restart that re-opens the same block device.

Source

pub fn checkpoint(&self) -> Result<()>

Serialize the inner value and write it to block storage.

Writes a header to block 0 (magic, version, data length) followed by the postcard-serialized data across blocks 1..N.

§Errors
Source

pub fn restore(block_lease: BlockLease) -> Result<Durable<T>>

Read a checkpoint from block storage and reconstruct the value.

Reads block 0, validates the magic ("DCHK") and version, then reads the data blocks and deserializes the value.

§Errors

Trait Implementations§

Source§

impl<T> Deref for Durable<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> DerefMut for Durable<T>

Source§

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

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<T> Freeze for Durable<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for Durable<T>

§

impl<T> !Send for Durable<T>

§

impl<T> !Sync for Durable<T>

§

impl<T> Unpin for Durable<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Durable<T>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.