233 lines
6.2 KiB
Rust
233 lines
6.2 KiB
Rust
use serde::{de::DeserializeOwned, Serialize};
|
|
use std::sync::RwLock;
|
|
|
|
pub struct NoSupplier;
|
|
pub type DiskCache<K, V, S=NoSupplier> = DiskCacheImpl<Entries<K, V>, S>;
|
|
pub type SyncDiskCache<K, V, S=NoSupplier> = DiskCacheImpl<SyncEntries<K, V>, S>;
|
|
|
|
pub struct DiskCacheImpl<E, S=NoSupplier> {
|
|
path: String,
|
|
entries: E,
|
|
supplier: S,
|
|
}
|
|
|
|
impl<E: EntriesCap> DiskCacheImpl<E, NoSupplier>
|
|
where
|
|
E::Key: DeserializeOwned,
|
|
E::Value: DeserializeOwned,
|
|
{
|
|
#[allow(dead_code)]
|
|
pub fn new(path: &str) -> Self {
|
|
Self::new_with_supplier(path, NoSupplier)
|
|
}
|
|
}
|
|
|
|
impl<E: EntriesCap, S> DiskCacheImpl<E, S>
|
|
where
|
|
E::Key: DeserializeOwned,
|
|
E::Value: DeserializeOwned,
|
|
{
|
|
pub fn new_with_supplier(path: &str, supplier: S) -> Self {
|
|
let entries = Self::load_from_disk(path).unwrap_or_default();
|
|
Self {
|
|
path: path.into(),
|
|
entries: E::from_vec(entries),
|
|
supplier,
|
|
}
|
|
}
|
|
fn load_from_disk(path: &str) -> Option<Vec<(E::Key, E::Value)>> {
|
|
let reader = std::io::BufReader::new(std::fs::File::open(path).ok()?);
|
|
bincode::deserialize_from(reader).ok()
|
|
}
|
|
}
|
|
|
|
impl<E: EntriesCap, S> DiskCacheImpl<E, S>
|
|
where
|
|
E::Key: Serialize + Clone,
|
|
E::Value: Serialize + Clone,
|
|
{
|
|
fn flush(&self) {
|
|
let writer = std::io::BufWriter::new(std::fs::File::create(&self.path).unwrap());
|
|
bincode::serialize_into(writer, &self.entries.to_vec()).unwrap();
|
|
}
|
|
fn flush_if_inserted(&self, v: GetOrInsert<E::Value>) -> E::Value {
|
|
match v {
|
|
GetOrInsert::Get(v) => v,
|
|
GetOrInsert::Inserted(v) => {
|
|
self.flush();
|
|
v
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<E: EntriesCap, S> DiskCacheImpl<E, S>
|
|
where
|
|
E::Key: PartialEq,
|
|
E::Value: Clone,
|
|
{
|
|
#[allow(dead_code)]
|
|
pub fn get(&self, k: &E::Key) -> Option<E::Value> {
|
|
self.entries.get(k)
|
|
}
|
|
}
|
|
|
|
|
|
// non-sync insert is mut, while sync is immute
|
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S> DiskCache<K, V, S> {
|
|
#[allow(dead_code)]
|
|
/// insert this k/v ONLY IF NOT PRESENT
|
|
pub fn insert(&mut self, k: K, v: V) {
|
|
self.entries.insert(k, v);
|
|
self.flush();
|
|
}
|
|
}
|
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S> SyncDiskCache<K, V, S> {
|
|
#[allow(dead_code)]
|
|
/// insert this k/v ONLY IF NOT PRESENT
|
|
pub fn insert(&self, k: K, v: V) {
|
|
self.entries.insert(k, v);
|
|
self.flush();
|
|
}
|
|
}
|
|
|
|
// non-sync insert is mut, while sync is immute
|
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S> DiskCache<K, V, S> {
|
|
#[allow(dead_code)]
|
|
pub fn get_or_insert_with<F: FnOnce() -> V>(&mut self, k: K, f: F) -> V {
|
|
let v = self.entries.get_or_insert_with(k, |_| f());
|
|
self.flush_if_inserted(v)
|
|
}
|
|
}
|
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S> SyncDiskCache<K, V, S> {
|
|
#[allow(dead_code)]
|
|
pub fn get_or_insert_with<F: FnOnce() -> V>(&self, k: K, f: F) -> V {
|
|
let v = self.entries.get_or_insert_with(k, |_| f());
|
|
self.flush_if_inserted(v)
|
|
}
|
|
}
|
|
|
|
// non-sync insert is mut, while sync is immute
|
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S: FnMut(&K) -> V> DiskCache<K, V, S> {
|
|
#[allow(dead_code)]
|
|
pub fn get_or_insert_from_supplier(&mut self, k: K) -> V {
|
|
let v = self.entries.get_or_insert_with(k, |k| (self.supplier)(k));
|
|
self.flush_if_inserted(v)
|
|
}
|
|
}
|
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S: Fn(&K) -> V> SyncDiskCache<K, V, S> {
|
|
#[allow(dead_code)]
|
|
pub fn get_or_insert_from_supplier(&self, k: K) -> V {
|
|
let v = self.entries.get_or_insert_with(k, |k| (self.supplier)(k));
|
|
self.flush_if_inserted(v)
|
|
}
|
|
}
|
|
|
|
enum GetOrInsert<V> {
|
|
Get(V),
|
|
Inserted(V),
|
|
}
|
|
|
|
//---------- disk cache entries ----------
|
|
// we have the non-sync and the sync K/V collections,
|
|
// which the DiskCacheImpl wraps.
|
|
|
|
pub struct Entries<K, V>(Vec<(K, V)>);
|
|
pub struct SyncEntries<K, V>(RwLock<Entries<K, V>>);
|
|
|
|
pub trait EntriesCap {
|
|
type Key;
|
|
type Value;
|
|
|
|
fn from_vec(v: Vec<(Self::Key, Self::Value)>) -> Self;
|
|
fn to_vec(&self) -> Vec<(Self::Key, Self::Value)>
|
|
where
|
|
Self::Key: Clone,
|
|
Self::Value: Clone;
|
|
fn get(&self, k: &Self::Key) -> Option<Self::Value>
|
|
where
|
|
Self::Key: PartialEq,
|
|
Self::Value: Clone;
|
|
}
|
|
|
|
impl<K, V> EntriesCap for Entries<K, V> {
|
|
type Key = K;
|
|
type Value = V;
|
|
fn from_vec(v: Vec<(K, V)>) -> Self {
|
|
Self(v)
|
|
}
|
|
fn to_vec(&self) -> Vec<(K, V)>
|
|
where
|
|
K: Clone,
|
|
V: Clone,
|
|
{
|
|
self.0.clone()
|
|
}
|
|
fn get(&self, k: &K) -> Option<V>
|
|
where
|
|
K: PartialEq,
|
|
V: Clone,
|
|
{
|
|
self.0.iter().find(|(comp_k, _v): &&(K, V)| comp_k == k).map(|(_k, v)| v.clone())
|
|
}
|
|
}
|
|
|
|
impl<K, V> EntriesCap for SyncEntries<K, V> {
|
|
type Key = K;
|
|
type Value = V;
|
|
fn from_vec(v: Vec<(K, V)>) -> Self {
|
|
Self(RwLock::new(Entries::from_vec(v)))
|
|
}
|
|
fn to_vec(&self) -> Vec<(K, V)>
|
|
where
|
|
K: Clone,
|
|
V: Clone,
|
|
{
|
|
self.0.read().unwrap().to_vec()
|
|
}
|
|
fn get(&self, k: &K) -> Option<V>
|
|
where
|
|
K: PartialEq,
|
|
V: Clone,
|
|
{
|
|
self.0.read().unwrap().get(k)
|
|
}
|
|
}
|
|
|
|
impl<K: PartialEq, V> Entries<K, V> {
|
|
fn insert(&mut self, k: K, v: V) {
|
|
|
|
if !self.0.iter().any(|(comp_k, _v): &(K, V)| comp_k == &k) {
|
|
self.0.push((k, v))
|
|
}
|
|
}
|
|
}
|
|
impl<K: Clone + PartialEq, V: Clone> Entries<K, V> {
|
|
fn get_or_insert_with<F: FnOnce(&K) -> V>(&mut self, k: K, f: F) -> GetOrInsert<V> {
|
|
if let Some(v) = self.get(&k) {
|
|
return GetOrInsert::Get(v.clone());
|
|
}
|
|
|
|
let v = f(&k);
|
|
self.insert(k, v.clone());
|
|
GetOrInsert::Inserted(v)
|
|
}
|
|
}
|
|
|
|
impl<K: PartialEq, V> SyncEntries<K, V> {
|
|
fn insert(&self, k: K, v: V) {
|
|
self.0.write().unwrap().insert(k, v)
|
|
}
|
|
}
|
|
impl<K: Clone + PartialEq, V: Clone> SyncEntries<K, V> {
|
|
fn get_or_insert_with<F: FnOnce(&K) -> V>(&self, k: K, f: F) -> GetOrInsert<V> {
|
|
if let Some(v) = self.get(&k) {
|
|
return GetOrInsert::Get(v.clone());
|
|
}
|
|
|
|
let v = f(&k);
|
|
self.insert(k, v.clone());
|
|
GetOrInsert::Inserted(v)
|
|
}
|
|
}
|