Struct Writer
pub(super) struct Writer {
file: Option<JournalFile>,
header_written: bool,
}tokio only.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: boolWhether the header has been written. Left false while the file is
empty. An unused journal remains a zero-byte file.
Implementations§
§impl Writer
impl Writer
pub(super) async fn open(path: PathBuf) -> Result<Self>
pub(super) async fn open(path: PathBuf) -> Result<Self>
Opens (or creates) the journal file for appending.
§Concurrency
Open each journal file with at most one Writer at a time, because this
method does not acquire an advisory file lock. If you need
multi-process mutual exclusion, implement your own locking outside this
crate, such as a companion .lock file.
§Errors
Returns an I/O error if the file cannot be opened, or if it starts with a header that specifies a format version this crate does not write.
pub(super) async fn append(&mut self, buf: &mut Vec<u8>) -> Result<()>
pub(super) 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 does not allocate 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(super) async fn compact(&mut self, snapshot: Vec<u8>) -> Result<()>
pub(super) 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>
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 UnsafeUnpin for Writer
impl UnwindSafe for Writer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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