Skip to main content

MapCache

Struct MapCache 

Source
pub struct MapCache<K, V> { /* private fields */ }
Expand description

A map’s entries, held in the order the store lists them.

Keyed by the key itself, ordered by the name it borrows. A store orders its keys by their levels, and an entry is one level, so the name’s own order is the store’s - the two cannot come apart, because the cache is not holding a second thing to compare.

A read takes a version and holds nothing, so a walk neither blocks a writer nor waits for one, whatever thread either is on. A write publishes a new version that shares every node it did not touch, so it stays O(log n) rather than copying the map.

It is a trade, not a free win. Writes and memory both cost more than a RwLock<BTreeMap> would, and crates/main/amethystate/benches/map_snapshot_bench.rs measures against one.

What it buys is that writing during a walk cannot deadlock. It does not make it correct: the walk goes on yielding its own version, so a write made inside the loop is invisible to the rest of it. for k in keys { remove(k) } wants exactly that; a loop that writes and reads the same key back gets a stale answer and nothing says so. Loud failure was traded for quiet staleness.

The shape is not selectable per map, though it could be - the backing type is private either way. Nothing would pick the locking one: it wins only where a map is written in bulk and never walked while it is written, and a map nobody observes is the store’s job.

Implementations§

Source§

impl<K: AsRef<str> + Clone, V: Clone> MapCache<K, V>

Source

pub fn new() -> Self

Source

pub fn get(&self, name: &str) -> Option<V>

Source

pub fn contains_key(&self, name: &str) -> bool

Source

pub fn owned_key(&self, name: &str) -> Option<K>

The key as the map holds it, for a caller that looked one up by the name it borrows and needs the owned form back.

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn clear(&self)

Source

pub fn view(&self) -> Entries<K, V>

The entries themselves, in order, without copying any of them.

The version is taken when the view is, so writes that land afterwards are not in it however long it is held.

Source

pub fn keys(&self) -> Walk<K, V, K> ⓘ

Every key, in the order the contract promises, one at a time.

Source

pub fn entries(&self) -> Walk<K, V, (K, V)> ⓘ

Every entry, in that order, one at a time.

Source§

impl<K: AsRef<str> + Clone, V: Clone> MapCache<K, V>

Source

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

Source

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

Trait Implementations§

Source§

impl<K: AsRef<str> + Debug, V: Debug> Debug for MapCache<K, V>

Source§

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

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

impl<K: AsRef<str>, V> Default for MapCache<K, V>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, V> !Freeze for MapCache<K, V>

§

impl<K, V> RefUnwindSafe for MapCache<K, V>

§

impl<K, V> Send for MapCache<K, V>
where ArcSwapAny<Arc<RedBlackTreeMap<ByName<K>, V, ArcTK>>>: Send,

§

impl<K, V> Sync for MapCache<K, V>
where ArcSwapAny<Arc<RedBlackTreeMap<ByName<K>, V, ArcTK>>>: Sync,

§

impl<K, V> Unpin for MapCache<K, V>
where ArcSwapAny<Arc<RedBlackTreeMap<ByName<K>, V, ArcTK>>>: Unpin,

§

impl<K, V> UnsafeUnpin for MapCache<K, V>

§

impl<K, V> UnwindSafe for MapCache<K, V>
where ArcSwapAny<Arc<RedBlackTreeMap<ByName<K>, V, ArcTK>>>: UnwindSafe,

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<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> OpaqueAttachment for T
where T: Send + Sync + 'static,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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