CompressibleMap

Struct CompressibleMap 

Source
pub struct CompressibleMap<K, V, A, H = RandomState>
where A: Compression<Data = V>,
{ /* private fields */ }
Expand description

A hash map that allows compressing the least recently used values. Useful when you need to store a lot of large values in memory. You must define your own compression method for the value type using the Compressible and Decompressible traits.

Call the compress_lru method to compress the least recently used value. The most recently used values will stay uncompressed in a cache.

Any mutable access (&mut self) that misses the cache will decompress and cache the value inline. You can call get to prefetch into the cache and avoid extra latency on further accesses.

Any immutable access (&self, e.g. from multiple threads), like get_const, cannot update the cache. Instead, it will record accesses and store decompressed values in a LocalCache that can be used later to update the cache with flush_local_cache.

Implementations§

Source§

impl<K, V, H, A> CompressibleMap<K, V, A, H>
where K: Clone + Eq + Hash, H: BuildHasher + Default, A: Compression<Data = V>,

Source

pub fn new(compression_params: A) -> Self

Source

pub fn compression_params(&self) -> &A

Source

pub fn from_all_compressed( compression_params: A, compressed: HashMap<K, Compressed<A>, H>, ) -> Self

Source

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

Insert a new value and return the old one if it exists.

Source

pub fn insert_compressed( &mut self, key: K, value: Compressed<A>, ) -> Option<MaybeCompressed<V, Compressed<A>>>

Insert a compressed value, returning any pre-existing entry.

Source

pub fn insert_maybe_compressed( &mut self, key: K, value: MaybeCompressed<V, Compressed<A>>, ) -> Option<MaybeCompressed<V, Compressed<A>>>

Source

pub fn compress_lru(&mut self)

Source

pub fn remove_lru(&mut self) -> Option<(K, V)>

Source

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

Source

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

Source

pub fn get_or_insert_with( &mut self, key: K, on_missing: impl FnOnce() -> V, ) -> &mut V

Source

pub fn insert_if_vacant(&mut self, key: K, value: V) -> &mut V

Source

pub fn get_const<'a>( &'a self, key: K, local_cache: &'a LocalCache<K, V, H>, ) -> Option<&'a V>

Used for thread-safe access or to borrow multiple values at once. The cache will not be updated, but accesses will be recorded in the provided LocalCache. The interior mutability of the local cache has a cost (more heap indirection), but it allows us to borrow multiple values at once. Call flush_local_cache to update the “global” cache with the local cache.

Source

pub fn get_copy_without_caching( &self, key: &K, ) -> Option<MaybeCompressed<V, Compressed<A>>>
where V: Clone, Compressed<A>: Clone,

Returns a copy of the value at key. WARNING: the cache will not be updated. This is useful for read-modify-write scenarios where you would just insert the modified value back into the map, which defeats the purpose of caching it on read.

Source

pub fn flush_local_cache(&mut self, local_cache: LocalCache<K, V, H>)

Updates the cache and it’s approximate LRU order after calling get_const some number of times. WARNING/TODO: There is currently no mechanism to prevent overwriting newer compressed data with old data from a local cache.

Source

pub fn drop(&mut self, key: &K)

Source

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

Removes the value and returns it if it exists.

Source

pub fn clear(&mut self)

Source

pub fn len(&self) -> usize

Source

pub fn len_cached(&self) -> usize

Source

pub fn len_compressed(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn keys<'a>(&'a self) -> impl Iterator<Item = &K>
where Compressed<A>: 'a,

Source

pub fn iter<'a>( &'a self, ) -> impl Iterator<Item = (&K, MaybeCompressed<&V, &Compressed<A>>)>
where Compressed<A>: 'a,

Iterate over all (key, value) pairs, but compressed values will not be decompressed inline. Does not affect the cache.

Source

pub fn into_iter( self, ) -> impl Iterator<Item = (K, MaybeCompressed<V, Compressed<A>>)>

Auto Trait Implementations§

§

impl<K, V, A, H> Freeze for CompressibleMap<K, V, A, H>
where A: Freeze, H: Freeze,

§

impl<K, V, A, H> RefUnwindSafe for CompressibleMap<K, V, A, H>

§

impl<K, V, A, H> Send for CompressibleMap<K, V, A, H>
where A: Send, H: Send, K: Send, <A as Compression>::CompressedData: Send, V: Send,

§

impl<K, V, A, H> Sync for CompressibleMap<K, V, A, H>
where A: Sync, H: Sync, K: Sync, <A as Compression>::CompressedData: Sync, V: Sync,

§

impl<K, V, A, H> Unpin for CompressibleMap<K, V, A, H>
where A: Unpin, H: Unpin, K: Unpin, <A as Compression>::CompressedData: Unpin, V: Unpin,

§

impl<K, V, A, H> UnwindSafe for CompressibleMap<K, V, A, H>

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