Struct queue_file::QueueFile [−][src]
pub struct QueueFile { /* fields omitted */ }
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("example.qf")
.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
Returns true if removing an element will also overwrite data with zero bytes.
If set to true removing an element will also overwrite data with zero bytes.
Returns true if every write to file will be followed by sync_data()
call.
If set to true every write to file will be followed by sync_data()
call.
Reads the eldest element. Returns OK(None)
if the queue is empty.
Clears this queue. Truncates the file to the initial size.