use crate::dao::GarrisonDao;
use crate::error::GarrisonResult;
pub mod impls {
use super::*;
pub async fn set_if_absent<D: GarrisonDao + ?Sized>(
dao: &D,
key: &str,
value: &str,
ttl_seconds: u64,
) -> GarrisonResult<bool> {
if dao.get(key).await?.is_some() {
return Ok(false);
}
dao.set(key, value, ttl_seconds).await?;
Ok(true)
}
pub async fn rename<D: GarrisonDao + ?Sized>(
dao: &D,
old_key: &str,
new_key: &str,
) -> GarrisonResult<()> {
let value = dao.get(old_key).await?.ok_or_else(|| {
crate::error::GarrisonError::InvalidParam(format!("dao-key-missing::{}", old_key))
})?;
dao.set_permanent(new_key, &value).await?;
dao.delete(old_key).await
}
pub async fn get_and_delete<D: GarrisonDao + ?Sized>(
dao: &D,
key: &str,
) -> GarrisonResult<Option<String>> {
let value = dao.get(key).await?;
if value.is_some() {
dao.delete(key).await?;
}
Ok(value)
}
pub async fn incr<D: GarrisonDao + ?Sized>(
dao: &D,
key: &str,
ttl_seconds: u64,
) -> GarrisonResult<u64> {
match dao.get(key).await? {
Some(v) => {
let cur_val: u64 = v.parse().map_err(|_| {
crate::error::GarrisonError::Dao(format!("dao-incr-parse-u64::{}::{}", key, v))
})?;
let new_val = cur_val + 1;
dao.update(key, &new_val.to_string()).await?;
Ok(new_val)
},
None => {
dao.set(key, "1", ttl_seconds).await?;
Ok(1)
},
}
}
pub async fn decr<D: GarrisonDao + ?Sized>(dao: &D, key: &str) -> GarrisonResult<u64> {
match dao.get(key).await? {
Some(v) => {
let cur_val: u64 = v.parse().map_err(|_| {
crate::error::GarrisonError::Dao(format!("dao-decr-parse-u64::{}::{}", key, v))
})?;
if cur_val == 0 {
return Ok(0);
}
let new_val = cur_val - 1;
if new_val == 0 {
dao.delete(key).await?;
} else {
dao.update(key, &new_val.to_string()).await?;
}
Ok(new_val)
},
None => Ok(0),
}
}
pub async fn compare_and_swap<D: GarrisonDao + ?Sized>(
dao: &D,
key: &str,
expected: Option<&str>,
new_value: &str,
ttl_seconds: u64,
) -> GarrisonResult<bool> {
let current = dao.get(key).await?;
if current.as_deref() == expected {
if ttl_seconds == 0 {
dao.set_permanent(key, new_value).await?;
} else {
dao.set(key, new_value, ttl_seconds).await?;
}
Ok(true)
} else {
Ok(false)
}
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! atomic_test_fallback {
() => {
$crate::atomic_test_fallback_no_get_and_delete!();
$crate::__atomic_test_fallback_get_and_delete!();
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! atomic_test_fallback_no_get_and_delete {
() => {
#[allow(clippy::type_complexity)]
fn set_if_absent<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: &'life2 str,
ttl_seconds: u64,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<Output = $crate::error::GarrisonResult<bool>>
+ ::core::marker::Send
+ 'async_trait,
>,
>
where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: ::core::marker::Sync + 'async_trait,
{
Box::pin(async move {
$crate::dao::atomic_fallback::impls::set_if_absent(self, key, value, ttl_seconds)
.await
})
}
#[allow(clippy::type_complexity)]
fn rename<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
old_key: &'life1 str,
new_key: &'life2 str,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<Output = $crate::error::GarrisonResult<()>>
+ ::core::marker::Send
+ 'async_trait,
>,
>
where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: ::core::marker::Sync + 'async_trait,
{
Box::pin(async move {
$crate::dao::atomic_fallback::impls::rename(self, old_key, new_key).await
})
}
#[allow(clippy::type_complexity)]
fn incr<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
ttl_seconds: u64,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<Output = $crate::error::GarrisonResult<u64>>
+ ::core::marker::Send
+ 'async_trait,
>,
>
where
'life0: 'async_trait,
'life1: 'async_trait,
Self: ::core::marker::Sync + 'async_trait,
{
Box::pin(async move {
$crate::dao::atomic_fallback::impls::incr(self, key, ttl_seconds).await
})
}
#[allow(clippy::type_complexity)]
fn decr<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<Output = $crate::error::GarrisonResult<u64>>
+ ::core::marker::Send
+ 'async_trait,
>,
>
where
'life0: 'async_trait,
'life1: 'async_trait,
Self: ::core::marker::Sync + 'async_trait,
{
Box::pin(async move { $crate::dao::atomic_fallback::impls::decr(self, key).await })
}
#[allow(clippy::type_complexity)]
fn compare_and_swap<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
key: &'life1 str,
expected: Option<&'life2 str>,
new_value: &'life3 str,
ttl_seconds: u64,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<Output = $crate::error::GarrisonResult<bool>>
+ ::core::marker::Send
+ 'async_trait,
>,
>
where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: ::core::marker::Sync + 'async_trait,
{
Box::pin(async move {
$crate::dao::atomic_fallback::impls::compare_and_swap(
self,
key,
expected,
new_value,
ttl_seconds,
)
.await
})
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __atomic_test_fallback_get_and_delete {
() => {
#[allow(clippy::type_complexity)]
fn get_and_delete<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<Output = $crate::error::GarrisonResult<Option<String>>>
+ ::core::marker::Send
+ 'async_trait,
>,
>
where
'life0: 'async_trait,
'life1: 'async_trait,
Self: ::core::marker::Sync + 'async_trait,
{
Box::pin(
async move { $crate::dao::atomic_fallback::impls::get_and_delete(self, key).await },
)
}
};
}