pub mod fs;
pub type Hashed = String;
pub type Key = String;
pub type Item = String;
use std::fmt::Debug;
use std::{
collections::{HashMap, HashSet},
hash::{DefaultHasher, Hash, Hasher},
};
use fs::Content;
fn get_hash<T: Hash>(item: &T) -> Hashed {
let mut hasher = DefaultHasher::new();
item.hash(&mut hasher);
format!("{:x}", hasher.finish())
}
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct PropertyCacheKey {
pub property: String,
pub value: String,
}
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct RefCacheKey {
pub reftype: String,
pub id: String,
}
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum CacheKey {
Property(PropertyCacheKey),
ItemRef(RefCacheKey),
}
impl CacheKey {
fn to_string(&self) -> String {
match self {
Self::Property(PropertyCacheKey { property, value }) => format!("{property}{value}"),
Self::ItemRef(RefCacheKey { reftype, id }) => format!("{reftype}{}", id),
}
}
}
pub trait SnapStorage {
fn save_on_gen(&self, key: &str, prev_generation: Option<&str>, item_hash: &str) -> Hashed;
fn save_item(&self, item_hash: &str, item: Vec<u8>);
fn get_cache(&self, gen_hash: &str, cache_key: &CacheKey) -> Vec<String>;
fn insert_cache(&self, gen_hash: &str, cache_key: &CacheKey, item: &str) -> Hashed;
fn remove_cache(&self, gen_hash: &str, cache_key: &CacheKey, item: &str) -> Hashed;
fn get_all(&self, gen_hash: &str) -> HashMap<String, Vec<u8>>;
fn all_paths(&self, _gen_hashh: &str) -> HashSet<Content> {
todo!()
}
fn save(&self, gen_hash: Option<&str>, key: &str, item: Vec<u8>) -> Hashed {
let item_hash = get_hash(&item);
self.save_item(&item_hash, item);
self.save_on_gen(key, gen_hash, &item_hash)
}
fn remove(&self, gen_hash: &str, key: &str) -> Hashed;
fn get(&self, hash: &str, key: &str) -> Option<Vec<u8>>;
}