Skip to main content

SnapshotFsWriter

Struct SnapshotFsWriter 

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

A file system backed snapshot writer using atomic write-to-temp-then-rename.

The writer opens a temporary sibling file (target path + .tmp suffix) and writes encoded snapshot data to it using the same framing format as the WAL (version + length + crc32 + payload). On close(), the temp file is flushed, renamed atomically over the target path, and the parent directory is fsynced. If the writer is dropped without calling close(), the temp file is removed and the original snapshot (if any) remains intact.

Implementations§

Source§

impl SnapshotFsWriter

Source

pub fn new(path: PathBuf) -> Result<Self, SnapshotFsWriterInitError>

Creates a new snapshot writer at the given path.

Instead of opening the target path directly, a temporary sibling file (target path with .tmp suffix appended) is created and written to. The target file is only replaced atomically when close() is called, ensuring crash safety.

§Arguments
  • path - The filesystem path where the snapshot file should ultimately reside
§Errors

Returns SnapshotFsWriterInitError::IoError if the temporary file cannot be created or opened for writing.

Trait Implementations§

Source§

impl Drop for SnapshotFsWriter

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl SnapshotWriter for SnapshotFsWriter

Source§

fn write(&mut self, snapshot: &Snapshot) -> Result<(), SnapshotWriterError>

Write a snapshot to storage.

The snapshot is encoded using the same framing format as the WAL:

  • 4 bytes version (currently 4)
  • 4 bytes length of payload
  • 4 bytes CRC-32 of payload
  • JSON-serialized Snapshot
§Arguments
  • snapshot - The snapshot to write
§Errors

Returns SnapshotWriterError::Closed if the writer has been closed. Returns SnapshotWriterError::IoError if the write operation fails. Returns SnapshotWriterError::EncodeError if serialization fails.

Source§

fn flush(&mut self) -> Result<(), SnapshotWriterError>

Flushes pending writes to durable storage.

This ensures that all buffered data is written to disk before returning. The flush operation is synchronous and will block until the data is persisted.

§Errors

Returns SnapshotWriterError::Closed if the writer has been closed. Returns SnapshotWriterError::IoError if the flush operation fails.

Source§

fn close(self) -> Result<(), SnapshotWriterError>

Closes the writer, atomically replacing the target snapshot file.

This performs:

  1. sync_all() on the temp file to ensure data is durable
  2. rename(temp, target) for atomic replacement
  3. fsync on the parent directory to make the rename durable

Once closed, the writer cannot be used for further operations. If close() is not called (e.g. due to a crash or error), the temp file is cleaned up on drop and the original snapshot is preserved.

§Errors

Returns SnapshotWriterError::IoError if the flush, rename, or directory sync operation fails.

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> Instrument for T

Source§

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

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

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, 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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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