Writer

Struct Writer 

pub struct Writer {
    file: Option<JournalFile>,
    header_written: bool,
}
Expand description

Append-only writer for a journal file with compaction support.

Fields§

§file: Option<JournalFile>

None only transiently while append’s blocking operation owns the handle, or permanently after that operation panicked, or after compact renamed the journal but could not reopen it (in which case further ops error).

§header_written: bool

Whether the header has been written. Left false while the file is empty, so an unused journal stays a zero-byte file.

Implementations§

§

impl Writer

pub async fn open(path: PathBuf) -> Result<Self>

Opens (or creates) the journal file for appending.

§Concurrency

Only one Writer should hold a given journal file open at a time. This method does not acquire an advisory file lock; callers that need multi-process mutual exclusion must implement their own locking (e.g. a companion .lock file) outside this crate.

§Errors

Returns an I/O error if the file cannot be opened, or if it starts with a header naming a format version this crate does not write.

pub async fn append(&mut self, buf: &mut Vec<u8>) -> Result<()>

Appends framed journal bytes and fsyncs, writing the file header first if this is the first data written to the file.

Moves buf’s contents to the blocking pool without a copy and restores them into buf afterwards, on success and on a write error alike, so the caller can refill and reuse the same allocation; a steady stream of flushes therefore allocates nothing once warm. The bytes are produced by encode_frames.

A write or sync failure partway through is rolled back to the file’s pre-append length, so the file always ends at a clean boundary and a retried append never layers new bytes after a torn write.

§Errors

Returns an I/O error if the write or sync fails.

pub async fn compact(&mut self, snapshot: Vec<u8>) -> Result<()>

Atomically replaces the journal with snapshot, a freshly framed encoding of the whole session state.

Writes the header (unless snapshot is empty) followed by the snapshot to a temp path, fsyncs it, then renames it over the existing journal. A crash during compaction never corrupts the existing file. Produce snapshot by framing snapshot_ops with encode_frames.

§Errors

Returns an I/O error if any file operation fails. A failure writing or syncing the temp file leaves the writer exactly as usable as before this call, since its existing handle is never touched until the rename below succeeds. A failure reopening the file after that rename is the one case that leaves the writer without a handle: see file.

fn take_file(&mut self) -> Result<JournalFile>

Takes the file handle for a blocking operation, erroring if a previous operation left the writer without one (only possible after a panic, or after compact failed to reopen the file post-compaction).

Auto Trait Implementations§

§

impl Freeze for Writer

§

impl RefUnwindSafe for Writer

§

impl Send for Writer

§

impl Sync for Writer

§

impl Unpin for Writer

§

impl UnwindSafe for Writer

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.

§

impl<T> Instrument for T

§

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

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

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,