use crate::GraphDb;
use core_storage::RealFs;
use core_storage::Result;
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::sync::{Arc, RwLock};
#[derive(Clone)]
pub struct SharedDb {
inner: Arc<RwLock<GraphDb<RealFs>>>,
}
const _: () = {
fn assert_send_sync<T: Send + Sync>() {}
let _ = assert_send_sync::<SharedDb>;
};
impl SharedDb {
pub fn open(dir: &Path) -> Result<Self> {
Ok(Self {
inner: Arc::new(RwLock::new(GraphDb::open(dir)?)),
})
}
pub fn read(&self) -> impl Deref<Target = GraphDb<RealFs>> + '_ {
self.inner.read().unwrap_or_else(|e| e.into_inner())
}
pub fn write(&self) -> impl DerefMut<Target = GraphDb<RealFs>> + '_ {
self.inner.write().unwrap_or_else(|e| e.into_inner())
}
}