2020-04-19 15:15:48 +00:00
|
|
|
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::*;
|
2020-11-20 22:01:12 +00:00
|
|
|
|
|
|
|
use crate::key_table::PermissionSet;
|
|
|
|
|
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.
|
2020-04-19 15:15:48 +00:00
|
|
|
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Bucket {
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Name of the bucket
|
2020-04-19 15:15:48 +00:00
|
|
|
pub name: String,
|
2021-03-26 20:53:28 +00:00
|
|
|
/// State, and configuration if not deleted, of the bucket
|
2021-05-02 21:13:08 +00:00
|
|
|
pub state: crdt::Lww<BucketState>,
|
2020-11-20 22:01:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// State of a bucket
|
2020-11-20 22:01:12 +00:00
|
|
|
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum BucketState {
|
2021-03-26 20:53:28 +00:00
|
|
|
/// The bucket is deleted
|
2020-11-20 22:01:12 +00:00
|
|
|
Deleted,
|
2021-03-26 20:53:28 +00:00
|
|
|
/// The bucket exists
|
2020-12-12 16:00:31 +00:00
|
|
|
Present(BucketParams),
|
2020-11-20 22:01:12 +00:00
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
|
2021-05-02 21:13:08 +00:00
|
|
|
impl Crdt for BucketState {
|
2020-11-20 22:01:12 +00:00
|
|
|
fn merge(&mut self, o: &Self) {
|
|
|
|
match o {
|
|
|
|
BucketState::Deleted => *self = BucketState::Deleted,
|
2020-12-12 16:00:31 +00:00
|
|
|
BucketState::Present(other_params) => {
|
|
|
|
if let BucketState::Present(params) = self {
|
|
|
|
params.merge(other_params);
|
2020-11-20 22:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
|
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-03-26 20:53:28 +00:00
|
|
|
/// Map of key with access to the bucket, and what kind of access they give
|
2021-05-02 21:13:08 +00:00
|
|
|
pub authorized_keys: crdt::LwwMap<String, PermissionSet>,
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Is the bucket served as http
|
2021-05-02 21:13:08 +00:00
|
|
|
pub website: crdt::Lww<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-05-02 21:13:08 +00:00
|
|
|
authorized_keys: crdt::LwwMap::new(),
|
|
|
|
website: crdt::Lww::new(false),
|
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);
|
|
|
|
self.website.merge(&o.website);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for BucketParams {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:16:33 +00:00
|
|
|
impl Bucket {
|
2021-04-08 13:13:02 +00:00
|
|
|
/// Initializes a new instance of the Bucket struct
|
2020-11-20 22:01:12 +00:00
|
|
|
pub fn new(name: String) -> Self {
|
2020-11-20 22:20:20 +00:00
|
|
|
Bucket {
|
2020-04-23 18:16:33 +00:00
|
|
|
name,
|
2021-05-02 21:13:08 +00:00
|
|
|
state: crdt::Lww::new(BucketState::Present(BucketParams::new())),
|
2020-11-20 22:20:20 +00:00
|
|
|
}
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
2021-03-26 20:53:28 +00:00
|
|
|
|
2021-04-07 11:39:34 +00:00
|
|
|
/// Returns true if this represents a deleted bucket
|
2020-11-20 22:01:12 +00:00
|
|
|
pub fn is_deleted(&self) -> bool {
|
|
|
|
*self.state.get() == BucketState::Deleted
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
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
|
2020-11-20 22:01:12 +00:00
|
|
|
pub fn authorized_keys(&self) -> &[(String, u64, PermissionSet)] {
|
|
|
|
match self.state.get() {
|
|
|
|
BucketState::Deleted => &[],
|
2020-12-12 16:00:31 +00:00
|
|
|
BucketState::Present(state) => state.authorized_keys.items(),
|
2020-11-20 22:01:12 +00:00
|
|
|
}
|
2020-04-23 20:25:45 +00:00
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Entry<EmptyKey, String> for Bucket {
|
|
|
|
fn partition_key(&self) -> &EmptyKey {
|
|
|
|
&EmptyKey
|
|
|
|
}
|
|
|
|
fn sort_key(&self) -> &String {
|
|
|
|
&self.name
|
|
|
|
}
|
2021-03-10 15:21:56 +00:00
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
|
2021-05-02 21:13:08 +00:00
|
|
|
impl Crdt for Bucket {
|
2020-04-19 15:15:48 +00:00
|
|
|
fn merge(&mut self, other: &Self) {
|
2020-11-20 22:01:12 +00:00
|
|
|
self.state.merge(&other.state);
|
2020-04-19 15:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BucketTable;
|
|
|
|
|
|
|
|
impl TableSchema for BucketTable {
|
|
|
|
type P = EmptyKey;
|
|
|
|
type S = String;
|
|
|
|
type E = Bucket;
|
2020-11-20 19:11:04 +00:00
|
|
|
type Filter = DeletedFilter;
|
2020-04-19 15:15:48 +00:00
|
|
|
|
2020-11-20 19:11:04 +00:00
|
|
|
fn matches_filter(entry: &Self::E, filter: &Self::Filter) -> bool {
|
2020-11-20 22:01:12 +00:00
|
|
|
filter.apply(entry.is_deleted())
|
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
}
|