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, and open clears what one left behind. On
Linux and other Unix systems the directory is synced after the rename as well,
so an append that returned is on disk. On Windows the directory entry is left
to the file system, so an append that returned just before a power cut can be
missing afterwards, though never half written. pop reads and
deletes the oldest record.
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
impl FileStore
Sourcepub fn open(dir: impl AsRef<Path>) -> Result<Self>
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, and a record a power cut interrupted mid-write is deleted.
§Arguments
dir- the directory that holds the queue’s record files.
§Returns
A store with no limit on how many records it holds, ready to append and drain records.
§Errors
Returns Error::Io if the directory cannot be
created or scanned, or an interrupted record cannot be deleted.
Sourcepub fn open_with_capacity(
dir: impl AsRef<Path>,
capacity: usize,
) -> Result<Self>
pub fn open_with_capacity( dir: impl AsRef<Path>, capacity: usize, ) -> Result<Self>
Opens a store rooted at dir that holds at most capacity records.
An append that would take the store past capacity is refused rather
than written, which on a device writing to an SD card is what keeps a long
outage from filling the card. A store reopened with more records than
capacity keeps them all and refuses appends until it is drained below it.
§Arguments
dir- the directory that holds the queue’s record files.capacity- the most records the store holds.
§Returns
A bounded store, ready to append and drain records.
§Errors
As for open.