Skip to content

Commit

Permalink
tarantool: fix compatibility with new interface of traits
Browse files Browse the repository at this point in the history
  • Loading branch information
lowitea committed Sep 1, 2024
1 parent 0d1e8aa commit 71850fe
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 45 deletions.
9 changes: 9 additions & 0 deletions hitbox-backend/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ impl<U> SerializableCachedValue<U> {
}
}

impl<T> From<CachedValue<T>> for SerializableCachedValue<T> {
fn from(value: CachedValue<T>) -> SerializableCachedValue<T> {
SerializableCachedValue {
data: value.data,
expired: value.expired,
}
}
}

#[derive(Default)]
pub struct JsonSerializer<Raw = Vec<u8>> {
_raw: PhantomData<Raw>,
Expand Down
1 change: 1 addition & 0 deletions hitbox-tarantool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ keywords = ["cache", "async", "cache-backend", "hitbox", "tarantool"]

[dependencies]
hitbox-backend = { path = "../hitbox-backend", version = "0.1.0" }
hitbox-core = { path = "../hitbox-core", version = "0.1.0" }
async-trait = "0.1"
serde = "1"
rusty_tarantool = "0.3.0"
Expand Down
16 changes: 8 additions & 8 deletions hitbox-tarantool/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use async_trait::async_trait;
use hitbox_backend::{
serializer::SerializableCachedValue, BackendError, BackendResult, CacheBackend,
CacheableResponse, CachedValue, DeleteStatus,
serializer::SerializableCachedValue, BackendError, BackendResult, CacheBackend, DeleteStatus,
};
use hitbox_core::{CacheKey, CacheableResponse, CachedValue};
use rusty_tarantool::tarantool::{Client, ClientConfig, ExecWithParamaters};
use serde::{Deserialize, Serialize};
use std::io::Error;
Expand Down Expand Up @@ -82,14 +82,14 @@ pub struct CacheEntry<T> {

#[async_trait]
impl CacheBackend for TarantoolBackend {
async fn get<T>(&self, key: String) -> BackendResult<Option<CachedValue<T::Cached>>>
async fn get<T>(&self, key: &CacheKey) -> BackendResult<Option<CachedValue<T::Cached>>>
where
T: CacheableResponse,
<T as CacheableResponse>::Cached: serde::de::DeserializeOwned,
{
self.client
.prepare_fn_call("hitbox.get")
.bind_ref(&(key))
.bind_ref(&(key.serialize()))
.map_err(TarantoolBackend::map_err)?
.execute()
.await
Expand All @@ -99,11 +99,11 @@ impl CacheBackend for TarantoolBackend {
.map(|v| v.map(|v| v.value.into_cached_value()))
}

async fn delete(&self, key: String) -> BackendResult<DeleteStatus> {
async fn delete(&self, key: &CacheKey) -> BackendResult<DeleteStatus> {
let result: bool = self
.client
.prepare_fn_call("hitbox.delete")
.bind_ref(&(key))
.bind_ref(&(key.serialize()))
.map_err(TarantoolBackend::map_err)?
.execute()
.await
Expand All @@ -118,7 +118,7 @@ impl CacheBackend for TarantoolBackend {

async fn set<T>(
&self,
key: String,
key: &CacheKey,
value: &CachedValue<T::Cached>,
ttl: Option<u32>,
) -> BackendResult<()>
Expand All @@ -127,7 +127,7 @@ impl CacheBackend for TarantoolBackend {
T::Cached: serde::Serialize + Send + Sync,
{
let entry: CacheEntry<T::Cached> = CacheEntry {
key,
key: key.serialize(),
ttl,
value: value.clone().into(),
};
Expand Down
84 changes: 47 additions & 37 deletions hitbox-tarantool/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use hitbox_backend::{
serializer::SerializableCachedValue, CacheBackend, CacheableResponse, CachedValue, DeleteStatus,
};
use hitbox_backend::{CacheBackend, DeleteStatus};
use hitbox_core::{CacheKey, CachePolicy, CacheableResponse, CachedValue, PredicateResult};
use hitbox_tarantool::{backend::CacheEntry, backend::TarantoolBackend, Tarantool};
use once_cell::sync::Lazy;
use rusty_tarantool::tarantool::{Client, ClientConfig, ExecWithParamaters};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, str::FromStr, thread, time::Duration};
use testcontainers::{clients, core::WaitFor, Container, Image};

static DOCKER: Lazy<clients::Cli> = Lazy::new(|| clients::Cli::default());
static DOCKER: Lazy<clients::Cli> = Lazy::new(clients::Cli::default);

impl Image for TarantoolImage {
type Args = ();
Expand Down Expand Up @@ -97,9 +96,24 @@ struct Test {
#[async_trait]
impl CacheableResponse for Test {
type Cached = Self;
type Subject = Self;

async fn into_cached(self) -> Self::Cached {
self
async fn cache_policy<P>(self, predicates: P) -> hitbox_core::ResponseCachePolicy<Self>
where
P: hitbox_core::Predicate<Subject = Self::Subject> + Send + Sync,
{
match predicates.check(self).await {
PredicateResult::Cacheable(cacheable) => match cacheable.into_cached().await {
CachePolicy::Cacheable(res) => {
CachePolicy::Cacheable(CachedValue::new(res, Utc::now()))
}
CachePolicy::NonCacheable(res) => CachePolicy::NonCacheable(res),
},
PredicateResult::NonCacheable(res) => CachePolicy::NonCacheable(res),
}
}
async fn into_cached(self) -> CachePolicy<Self::Cached, Self> {
CachePolicy::Cacheable(self)
}
async fn from_cached(cached: Self::Cached) -> Self {
cached
Expand Down Expand Up @@ -139,21 +153,20 @@ async fn test_init() {
#[tokio::test]
async fn test_set() {
let t = TarantoolImage::start().await;
let key = "test_key".to_string();
let key = CacheKey::from_str("test_key", "1");
let dt = "2012-12-12T12:12:12Z";
let ttl = 42;
let value = CachedValue::new(Test::default(), DateTime::from_str(&dt).unwrap());
let value = CachedValue::new(Test::default(), DateTime::from_str(dt).unwrap());

t.backend
.set::<Test>(key.clone(), &value, Some(ttl))
.set::<Test>(&key, &value, Some(ttl))
.await
.unwrap();

// let result: Vec<CacheEntry> = t.call("get", &(key.clone())).await;
let result = t
.client
.prepare_fn_call(format!("box.space.hitbox_cache:get"))
.bind_ref(&key)
.prepare_fn_call("box.space.hitbox_cache:get")
.bind_ref(&key.serialize())
.unwrap()
.execute()
.await
Expand All @@ -168,21 +181,18 @@ async fn test_set() {
#[tokio::test]
async fn test_expire() {
let t = TarantoolImage::start().await;
let key = "test_key".to_owned();
let key = CacheKey::from_str("test_key", "1");
let dt = "2012-12-12T12:12:12Z";
let value = CachedValue::new(Test::default(), DateTime::from_str(&dt).unwrap());
let value = CachedValue::new(Test::default(), DateTime::from_str(dt).unwrap());

t.backend
.set::<Test>(key.clone(), &value, Some(0))
.await
.unwrap();
t.backend.set::<Test>(&key, &value, Some(0)).await.unwrap();

thread::sleep(Duration::from_secs(1));

let result = t
.client
.prepare_fn_call(format!("box.space.hitbox_cache:get"))
.bind_ref(&key)
.prepare_fn_call("box.space.hitbox_cache:get")
.bind_ref(&key.serialize())
.unwrap()
.execute()
.await
Expand All @@ -195,34 +205,34 @@ async fn test_expire() {
#[tokio::test]
async fn test_delete() {
let t = TarantoolImage::start().await;
let key = "test_key";
let dt: DateTime<Utc> = DateTime::from_str(&"2012-12-12T12:12:12Z").unwrap();
let key = CacheKey::from_str("test_key", "1");
let dt: DateTime<Utc> = DateTime::from_str("2012-12-12T12:12:12Z").unwrap();
let value = Test::default();
let cached_value = SerializableCachedValue::new(&value, dt);
let cached_value = CachedValue::new(&value, dt);
let entry = CacheEntry {
key: key.into(),
key: key.serialize(),
ttl: Some(42),
value: cached_value,
value: cached_value.into(),
};

let status = t.backend.delete(key.to_string()).await.unwrap();
let status = t.backend.delete(&key).await.unwrap();
assert_eq!(status, DeleteStatus::Missing);

t.client
.prepare_fn_call(format!("box.space.hitbox_cache:replace"))
.prepare_fn_call("box.space.hitbox_cache:replace")
.bind_ref(&entry)
.unwrap()
.execute()
.await
.unwrap();

let status = t.backend.delete(key.to_string()).await.unwrap();
let status = t.backend.delete(&key).await.unwrap();
assert_eq!(status, DeleteStatus::Deleted(1));

let result = t
.client
.prepare_fn_call(format!("box.space.hitbox_cache:get"))
.bind_ref(&key)
.prepare_fn_call("box.space.hitbox_cache:get")
.bind_ref(&key.serialize())
.unwrap()
.execute()
.await
Expand All @@ -236,26 +246,26 @@ async fn test_delete() {
#[tokio::test]
async fn test_get() {
let t = TarantoolImage::start().await;
let key = "test_key";
let dt: DateTime<Utc> = DateTime::from_str(&"2012-12-12T12:12:12Z").unwrap();
let key = CacheKey::from_str("test_key", "1");
let dt: DateTime<Utc> = DateTime::from_str("2012-12-12T12:12:12Z").unwrap();

let value = Test::default();
let cached_value = SerializableCachedValue::new(&value, dt);
let cached_value = CachedValue::new(&value, dt);
let entry = CacheEntry {
key: key.into(),
key: key.serialize(),
ttl: Some(42),
value: cached_value,
value: cached_value.into(),
};

t.client
.prepare_fn_call(format!("box.space.hitbox_cache:replace"))
.prepare_fn_call("box.space.hitbox_cache:replace")
.bind_ref(&entry)
.unwrap()
.execute()
.await
.unwrap();

let data = t.backend.get::<Test>(key.into()).await.unwrap().unwrap();
let data = t.backend.get::<Test>(&key).await.unwrap().unwrap();

assert_eq!(data.data, value);
assert_eq!(data.expired, dt);
Expand Down

0 comments on commit 71850fe

Please sign in to comment.