use std::io::Cursor;
use crate::{block::BlockStore, key_value::KeyValueStore};
use anyhow::Result;
use async_trait::async_trait;
use cid::Cid;
use libipld_cbor::DagCborCodec;
use libipld_core::{
codec::{Codec, Decode},
ipld::Ipld,
serde::{from_ipld, to_ipld},
};
use noosphere_common::{ConditionalSend, ConditionalSync};
use serde::{de::DeserializeOwned, Serialize};
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
pub trait Store: Clone + ConditionalSync {
async fn read(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
async fn write(&mut self, key: &[u8], bytes: &[u8]) -> Result<Option<Vec<u8>>>;
async fn remove(&mut self, key: &[u8]) -> Result<Option<Vec<u8>>>;
async fn flush(&self) -> Result<()> {
Ok(())
}
}
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl<S> BlockStore for S
where
S: Store,
{
async fn put_block(&mut self, cid: &Cid, block: &[u8]) -> Result<()> {
self.write(&cid.to_bytes(), block).await?;
Ok(())
}
async fn get_block(&self, cid: &Cid) -> Result<Option<Vec<u8>>> {
self.read(&cid.to_bytes()).await
}
async fn flush(&self) -> Result<()> {
Store::flush(self).await
}
}
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl<S> KeyValueStore for S
where
S: Store,
{
async fn set_key<K, V>(&mut self, key: K, value: V) -> Result<()>
where
K: AsRef<[u8]> + ConditionalSend,
V: Serialize + ConditionalSend,
{
let ipld = to_ipld(value)?;
let codec = DagCborCodec;
let cbor = codec.encode(&ipld)?;
let key_bytes = K::as_ref(&key);
self.write(key_bytes, &cbor).await?;
Ok(())
}
async fn unset_key<K>(&mut self, key: K) -> Result<()>
where
K: AsRef<[u8]> + ConditionalSend,
{
let key_bytes = K::as_ref(&key);
self.remove(key_bytes).await?;
Ok(())
}
async fn get_key<K, V>(&self, key: K) -> Result<Option<V>>
where
K: AsRef<[u8]> + ConditionalSend,
V: DeserializeOwned + ConditionalSend,
{
let key_bytes = K::as_ref(&key);
Ok(match self.read(key_bytes).await? {
Some(bytes) => Some(from_ipld(Ipld::decode(
DagCborCodec,
&mut Cursor::new(bytes),
)?)?),
None => None,
})
}
async fn flush(&self) -> Result<()> {
Store::flush(self).await
}
}