Skip to content

Commit

Permalink
tarantool: add a basic crate structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jul 9, 2023
1 parent 55d100b commit 67b081e
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ members = [
"hitbox-backend",
"hitbox-redis",
"hitbox-tower",
"hitbox-tarantool",
"examples",
]
12 changes: 12 additions & 0 deletions hitbox-tarantool/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- Initial release
21 changes: 21 additions & 0 deletions hitbox-tarantool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "hitbox-tarantool"
version = "0.1.0"
authors = [
"Evgeniy <ea@lowit.ru>",
"Belousov Max <mail@singulared.space>",
"Andrey Ermilov <andrerm@ya.ru>",
]
license = "MIT"
edition = "2021"
description = "Hitbox tarantool backend."
readme = "README.md"
repository = "https://github.com/hit-box/hitbox/"
categories = ["caching", "asynchronous"]
keywords = ["cache", "async", "cache-backend", "hitbox", "tarantool"]

[dependencies]
hitbox-backend = { path = "../hitbox-backend", version = "0.1.0" }
async-trait = "0.1"
serde = "1"
rusty_tarantool = "0.3.0"
21 changes: 21 additions & 0 deletions hitbox-tarantool/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Makc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions hitbox-tarantool/TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# TODO

- [ ] Backend
- [ ] Tracing
- [ ] Metrics
- [ ] Tests
- [ ] Logs
- [ ] Remove unwraps
- [ ] Add readme
- [ ] Remove TODO
127 changes: 127 additions & 0 deletions hitbox-tarantool/src/backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use async_trait::async_trait;
use hitbox_backend::{
serializer::{JsonSerializer, Serializer},
BackendError, BackendResult, CacheBackend, CacheableResponse, CachedValue, DeleteStatus,
};
use rusty_tarantool::tarantool::{Client, ClientConfig, ExecWithParamaters};

#[derive(Clone)]
pub struct TarantoolBackend {
client: Client,
}

impl TarantoolBackend {
pub fn new() -> Result<TarantoolBackend, BackendError> {
Ok(Self::builder().build())
}

/// Creates new TarantoolBackend builder with default settings.
pub fn builder() -> TarantoolBackendBuilder {
TarantoolBackendBuilder::default()
}
}

/// Part of builder pattern implementation for TarantoolBackend actor.
pub struct TarantoolBackendBuilder {
user: String,
password: String,
host: String,
port: String,
}

impl Default for TarantoolBackendBuilder {
fn default() -> Self {
Self {
user: "hitbox".to_owned(),
password: "hitbox".to_owned(),
host: "127.0.0.1".to_owned(),
port: "3301".to_owned(),
}
}
}

impl TarantoolBackendBuilder {
pub fn user(mut self, user: String) -> Self {
self.user = user;
self
}

pub fn password(mut self, password: String) -> Self {
self.password = password;
self
}

pub fn host(mut self, host: String) -> Self {
self.host = host;
self
}

pub fn port(mut self, port: String) -> Self {
self.port = port;
self
}

pub fn build(self) -> TarantoolBackend {
let client = ClientConfig::new(
format!("{}:{}", self.host, self.port),
self.user,
self.password,
)
.build();

TarantoolBackend { client }
}
}

#[async_trait]
impl CacheBackend for TarantoolBackend {
async fn get<T>(&self, key: String) -> BackendResult<Option<CachedValue<T::Cached>>>
where
T: CacheableResponse,
<T as CacheableResponse>::Cached: serde::de::DeserializeOwned,
{
let client = self.client.clone();

// TODO
let response = client
.prepare_fn_call("test")
.bind_ref(&("aa", "aa"))
.unwrap()
.bind(1)
.unwrap()
.execute()
.await
.unwrap();
response
.decode::<String>()
.map(|value| {
Some(
JsonSerializer::<Vec<u8>>::deserialize(value.into())
.map_err(BackendError::from)
.unwrap(),
)
})
.map_err(|err| BackendError::InternalError(Box::new(err)))
}

async fn delete(&self, key: String) -> BackendResult<DeleteStatus> {
todo!()
}

async fn set<T>(
&self,
key: String,
value: CachedValue<T::Cached>,
ttl: Option<u32>,
) -> BackendResult<()>
where
T: CacheableResponse + Send,
<T as CacheableResponse>::Cached: serde::Serialize + Send,
{
todo!()
}

async fn start(&self) -> BackendResult<()> {
Ok(())
}
}
4 changes: 4 additions & 0 deletions hitbox-tarantool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod backend;

#[doc(inline)]
pub use crate::backend::{TarantoolBackend, TarantoolBackendBuilder};

0 comments on commit 67b081e

Please sign in to comment.