use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use crate::types::Error;
pub mod mkvs;
pub use self::mkvs::MKVS;
pub trait KeyValue: Send + Sync {
fn get(&self, key: Vec<u8>) -> Result<Vec<u8>, Error>;
fn insert(&self, key: Vec<u8>, value: Vec<u8>) -> Result<(), Error>;
}
impl<T: ?Sized + KeyValue> KeyValue for Arc<T> {
fn get(&self, key: Vec<u8>) -> Result<Vec<u8>, Error> {
KeyValue::get(&**self, key)
}
fn insert(&self, key: Vec<u8>, value: Vec<u8>) -> Result<(), Error> {
KeyValue::insert(&**self, key, value)
}
}
pub struct UntrustedInMemoryStorage {
store: Mutex<HashMap<Vec<u8>, Vec<u8>>>,
}
impl UntrustedInMemoryStorage {
pub fn new() -> Self {
Self {
store: Mutex::new(HashMap::new()),
}
}
}
impl KeyValue for UntrustedInMemoryStorage {
fn get(&self, key: Vec<u8>) -> Result<Vec<u8>, Error> {
let cache = self.store.lock().unwrap();
let value = cache.get(&key).cloned().unwrap_or_default();
Ok(value)
}
fn insert(&self, key: Vec<u8>, value: Vec<u8>) -> Result<(), Error> {
let mut cache = self.store.lock().unwrap();
cache.insert(key, value);
Ok(())
}
}
impl Default for UntrustedInMemoryStorage {
fn default() -> Self {
Self::new()
}
}