pub mod corruptabledb;
pub mod errors;
pub mod manager;
pub mod memdb;
pub mod rpcdb;
use std::io::Result;
use num_derive::{FromPrimitive, ToPrimitive};
use crate::rpcchainvm::health::Checkable;
#[tonic::async_trait]
pub trait Closer {
async fn close(&self) -> Result<()>;
}
#[tonic::async_trait]
pub trait Database: CloneBox + KeyValueReaderWriterDeleter + Closer + Checkable {}
#[tonic::async_trait]
pub trait KeyValueReaderWriterDeleter {
async fn has(&self, key: &[u8]) -> Result<bool>;
async fn get(&self, key: &[u8]) -> Result<Vec<u8>>;
async fn put(&mut self, key: &[u8], value: &[u8]) -> Result<()>;
async fn delete(&mut self, key: &[u8]) -> Result<()>;
}
pub trait CloneBox {
fn clone_box(&self) -> Box<dyn Database + Send + Sync>;
}
impl<T> CloneBox for T
where
T: 'static + Database + Clone + Send + Sync,
{
fn clone_box(&self) -> Box<dyn Database + Send + Sync> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn Database + Send + Sync> {
fn clone(&self) -> Box<dyn Database + Send + Sync> {
self.clone_box()
}
}
#[tonic::async_trait]
pub trait VersionedDatabase {
async fn close(&mut self) -> Result<()>;
}
#[derive(Copy, Clone, Debug, FromPrimitive, ToPrimitive)]
pub enum DatabaseError {
None = 0,
Closed,
NotFound,
}
#[tokio::test]
async fn clone_box_test() {
let mut db = memdb::Database::new();
let resp = db.put("foo".as_bytes(), "bar".as_bytes()).await;
assert!(!resp.is_err());
let mut cloned_db = db.clone();
let resp = cloned_db.delete("foo".as_bytes()).await;
assert!(!resp.is_err());
let resp = cloned_db.get("foo".as_bytes()).await;
assert!(resp.is_err());
}