aerogramme/src/storage/mod.rs

116 lines
2.9 KiB
Rust
Raw Normal View History

/*
*
* This abstraction goal is to leverage all the semantic of Garage K2V+S3,
* to be as tailored as possible to it ; it aims to be a zero-cost abstraction
* compared to when we where directly using the K2V+S3 client.
2023-11-01 14:15:57 +00:00
*
* My idea: we can encapsulate the causality token
* into the object system so it is not exposed.
*/
2023-11-02 09:38:47 +00:00
use futures::future::BoxFuture;
2023-11-01 14:36:06 +00:00
pub mod in_memory;
pub mod garage;
pub enum Selector<'a> {
Range{ begin: &'a str, end: &'a str },
Filter(u64),
}
pub enum Alternative {
Tombstone,
Value(Vec<u8>),
}
type ConcurrentValues = Vec<Alternative>;
2023-11-01 14:15:57 +00:00
#[derive(Debug)]
2023-11-02 10:51:03 +00:00
pub enum StorageError {
NotFound,
Internal,
}
2023-11-02 10:51:03 +00:00
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 {}
2023-11-02 08:57:58 +00:00
pub struct Engine {
2023-11-02 08:42:50 +00:00
pub bucket: String,
2023-11-02 10:51:03 +00:00
pub builders: Builder,
}
2023-11-02 09:38:47 +00:00
impl Clone for Engine {
fn clone(&self) -> Self {
Engine {
bucket: "test".into(),
2023-11-02 10:51:03 +00:00
builders: Box::new(in_memory::FullMem{})
2023-11-02 09:38:47 +00:00
}
}
}
impl std::fmt::Debug for Engine {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Engine").field("bucket", &self.bucket).finish()
}
}
2023-11-02 10:51:03 +00:00
// Utils
pub type AsyncResult<'a, T> = BoxFuture<'a, Result<T, StorageError>>;
2023-11-02 10:51:03 +00:00
pub trait IBuilder {
fn row_store(&self) -> Result<RowStore, StorageError>;
fn blob_store(&self) -> Result<BlobStore, StorageError>;
2023-10-30 17:07:40 +00:00
}
2023-11-02 10:51:03 +00:00
pub type Builder = Box<dyn IBuilder + Send + Sync>;
2023-10-30 17:07:40 +00:00
2023-11-02 10:51:03 +00:00
// ------ Row
2023-11-02 08:57:58 +00:00
pub trait IRowStore
2023-11-01 14:15:57 +00:00
{
2023-11-02 08:57:58 +00:00
fn new_row(&self, partition: &str, sort: &str) -> RowRef;
2023-10-30 17:07:40 +00:00
}
2023-11-02 10:51:03 +00:00
pub type RowStore = Box<dyn IRowStore + Sync + Send>;
2023-10-30 17:07:40 +00:00
2023-11-02 08:57:58 +00:00
pub trait IRowRef
2023-11-01 14:15:57 +00:00
{
2023-11-02 08:57:58 +00:00
fn set_value(&self, content: Vec<u8>) -> RowValue;
2023-11-02 09:38:47 +00:00
fn fetch(&self) -> AsyncResult<RowValue>;
fn rm(&self) -> AsyncResult<()>;
fn poll(&self) -> AsyncResult<Option<RowValue>>;
}
2023-11-02 10:51:03 +00:00
pub type RowRef = Box<dyn IRowRef>;
2023-10-30 17:07:40 +00:00
2023-11-02 08:57:58 +00:00
pub trait IRowValue
2023-11-01 14:15:57 +00:00
{
2023-11-02 08:57:58 +00:00
fn to_ref(&self) -> RowRef;
2023-11-01 14:15:57 +00:00
fn content(&self) -> ConcurrentValues;
2023-11-02 09:38:47 +00:00
fn push(&self) -> AsyncResult<()>;
2023-10-30 17:07:40 +00:00
}
2023-11-02 10:51:03 +00:00
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>;