use crate::digest::ValueDigest;
use crate::node::ProllyNode;
use std::collections::HashMap;
pub trait NodeStorage<const N: usize>: Send + Sync {
fn get_node_by_hash(&self, hash: &ValueDigest<N>) -> Option<ProllyNode<N>>;
fn insert_node(&mut self, hash: ValueDigest<N>, node: ProllyNode<N>) -> Option<()>;
fn delete_node(&mut self, hash: &ValueDigest<N>) -> Option<()>;
fn save_config(&self, key: &str, config: &[u8]);
fn get_config(&self, key: &str) -> Option<Vec<u8>>;
}
#[derive(Clone)]
pub struct InMemoryNodeStorage<const N: usize> {
map: HashMap<ValueDigest<N>, ProllyNode<N>>,
configs: HashMap<String, Vec<u8>>,
}
impl<const N: usize> Default for InMemoryNodeStorage<N> {
fn default() -> Self {
Self::new()
}
}
impl<const N: usize> InMemoryNodeStorage<N> {
pub fn new() -> Self {
InMemoryNodeStorage {
map: HashMap::new(),
configs: HashMap::new(),
}
}
}
impl<const N: usize> NodeStorage<N> for InMemoryNodeStorage<N> {
fn get_node_by_hash(&self, hash: &ValueDigest<N>) -> Option<ProllyNode<N>> {
self.map.get(hash).cloned()
}
fn insert_node(&mut self, hash: ValueDigest<N>, node: ProllyNode<N>) -> Option<()> {
self.map.insert(hash, node);
Some(())
}
fn delete_node(&mut self, hash: &ValueDigest<N>) -> Option<()> {
self.map.remove(hash);
Some(())
}
fn save_config(&self, key: &str, config: &[u8]) {
let mut configs = self.configs.clone();
configs.insert(key.to_string(), config.to_vec());
}
fn get_config(&self, key: &str) -> Option<Vec<u8>> {
self.configs.get(key).cloned()
}
}