From 553ea25f1854706b60ce6f087545968533ef6140 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 2 Nov 2023 11:51:03 +0100 Subject: gradually implement our interface --- src/login/mod.rs | 2 +- src/mail/mailbox.rs | 9 +++---- src/storage/garage.rs | 8 +++++-- src/storage/in_memory.rs | 10 +++++--- src/storage/mod.rs | 61 ++++++++++++++++++++++++++++++++++++------------ 5 files changed, 65 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/login/mod.rs b/src/login/mod.rs index 6c948cc..afade28 100644 --- a/src/login/mod.rs +++ b/src/login/mod.rs @@ -109,7 +109,7 @@ impl Region { impl Credentials { - pub fn k2v_client(&self) -> Result { + pub fn k2v_client(&self) -> Result { self.storage.row.row_store() } pub fn s3_client(&self) -> Result { diff --git a/src/mail/mailbox.rs b/src/mail/mailbox.rs index d92140d..614382e 100644 --- a/src/mail/mailbox.rs +++ b/src/mail/mailbox.rs @@ -14,6 +14,7 @@ use crate::login::Credentials; use crate::mail::uidindex::*; use crate::mail::unique_ident::*; use crate::mail::IMF; +use crate::storage::{RowStore, BlobStore}; use crate::time::now_msec; pub struct Mailbox { @@ -50,8 +51,8 @@ impl Mailbox { id, bucket: creds.bucket().to_string(), encryption_key: creds.keys.master.clone(), - k2v: creds.k2v_client()?, - s3: creds.s3_client()?, + k2v: creds.storage.builders.row_store()?, + s3: creds.storage.builders.blob_store()?, uid_index, mail_path, }); @@ -186,8 +187,8 @@ struct MailboxInternal { mail_path: String, encryption_key: Key, - k2v: K2vClient, - s3: S3Client, + k2v: RowStore, + s3: BlobStore, uid_index: Bayou, } diff --git a/src/storage/garage.rs b/src/storage/garage.rs index c2ca1d3..dfee88d 100644 --- a/src/storage/garage.rs +++ b/src/storage/garage.rs @@ -6,8 +6,12 @@ pub struct GrgStore {} pub struct GrgRef {} pub struct GrgValue {} -impl IRowBuilder for GrgCreds { - fn row_store(&self) -> Result { +impl IBuilder for GrgCreds { + fn row_store(&self) -> Result { + unimplemented!(); + } + + fn blob_store(&self) -> Result { unimplemented!(); } } diff --git a/src/storage/in_memory.rs b/src/storage/in_memory.rs index 6fa8138..80e7fdf 100644 --- a/src/storage/in_memory.rs +++ b/src/storage/in_memory.rs @@ -2,13 +2,17 @@ use futures::FutureExt; use crate::storage::*; #[derive(Clone, Debug)] -pub struct MemCreds {} +pub struct FullMem {} pub struct MemStore {} pub struct MemRef {} pub struct MemValue {} -impl IRowBuilder for MemCreds { - fn row_store(&self) -> Result { +impl IBuilder for FullMem { + fn row_store(&self) -> Result { + unimplemented!(); + } + + fn blob_store(&self) -> Result { unimplemented!(); } } diff --git a/src/storage/mod.rs b/src/storage/mod.rs index c20853b..a2bdd43 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -25,20 +25,30 @@ pub enum Alternative { type ConcurrentValues = Vec; #[derive(Debug)] -pub enum Error { +pub enum StorageError { NotFound, 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 bucket: String, - pub row: RowBuilder, + pub builders: Builder, } impl Clone for Engine { fn clone(&self) -> Self { Engine { 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 -pub type AsyncResult<'a, T> = BoxFuture<'a, Result>; +// Utils +pub type AsyncResult<'a, T> = BoxFuture<'a, Result>; -// ------ Row Builder -pub trait IRowBuilder -{ - fn row_store(&self) -> Result; +pub trait IBuilder { + fn row_store(&self) -> Result; + fn blob_store(&self) -> Result; } -pub type RowBuilder = Box; +pub type Builder = Box; -// ------ Row Store +// ------ Row pub trait IRowStore { fn new_row(&self, partition: &str, sort: &str) -> RowRef; } -pub type RowStore = Box; +pub type RowStore = Box; -// ------- Row Item pub trait IRowRef { fn set_value(&self, content: Vec) -> RowValue; @@ -73,7 +81,7 @@ pub trait IRowRef fn rm(&self) -> AsyncResult<()>; fn poll(&self) -> AsyncResult>; } -type RowRef = Box; +pub type RowRef = Box; pub trait IRowValue { @@ -81,4 +89,27 @@ pub trait IRowValue fn content(&self) -> ConcurrentValues; fn push(&self) -> AsyncResult<()>; } -type RowValue = Box; +pub type RowValue = Box; + +// ------- Blob +pub trait IBlobStore +{ + fn new_blob(&self, key: &str) -> BlobRef; + fn list(&self) -> AsyncResult>; +} +pub type BlobStore = Box; + +pub trait IBlobRef +{ + fn set_value(&self, content: Vec) -> BlobValue; + fn fetch(&self) -> AsyncResult; + fn copy(&self, dst: &BlobRef) -> AsyncResult<()>; + fn rm(&self) -> AsyncResult<()>; +} +pub type BlobRef = Box; + +pub trait IBlobValue { + fn to_ref(&self) -> BlobRef; + fn push(&self) -> AsyncResult<()>; +} +pub type BlobValue = Box; -- cgit v1.2.3