aerogramme/aero-user/src/storage/mod.rs

181 lines
4.8 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-01 14:36:06 +00:00
pub mod garage;
2023-12-27 13:58:28 +00:00
pub mod in_memory;
2023-12-27 13:58:28 +00:00
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
2023-12-16 10:13:32 +00:00
2024-03-08 07:17:03 +00:00
use async_trait::async_trait;
2023-12-16 10:13:32 +00:00
#[derive(Debug, Clone)]
pub enum Alternative {
Tombstone,
Value(Vec<u8>),
}
type ConcurrentValues = Vec<Alternative>;
2023-12-18 16:09:44 +00:00
#[derive(Debug, Clone)]
2023-12-16 10:13:32 +00:00
pub enum StorageError {
NotFound,
Internal,
}
2023-12-18 16:09:44 +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-12-16 10:13:32 +00:00
2023-12-18 16:09:44 +00:00
#[derive(Debug, Clone, PartialEq)]
2023-12-16 10:13:32 +00:00
pub struct RowUid {
2023-12-18 16:09:44 +00:00
pub shard: String,
pub sort: String,
2023-12-16 10:13:32 +00:00
}
2023-12-18 16:09:44 +00:00
#[derive(Debug, Clone, PartialEq)]
2023-12-16 10:13:32 +00:00
pub struct RowRef {
2023-12-18 16:09:44 +00:00
pub uid: RowUid,
pub causality: Option<String>,
}
2023-12-19 20:41:35 +00:00
impl std::fmt::Display for RowRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2023-12-27 13:58:28 +00:00
write!(
f,
"RowRef({}, {}, {:?})",
self.uid.shard, self.uid.sort, self.causality
)
2023-12-19 20:41:35 +00:00
}
}
2023-12-18 16:09:44 +00:00
impl RowRef {
pub fn new(shard: &str, sort: &str) -> Self {
Self {
2023-12-27 13:58:28 +00:00
uid: RowUid {
2023-12-18 16:09:44 +00:00
shard: shard.to_string(),
sort: sort.to_string(),
},
causality: None,
}
}
2023-12-19 18:02:22 +00:00
pub fn with_causality(mut self, causality: String) -> Self {
self.causality = Some(causality);
self
}
2023-12-16 10:13:32 +00:00
}
#[derive(Debug, Clone)]
pub struct RowVal {
2023-12-18 16:09:44 +00:00
pub row_ref: RowRef,
pub value: ConcurrentValues,
2023-12-16 10:13:32 +00:00
}
2023-12-18 16:09:44 +00:00
impl RowVal {
pub fn new(row_ref: RowRef, value: Vec<u8>) -> Self {
Self {
row_ref,
value: vec![Alternative::Value(value)],
}
}
}
2023-12-16 10:13:32 +00:00
#[derive(Debug, Clone)]
2023-12-18 16:09:44 +00:00
pub struct BlobRef(pub String);
2023-12-19 20:41:35 +00:00
impl std::fmt::Display for BlobRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "BlobRef({})", self.0)
}
}
2023-12-16 10:13:32 +00:00
#[derive(Debug, Clone)]
pub struct BlobVal {
2023-12-18 16:09:44 +00:00
pub blob_ref: BlobRef,
pub meta: HashMap<String, String>,
pub value: Vec<u8>,
}
impl BlobVal {
pub fn new(blob_ref: BlobRef, value: Vec<u8>) -> Self {
Self {
2023-12-27 13:58:28 +00:00
blob_ref,
value,
2023-12-18 16:09:44 +00:00
meta: HashMap::new(),
}
}
pub fn with_meta(mut self, k: String, v: String) -> Self {
self.meta.insert(k, v);
self
}
2023-12-16 10:13:32 +00:00
}
2023-12-19 20:41:35 +00:00
#[derive(Debug)]
2023-12-16 10:13:32 +00:00
pub enum Selector<'a> {
2023-12-27 13:58:28 +00:00
Range {
shard: &'a str,
sort_begin: &'a str,
sort_end: &'a str,
},
List(Vec<RowRef>), // list of (shard_key, sort_key)
2023-12-27 13:58:09 +00:00
#[allow(dead_code)]
2023-12-27 13:58:28 +00:00
Prefix {
shard: &'a str,
sort_prefix: &'a str,
},
2023-12-18 16:09:44 +00:00
Single(&'a RowRef),
2023-12-16 10:13:32 +00:00
}
2023-12-19 20:41:35 +00:00
impl<'a> std::fmt::Display for Selector<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
2023-12-27 13:58:28 +00:00
Self::Range {
shard,
sort_begin,
sort_end,
} => write!(f, "Range({}, [{}, {}[)", shard, sort_begin, sort_end),
2023-12-19 20:41:35 +00:00
Self::List(list) => write!(f, "List({:?})", list),
Self::Prefix { shard, sort_prefix } => write!(f, "Prefix({}, {})", shard, sort_prefix),
Self::Single(row_ref) => write!(f, "Single({})", row_ref),
}
}
}
2023-12-16 10:13:32 +00:00
#[async_trait]
pub trait IStore {
async fn row_fetch<'a>(&self, select: &Selector<'a>) -> Result<Vec<RowVal>, StorageError>;
async fn row_rm<'a>(&self, select: &Selector<'a>) -> Result<(), StorageError>;
async fn row_insert(&self, values: Vec<RowVal>) -> Result<(), StorageError>;
2023-12-18 16:09:44 +00:00
async fn row_poll(&self, value: &RowRef) -> Result<RowVal, StorageError>;
2023-12-16 10:13:32 +00:00
async fn blob_fetch(&self, blob_ref: &BlobRef) -> Result<BlobVal, StorageError>;
2023-12-22 18:32:07 +00:00
async fn blob_insert(&self, blob_val: BlobVal) -> Result<(), StorageError>;
2023-12-19 18:02:22 +00:00
async fn blob_copy(&self, src: &BlobRef, dst: &BlobRef) -> Result<(), StorageError>;
2023-12-16 10:13:32 +00:00
async fn blob_list(&self, prefix: &str) -> Result<Vec<BlobRef>, StorageError>;
async fn blob_rm(&self, blob_ref: &BlobRef) -> Result<(), StorageError>;
}
2023-12-27 13:58:28 +00:00
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
2023-12-18 16:09:44 +00:00
pub struct UnicityBuffer(Vec<u8>);
2023-11-02 16:25:56 +00:00
2023-12-21 20:54:36 +00:00
#[async_trait]
2023-12-18 16:09:44 +00:00
pub trait IBuilder: std::fmt::Debug {
2023-12-21 20:54:36 +00:00
async fn build(&self) -> Result<Store, StorageError>;
2023-12-16 10:13:32 +00:00
2023-12-18 16:09:44 +00:00
/// Returns an opaque buffer that uniquely identifies this builder
fn unique(&self) -> UnicityBuffer;
2023-11-02 10:51:03 +00:00
}
2023-12-18 16:09:44 +00:00
pub type Builder = Arc<dyn IBuilder + Send + Sync>;
pub type Store = Box<dyn IStore + Send + Sync>;