2023-04-27 14:14:44 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2020-07-08 14:10:53 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2022-06-08 08:01:44 +00:00
|
|
|
use garage_db as db;
|
2020-07-08 14:10:53 +00:00
|
|
|
use garage_util::data::*;
|
2023-01-03 13:44:47 +00:00
|
|
|
use garage_util::migrate::Migrate;
|
2020-07-08 14:10:53 +00:00
|
|
|
|
2021-05-02 21:13:08 +00:00
|
|
|
use crate::crdt::Crdt;
|
2021-03-10 15:21:56 +00:00
|
|
|
|
2023-04-27 15:57:54 +00:00
|
|
|
// =================================== PARTITION KEYS
|
|
|
|
|
2021-04-06 03:25:28 +00:00
|
|
|
/// Trait for field used to partition data
|
2023-01-03 14:08:37 +00:00
|
|
|
pub trait PartitionKey:
|
|
|
|
Clone + PartialEq + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static
|
|
|
|
{
|
2021-03-26 18:41:46 +00:00
|
|
|
/// Get the key used to partition
|
2020-07-08 14:10:53 +00:00
|
|
|
fn hash(&self) -> Hash;
|
|
|
|
}
|
|
|
|
|
2020-11-20 19:11:04 +00:00
|
|
|
impl PartitionKey for String {
|
2020-07-08 14:10:53 +00:00
|
|
|
fn hash(&self) -> Hash {
|
2021-03-10 15:23:57 +00:00
|
|
|
blake2sum(self.as_bytes())
|
2020-07-08 14:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-22 17:50:08 +00:00
|
|
|
/// Values of type FixedBytes32 are assumed to be random,
|
|
|
|
/// either a hash or a random UUID. This means we can use
|
|
|
|
/// them directly as an index into the hash table.
|
2021-12-14 12:55:11 +00:00
|
|
|
impl PartitionKey for FixedBytes32 {
|
2020-07-08 14:10:53 +00:00
|
|
|
fn hash(&self) -> Hash {
|
2021-04-23 19:42:52 +00:00
|
|
|
*self
|
2020-07-08 14:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-20 19:11:04 +00:00
|
|
|
|
2023-04-27 15:57:54 +00:00
|
|
|
// =================================== SORT KEYS
|
|
|
|
|
2021-04-06 03:25:28 +00:00
|
|
|
/// Trait for field used to sort data
|
2023-01-03 14:08:37 +00:00
|
|
|
pub trait SortKey: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static {
|
2021-03-26 18:41:46 +00:00
|
|
|
/// Get the key used to sort
|
2023-04-27 14:14:44 +00:00
|
|
|
fn sort_key(&self) -> Cow<'_, [u8]>;
|
2020-11-20 19:11:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 14:10:53 +00:00
|
|
|
impl SortKey for String {
|
2023-04-27 14:14:44 +00:00
|
|
|
fn sort_key(&self) -> Cow<'_, [u8]> {
|
|
|
|
Cow::from(self.as_bytes())
|
2020-07-08 14:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:55:11 +00:00
|
|
|
impl SortKey for FixedBytes32 {
|
2023-04-27 14:14:44 +00:00
|
|
|
fn sort_key(&self) -> Cow<'_, [u8]> {
|
|
|
|
Cow::from(self.as_slice())
|
2020-07-08 14:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 15:57:54 +00:00
|
|
|
impl SortKey for u32 {
|
|
|
|
fn sort_key(&self) -> Cow<'_, [u8]> {
|
|
|
|
Cow::from(u32::to_be_bytes(*self).to_vec())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// =================================== SCHEMA
|
|
|
|
|
2021-03-26 18:41:46 +00:00
|
|
|
/// Trait for an entry in a table. It must be sortable and partitionnable.
|
2020-11-20 19:11:04 +00:00
|
|
|
pub trait Entry<P: PartitionKey, S: SortKey>:
|
2023-01-03 13:44:47 +00:00
|
|
|
Crdt + PartialEq + Clone + Migrate + Send + Sync + 'static
|
2020-11-20 19:11:04 +00:00
|
|
|
{
|
2021-03-26 18:41:46 +00:00
|
|
|
/// Get the key used to partition
|
2020-11-20 19:11:04 +00:00
|
|
|
fn partition_key(&self) -> &P;
|
2021-03-26 18:41:46 +00:00
|
|
|
/// Get the key used to sort
|
2020-11-20 19:11:04 +00:00
|
|
|
fn sort_key(&self) -> &S;
|
2021-03-12 18:57:37 +00:00
|
|
|
|
2021-03-26 18:41:46 +00:00
|
|
|
/// Is the entry a tombstone? Default implementation always return false
|
2021-03-12 20:52:19 +00:00
|
|
|
fn is_tombstone(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2020-11-20 19:11:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 18:41:46 +00:00
|
|
|
/// Trait for the schema used in a table
|
2022-07-22 13:20:00 +00:00
|
|
|
pub trait TableSchema: Send + Sync + 'static {
|
2021-12-14 11:34:01 +00:00
|
|
|
/// The name of the table in the database
|
|
|
|
const TABLE_NAME: &'static str;
|
|
|
|
|
2021-03-26 18:41:46 +00:00
|
|
|
/// The partition key used in that table
|
2023-01-03 14:08:37 +00:00
|
|
|
type P: PartitionKey;
|
2021-03-26 18:41:46 +00:00
|
|
|
/// The sort key used int that table
|
2023-01-03 14:08:37 +00:00
|
|
|
type S: SortKey;
|
2021-12-14 11:34:01 +00:00
|
|
|
|
2021-03-26 18:41:46 +00:00
|
|
|
/// They type for an entry in that table
|
2020-07-08 14:10:53 +00:00
|
|
|
type E: Entry<Self::P, Self::S>;
|
2021-12-14 11:34:01 +00:00
|
|
|
|
|
|
|
/// The type for a filter that can be applied to select entries
|
|
|
|
/// (e.g. filter out deleted entries)
|
2023-01-03 13:44:47 +00:00
|
|
|
type Filter: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static;
|
2020-07-08 14:10:53 +00:00
|
|
|
|
2022-06-08 08:01:44 +00:00
|
|
|
/// Actions triggered by data changing in a table. If such actions
|
|
|
|
/// include updates to the local database that should be applied
|
|
|
|
/// atomically with the item update itself, a db transaction is
|
|
|
|
/// provided on which these changes should be done.
|
|
|
|
/// This function can return a DB error but that's all.
|
|
|
|
fn updated(
|
|
|
|
&self,
|
|
|
|
_tx: &mut db::Transaction,
|
|
|
|
_old: Option<&Self::E>,
|
|
|
|
_new: Option<&Self::E>,
|
|
|
|
) -> db::TxOpResult<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-23 19:25:15 +00:00
|
|
|
|
2021-03-26 18:41:46 +00:00
|
|
|
fn matches_filter(entry: &Self::E, filter: &Self::Filter) -> bool;
|
2020-07-08 14:10:53 +00:00
|
|
|
}
|