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 get_ref(&self, key: &[u8]) -> Option<&[u8]>
fn get_ref(&self, key: &[u8]) -> Option<&[u8]>
Source§fn put(&mut self, key: &[u8], value: Bytes) -> Result<(), StateError>
fn put(&mut self, key: &[u8], value: Bytes) -> 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 UnsafeUnpin for MmapStateStore
impl UnwindSafe for MmapStateStore
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more