garage/src/model/bucket_table.rs

126 lines
3.1 KiB
Rust
Raw Normal View History

use serde::{Deserialize, Serialize};
2021-05-02 21:13:08 +00:00
use garage_table::crdt::Crdt;
2020-04-24 10:10:01 +00:00
use garage_table::*;
2021-12-14 12:55:11 +00:00
use garage_util::data::*;
use garage_util::time::*;
2021-12-14 12:55:11 +00:00
use crate::permission::BucketKeyPerm;
2020-12-12 20:35:29 +00:00
/// A bucket is a collection of objects
///
/// Its parameters are not directly accessible as:
/// - It must be possible to merge paramaters, hence the use of a LWW CRDT.
/// - A bucket has 2 states, Present or Deleted and parameters make sense only if present.
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct Bucket {
2021-12-14 12:55:11 +00:00
/// ID of the bucket
pub id: Uuid,
2021-03-26 20:53:28 +00:00
/// State, and configuration if not deleted, of the bucket
2021-12-14 12:55:11 +00:00
pub state: crdt::Deletable<BucketParams>,
}
2021-03-26 20:53:28 +00:00
/// Configuration for a bucket
2020-12-12 16:00:31 +00:00
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct BucketParams {
2021-12-14 12:55:11 +00:00
/// Bucket's creation date
pub creation_date: u64,
2021-03-26 20:53:28 +00:00
/// Map of key with access to the bucket, and what kind of access they give
2021-12-14 12:55:11 +00:00
pub authorized_keys: crdt::Map<String, BucketKeyPerm>,
/// Map of aliases that are or have been given to this bucket
/// in the global namespace
/// (not authoritative: this is just used as an indication to
/// map back to aliases when doing ListBuckets)
pub aliases: crdt::LwwMap<String, bool>,
/// Map of aliases that are or have been given to this bucket
/// in namespaces local to keys
/// key = (access key id, alias name)
pub local_aliases: crdt::LwwMap<(String, String), bool>,
2020-12-12 16:00:31 +00:00
}
2020-12-14 20:46:49 +00:00
impl BucketParams {
2021-04-08 13:13:02 +00:00
/// Create an empty BucketParams with no authorized keys and no website accesss
2020-12-14 20:46:49 +00:00
pub fn new() -> Self {
BucketParams {
2021-12-14 12:55:11 +00:00
creation_date: now_msec(),
authorized_keys: crdt::Map::new(),
aliases: crdt::LwwMap::new(),
local_aliases: crdt::LwwMap::new(),
2020-12-14 20:46:49 +00:00
}
}
}
2021-05-02 21:13:08 +00:00
impl Crdt for BucketParams {
2021-04-23 19:57:32 +00:00
fn merge(&mut self, o: &Self) {
self.authorized_keys.merge(&o.authorized_keys);
2021-12-14 12:55:11 +00:00
self.aliases.merge(&o.aliases);
self.local_aliases.merge(&o.local_aliases);
}
}
impl Default for Bucket {
fn default() -> Self {
Self::new()
2021-04-23 19:57:32 +00:00
}
}
impl Default for BucketParams {
fn default() -> Self {
Self::new()
}
}
impl Bucket {
2021-04-08 13:13:02 +00:00
/// Initializes a new instance of the Bucket struct
2021-12-14 12:55:11 +00:00
pub fn new() -> Self {
2020-11-20 22:20:20 +00:00
Bucket {
2021-12-14 12:55:11 +00:00
id: gen_uuid(),
state: crdt::Deletable::present(BucketParams::new()),
2020-11-20 22:20:20 +00:00
}
}
2021-03-26 20:53:28 +00:00
/// Returns true if this represents a deleted bucket
pub fn is_deleted(&self) -> bool {
2021-12-14 12:55:11 +00:00
self.state.is_deleted()
}
2021-03-26 20:53:28 +00:00
/// Return the list of authorized keys, when each was updated, and the permission associated to
/// the key
2021-12-14 12:55:11 +00:00
pub fn authorized_keys(&self) -> &[(String, BucketKeyPerm)] {
match &self.state {
crdt::Deletable::Deleted => &[],
crdt::Deletable::Present(state) => state.authorized_keys.items(),
}
2020-04-23 20:25:45 +00:00
}
}
2021-12-14 12:55:11 +00:00
impl Entry<Uuid, EmptyKey> for Bucket {
fn partition_key(&self) -> &Uuid {
&self.id
}
2021-12-14 12:55:11 +00:00
fn sort_key(&self) -> &EmptyKey {
&EmptyKey
}
}
2021-05-02 21:13:08 +00:00
impl Crdt for Bucket {
fn merge(&mut self, other: &Self) {
self.state.merge(&other.state);
}
}
pub struct BucketTable;
impl TableSchema for BucketTable {
2021-12-14 12:55:11 +00:00
const TABLE_NAME: &'static str = "bucket_v2";
2021-12-14 11:34:01 +00:00
2021-12-14 12:55:11 +00:00
type P = Uuid;
type S = EmptyKey;
type E = Bucket;
type Filter = DeletedFilter;
fn matches_filter(entry: &Self::E, filter: &Self::Filter) -> bool {
filter.apply(entry.is_deleted())
}
}