Struct queue_file::QueueFile

source ·
pub struct QueueFile { /* private fields */ }
Expand description

QueueFile is a lightning-fast, transactional, file-based FIFO.

Addition and removal from an instance is an O(1) operation and is atomic. Writes are synchronous by default; data will be written to disk before an operation returns.

The underlying file. Uses a ring buffer to store entries. Designed so that a modification isn’t committed or visible until we write the header. The header is much smaller than a segment. So long as the underlying file system supports atomic segment writes, changes to the queue are atomic. Storing the file length ensures we can recover from a failed expansion (i.e. if setting the file length succeeds but the process dies before the data can be copied).

Example

use queue_file::QueueFile;

let mut qf = QueueFile::open(path)
    .expect("cannot open queue file");
let data = "Welcome to QueueFile!".as_bytes();

qf.add(&data).expect("add failed");

if let Ok(Some(bytes)) = qf.peek() {
    assert_eq!(data, bytes.as_ref());
}

qf.remove().expect("remove failed");

File format

  16-32 bytes      Header
  ...              Data

This implementation supports two versions of the header format.

Versioned Header (32 bytes):
  1 bit            Versioned indicator [0 = legacy, 1 = versioned]
  31 bits          Version, always 1
  8 bytes          File length
  4 bytes          Element count
  8 bytes          Head element position
  8 bytes          Tail element position

Legacy Header (16 bytes):
  1 bit            Legacy indicator, always 0
  31 bits          File length
  4 bytes          Element count
  4 bytes          Head element position
  4 bytes          Tail element position

Each element stored is represented by:

Element:
  4 bytes          Data length
  ...              Data

Implementations§

source§

impl QueueFile

source

pub fn with_capacity<P: AsRef<Path>>( path: P, capacity: u64 ) -> Result<Self, Error>

Open or create QueueFile at path with specified minimal file size.

Example
let qf = QueueFile::with_capacity(path, 120).expect("failed to open queue");
source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Open or create QueueFile at path.

Example
let qf = QueueFile::open(path).expect("failed to open queue");
source

pub fn open_legacy<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Open or create QueueFile at path forcing legacy format.

Example
let qf = QueueFile::open_legacy(path).expect("failed to open queue");
source

pub const fn overwrite_on_remove(&self) -> bool

Returns true if removing an element will also overwrite data with zero bytes.

source

pub const fn get_overwrite_on_remove(&self) -> bool

👎Deprecated since 1.4.7: Use <code>overwrite_on_remove</code> instead.
source

pub fn set_overwrite_on_remove(&mut self, value: bool)

If set to true removing an element will also overwrite data with zero bytes.

source

pub const fn sync_writes(&self) -> bool

Returns true if every write to file will be followed by sync_data() call.

source

pub const fn get_sync_writes(&self) -> bool

👎Deprecated since 1.4.7: Use <code>sync_writes</code> instead.
source

pub fn set_sync_writes(&mut self, value: bool)

If set to true every write to file will be followed by sync_data() call.

source

pub const fn skip_write_header_on_add(&self) -> bool

Returns true if skips header update upon adding enabled.

source

pub const fn get_skip_write_header_on_add(&self) -> bool

👎Deprecated since 1.4.7: Use <code>skip_write_header_on_add</code> instead.
source

pub fn set_skip_write_header_on_add(&mut self, value: bool)

If set to true skips header update upon adding.

source

pub fn set_read_buffer_size(&mut self, size: usize)

Changes buffer size used for data reading.

source

pub const fn cache_offset_policy(&self) -> Option<OffsetCacheKind>

source

pub const fn get_cache_offset_policy(&self) -> Option<OffsetCacheKind>

👎Deprecated since 1.4.7: Use <code>cache_offset_policy</code> instead.
source

pub fn set_cache_offset_policy( &mut self, kind: impl Into<Option<OffsetCacheKind>> )

source

pub const fn is_empty(&self) -> bool

Returns true if this queue contains no entries.

source

pub const fn size(&self) -> usize

Returns the number of elements in this queue.

source

pub fn sync_all(&mut self) -> Result<(), Error>

Synchronizes the underlying file, look at File::sync_all doc for more info.

source

pub fn add_n( &mut self, elems: impl IntoIterator<Item = impl AsRef<[u8]>> + Clone ) -> Result<(), Error>

source

pub fn add(&mut self, buf: &[u8]) -> Result<(), Error>

Adds an element to the end of the queue.

source

pub fn peek(&mut self) -> Result<Option<Box<[u8]>>, Error>

Reads the eldest element. Returns OK(None) if the queue is empty.

source

pub fn remove(&mut self) -> Result<(), Error>

Removes the eldest element.

source

pub fn remove_n(&mut self, n: usize) -> Result<(), Error>

Removes the eldest n elements.

source

pub fn clear(&mut self) -> Result<(), Error>

Clears this queue. Truncates the file to the initial size.

source

pub fn iter(&mut self) -> Iter<'_>

Returns an iterator over elements in this queue.

Example
let mut qf = QueueFile::open(path).expect("failed to open queue");
let items = vec![vec![1, 2], vec![], vec![3]];
qf.add_n(&items).expect("failed to add elements to queue");

let stored = qf.iter().map(Vec::from).collect::<Vec<_>>();
assert_eq!(items, stored);
source

pub const fn file_len(&self) -> u64

Returns the amount of bytes used by the backed file. Always >= Self::used_bytes.

source

pub const fn used_bytes(&self) -> u64

Returns the amount of bytes used by the queue.

source

pub fn into_inner_file(self) -> File

Returns underlying file of the queue.

Trait Implementations§

source§

impl Debug for QueueFile

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for QueueFile

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.