aerogramme/src/storage/mod.rs

132 lines
3.4 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.
*/
use std::hash::{Hash, Hasher};
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 {}
// Utils
pub type AsyncResult<'a, T> = BoxFuture<'a, Result<T, StorageError>>;
// ----- Builders
pub trait IBuilders {
fn row_store(&self) -> Result<RowStore, StorageError>;
fn blob_store(&self) -> Result<BlobStore, StorageError>;
fn url(&self) -> &str;
}
pub type Builders = Box<dyn IBuilders + Send + Sync>;
impl Clone for Builders {
2023-11-02 09:38:47 +00:00
fn clone(&self) -> Self {
// @FIXME write a real implementation with a box_clone function
Box::new(in_memory::FullMem{})
2023-11-02 09:38:47 +00:00
}
}
impl std::fmt::Debug for Builders {
2023-11-02 09:38:47 +00:00
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("aerogramme::storage::Builder")
2023-11-02 09:38:47 +00:00
}
}
impl PartialEq for Builders {
fn eq(&self, other: &Self) -> bool {
self.url() == other.url()
}
}
impl Eq for Builders {}
impl Hash for Builders {
fn hash<H: Hasher>(&self, state: &mut H) {
self.url().hash(state);
}
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 11:58:45 +00:00
fn 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 14:28:19 +00:00
fn clone_boxed(&self) -> RowRef;
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<()>;
2023-11-02 14:28:19 +00:00
fn poll(&self) -> AsyncResult<RowValue>;
}
2023-11-02 11:58:45 +00:00
pub type RowRef = Box<dyn IRowRef + Send + Sync>;
2023-11-02 14:28:19 +00:00
impl Clone for RowRef {
fn clone(&self) -> Self {
return self.clone_boxed()
}
}
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 11:58:45 +00:00
pub type RowValue = Box<dyn IRowValue + Send + Sync>;
2023-11-02 10:51:03 +00:00
// ------- Blob
pub trait IBlobStore
{
2023-11-02 14:28:19 +00:00
fn blob(&self, key: &str) -> BlobRef;
fn list(&self, prefix: &str) -> AsyncResult<Vec<BlobRef>>;
2023-11-02 10:51:03 +00:00
}
pub type BlobStore = Box<dyn IBlobStore + Send + Sync>;
pub trait IBlobRef
{
fn set_value(&self, content: Vec<u8>) -> BlobValue;
2023-11-02 14:28:19 +00:00
fn key(&self) -> &str;
2023-11-02 10:51:03 +00:00
fn fetch(&self) -> AsyncResult<BlobValue>;
fn copy(&self, dst: &BlobRef) -> AsyncResult<()>;
fn rm(&self) -> AsyncResult<()>;
}
2023-11-02 11:58:45 +00:00
pub type BlobRef = Box<dyn IBlobRef + Send + Sync>;
2023-11-02 10:51:03 +00:00
pub trait IBlobValue {
fn to_ref(&self) -> BlobRef;
2023-11-02 14:28:19 +00:00
fn get_meta(&self, key: &str) -> Option<&[u8]>;
fn content(&self) -> Option<&[u8]>;
2023-11-02 10:51:03 +00:00
fn push(&self) -> AsyncResult<()>;
}
2023-11-02 11:58:45 +00:00
pub type BlobValue = Box<dyn IBlobValue + Send + Sync>;