use std::convert::{AsRef, TryFrom, TryInto};
use std::sync::Arc;
use std::time::Duration;
use crate::dev::{BastehBuilder, OwnedValue, Provider};
use crate::error::Result;
use crate::mutation::Mutation;
use crate::value::Value;
use crate::BastehError;
#[derive(Clone)]
pub struct Basteh {
pub(crate) scope: Arc<str>,
pub(crate) provider: Arc<dyn Provider>,
}
impl Basteh {
pub fn build() -> BastehBuilder {
BastehBuilder::default()
}
pub fn scope(&self, scope: &str) -> Basteh {
Basteh {
scope: scope.into(),
provider: self.provider.clone(),
}
}
pub async fn keys(&self) -> Result<Box<dyn Iterator<Item = Vec<u8>>>> {
self.provider.keys(self.scope.as_ref()).await
}
pub async fn set<'a>(&self, key: impl AsRef<[u8]>, value: impl Into<Value<'a>>) -> Result<()> {
self.provider
.set(self.scope.as_ref(), key.as_ref(), value.into())
.await
}
pub async fn set_expiring(
&self,
key: impl AsRef<[u8]>,
value: impl Into<Value<'_>>,
expires_in: Duration,
) -> Result<()> {
self.provider
.set_expiring(
self.scope.as_ref(),
key.as_ref().into(),
value.into(),
expires_in,
)
.await
}
pub async fn get<'a, T: TryFrom<OwnedValue, Error = impl Into<BastehError>>>(
&'a self,
key: impl AsRef<[u8]>,
) -> Result<Option<T>> {
self.provider
.get(self.scope.as_ref(), key.as_ref().into())
.await?
.map(TryInto::try_into)
.transpose()
.map_err(Into::into)
}
pub async fn get_range<'a, T: TryFrom<OwnedValue, Error = impl Into<BastehError>>>(
&'a self,
key: impl AsRef<[u8]>,
start: i64,
end: i64,
) -> Result<Vec<T>> {
self.provider
.get_range(self.scope.as_ref(), key.as_ref().into(), start, end)
.await?
.into_iter()
.map(|v| v.try_into().map_err(Into::into))
.collect::<Result<Vec<_>>>()
.map_err(Into::into)
}
pub async fn get_expiring<'a, T: TryFrom<OwnedValue, Error = impl Into<BastehError>>>(
&'a self,
key: impl AsRef<[u8]>,
) -> Result<Option<(T, Option<Duration>)>> {
self.provider
.get_expiring(self.scope.as_ref(), key.as_ref().into())
.await?
.map(|(v, e)| v.try_into().map(|v| (v, e)).map_err(Into::into))
.transpose()
}
pub async fn push<'a>(&self, key: impl AsRef<[u8]>, value: impl Into<Value<'a>>) -> Result<()> {
self.provider
.push(self.scope.as_ref(), key.as_ref(), value.into())
.await
}
pub async fn push_mutiple<'a>(
&self,
key: impl AsRef<[u8]>,
values: impl Iterator<Item = impl Into<Value<'a>>>,
) -> Result<()> {
self.provider
.push_multiple(
self.scope.as_ref(),
key.as_ref(),
values.map(|v| v.into()).collect(),
)
.await
}
pub async fn pop<'a, T: TryFrom<OwnedValue, Error = impl Into<BastehError>>>(
&'a self,
key: impl AsRef<[u8]>,
) -> Result<Option<T>> {
self.provider
.pop(self.scope.as_ref(), key.as_ref().into())
.await?
.map(TryInto::try_into)
.transpose()
.map_err(Into::into)
}
pub async fn mutate(
&self,
key: impl AsRef<[u8]>,
mutate_f: impl Fn(Mutation) -> Mutation,
) -> Result<i64> {
self.provider
.mutate(
self.scope.as_ref(),
key.as_ref().into(),
mutate_f(Mutation::new()),
)
.await
}
pub async fn remove<T: TryFrom<OwnedValue, Error = impl Into<BastehError>>>(
&self,
key: impl AsRef<[u8]>,
) -> Result<Option<T>> {
self.provider
.remove(self.scope.as_ref(), key.as_ref().into())
.await?
.map(TryInto::try_into)
.transpose()
.map_err(Into::into)
}
pub async fn contains_key(&self, key: impl AsRef<[u8]>) -> Result<bool> {
self.provider
.contains_key(self.scope.as_ref(), key.as_ref().into())
.await
}
pub async fn expire(&self, key: impl AsRef<[u8]>, expire_in: Duration) -> Result<()> {
self.provider
.expire(self.scope.as_ref(), key.as_ref().into(), expire_in)
.await
}
pub async fn expiry(&self, key: impl AsRef<[u8]>) -> Result<Option<Duration>> {
self.provider
.expiry(self.scope.as_ref(), key.as_ref().into())
.await
}
pub async fn extend(&self, key: impl AsRef<[u8]>, expire_in: Duration) -> Result<()> {
self.provider
.extend(self.scope.as_ref(), key.as_ref().into(), expire_in)
.await
}
pub async fn persist(&self, key: impl AsRef<[u8]>) -> Result<()> {
self.provider
.persist(self.scope.as_ref(), key.as_ref().into())
.await
}
}