use dragonfly_client_core::{
error::{ErrorType, OrErr},
Result,
};
use serde::{de::DeserializeOwned, Serialize};
pub mod rocksdb;
pub trait DatabaseObject: Serialize + DeserializeOwned {
const NAMESPACE: &'static str;
fn serialized(&self) -> Result<Vec<u8>> {
Ok(bincode::serialize(self).or_err(ErrorType::SerializeError)?)
}
fn deserialize_from(bytes: &[u8]) -> Result<Self> {
Ok(bincode::deserialize(bytes).or_err(ErrorType::SerializeError)?)
}
}
pub trait StorageEngine<'db>: Operations {}
pub trait StorageEngineOwned: for<'db> StorageEngine<'db> {}
impl<T: for<'db> StorageEngine<'db>> StorageEngineOwned for T {}
pub trait Operations {
fn get<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>>;
fn exists<O: DatabaseObject>(&self, key: &[u8]) -> Result<bool>;
fn put<O: DatabaseObject>(&self, key: &[u8], value: &O) -> Result<()>;
fn delete<O: DatabaseObject>(&self, key: &[u8]) -> Result<()>;
fn iter<O: DatabaseObject>(&self) -> Result<impl Iterator<Item = Result<(Box<[u8]>, O)>>>;
#[allow(clippy::type_complexity)]
fn iter_raw<O: DatabaseObject>(
&self,
) -> Result<impl Iterator<Item = Result<(Box<[u8]>, Box<[u8]>)>>>;
fn prefix_iter<O: DatabaseObject>(
&self,
prefix: &[u8],
) -> Result<impl Iterator<Item = Result<(Box<[u8]>, O)>>>;
#[allow(clippy::type_complexity)]
fn prefix_iter_raw<O: DatabaseObject>(
&self,
prefix: &[u8],
) -> Result<impl Iterator<Item = Result<(Box<[u8]>, Box<[u8]>)>>>;
fn batch_delete<O: DatabaseObject>(&self, keys: Vec<&[u8]>) -> Result<()>;
}