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>
impl<T: Serialize + DeserializeOwned> Durable<T>
Sourcepub fn new(inner: T, block_lease: BlockLease) -> Self
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.
Sourcepub fn inner_mut(&mut self) -> &mut T
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.
Sourcepub fn set_auto_checkpoint(&mut self, batch_size: Option<u64>)
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.
Sourcepub fn mutate<F>(&mut self, f: F) -> Result<()>
pub fn mutate<F>(&mut self, f: F) -> Result<()>
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.
Sourcepub fn mutation_count(&self) -> u64
pub fn mutation_count(&self) -> u64
Returns the number of mutations since the last checkpoint (or since auto-checkpoint was enabled).
Sourcepub fn lease_id(&self) -> u128
pub fn lease_id(&self) -> u128
Returns the lease ID of the block lease for external renewal
management (e.g. via [grafos_leasekit::RenewalManager]).
Sourcepub fn expires_at_unix_secs(&self) -> u64
pub fn expires_at_unix_secs(&self) -> u64
Returns the expiry time (unix seconds) of the block lease for external renewal management.
Sourcepub fn into_block_lease(self) -> BlockLease
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.
Sourcepub fn checkpoint(&self) -> Result<()>
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
FabricError::CapacityExceededif the serialized data requires more blocks than available on the device.FabricError::IoErrorif serialization or block write fails.
Sourcepub fn restore(block_lease: BlockLease) -> Result<Durable<T>>
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
FabricError::IoError(-2)if the magic bytes do not match.FabricError::IoError(-3)if the version is unsupported.FabricError::IoError(-1)if deserialization fails.