governor::state::keyed

Type Alias DashMapStateStore

Source
pub type DashMapStateStore<K> = DashMap<K, InMemoryState>;
Expand description

A concurrent, thread-safe and fairly performant hashmap based on DashMap.

Aliased Type§

struct DashMapStateStore<K> { /* private fields */ }

Implementations

Source§

impl<'a, K, V> DashMap<K, V>
where K: 'a + Eq + Hash, V: 'a,

Source

pub fn new() -> DashMap<K, V>

Creates a new DashMap with a capacity of 0.

§Examples
use dashmap::DashMap;

let reviews = DashMap::new();
reviews.insert("Veloren", "What a fantastic game!");
Source

pub fn with_capacity(capacity: usize) -> DashMap<K, V>

Creates a new DashMap with a specified starting capacity.

§Examples
use dashmap::DashMap;

let mappings = DashMap::with_capacity(2);
mappings.insert(2, 4);
mappings.insert(8, 16);
Source

pub fn with_shard_amount(shard_amount: usize) -> DashMap<K, V>

Creates a new DashMap with a specified shard amount

shard_amount should greater than 0 and be a power of two. If a shard_amount which is not a power of two is provided, the function will panic.

§Examples
use dashmap::DashMap;

let mappings = DashMap::with_shard_amount(32);
mappings.insert(2, 4);
mappings.insert(8, 16);
Source

pub fn with_capacity_and_shard_amount( capacity: usize, shard_amount: usize, ) -> DashMap<K, V>

Creates a new DashMap with a specified capacity and shard amount.

shard_amount should greater than 0 and be a power of two. If a shard_amount which is not a power of two is provided, the function will panic.

§Examples
use dashmap::DashMap;

let mappings = DashMap::with_capacity_and_shard_amount(32, 32);
mappings.insert(2, 4);
mappings.insert(8, 16);
Source§

impl<'a, K, V, S> DashMap<K, V, S>
where K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone,

Source

pub fn into_read_only(self) -> ReadOnlyView<K, V, S>

Wraps this DashMap into a read-only view. This view allows to obtain raw references to the stored values.

Source

pub fn with_hasher(hasher: S) -> DashMap<K, V, S>

Creates a new DashMap with a capacity of 0 and the provided hasher.

§Examples
use dashmap::DashMap;
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let reviews = DashMap::with_hasher(s);
reviews.insert("Veloren", "What a fantastic game!");
Source

pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> DashMap<K, V, S>

Creates a new DashMap with a specified starting capacity and hasher.

§Examples
use dashmap::DashMap;
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let mappings = DashMap::with_capacity_and_hasher(2, s);
mappings.insert(2, 4);
mappings.insert(8, 16);
Source

pub fn with_hasher_and_shard_amount( hasher: S, shard_amount: usize, ) -> DashMap<K, V, S>

Creates a new DashMap with a specified hasher and shard amount

shard_amount should greater than 0 and be a power of two. If a shard_amount which is not a power of two is provided, the function will panic.

§Examples
use dashmap::DashMap;
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let mappings = DashMap::with_hasher_and_shard_amount(s, 32);
mappings.insert(2, 4);
mappings.insert(8, 16);
Source

pub fn with_capacity_and_hasher_and_shard_amount( capacity: usize, hasher: S, shard_amount: usize, ) -> DashMap<K, V, S>

Creates a new DashMap with a specified starting capacity, hasher and shard_amount.

shard_amount should greater than 0 and be a power of two. If a shard_amount which is not a power of two is provided, the function will panic.

§Examples
use dashmap::DashMap;
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let mappings = DashMap::with_capacity_and_hasher_and_shard_amount(2, s, 32);
mappings.insert(2, 4);
mappings.insert(8, 16);
Source

pub fn hash_usize<T>(&self, item: &T) -> usize
where T: Hash,

Hash a given item to produce a usize. Uses the provided or default HashBuilder.

Source

pub fn hasher(&self) -> &S

Returns a reference to the map’s BuildHasher.

§Examples
use dashmap::DashMap;
use std::collections::hash_map::RandomState;

let hasher = RandomState::new();
let map: DashMap<i32, i32> = DashMap::new();
let hasher: &RandomState = map.hasher();
Source

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

Inserts a key and a value into the map. Returns the old value associated with the key if there was one.

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

§Examples
use dashmap::DashMap;

let map = DashMap::new();
map.insert("I am the key!", "And I am the value!");
Source

pub fn remove<Q>(&self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes an entry from the map, returning the key and value if they existed in the map.

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

§Examples
use dashmap::DashMap;

let soccer_team = DashMap::new();
soccer_team.insert("Jack", "Goalie");
assert_eq!(soccer_team.remove("Jack").unwrap().1, "Goalie");
Source

pub fn remove_if<Q>( &self, key: &Q, f: impl FnOnce(&K, &V) -> bool, ) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes an entry from the map, returning the key and value if the entry existed and the provided conditional function returned true.

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

use dashmap::DashMap;

let soccer_team = DashMap::new();
soccer_team.insert("Sam", "Forward");
soccer_team.remove_if("Sam", |_, position| position == &"Goalie");
assert!(soccer_team.contains_key("Sam"));
use dashmap::DashMap;

let soccer_team = DashMap::new();
soccer_team.insert("Sam", "Forward");
soccer_team.remove_if("Sam", |_, position| position == &"Forward");
assert!(!soccer_team.contains_key("Sam"));
Source

pub fn remove_if_mut<Q>( &self, key: &Q, f: impl FnOnce(&K, &mut V) -> bool, ) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source

pub fn iter(&'a self) -> Iter<'a, K, V, S>

Creates an iterator over a DashMap yielding immutable references.

Locking behaviour: May deadlock if called when holding a mutable reference into the map.

§Examples
use dashmap::DashMap;

let words = DashMap::new();
words.insert("hello", "world");
assert_eq!(words.iter().count(), 1);
Source

pub fn iter_mut(&'a self) -> IterMut<'a, K, V, S>

Iterator over a DashMap yielding mutable references.

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

§Examples
use dashmap::DashMap;

let map = DashMap::new();
map.insert("Johnny", 21);
map.iter_mut().for_each(|mut r| *r += 1);
assert_eq!(*map.get("Johnny").unwrap(), 22);
Source

pub fn get<Q>(&'a self, key: &Q) -> Option<Ref<'a, K, V, S>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get a immutable reference to an entry in the map

Locking behaviour: May deadlock if called when holding a mutable reference into the map.

§Examples
use dashmap::DashMap;

let youtubers = DashMap::new();
youtubers.insert("Bosnian Bill", 457000);
assert_eq!(*youtubers.get("Bosnian Bill").unwrap(), 457000);
Source

pub fn get_mut<Q>(&'a self, key: &Q) -> Option<RefMut<'a, K, V, S>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get a mutable reference to an entry in the map

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

§Examples
use dashmap::DashMap;

let class = DashMap::new();
class.insert("Albin", 15);
*class.get_mut("Albin").unwrap() -= 1;
assert_eq!(*class.get("Albin").unwrap(), 14);
Source

pub fn try_get<Q>(&'a self, key: &Q) -> TryResult<Ref<'a, K, V, S>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get an immutable reference to an entry in the map, if the shard is not locked. If the shard is locked, the function will return TryResult::Locked.

§Examples
use dashmap::DashMap;
use dashmap::try_result::TryResult;

let map = DashMap::new();
map.insert("Johnny", 21);

assert_eq!(*map.try_get("Johnny").unwrap(), 21);

let _result1_locking = map.get_mut("Johnny");

let result2 = map.try_get("Johnny");
assert!(result2.is_locked());
Source

pub fn try_get_mut<Q>(&'a self, key: &Q) -> TryResult<RefMut<'a, K, V, S>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get a mutable reference to an entry in the map, if the shard is not locked. If the shard is locked, the function will return TryResult::Locked.

§Examples
use dashmap::DashMap;
use dashmap::try_result::TryResult;

let map = DashMap::new();
map.insert("Johnny", 21);

*map.try_get_mut("Johnny").unwrap() += 1;
assert_eq!(*map.get("Johnny").unwrap(), 22);

let _result1_locking = map.get("Johnny");

let result2 = map.try_get_mut("Johnny");
assert!(result2.is_locked());
Source

pub fn shrink_to_fit(&self)

Remove excess capacity to reduce memory usage.

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

Source

pub fn retain(&self, f: impl FnMut(&K, &mut V) -> bool)

Retain elements that whose predicates return true and discard elements whose predicates return false.

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

§Examples
use dashmap::DashMap;

let people = DashMap::new();
people.insert("Albin", 15);
people.insert("Jones", 22);
people.insert("Charlie", 27);
people.retain(|_, v| *v > 20);
assert_eq!(people.len(), 2);
Source

pub fn len(&self) -> usize

Fetches the total number of key-value pairs stored in the map.

Locking behaviour: May deadlock if called when holding a mutable reference into the map.

§Examples
use dashmap::DashMap;

let people = DashMap::new();
people.insert("Albin", 15);
people.insert("Jones", 22);
people.insert("Charlie", 27);
assert_eq!(people.len(), 3);
Source

pub fn is_empty(&self) -> bool

Checks if the map is empty or not.

Locking behaviour: May deadlock if called when holding a mutable reference into the map.

§Examples
use dashmap::DashMap;

let map = DashMap::<(), ()>::new();
assert!(map.is_empty());
Source

pub fn clear(&self)

Removes all key-value pairs in the map.

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

§Examples
use dashmap::DashMap;

let stats = DashMap::new();
stats.insert("Goals", 4);
assert!(!stats.is_empty());
stats.clear();
assert!(stats.is_empty());
Source

pub fn capacity(&self) -> usize

Returns how many key-value pairs the map can store without reallocating.

Locking behaviour: May deadlock if called when holding a mutable reference into the map.

Source

pub fn alter<Q>(&self, key: &Q, f: impl FnOnce(&K, V) -> V)
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Modify a specific value according to a function.

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

§Examples
use dashmap::DashMap;

let stats = DashMap::new();
stats.insert("Goals", 4);
stats.alter("Goals", |_, v| v * 2);
assert_eq!(*stats.get("Goals").unwrap(), 8);
§Panics

If the given closure panics, then alter will abort the process

Source

pub fn alter_all(&self, f: impl FnMut(&K, V) -> V)

Modify every value in the map according to a function.

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

§Examples
use dashmap::DashMap;

let stats = DashMap::new();
stats.insert("Wins", 4);
stats.insert("Losses", 2);
stats.alter_all(|_, v| v + 1);
assert_eq!(*stats.get("Wins").unwrap(), 5);
assert_eq!(*stats.get("Losses").unwrap(), 3);
§Panics

If the given closure panics, then alter_all will abort the process

Source

pub fn view<Q, R>(&self, key: &Q, f: impl FnOnce(&K, &V) -> R) -> Option<R>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Scoped access into an item of the map according to a function.

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

§Examples
use dashmap::DashMap;

let warehouse = DashMap::new();
warehouse.insert(4267, ("Banana", 100));
warehouse.insert(2359, ("Pear", 120));
let fruit = warehouse.view(&4267, |_k, v| *v);
assert_eq!(fruit, Some(("Banana", 100)));
§Panics

If the given closure panics, then view will abort the process

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Checks if the map contains a specific key.

Locking behaviour: May deadlock if called when holding a mutable reference into the map.

§Examples
use dashmap::DashMap;

let team_sizes = DashMap::new();
team_sizes.insert("Dakota Cherries", 23);
assert!(team_sizes.contains_key("Dakota Cherries"));
Source

pub fn entry(&'a self, key: K) -> Entry<'a, K, V, S>

Advanced entry API that tries to mimic std::collections::HashMap. See the documentation on dashmap::mapref::entry for more details.

Locking behaviour: May deadlock if called when holding any sort of reference into the map.

Source

pub fn try_entry(&'a self, key: K) -> Option<Entry<'a, K, V, S>>

Advanced entry API that tries to mimic std::collections::HashMap. See the documentation on dashmap::mapref::entry for more details.

Returns None if the shard is currently locked.

Trait Implementations§

Source§

impl<K: Hash + Eq + Clone> ShrinkableKeyedStateStore<K> for DashMapStateStore<K>

Source§

fn retain_recent(&self, drop_below: Nanos)

Remove those keys with state older than drop_below.
Source§

fn shrink_to_fit(&self)

Shrinks the capacity of the state store, if possible. Read more
Source§

fn len(&self) -> usize

Returns the number of “live” keys stored in the state store. Read more
Source§

fn is_empty(&self) -> bool

Returns true if self has no keys stored in it. Read more
Source§

impl<K: Hash + Eq + Clone> StateStore for DashMapStateStore<K>

Source§

type Key = K

The type of key that the state store can represent.
Source§

fn measure_and_replace<T, F, E>(&self, key: &Self::Key, f: F) -> Result<T, E>
where F: Fn(Option<Nanos>) -> Result<(T, Nanos), E>,

Updates a state store’s rate limiting state for a given key, using the given closure. Read more
Source§

impl<K, V, S> Clone for DashMap<K, V, S>
where K: Eq + Hash + Clone, V: Clone, S: Clone,

Source§

fn clone(&self) -> DashMap<K, V, S>

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K, V, S> Debug for DashMap<K, V, S>
where K: Eq + Hash + Debug, V: Debug, S: BuildHasher + Clone,

Source§

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

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

impl<K, V, S> Default for DashMap<K, V, S>
where K: Eq + Hash, S: Default + BuildHasher + Clone,

Source§

fn default() -> DashMap<K, V, S>

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

impl<K, V, S> Extend<(K, V)> for DashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Clone,

Source§

fn extend<I>(&mut self, intoiter: I)
where I: IntoIterator<Item = (K, V)>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V, S> FromIterator<(K, V)> for DashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Clone + Default,

Source§

fn from_iter<I>(intoiter: I) -> DashMap<K, V, S>
where I: IntoIterator<Item = (K, V)>,

Creates a value from an iterator. Read more
Source§

impl<'a, K, V, S> IntoIterator for DashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Clone,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = OwningIter<K, V, S>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <DashMap<K, V, S> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, S> Map<'a, K, V, S> for DashMap<K, V, S>
where K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone,

Source§

fn _shard_count(&self) -> usize

Source§

unsafe fn _get_read_shard( &'a self, i: usize, ) -> &'a HashMap<K, SharedValue<V>, S>

Safety Read more
Source§

unsafe fn _yield_read_shard( &'a self, i: usize, ) -> RwLockReadGuard<'a, RawRwLock, HashMap<K, SharedValue<V>, S>>

Safety Read more
Source§

unsafe fn _yield_write_shard( &'a self, i: usize, ) -> RwLockWriteGuard<'a, RawRwLock, HashMap<K, SharedValue<V>, S>>

Safety Read more
Source§

unsafe fn _try_yield_read_shard( &'a self, i: usize, ) -> Option<RwLockReadGuard<'a, RawRwLock, HashMap<K, SharedValue<V>, S>>>

Safety Read more
Source§

unsafe fn _try_yield_write_shard( &'a self, i: usize, ) -> Option<RwLockWriteGuard<'a, RawRwLock, HashMap<K, SharedValue<V>, S>>>

Safety Read more
Source§

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

Source§

fn _remove<Q>(&self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn _remove_if<Q>( &self, key: &Q, f: impl FnOnce(&K, &V) -> bool, ) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn _remove_if_mut<Q>( &self, key: &Q, f: impl FnOnce(&K, &mut V) -> bool, ) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn _iter(&'a self) -> Iter<'a, K, V, S>

Source§

fn _iter_mut(&'a self) -> IterMut<'a, K, V, S>

Source§

fn _get<Q>(&'a self, key: &Q) -> Option<Ref<'a, K, V, S>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn _get_mut<Q>(&'a self, key: &Q) -> Option<RefMut<'a, K, V, S>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn _try_get<Q>(&'a self, key: &Q) -> TryResult<Ref<'a, K, V, S>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn _try_get_mut<Q>(&'a self, key: &Q) -> TryResult<RefMut<'a, K, V, S>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn _shrink_to_fit(&self)

Source§

fn _retain(&self, f: impl FnMut(&K, &mut V) -> bool)

Source§

fn _len(&self) -> usize

Source§

fn _capacity(&self) -> usize

Source§

fn _alter<Q>(&self, key: &Q, f: impl FnOnce(&K, V) -> V)
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn _alter_all(&self, f: impl FnMut(&K, V) -> V)

Source§

fn _view<Q, R>(&self, key: &Q, f: impl FnOnce(&K, &V) -> R) -> Option<R>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn _entry(&'a self, key: K) -> Entry<'a, K, V, S>

Source§

fn _try_entry(&'a self, key: K) -> Option<Entry<'a, K, V, S>>

Source§

fn _hasher(&self) -> S

Source§

fn _clear(&self)

Source§

fn _contains_key<Q>(&'a self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn _is_empty(&self) -> bool