pub mod deser_cache;
pub use deser_cache::DeserCache;
use {
crate::{
core::{
primitive::{appendlog, commitlog, prollylist, prollytree},
storage::Error,
},
Addr,
},
std::{convert::TryFrom, ops::Deref},
tokio::io::{AsyncRead, AsyncWrite},
};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Debug, Clone)]
pub enum Structured {
ProllyTreeNode(prollytree::NodeOwned),
ProllyListNode(prollylist::NodeOwned),
CommitLogNode(appendlog::LogNode<commitlog::CommitNode>),
}
#[allow(clippy::module_name_repetitions)]
#[async_trait::async_trait]
pub trait CacheRead: Sync {
type OwnedRef: OwnedRef + Send;
async fn read_unstructured<A, W>(&self, addr: A, w: W) -> Result<u64, Error>
where
A: AsRef<Addr> + Into<Addr> + Send,
W: AsyncWrite + Unpin + Send;
async fn read_structured<A>(&self, addr: A) -> Result<Self::OwnedRef, Error>
where
A: AsRef<Addr> + Into<Addr> + Send;
}
pub trait OwnedRef {
type Ref: Deref<Target = Structured>;
fn as_ref_structured(&self) -> &Self::Ref;
fn into_owned_structured(self) -> Structured;
}
#[allow(clippy::module_name_repetitions)]
#[async_trait::async_trait]
pub trait CacheWrite: Sync {
async fn write_unstructured<R>(&self, r: R) -> Result<Addr, Error>
where
R: AsyncRead + Unpin + Send;
async fn write_structured<T>(&self, structured: T) -> Result<Addr, Error>
where
T: Into<Structured> + Send;
}
pub trait AsCacheRef {
type Cache: CacheRead + CacheWrite;
fn as_cache_ref(&self) -> &Self::Cache;
}
impl From<prollytree::NodeOwned> for Structured {
fn from(t: prollytree::NodeOwned) -> Self {
Self::ProllyTreeNode(t)
}
}
impl From<prollylist::NodeOwned> for Structured {
fn from(t: prollylist::NodeOwned) -> Self {
Self::ProllyListNode(t)
}
}
impl TryFrom<Structured> for prollytree::NodeOwned {
type Error = Error;
fn try_from(t: Structured) -> Result<Self, Error> {
match t {
Structured::ProllyTreeNode(t) => Ok(t),
_ => Err(Error::Unhandled {
message: "misaligned cache types".to_owned(),
}),
}
}
}
impl<'a> TryFrom<&'a Structured> for &'a prollytree::NodeOwned {
type Error = Error;
fn try_from(t: &'a Structured) -> Result<&'a prollytree::NodeOwned, Error> {
match t {
Structured::ProllyTreeNode(t) => Ok(&t),
_ => Err(Error::Unhandled {
message: "misaligned cache types".to_owned(),
}),
}
}
}
impl TryFrom<Structured> for prollylist::NodeOwned {
type Error = Error;
fn try_from(t: Structured) -> Result<Self, Error> {
match t {
Structured::ProllyListNode(t) => Ok(t),
_ => Err(Error::Unhandled {
message: "misaligned cache types".to_owned(),
}),
}
}
}
impl TryFrom<Structured> for appendlog::LogNode<commitlog::CommitNode> {
type Error = Error;
fn try_from(t: Structured) -> Result<Self, Error> {
match t {
Structured::CommitLogNode(t) => Ok(t),
_ => Err(Error::Unhandled {
message: "misaligned cache types".to_owned(),
}),
}
}
}