Skip to main content

omega_cache/core/
index.rs

1use crate::core::key::Key;
2use dashmap::DashMap;
3use std::borrow::Borrow;
4use std::hash::Hash;
5
6pub struct IndexTable<K>
7where
8    K: Eq + Hash,
9{
10    table: DashMap<Key<K>, u64>,
11}
12
13impl<K> IndexTable<K>
14where
15    K: Eq + Hash,
16{
17    pub fn new() -> Self {
18        Self {
19            table: DashMap::new(),
20        }
21    }
22
23    #[inline]
24    pub fn get<Q>(&self, key: &Q) -> Option<u64>
25    where
26        Key<K>: Borrow<Q>,
27        Q: Eq + Hash + ?Sized,
28    {
29        self.table.get(key).map(|entry| *entry.value())
30    }
31
32    #[inline]
33    pub fn insert(&self, key: Key<K>, index: u64) {
34        self.table.insert(key, index);
35    }
36
37    #[inline]
38    pub fn remove<Q>(&self, key: &Q) -> Option<u64>
39    where
40        Key<K>: Borrow<Q>,
41        Q: Eq + Hash + ?Sized,
42    {
43        self.table.remove(key).map(|(_, index)| index)
44    }
45}
46
47impl<K> Default for IndexTable<K>
48where
49    K: Eq + Hash,
50{
51    fn default() -> Self {
52        Self::new()
53    }
54}