Skip to main content

SegmentedStore

Struct SegmentedStore 

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

A WalStore that stripes one flat byte space across fixed-size segment files in a directory.

Open one with SegmentedStore::open and hand it to Wal::with_store, or use the Wal::open_segmented convenience constructor. Segments are created lazily as the log grows, and sync flushes every segment written since the last sync, not the whole history.

The segment size is recorded in a meta file next to the segments; opening with another size is refused rather than reading the bytes at the wrong offsets.

§Examples

use wal_db::{SegmentedStore, Wal};

// 1 MiB segments. Records larger than a segment simply span several.
let store = SegmentedStore::open(dir.path(), 1024 * 1024)?;
let wal = Wal::with_store(store)?;
wal.append(b"spans nothing yet")?;
wal.sync()?;

Implementations§

Source§

impl SegmentedStore

Source

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

Open the segmented log in dir, creating the directory if needed, with segments of segment_size bytes.

Existing segment files are picked up so the log can be recovered. The store does not validate record contents — that is Wal::open’s recovery scan, which runs unchanged over the flat space. Every existing segment is flushed first: a process that stopped without syncing may have left bytes only in the page cache, and recovery must not trust what a power loss could still take away.

§Errors
  • WalError::Io if segment_size is zero, or if the directory or a segment cannot be created, read, or flushed.
  • WalError::Corruption if the log was written with another segment size, or if its first segments were dropped and the head marker that says where it now starts is missing or damaged. Nothing is deleted.
Source

pub fn dir(&self) -> &Path

The directory holding the segment files.

Source

pub fn segment_size(&self) -> u64

The configured segment size in bytes.

Trait Implementations§

Source§

impl Debug for SegmentedStore

Source§

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

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

impl WalStore for SegmentedStore

Source§

fn write_at(&self, offset: u64, bytes: &[u8]) -> Result<()>

Write bytes at byte offset, growing the store and zero-filling any gap if offset is beyond the current end.
Source§

fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize>

Read into buf starting at byte offset, returning the number of bytes read. Read more
Source§

fn truncate(&self, len: u64) -> Result<()>

Discard every byte at or after len, shrinking the store to exactly len bytes.
Source§

fn sync(&self) -> Result<()>

Flush every preceding write_at to stable storage. Read more
Source§

fn len(&self) -> Result<u64>

The current size of the store in bytes.
Source§

fn head(&self) -> Result<u64>

The lowest offset that still holds data. Read more
Source§

fn truncate_before(&self, offset: u64) -> Result<u64>

Discard storage entirely below offset, if the backend can, returning the new head. Read more
Source§

fn is_empty(&self) -> Result<bool>

Whether the store holds no bytes. Read more

Auto Trait Implementations§

§

impl !Freeze for SegmentedStore

§

impl Forget for SegmentedStore

§

impl Move for SegmentedStore

§

impl RefUnwindSafe for SegmentedStore

§

impl Send for SegmentedStore
where RawVecInner: Send, RwLock<HashMap<u64, Arc<Segment>>>: Send, Atomic<u64>: Send, Atomic<bool>: Send,

§

impl Sync for SegmentedStore
where RawVecInner: Sync, RwLock<HashMap<u64, Arc<Segment>>>: Sync, Atomic<u64>: Sync, Atomic<bool>: Sync,

§

impl Unpin for SegmentedStore
where *const u8 is !null: Unpin, Align4<u32>: Unpin, Align1<u8>: Unpin, Arc<Segment>: Unpin, Align8<u64>: Unpin,

§

impl UnsafeUnpin for SegmentedStore
where *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, Align4<u32>: UnsafeUnpin, Align1<u8>: UnsafeUnpin, PhantomData<(u64, Arc<Segment>)>: UnsafeUnpin, Align8<u64>: UnsafeUnpin,

§

impl UnwindSafe for SegmentedStore
where RawVecInner: UnwindSafe, RwLock<HashMap<u64, Arc<Segment>>>: UnwindSafe, Align8<u64>: UnwindSafe, Align1<u8>: 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error