Skip to main content

InMemoryStore

Struct InMemoryStore 

Source
pub struct InMemoryStore<K, V>
where K: Eq + Hash + Clone + Send + Sync, V: Clone + Send + Sync,
{ /* 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>
where K: Eq + Hash + Clone + Send + Sync, V: Clone + Send + Sync,

Source

pub fn new() -> Self

Create empty store

Source

pub fn count(&self) -> usize

Get number of entries

Source

pub fn clear(&self)

Remove all entries

Source

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.

Source

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.

Source

pub fn insert(&self, key: K, value: V) -> Option<V>

Insert or update value

Source

pub fn remove(&self, key: &K) -> Option<V>

Remove entry by key

Source

pub fn filter<P>(&self, predicate: P) -> Vec<V>
where P: Fn(&V) -> bool,

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).

Source

pub fn filter_limited<P>( &self, predicate: P, result_limit: usize, scan_limit: usize, ) -> (Vec<V>, bool)
where P: Fn(&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 most result_limit items)
  • 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
}
Source

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.

Source

pub fn is_empty(&self) -> bool

Check if store is empty

Source

pub fn update_with<F, R>(&self, key: &K, f: F) -> Option<R>
where F: FnOnce(&mut V) -> 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()
});
Source

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>
where K: Eq + Hash + Clone + Send + Sync, V: Clone + Send + Sync,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K, V> Debug for InMemoryStore<K, V>
where K: Eq + Hash + Clone + Send + Sync + Debug, V: Clone + Send + Sync + Debug,

Source§

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

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

impl<K, V> Default for InMemoryStore<K, V>
where K: Eq + Hash + Clone + Send + Sync, V: Clone + Send + Sync,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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