txmap 0.1.0

A concurrent transactional hash map with fine-grained locking and internal mutability
Documentation
use crate::{indexer::Indexer, ops::op_trait::OpTrait, result::MISSING_MUTEX_GUARD_ERROR};
use hashbrown::HashMap;
use intmap::IntMap;
use parking_lot::MutexGuard;
use std::hash::Hash;

pub(crate) struct InsertWithIfAbsentOp<K, V, P = ()> {
    guards_bitmask: u128,
    key_index: u8,
    key: K,
    #[allow(clippy::type_complexity)]
    value_generator: Box<dyn Fn(&K, &P) -> V>,
}

impl<K, V, P> InsertWithIfAbsentOp<K, V, P>
where
    K: Hash,
{
    pub fn new_with_params<G>(indexer: &Indexer, key: K, value_generator: G) -> Self
    where
        G: Fn(&K, &P) -> V + 'static,
    {
        let key_index = indexer.index(&key);
        Self {
            guards_bitmask: 1 << key_index,
            key_index,
            key,
            value_generator: Box::new(value_generator),
        }
    }
}

impl<K, V> InsertWithIfAbsentOp<K, V, ()>
where
    K: Hash,
{
    pub fn new<G>(indexer: &Indexer, key: K, value_generator: G) -> Self
    where
        G: Fn(&K) -> V + 'static,
    {
        Self::new_with_params(indexer, key, move |k, _| value_generator(k))
    }
}

impl<K, V, P> OpTrait<K, V, P> for InsertWithIfAbsentOp<K, V, P>
where
    K: Clone + Hash + Eq,
{
    fn guards_bitmask(&self) -> u128 {
        self.guards_bitmask
    }
    fn apply(&self, mutex_guards: &mut IntMap<u8, MutexGuard<'_, HashMap<K, V>>>, params: &P) {
        let mutex_guard = mutex_guards
            .get_mut(self.key_index)
            .expect(MISSING_MUTEX_GUARD_ERROR);
        mutex_guard
            .entry(self.key.clone())
            .or_insert_with(|| (self.value_generator)(&self.key, params));
    }
}