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
impl SnapshotFsWriter
Sourcepub fn new(path: PathBuf) -> Result<Self, SnapshotFsWriterInitError>
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
impl Drop for SnapshotFsWriter
Source§impl SnapshotWriter for SnapshotFsWriter
impl SnapshotWriter for SnapshotFsWriter
Source§fn write(&mut self, snapshot: &Snapshot) -> Result<(), SnapshotWriterError>
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>
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>
fn close(self) -> Result<(), SnapshotWriterError>
Closes the writer, atomically replacing the target snapshot file.
This performs:
sync_all()on the temp file to ensure data is durablerename(temp, target)for atomic replacementfsyncon 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.