pub struct RecordLogWriter { /* private fields */ }Expand description
Append-only record log writer.
Single-writer assumption: unlike crate::walog::WalWriter, this type does
not use an advisory lockfile. Two writers on the same path will interleave
writes and corrupt the log. Callers are responsible for ensuring at most one
active writer per path.
§Example
use durability::recordlog::{RecordLogWriter, RecordLogReader, RecordLogReadMode};
use durability::storage::MemoryDirectory;
let dir = MemoryDirectory::arc();
let mut w = RecordLogWriter::new(dir.clone(), "log.bin");
w.append_postcard(&"hello").unwrap();
w.append_postcard(&"world").unwrap();
w.flush().unwrap();
let r = RecordLogReader::new(dir, "log.bin");
let msgs: Vec<String> = r.read_all_postcard(RecordLogReadMode::Strict).unwrap();
assert_eq!(msgs, vec!["hello", "world"]);Implementations§
Source§impl RecordLogWriter
impl RecordLogWriter
Sourcepub fn new(dir: impl Into<Arc<dyn Directory>>, path: impl Into<String>) -> Self
pub fn new(dir: impl Into<Arc<dyn Directory>>, path: impl Into<String>) -> Self
Create a record log writer that appends to path.
Sourcepub fn with_flush_policy(
dir: impl Into<Arc<dyn Directory>>,
path: impl Into<String>,
flush_policy: FlushPolicy,
) -> Self
pub fn with_flush_policy( dir: impl Into<Arc<dyn Directory>>, path: impl Into<String>, flush_policy: FlushPolicy, ) -> Self
Create a record log writer with an explicit flush policy.
Sourcepub fn with_options(
dir: impl Into<Arc<dyn Directory>>,
path: impl Into<String>,
flush_policy: FlushPolicy,
write_buffer_limit_bytes: usize,
) -> Self
pub fn with_options( dir: impl Into<Arc<dyn Directory>>, path: impl Into<String>, flush_policy: FlushPolicy, write_buffer_limit_bytes: usize, ) -> Self
Create a record log writer with explicit flush policy and write buffer size.
write_buffer_limit_bytes == 0 disables buffering (writes are issued on each append).
Sourcepub fn flush(&mut self) -> PersistenceResult<()>
pub fn flush(&mut self) -> PersistenceResult<()>
Flush the underlying writer (if one is open).
Sourcepub fn flush_and_sync(&mut self) -> PersistenceResult<()>
pub fn flush_and_sync(&mut self) -> PersistenceResult<()>
Flush buffered bytes and attempt to make the record log durable on stable storage.
This is an opt-in stronger guarantee than flush():
flush()is a visibility boundary (userspace → OS / underlying writer).flush_and_sync()additionally callssync_allon the underlying file.
Returns NotSupported if the underlying Directory does not provide file_path().
Sourcepub fn append_bytes(&mut self, payload: &[u8]) -> PersistenceResult<()>
pub fn append_bytes(&mut self, payload: &[u8]) -> PersistenceResult<()>
Append one record containing payload.
Sourcepub fn append_postcard<T: Serialize>(
&mut self,
value: &T,
) -> PersistenceResult<()>
pub fn append_postcard<T: Serialize>( &mut self, value: &T, ) -> PersistenceResult<()>
Append one postcard-encoded value as a record payload.