use std::time::Duration;
use kernable_core::{
PROTOCOL,
proto::{
CacheRequest, DataRequest, cache_service_client::CacheServiceClient,
data_service_client::DataServiceClient,
},
signing,
};
use crate::{Client, Error, RequestContext, issued_now};
const MAX_MESSAGE_BYTES: usize = 129 << 20;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Data {
pub data: Vec<u8>,
pub content_type: String,
pub sha256: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Listed {
pub key: String,
pub content_type: String,
pub size: u64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Listing {
pub files: Vec<Listed>,
pub next: Option<String>,
}
#[derive(Clone)]
pub struct Files {
client: Client,
tenant: String,
context: RequestContext,
}
impl Files {
pub(crate) fn new(client: Client, tenant: String, context: RequestContext) -> Files {
Files {
client,
tenant,
context,
}
}
#[must_use]
pub fn tenant(mut self, tenant: impl Into<String>) -> Files {
self.tenant = tenant.into();
self
}
#[must_use]
pub fn context(mut self, context: RequestContext) -> Files {
self.context = context;
self
}
pub async fn put(
&self,
domain: &str,
key: &str,
data: impl Into<Vec<u8>>,
content_type: &str,
) -> Result<String, Error> {
let request = self.request("put", domain, key, data.into(), content_type, "");
let res = self
.client
.routed(&request.tenant_id.clone(), |client| {
let request = request.clone();
async move {
client
.call(None, request, |channel, r| {
let mut service = Self::service(channel);
async move { service.put_data(r).await }
})
.await
}
})
.await?;
Ok(hex::encode(res.sha256))
}
pub async fn get(&self, domain: &str, key: &str) -> Result<Option<Data>, Error> {
let request = self.request("get", domain, key, Vec::new(), "", "");
let res = self
.client
.routed(&request.tenant_id.clone(), |client| {
let request = request.clone();
async move {
client
.call_with(None, request, true, |channel, r| {
let mut service = Self::service(channel);
async move { service.get_data(r).await }
})
.await
}
})
.await?;
Ok(res.found.then_some(Data {
data: res.data,
content_type: res.content_type,
sha256: hex::encode(res.sha256),
}))
}
pub async fn list(&self, domain: &str, prefix: &str, after: &str) -> Result<Listing, Error> {
let request = self.request("list", domain, prefix, Vec::new(), "", after);
let res = self
.client
.routed(&request.tenant_id.clone(), |client| {
let request = request.clone();
async move {
client
.call_with(None, request, true, |channel, r| {
let mut service = Self::service(channel);
async move { service.list_data(r).await }
})
.await
}
})
.await?;
Ok(Listing {
files: res
.files
.into_iter()
.map(|f| Listed {
key: f.key,
content_type: f.content_type,
size: f.size,
})
.collect(),
next: (!res.next.is_empty()).then_some(res.next),
})
}
pub async fn delete(&self, domain: &str, key: &str) -> Result<bool, Error> {
let request = self.request("delete", domain, key, Vec::new(), "", "");
let res = self
.client
.routed(&request.tenant_id.clone(), |client| {
let request = request.clone();
async move {
client
.call(None, request, |channel, r| {
let mut service = Self::service(channel);
async move { service.delete_data(r).await }
})
.await
}
})
.await?;
Ok(res.found)
}
fn service(channel: tonic::transport::Channel) -> DataServiceClient<tonic::transport::Channel> {
DataServiceClient::new(channel)
.max_encoding_message_size(MAX_MESSAGE_BYTES)
.max_decoding_message_size(MAX_MESSAGE_BYTES)
}
fn request(
&self,
op: &str,
domain: &str,
key: &str,
data: Vec<u8>,
content_type: &str,
after: &str,
) -> DataRequest {
let mut r = DataRequest {
tenant_id: self.tenant.clone(),
domain: domain.to_owned(),
key: key.to_owned(),
data,
content_type: content_type.to_owned(),
issued_at: issued_now(),
client_sig: None,
protocol: PROTOCOL,
context: Some(self.context.to_wire()),
after: after.to_owned(),
};
r.client_sig = Some(self.client.sign(&signing::data_message(op, &r)));
r
}
}
#[derive(Clone)]
pub struct Cache {
client: Client,
tenant: String,
context: RequestContext,
}
impl Cache {
pub(crate) fn new(client: Client, tenant: String, context: RequestContext) -> Cache {
Cache {
client,
tenant,
context,
}
}
#[must_use]
pub fn tenant(mut self, tenant: impl Into<String>) -> Cache {
self.tenant = tenant.into();
self
}
#[must_use]
pub fn context(mut self, context: RequestContext) -> Cache {
self.context = context;
self
}
pub async fn set(
&self,
domain: &str,
key: &str,
value: impl Into<Vec<u8>>,
ttl: Option<Duration>,
) -> Result<(), Error> {
let ttl_ms = ttl.map_or(0, |t| (t.as_millis() as u64).max(1));
let request = self.request("set", domain, key, value.into(), ttl_ms);
self.client
.routed(&request.tenant_id.clone(), |client| {
let request = request.clone();
async move {
client
.call(None, request, |channel, r| {
let mut service = Self::service(channel);
async move { service.set_cache(r).await }
})
.await
}
})
.await?;
Ok(())
}
pub async fn get(&self, domain: &str, key: &str) -> Result<Option<Vec<u8>>, Error> {
let request = self.request("get", domain, key, Vec::new(), 0);
let res = self
.client
.routed(&request.tenant_id.clone(), |client| {
let request = request.clone();
async move {
client
.call_with(None, request, true, |channel, r| {
let mut service = Self::service(channel);
async move { service.get_cache(r).await }
})
.await
}
})
.await?;
Ok(res.found.then_some(res.value))
}
pub async fn delete(&self, domain: &str, key: &str) -> Result<bool, Error> {
let request = self.request("delete", domain, key, Vec::new(), 0);
let res = self
.client
.routed(&request.tenant_id.clone(), |client| {
let request = request.clone();
async move {
client
.call(None, request, |channel, r| {
let mut service = Self::service(channel);
async move { service.delete_cache(r).await }
})
.await
}
})
.await?;
Ok(res.found)
}
fn service(
channel: tonic::transport::Channel,
) -> CacheServiceClient<tonic::transport::Channel> {
CacheServiceClient::new(channel)
}
fn request(
&self,
op: &str,
domain: &str,
key: &str,
value: Vec<u8>,
ttl_ms: u64,
) -> CacheRequest {
let mut r = CacheRequest {
tenant_id: self.tenant.clone(),
domain: domain.to_owned(),
key: key.to_owned(),
value,
ttl_ms,
issued_at: issued_now(),
client_sig: None,
protocol: PROTOCOL,
context: Some(self.context.to_wire()),
};
r.client_sig = Some(self.client.sign(&signing::cache_message(op, &r)));
r
}
}