1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
pub(crate) mod get_or_insert;
pub(crate) use get_or_insert::{GetOrInsertFuture, GetOrTryInsertFuture};

pub(crate) mod get_or_insert_race;
pub(crate) use get_or_insert_race::{GetOrInsertRace, GetOrTryInsertRace};

mod no_policy;
pub use no_policy::NoPolicy;

use crate::map::LightMap;
use crate::policy::{NoopPolicy, Policy};
pub use hashbrown::hash_map::DefaultHashBuilder;

use std::future::Future;
use std::hash::{BuildHasher, Hash};
use std::sync::Arc;

/// A concurrent hashmap that allows for efficient async insertion of values
/// 
/// LightCache is cloneable and can be shared across threads, so theres no need to explicitly wrap it in an Arc
/// 
/// ## LightCache offers two modes for async insertion:
/// ### Cooperative: 
/// [`Self::get_or_insert`] and [`Self::get_or_try_insert`] only allow one worker task at a time to poll thier futures
/// and will wake up the other tasks when the value is inserted
/// 
/// ### Race: 
/// [`Self::get_or_insert_race`] and [`Self::get_or_try_insert_race`] allow all worker tasks to poll thier futures at the same time
pub struct LightCache<K, V, S = DefaultHashBuilder, P = NoopPolicy> {
    pub(crate) inner: Arc<LightCacheInner<K, V, S, P>>,
}

impl<K, V, S, P> Clone for LightCache<K, V, S, P> {
    fn clone(&self) -> Self {
        LightCache {
            inner: self.inner.clone(),
        }
    }
}

pub(crate) struct LightCacheInner<K, V, S = DefaultHashBuilder, P = NoopPolicy> {
    pub(crate) policy: P,
    map: LightMap<K, V, S>,
}

impl<K, V> LightCache<K, V> {
    pub fn new() -> Self {
        LightCache {
            inner: Arc::new(LightCacheInner {
                map: LightMap::new(),
                policy: NoopPolicy,
            }),
        }
    }

    pub fn with_capacity(capacity: usize) -> Self {
        LightCache {
            inner: Arc::new(LightCacheInner {
                map: LightMap::with_capacity(capacity),
                policy: NoopPolicy,
            }),
        }
    }
}

impl<K, V, S: BuildHasher, P> LightCache<K, V, S, P> {
    pub fn from_parts(policy: P, hasher: S) -> Self {
        LightCache {
            inner: Arc::new(LightCacheInner {
                map: LightMap::with_hasher(hasher),
                policy,
            }),
        }
    }

    pub fn from_parts_with_capacity(policy: P, hasher: S, capacity: usize) -> Self {
        LightCache {
            inner: Arc::new(LightCacheInner {
                map: LightMap::with_capacity_and_hasher(capacity, hasher),
                policy,
            }),
        }
    }
}

impl<K, V, S, P> LightCache<K, V, S, P> {
    pub fn len(&self) -> usize {
        self.inner.map.len()
    }
}

impl<K, V, S, P> LightCache<K, V, S, P>
where
    K: Eq + Hash + Copy,
    V: Clone + Sync,
    S: BuildHasher,
    P: Policy<K, V>,
{
    /// Get or insert the value for the given key
    ///
    /// In the case that a key is being inserted by another thread using this method or [`Self::get_or_try_insert`]
    /// tasks will cooperativley compute the value and notify the other task when the value is inserted.
    /// If a task fails to insert the value, (via panic or error) another task will take over until theyve all tried.
    ///
    /// ## Note
    /// If a call to remove is issued between the time of inserting, and waking up tasks, the other tasks will simply see the empty slot and try again
    pub async fn get_or_insert<F, Fut>(&self, key: K, init: F) -> V
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = V>,
    {
        if let Some(value) = self.get(&key) {
            return value;
        }

        self.get_or_insert_inner(key, init).await
    }

    /// Get or insert the value for the given key, returning an error if the value could not be inserted
    /// 
    /// See [`Self::get_or_insert`] for more information
    pub async fn get_or_try_insert<F, Fut, Err>(
        &self,
        key: K,
        init: F,
    ) -> Result<V, Err>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<V, Err>>,
    {
        if let Some(value) = self.get(&key) {
            return Ok(value);
        }

        self.get_or_try_insert_inner(key, init).await
    }

    /// Get or insert a value into the cache, but instead of waiting for a single caller to finish the insertion (coopertively),
    /// any callers of this method will always attempt to poll thier own future
    /// 
    /// This is safe to use with any other get_or_* method
    pub async fn get_or_insert_race<F, Fut>(&self, key: K, init: F) -> V
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = V>,
    {
        if let Some(val) = self.get(&key) {
            return val;
        }

        self.get_or_insert_race_inner(key, init).await
    }

    /// See [`Self::get_or_insert_race`] for more information
    pub async fn get_or_try_insert_race<F, Fut, E>(&self, key: K, init: F) -> Result<V, E>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = Result<V, E>>,
    {
        if let Some(val) = self.get(&key) {
            return Ok(val);
        }

        self.get_or_try_insert_race_inner(key, init).await
    }

    /// Insert a value directly into the cache
    /// 
    /// ## Warning: 
    /// Doing a [`Self::insert`] while another task is doing any type of async write ([Self::get_or_insert], [Self::get_or_try_insert], etc)
    /// will "leak" a wakers entry, which will never be removed unless another `get_or_*` method is fully completed without being
    /// interrupted by a call to this method.
    /// 
    /// This is mostly not a big deal as Wakers is small, and this pattern really should be avoided in practice.
    #[inline]
    pub fn insert(&self, key: K, value: V) -> Option<V> {
        self.policy().insert(key, value, self)
    }

    /// Try to get a value from the cache
    #[inline]
    pub fn get(&self, key: &K) -> Option<V> {
        self.policy().get(key, self)
    }

    /// Remove a value from the cache, returning the value if it was present
    ///
    /// ## Note: 
    /// If this is called while another task is trying to [`Self::get_or_insert`] or [`Self::get_or_try_insert`],
    /// it will force them to recompute the value and insert it again.
    #[inline]
    pub fn remove(&self, key: &K) -> Option<V> {
        self.policy().remove(key, self)
    }

    /// Prune the cache of any expired keys
    #[inline]
    pub fn prune(&self) {
        // todo: can we disblae must use warning? compiler should ignore the assignment though
        let _lock = self.policy().lock_and_prune(self);
    }
}

impl<K, V, S, P> LightCache<K, V, S, P>
where
    K: Eq + Hash + Copy,
    V: Clone + Sync,
    S: BuildHasher,
    P: Policy<K, V>,
{
    #[inline]
    fn get_or_insert_inner<F, Fut>(&self, key: K, init: F) -> GetOrInsertFuture<K, V, S, P, F, Fut>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = V>,
    {
        let (hash, shard) = self.inner.map.shard(&key).unwrap();

        GetOrInsertFuture::new(self, shard, hash, key, init)
    }

    #[inline]
    fn get_or_try_insert_inner<F, Fut, E>(
        &self,
        key: K,
        init: F,
    ) -> GetOrTryInsertFuture<K, V, S, P, F, Fut>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = Result<V, E>>,
    {
        let (hash, shard) = self.inner.map.shard(&key).unwrap();

        GetOrTryInsertFuture::new(self, shard, hash, key, init)
    }

    #[inline]
    fn get_or_insert_race_inner<F, Fut>(&self, key: K, init: F) -> GetOrInsertRace<K, V, S, P, Fut>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = V>,
    {
        let (hash, shard) = self.inner.map.shard(&key).unwrap();

        GetOrInsertRace::new(self, shard, hash, key, init())
    }

    #[inline]
    fn get_or_try_insert_race_inner<F, Fut, E>(
        &self,
        key: K,
        init: F,
    ) -> GetOrTryInsertRace<K, V, S, P, Fut>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = Result<V, E>>,
    {
        let (hash, shard) = self.inner.map.shard(&key).unwrap();

        GetOrTryInsertRace::new(self, shard, hash, key, init())
    }
}

impl<K, V, S, P> LightCache<K, V, S, P>
where
    K: Eq + Hash + Copy,
    V: Clone + Sync,
    S: BuildHasher,
{
    #[inline]
    pub(crate) fn get_no_policy(&self, key: &K) -> Option<V> {
        self.inner.map.get(key)
    }

    #[inline]
    pub(crate) fn insert_no_policy(&self, key: K, value: V) -> Option<V> {
        self.inner.map.insert(key, value)
    }

    #[inline]
    pub(crate) fn remove_no_policy(&self, key: &K) -> Option<V> {
        self.inner.map.remove(key)
    }

    #[inline]
    pub(crate) fn policy(&self) -> &P {
        &self.inner.policy
    }
}