Skip to main content

MmapStateStore

Struct MmapStateStore 

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

Memory-mapped state store implementation.

This store provides high-performance key-value storage with optional persistence via memory-mapped files. It achieves sub-500ns lookup latency by using BTreeMap for the index (enabling O(log n + k) prefix/range scans) and direct memory access for values.

§Modes

  • In-memory: Uses an arena allocator, fastest but not persistent
  • Persistent: Uses memory-mapped file, survives restarts

§Thread Safety

This store is Send but not Sync. It’s designed for single-threaded access within a reactor.

Implementations§

Source§

impl MmapStateStore

Source

pub fn in_memory(capacity: usize) -> Self

Creates a new in-memory state store with the given initial capacity.

This mode is the fastest but data is lost when the process exits.

§Arguments
  • capacity - Initial capacity in bytes for the data buffer
Source

pub fn persistent( path: &Path, initial_capacity: usize, ) -> Result<Self, StateError>

Creates a new persistent state store backed by a memory-mapped file.

If the file exists, it will be opened and validated. If it doesn’t exist, a new file will be created with the given initial capacity.

§Arguments
  • path - Path to the state file
  • initial_capacity - Initial file size if creating new
§Errors

Returns StateError::Io if file operations fail, or StateError::Corruption if the file exists but has an invalid format.

Source

pub fn is_persistent(&self) -> bool

Check if this store is persistent.

Source

pub fn path(&self) -> Option<&Path>

Get the path to the backing file (if persistent).

Source

pub fn compact(&mut self) -> Result<(), StateError>

Compact the store by rewriting live data.

This removes holes left by deleted entries and reduces file/memory usage.

§Errors

Returns StateError if the compaction fails.

Source

pub fn fragmentation(&self) -> f64

Get the fragmentation ratio (wasted space / total space).

Source

pub fn save_index(&self) -> Result<(), StateError>

Save the index to disk.

This writes the BTreeMap index to a separate .idx file. Format: [magic: 8B][version: 4B][last_write_pos: 8B][next_version: 8B][rkyv data]

§Errors

Returns StateError::Io if the file cannot be created or written. Returns StateError::Serialization if the index cannot be serialized.

Trait Implementations§

Source§

impl StateStore for MmapStateStore

Source§

fn get(&self, key: &[u8]) -> Option<Bytes>

Get a value by key. Read more
Source§

fn put(&mut self, key: &[u8], value: &[u8]) -> Result<(), StateError>

Store a key-value pair. Read more
Source§

fn delete(&mut self, key: &[u8]) -> Result<(), StateError>

Delete a key. Read more
Source§

fn prefix_scan<'a>( &'a self, prefix: &'a [u8], ) -> Box<dyn Iterator<Item = (Bytes, Bytes)> + 'a>

Scan all keys with a given prefix. Read more
Source§

fn range_scan<'a>( &'a self, range: Range<&'a [u8]>, ) -> Box<dyn Iterator<Item = (Bytes, Bytes)> + 'a>

Range scan between two keys (exclusive end). Read more
Source§

fn contains(&self, key: &[u8]) -> bool

Check if a key exists. Read more
Source§

fn size_bytes(&self) -> usize

Get approximate size in bytes. Read more
Source§

fn len(&self) -> usize

Get the number of entries in the store.
Source§

fn snapshot(&self) -> StateSnapshot

Create a snapshot for checkpointing. Read more
Source§

fn restore(&mut self, snapshot: StateSnapshot)

Restore from a snapshot. Read more
Source§

fn clear(&mut self)

Clear all entries.
Source§

fn flush(&mut self) -> Result<(), StateError>

Flush any pending writes to durable storage. Read more
Source§

fn is_empty(&self) -> bool

Check if the store is empty.
Source§

fn get_or_insert( &mut self, key: &[u8], default: &[u8], ) -> Result<Bytes, StateError>

Get a value or insert a default. Read more

Auto Trait Implementations§

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> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> StateStoreExt for T
where T: StateStore + ?Sized,

Source§

fn get_typed<T>(&self, key: &[u8]) -> Result<Option<T>, StateError>

Get a value and deserialize it using rkyv. Read more
Source§

fn put_typed<T>(&mut self, key: &[u8], value: &T) -> Result<(), StateError>

Serialize and store a value using rkyv. Read more
Source§

fn update<F>(&mut self, key: &[u8], f: F) -> Result<(), StateError>
where F: FnOnce(Option<Bytes>) -> Option<Vec<u8>>,

Update a value in place using a closure. 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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,