Struct AppendOnlyFile

Source
pub struct AppendOnlyFile<T> { /* private fields */ }
Expand description

Wrapper for a file that can be read at any position (random read) but for which writes are append only. Reads are backed by a memory map (mmap(2)), relying on the operating system for fast access and caching. The memory map is reallocated to expand it when new writes are flushed.

Despite being append-only, the file can still be pruned and truncated. The former simply happens by rewriting it, ignoring some of the data. The latter by truncating the underlying file and re-creating the mmap.

Implementations§

Source§

impl<T> AppendOnlyFile<T>
where T: Debug + Readable + Writeable,

Source

pub fn open<P>( path: P, size_info: SizeInfo, version: ProtocolVersion, ) -> Result<AppendOnlyFile<T>>
where P: AsRef<Path> + Debug,

Open a file (existing or not) as append-only, backed by a mmap.

Source

pub fn init(&mut self) -> Result<()>

(Re)init an underlying file and its associated memmap. Taking care to initialize the mmap_offset_cache for each element.

Source

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

Append data to the file. Until the append-only file is synced, data is only written to memory.

Source

pub fn rewind(&mut self, pos: u64)

Rewinds the data file back to a previous position. We simply “rewind” the buffer_start_pos to the specified position. Note: We do not currently support rewinding within the buffer itself.

Source

pub fn flush(&mut self) -> Result<()>

Syncs all writes (fsync), reallocating the memory map to make the newly written data accessible.

Source

pub fn discard(&mut self)

Discard the current non-flushed data.

Source

pub fn read(&self, pos: u64) -> Result<&[u8]>

Read the bytes representing the element at the given position (0-indexed). Uses the offset cache to determine the offset to read from and the size in bytes to actually read. Leverages the memory map.

Source

pub fn as_temp_file(&self) -> Result<File>

Create a new tempfile containing the contents of this append only file. This allows callers to see a consistent view of the data without locking the append only file.

Source

pub fn write_tmp_pruned(&self, prune_pos: &[u64]) -> Result<()>

Saves a copy of the current file content, skipping data at the provided prune positions. prune_pos must be ordered.

Source

pub fn replace_with_tmp(&mut self) -> Result<()>

Replace the underlying file with the file at tmp path. Rebuild and initialize from the new file.

Source

pub fn release(&mut self)

Release underlying file handles.

Source

pub fn size(&self) -> Result<u64>

Current size of the file in bytes.

Source

pub fn path(&self) -> &Path

Path of the underlying file

Auto Trait Implementations§

§

impl<T> Freeze for AppendOnlyFile<T>

§

impl<T> RefUnwindSafe for AppendOnlyFile<T>
where T: RefUnwindSafe,

§

impl<T> Send for AppendOnlyFile<T>
where T: Send,

§

impl<T> Sync for AppendOnlyFile<T>
where T: Sync,

§

impl<T> Unpin for AppendOnlyFile<T>
where T: Unpin,

§

impl<T> UnwindSafe for AppendOnlyFile<T>
where T: UnwindSafe,

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> SafeBorrow<T> for T
where T: ?Sized,

Source§

fn borrow_replacement(ptr: &T) -> &T

Given ptr, which was obtained from a prior call to Self::borrow(), return a value with the same nominal lifetime which is guaranteed to survive mutations to Self. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V