ezcache/stores/
mod.rs

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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//! Several implementations of cache stores for common use cases, all of require std for now:
//! - [`MemoryStore`]: So just [`HashMap`] cool wrapping around. You'll see it most for examples.
//! - [`ThreadSafeMemoryStore`]: Concurrent store in memory. Uses unsafe under the hood but should
//!   be optimized enough.
//!
//! With feature "file-stores":
//! - [`ThreadSafeFileStore`][file_stores::ThreadSafeFileStore]: A thread safe cache stores that
//!   works over files in a directory.
//! - [`ThreadSafeFileStoreSerializable`][file_stores::ThreadSafeFileStoreSerializable]: Same as
//!   [`ThreadSafeFileStore`][file_stores::ThreadSafeFileStore] BUT it serializes structs.
//!
//! # Examples
//!
//! ```rust
//! # use ezcache::{CacheStore, stores::MemoryStore};
//! #
//! // We create the in-memory store
//! let mut store: MemoryStore<&'static str, String> = MemoryStore::default();
//!
//! // Attempting to get a inexsistent key fails.
//! let value = store.get("key");
//! assert_eq!(value, None);
//!
//! // But if we set it
//! store.set("key", &("value".to_owned()));
//! // And get it
//! let value = store.get("key");
//! assert_eq!(value, Some(String::from("value"))); // Works!
//! ```
//!
//! ```rust
//! # use std::{thread, sync::Arc};
//! # use ezcache::{
//! #     TryCacheStore,
//! #     TryCacheStoreErrorMap,
//! #     stores::MemoryStore,
//! #     thread_safe::{
//! #         ThreadSafeTryCacheStore,
//! #         dumb_wrappers::{
//! #             DumbTryThreadSafeWrapper,
//! #             EmptyDumbError,
//! #         },
//! #     },
//! # };
//! #
//! // Or even for multithreading contexts
//!
//! // We can use a normal store
//! let memory_store: MemoryStore<(), String> = MemoryStore::default();
//! // And we make it fallible such that
//! let try_store: TryCacheStoreErrorMap<_, _, _, EmptyDumbError, _> =
//!     memory_store.into();
//! // we can wrap it around a dumb wrapper (explained in crate::thread_safe)
//! let store = DumbTryThreadSafeWrapper::new(try_store);
//!
//! // We make it atomic
//! let store = Arc::new(store);
//! let store_clone = Arc::clone(&store);
//!
//! // And threads can access it without problems
//! thread::spawn(move || {
//!     store_clone.ts_one_try_set(&(), &String::from("value in thread"))
//! }).join().unwrap();
//!
//! // Of course sharing the values within
//! let value = store.ts_one_try_get(&()).unwrap();
//! assert_eq!(value, Some(String::from("value in thread")));
//! ```

// ------- File Store
#[cfg(feature = "file-stores")]
pub mod file_stores;

use crate::__internal_prelude::*;

#[cfg(feature = "thread-safe")]
use crate::thread_safe::dumb_wrappers::EmptyDumbError;
#[cfg(feature = "thread-safe")]
use std::sync::{Mutex, RwLock};

use core::{borrow::Borrow, hash::Hash, ops::Deref};
use std::{
    collections::HashMap,
    sync::{RwLockReadGuard, RwLockWriteGuard},
};

#[derive(Default)]
/// Simple thread unsafe in memory cache store.
pub struct MemoryStore<K, V> {
    cache: HashMap<K, V>,
}

impl<K, V> MemoryStore<K, V> {
    #[must_use]
    pub fn new() -> Self {
        Self {
            cache: HashMap::default(),
        }
    }

    #[must_use]
    pub fn from_hashmap(hashmap: HashMap<K, V>) -> Self {
        Self { cache: hashmap }
    }
}

impl<K: Hash + Eq + Sized + Clone, V: Clone> CacheStore for MemoryStore<K, V> {
    type Key = K;
    type Value = V;

    fn get(&self, key: impl Borrow<Self::Key>) -> Option<Self::Value> {
        self.cache.get(key.borrow()).cloned()
    }

    fn set(&mut self, key: impl Borrow<Self::Key>, value: impl Borrow<Self::Value>) {
        self.cache
            .insert(key.borrow().clone(), value.borrow().clone());
    }

    fn exists(&self, key: impl Borrow<Self::Key>) -> bool {
        self.cache.contains_key(key.borrow())
    }
}

/// Wrapper around a [`RwLockReadGuard`] and a [`RwLockWriteGuard`] to allow any to be used.
#[derive(Debug)]
pub enum RwLockAnyGuard<'lock, 'guard, T> {
    Read(RwLockReadGuard<'lock, T>),
    Write(&'guard RwLockWriteGuard<'lock, T>),
}

impl<'lock, T> From<RwLockReadGuard<'lock, T>> for RwLockAnyGuard<'lock, '_, T> {
    fn from(value: RwLockReadGuard<'lock, T>) -> Self {
        Self::Read(value)
    }
}

impl<'lock, 'guard, T> From<&'guard RwLockWriteGuard<'lock, T>>
    for RwLockAnyGuard<'lock, 'guard, T>
{
    fn from(value: &'guard RwLockWriteGuard<'lock, T>) -> Self {
        Self::Write(value)
    }
}

impl<T> Deref for RwLockAnyGuard<'_, '_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Read(l) => l,
            Self::Write(l) => l,
        }
    }
}

/// This struct is unsafe under the hood, so you must be careful when using it. No professional
/// reviewed the unsafe usage and the safe code to do this would be too complex for me.
///
/// All unsafe usage is mainly to detach inner locks from the hashmap lock itself tho, so as long
/// as the hashmap itself doesn't move the value or the entry gets deleted, nothing should happen,
/// and I think both can't happen at least now.
#[derive(Default)]
#[cfg(feature = "thread-safe")]
pub struct ThreadSafeMemoryStore<K, V> {
    cache: Mutex<HashMap<K, RwLock<Option<V>>>>,
}

#[cfg(feature = "thread-safe")]
impl<K: Hash + Eq, V> ThreadSafeMemoryStore<K, V> {
    #[must_use]
    pub fn new(cache: HashMap<K, V>) -> Self {
        Self {
            cache: Mutex::new(
                cache
                    .into_iter()
                    .map(|(k, v)| (k, RwLock::new(Some(v))))
                    .collect(),
            ),
        }
    }
}

#[cfg(feature = "thread-safe")]
impl<'lock, K: Hash + Eq + Sized + Clone, V: Clone> ThreadSafeTryCacheStore<'lock>
    for ThreadSafeMemoryStore<K, V>
where
    Self: 'lock,
{
    type Key = K;
    type Value = V;
    type Error = EmptyDumbError;
    type SLock<'guard>
        = RwLockAnyGuard<'lock, 'guard, Option<V>>
    where
        'lock: 'guard;
    type XLock = RwLockWriteGuard<'lock, Option<V>>;

    fn ts_try_get(
        &'lock self,
        handle: &Self::SLock<'_>,
    ) -> Result<Option<Self::Value>, Self::Error> {
        Ok((*handle).clone())
    }

    fn ts_try_set(
        &'lock self,
        handle: &mut Self::XLock,
        value: &Self::Value,
    ) -> Result<(), Self::Error> {
        **handle = Some(value.clone());
        Ok(())
    }

    fn ts_try_exists(&'lock self, handle: &Self::SLock<'_>) -> Result<bool, Self::Error> {
        Ok((*handle).is_some())
    }

    fn ts_try_xlock(&'lock self, key: &'lock Self::Key) -> Result<Self::XLock, Self::Error> {
        let mut cache_lock = self.cache.lock()?;
        let value = if let Some(thing) = cache_lock.get(key) {
            thing
        } else {
            cache_lock.insert(key.clone(), RwLock::default());
            cache_lock.get(key).unwrap()
        };

        // Detach the lock itself from the HashMap guard lifetime
        let value: *const _ = value;
        let lock: Self::XLock = unsafe { (*value).write()? };
        drop(cache_lock);

        Ok(lock)
    }

    fn ts_try_slock(&'lock self, key: &'lock Self::Key) -> Result<Self::SLock<'lock>, Self::Error> {
        let mut cache_lock = self.cache.lock()?;
        let value = if let Some(thing) = cache_lock.get(key) {
            thing
        } else {
            cache_lock.insert(key.clone(), RwLock::default());
            cache_lock.get(key).unwrap()
        };

        // Detach the lock itself from the HashMap guard lifetime
        let value: *const _ = value;
        let lock: Self::SLock<'_> = unsafe { (*value).read()?.into() };
        drop(cache_lock);

        Ok(lock)
    }

    fn ts_try_xlock_nblock(&'lock self, key: &'lock Self::Key) -> Result<Self::XLock, Self::Error> {
        let mut cache_lock = self.cache.lock()?;
        let value = if let Some(thing) = cache_lock.get(key) {
            thing
        } else {
            cache_lock.insert(key.clone(), RwLock::default());
            cache_lock.get(key).unwrap()
        };

        // Detach the lock itself from the HashMap guard lifetime
        let value: *const _ = value;
        let lock: Self::XLock = unsafe { (*value).try_write()? };
        drop(cache_lock);

        Ok(lock)
    }

    fn ts_try_slock_nblock(
        &'lock self,
        key: &'lock Self::Key,
    ) -> Result<Self::SLock<'lock>, Self::Error> {
        let mut cache_lock = self.cache.lock()?;
        let value = if let Some(thing) = cache_lock.get(key) {
            thing
        } else {
            cache_lock.insert(key.clone(), RwLock::default());
            cache_lock.get(key).unwrap()
        };

        // Detach the lock itself from the HashMap guard lifetime
        let value: *const _ = value;
        let lock: Self::SLock<'_> = unsafe { (*value).try_read()?.into() };
        drop(cache_lock);

        Ok(lock)
    }
}

#[cfg(test)]
mod tests {
    use super::{ThreadSafeMemoryStore, ThreadSafeTryCacheStore};

    #[test]
    fn xlock_diff_keys() {
        let store = ThreadSafeMemoryStore::<usize, usize>::default();

        let x1 = store.ts_try_xlock_nblock(&0).expect("to xlock first key");
        let x2 = store.ts_try_xlock_nblock(&1).expect("to xlock second key");
        drop((x1, x2));
    }

    #[test]
    fn xlock_same_key() {
        let store = ThreadSafeMemoryStore::<usize, usize>::default();

        let x1 = store.ts_try_xlock_nblock(&0).expect("to lock xfirst key");
        let x2 = store
            .ts_try_xlock_nblock(&0)
            .expect_err("to not xlock first key");
        drop((x1, x2));
        let x3 = store
            .ts_try_xlock_nblock(&0)
            .expect("to re-xlock first key");
        drop(x3);
    }

    #[test]
    fn slock_same_key() {
        let store = ThreadSafeMemoryStore::<usize, usize>::default();

        let s1 = store.ts_try_slock_nblock(&0).expect("to slock first key");
        let s2 = store
            .ts_try_slock_nblock(&0)
            .expect("to also slock first key");
        drop((s1, s2));
    }

    #[test]
    fn xlock_slock_same_key() {
        let store = ThreadSafeMemoryStore::<usize, usize>::default();

        let x1 = store.ts_try_xlock_nblock(&0).expect("to xlock first key");
        let s1 = store
            .ts_try_slock_nblock(&0)
            .expect_err("to not slock first key");
        drop((x1, s1));
    }

    #[test]
    fn slock_twice_xlock_same_key() {
        let store = ThreadSafeMemoryStore::<usize, usize>::default();

        let s1 = store.ts_try_slock_nblock(&0).expect("to slock first key");
        let s2 = store
            .ts_try_slock_nblock(&0)
            .expect("to also slock first key");
        let x1 = store
            .ts_try_xlock_nblock(&0)
            .expect_err("to not xlock first key");

        drop((x1, s1, s2));
    }
}