gradually implement our interface
Some checks reported errors
Albatros default

This commit is contained in:
Quentin 2023-11-02 11:51:03 +01:00
parent 1f28832dea
commit 553ea25f18
Signed by: quentin
GPG key ID: E9602264D639FF68
5 changed files with 65 additions and 25 deletions

View file

@ -109,7 +109,7 @@ impl Region {
impl Credentials { impl Credentials {
pub fn k2v_client(&self) -> Result<RowStore, Error> { pub fn k2v_client(&self) -> Result<RowStore> {
self.storage.row.row_store() self.storage.row.row_store()
} }
pub fn s3_client(&self) -> Result<S3Client> { pub fn s3_client(&self) -> Result<S3Client> {

View file

@ -14,6 +14,7 @@ use crate::login::Credentials;
use crate::mail::uidindex::*; use crate::mail::uidindex::*;
use crate::mail::unique_ident::*; use crate::mail::unique_ident::*;
use crate::mail::IMF; use crate::mail::IMF;
use crate::storage::{RowStore, BlobStore};
use crate::time::now_msec; use crate::time::now_msec;
pub struct Mailbox { pub struct Mailbox {
@ -50,8 +51,8 @@ impl Mailbox {
id, id,
bucket: creds.bucket().to_string(), bucket: creds.bucket().to_string(),
encryption_key: creds.keys.master.clone(), encryption_key: creds.keys.master.clone(),
k2v: creds.k2v_client()?, k2v: creds.storage.builders.row_store()?,
s3: creds.s3_client()?, s3: creds.storage.builders.blob_store()?,
uid_index, uid_index,
mail_path, mail_path,
}); });
@ -186,8 +187,8 @@ struct MailboxInternal {
mail_path: String, mail_path: String,
encryption_key: Key, encryption_key: Key,
k2v: K2vClient, k2v: RowStore,
s3: S3Client, s3: BlobStore,
uid_index: Bayou<UidIndex>, uid_index: Bayou<UidIndex>,
} }

View file

@ -6,8 +6,12 @@ pub struct GrgStore {}
pub struct GrgRef {} pub struct GrgRef {}
pub struct GrgValue {} pub struct GrgValue {}
impl IRowBuilder for GrgCreds { impl IBuilder for GrgCreds {
fn row_store(&self) -> Result<RowStore, Error> { fn row_store(&self) -> Result<RowStore, StorageError> {
unimplemented!();
}
fn blob_store(&self) -> Result<BlobStore, StorageError> {
unimplemented!(); unimplemented!();
} }
} }

View file

@ -2,13 +2,17 @@ use futures::FutureExt;
use crate::storage::*; use crate::storage::*;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct MemCreds {} pub struct FullMem {}
pub struct MemStore {} pub struct MemStore {}
pub struct MemRef {} pub struct MemRef {}
pub struct MemValue {} pub struct MemValue {}
impl IRowBuilder for MemCreds { impl IBuilder for FullMem {
fn row_store(&self) -> Result<RowStore, Error> { fn row_store(&self) -> Result<RowStore, StorageError> {
unimplemented!();
}
fn blob_store(&self) -> Result<BlobStore, StorageError> {
unimplemented!(); unimplemented!();
} }
} }

View file

@ -25,20 +25,30 @@ pub enum Alternative {
type ConcurrentValues = Vec<Alternative>; type ConcurrentValues = Vec<Alternative>;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum StorageError {
NotFound, NotFound,
Internal, Internal,
} }
impl std::fmt::Display for StorageError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Storage Error: ");
match self {
Self::NotFound => f.write_str("Item not found"),
Self::Internal => f.write_str("An internal error occured"),
}
}
}
impl std::error::Error for StorageError {}
pub struct Engine { pub struct Engine {
pub bucket: String, pub bucket: String,
pub row: RowBuilder, pub builders: Builder,
} }
impl Clone for Engine { impl Clone for Engine {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Engine { Engine {
bucket: "test".into(), bucket: "test".into(),
row: Box::new(in_memory::MemCreds{}) builders: Box::new(in_memory::FullMem{})
} }
} }
} }
@ -48,24 +58,22 @@ impl std::fmt::Debug for Engine {
} }
} }
// A result // Utils
pub type AsyncResult<'a, T> = BoxFuture<'a, Result<T, Error>>; pub type AsyncResult<'a, T> = BoxFuture<'a, Result<T, StorageError>>;
// ------ Row Builder pub trait IBuilder {
pub trait IRowBuilder fn row_store(&self) -> Result<RowStore, StorageError>;
{ fn blob_store(&self) -> Result<BlobStore, StorageError>;
fn row_store(&self) -> Result<RowStore, Error>;
} }
pub type RowBuilder = Box<dyn IRowBuilder + Send + Sync>; pub type Builder = Box<dyn IBuilder + Send + Sync>;
// ------ Row Store // ------ Row
pub trait IRowStore pub trait IRowStore
{ {
fn new_row(&self, partition: &str, sort: &str) -> RowRef; fn new_row(&self, partition: &str, sort: &str) -> RowRef;
} }
pub type RowStore = Box<dyn IRowStore>; pub type RowStore = Box<dyn IRowStore + Sync + Send>;
// ------- Row Item
pub trait IRowRef pub trait IRowRef
{ {
fn set_value(&self, content: Vec<u8>) -> RowValue; fn set_value(&self, content: Vec<u8>) -> RowValue;
@ -73,7 +81,7 @@ pub trait IRowRef
fn rm(&self) -> AsyncResult<()>; fn rm(&self) -> AsyncResult<()>;
fn poll(&self) -> AsyncResult<Option<RowValue>>; fn poll(&self) -> AsyncResult<Option<RowValue>>;
} }
type RowRef = Box<dyn IRowRef>; pub type RowRef = Box<dyn IRowRef>;
pub trait IRowValue pub trait IRowValue
{ {
@ -81,4 +89,27 @@ pub trait IRowValue
fn content(&self) -> ConcurrentValues; fn content(&self) -> ConcurrentValues;
fn push(&self) -> AsyncResult<()>; fn push(&self) -> AsyncResult<()>;
} }
type RowValue = Box<dyn IRowValue>; pub type RowValue = Box<dyn IRowValue>;
// ------- Blob
pub trait IBlobStore
{
fn new_blob(&self, key: &str) -> BlobRef;
fn list(&self) -> AsyncResult<Vec<BlobRef>>;
}
pub type BlobStore = Box<dyn IBlobStore + Send + Sync>;
pub trait IBlobRef
{
fn set_value(&self, content: Vec<u8>) -> BlobValue;
fn fetch(&self) -> AsyncResult<BlobValue>;
fn copy(&self, dst: &BlobRef) -> AsyncResult<()>;
fn rm(&self) -> AsyncResult<()>;
}
pub type BlobRef = Box<dyn IBlobRef>;
pub trait IBlobValue {
fn to_ref(&self) -> BlobRef;
fn push(&self) -> AsyncResult<()>;
}
pub type BlobValue = Box<dyn IBlobValue>;