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
impl MmapStateStore
Sourcepub fn in_memory(capacity: usize) -> Self
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
Sourcepub fn persistent(
path: &Path,
initial_capacity: usize,
) -> Result<Self, StateError>
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 fileinitial_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.
Sourcepub fn is_persistent(&self) -> bool
pub fn is_persistent(&self) -> bool
Check if this store is persistent.
Sourcepub fn compact(&mut self) -> Result<(), StateError>
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.
Sourcepub fn fragmentation(&self) -> f64
pub fn fragmentation(&self) -> f64
Get the fragmentation ratio (wasted space / total space).
Sourcepub fn save_index(&self) -> Result<(), StateError>
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
impl StateStore for MmapStateStore
Source§fn put(&mut self, key: &[u8], value: &[u8]) -> Result<(), StateError>
fn put(&mut self, key: &[u8], value: &[u8]) -> Result<(), StateError>
Source§fn prefix_scan<'a>(
&'a self,
prefix: &'a [u8],
) -> Box<dyn Iterator<Item = (Bytes, Bytes)> + 'a>
fn prefix_scan<'a>( &'a self, prefix: &'a [u8], ) -> Box<dyn Iterator<Item = (Bytes, Bytes)> + 'a>
Source§fn range_scan<'a>(
&'a self,
range: Range<&'a [u8]>,
) -> Box<dyn Iterator<Item = (Bytes, Bytes)> + 'a>
fn range_scan<'a>( &'a self, range: Range<&'a [u8]>, ) -> Box<dyn Iterator<Item = (Bytes, Bytes)> + 'a>
Source§fn size_bytes(&self) -> usize
fn size_bytes(&self) -> usize
Source§fn snapshot(&self) -> StateSnapshot
fn snapshot(&self) -> StateSnapshot
Source§fn restore(&mut self, snapshot: StateSnapshot)
fn restore(&mut self, snapshot: StateSnapshot)
Source§fn flush(&mut self) -> Result<(), StateError>
fn flush(&mut self) -> Result<(), StateError>
Source§fn get_or_insert(
&mut self,
key: &[u8],
default: &[u8],
) -> Result<Bytes, StateError>
fn get_or_insert( &mut self, key: &[u8], default: &[u8], ) -> Result<Bytes, StateError>
Auto Trait Implementations§
impl Freeze for MmapStateStore
impl RefUnwindSafe for MmapStateStore
impl Send for MmapStateStore
impl Sync for MmapStateStore
impl Unpin for MmapStateStore
impl UnwindSafe for MmapStateStore
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.