pub struct InMemoryStore<K, V>{ /* private fields */ }Expand description
Generic thread-safe in-memory store
Uses DashMap for lock-free concurrent access with sharded hash maps.
Arc wrapper enables cheap cloning for shared ownership across async tasks.
§Iteration Consistency
This store uses DashMap which provides weakly consistent iteration:
- Individual items are always consistent (no torn reads)
- Items added during iteration may or may not be included
- Items removed during iteration may or may not be included
- Overall result represents a “fuzzy” snapshot of the store
For operations requiring strong consistency:
- Use single-key lookups (
get,contains_key) - Accept eventual consistency for bulk queries
This trade-off enables lock-free concurrent access without the overhead of MVCC or snapshot isolation.
Implementations§
Source§impl<K, V> InMemoryStore<K, V>
impl<K, V> InMemoryStore<K, V>
Sourcepub fn all_keys(&self) -> Vec<K>
pub fn all_keys(&self) -> Vec<K>
Get all keys
§Consistency
Returns a weakly consistent snapshot. Keys added or removed during iteration may or may not be included.
Sourcepub fn get(&self, key: &K) -> Option<V>
pub fn get(&self, key: &K) -> Option<V>
Get value by key
§Consistency
Single-key lookups are always consistent and provide the most recent committed value for the key.
Sourcepub fn filter<P>(&self, predicate: P) -> Vec<V>
pub fn filter<P>(&self, predicate: P) -> Vec<V>
Filter values by predicate
§Consistency
Results are weakly consistent. Items added or removed during iteration
may or may not be included. For authoritative checks, use single-key
lookups (get, contains_key).
Sourcepub fn filter_limited<P>(
&self,
predicate: P,
result_limit: usize,
scan_limit: usize,
) -> (Vec<V>, bool)
pub fn filter_limited<P>( &self, predicate: P, result_limit: usize, scan_limit: usize, ) -> (Vec<V>, bool)
Filter with bounded results and scan limit
Returns at most result_limit items matching predicate.
Stops iteration after scanning scan_limit items.
§Consistency
Results are weakly consistent. Items added or removed during iteration
may or may not be included. For authoritative checks, use single-key
lookups (get, contains_key).
§Returns
A tuple of (results, limit_reached) where:
results: Vec of matching items (at mostresult_limititems)limit_reached: true if either scan_limit or result_limit was hit, meaning the query stopped before examining all items. Results are still valid but potentially incomplete.
§Example
use super::limits::{MAX_SCAN_LIMIT, MAX_RESULTS_LIMIT};
let (results, truncated) = store.filter_limited(
|v| v.is_active(),
MAX_RESULTS_LIMIT,
MAX_SCAN_LIMIT,
);
if truncated {
// Results may be incomplete
}Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Check if key exists
§Consistency
Single-key lookups are always consistent and provide the most recent committed state.
Sourcepub fn update_with<F, R>(&self, key: &K, f: F) -> Option<R>
pub fn update_with<F, R>(&self, key: &K, f: F) -> Option<R>
Atomic read-modify-write operation
Applies function to mutable value reference if key exists. Returns the result of the function or None if key not found.
§Consistency
This operation is atomic with respect to the specific key. The function is executed while holding the shard lock for that key, ensuring no concurrent modifications to the same key.
§Example
store.update_with(&stream_id, |stream| {
stream.complete()
});Sourcepub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, K, V>>
pub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, K, V>>
Iterate over all entries
Returns an iterator that yields references to each entry. Useful for manual iteration with early abort.
§Consistency
Iteration is weakly consistent. Items added or removed during iteration may or may not be included. This is a fundamental property of DashMap that enables lock-free concurrent access.
Trait Implementations§
Source§impl<K, V> Clone for InMemoryStore<K, V>
impl<K, V> Clone for InMemoryStore<K, V>
Source§impl<K, V> Debug for InMemoryStore<K, V>
impl<K, V> Debug for InMemoryStore<K, V>
Auto Trait Implementations§
impl<K, V> !RefUnwindSafe for InMemoryStore<K, V>
impl<K, V> !UnwindSafe for InMemoryStore<K, V>
impl<K, V> Freeze for InMemoryStore<K, V>
impl<K, V> Send for InMemoryStore<K, V>
impl<K, V> Sync for InMemoryStore<K, V>
impl<K, V> Unpin for InMemoryStore<K, V>
impl<K, V> UnsafeUnpin for InMemoryStore<K, V>
Blanket Implementations§
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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> 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