Skip to main content

FileStore

Struct FileStore 

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

A durable first-in first-out queue backed by one file per record.

Each append writes a record to its own sequence-numbered file, flushes it to disk, and atomically renames it into place, so a power loss mid-write leaves the queue consistent: a partially written record is never visible. pop reads and deletes the oldest record. The directory itself is the durable state, so a store reopened after a crash resumes with every record that was fully written.

Delivery is at-least-once: if the process stops between reading a record and deleting it, the next open returns that record again, so consumers must tolerate the occasional redelivery.

§Examples

use pamoja_core::Store;
use pamoja_sync::FileStore;

let mut store = FileStore::open("/var/lib/pamoja/outbox")?;
store.append(b"reading").await?;
if let Some(record) = store.pop().await? {
    // forward `record` over a transport, then it is gone from the queue
    let _ = record;
}

Implementations§

Source§

impl FileStore

Source

pub fn open(dir: impl AsRef<Path>) -> Result<Self>

Opens a store rooted at dir, creating the directory if needed.

Records left in the directory by a previous run are adopted in sequence order, so the queue resumes where it left off.

§Arguments
  • dir - the directory that holds the queue’s record files.
§Returns

A store ready to append and drain records.

§Errors

Returns Error::Io if the directory cannot be created or scanned.

Trait Implementations§

Source§

impl Store for FileStore

Source§

async fn append(&mut self, record: &[u8]) -> Result<()>

Appends a record to the back of the queue. Read more
Source§

async fn peek(&self) -> Result<Option<Vec<u8>>>

Returns the oldest record without removing it. Read more
Source§

async fn pop(&mut self) -> Result<Option<Vec<u8>>>

Removes and returns the oldest record in the queue. Read more
Source§

async fn len(&self) -> Result<usize>

Returns the number of records currently buffered. Read more
§

async fn is_empty(&self) -> Result<bool, Error>

Returns whether the queue currently holds no records. Read more

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.