Skip to main content

SnapshotWriter

Struct SnapshotWriter 

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

Append-only snapshot writer.

Streams KvUpdate records to disk via a buffered writer. No in-memory state beyond the file handle and a byte counter for compaction triggering.

Compacts automatically when bytes written since last compaction exceeds compact_threshold. Compaction replays the log into a transient HashMap, rewrites via tempfile+rename, and reopens for append.

Implementations§

Source§

impl SnapshotWriter

Source

pub fn open(path: &Path, compact_threshold: u64) -> Result<Self, SnapshotError>

Open or create a snapshot log.

If the file doesn’t exist, writes the header. If it exists, opens for append. compact_threshold controls how many bytes accumulate before an automatic compaction.

Source

pub fn write_update(&mut self, update: &KvUpdate) -> Result<(), SnapshotError>

Write a single KvUpdate record to the log.

Buffered — does not flush to disk until checkpoint.

Source

pub fn checkpoint( &mut self, cursor: &WatchCursor, ) -> Result<bool, SnapshotError>

Write a cursor checkpoint and flush the buffer to the OS.

The flush is a write(2) into the page cache — it survives a process crash, but NOT power loss: there is no fsync on this path. The durable sync_all happens in compact. That’s deliberate — the snapshot is a cache backed by NATS and checkpoints are frequent, so an fsync per checkpoint isn’t worth its latency; a tail lost to power loss is rebuilt from a NATS scan + watch replay.

Returns true when the log has grown past the compaction threshold and the caller should run compact. Separating the check from the I/O lets async callers offload compaction to a blocking task instead of stalling the executor.

Source

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

Flush the buffer to disk without writing a cursor record.

Source

pub fn compact(&mut self) -> Result<(), SnapshotError>

Compact the snapshot log: replay, deduplicate, and rewrite.

Performs synchronous file I/O. In async contexts, run via spawn_blocking to avoid stalling the executor.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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.

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

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

Source§

fn vzip(self) -> V

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