2020-04-19 15:15:48 +00:00
|
|
|
use async_trait::async_trait;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2020-11-20 22:01:12 +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
|
|
|
|
2020-04-24 10:10:01 +00:00
|
|
|
use garage_util::error::Error;
|
2020-04-19 15:15:48 +00:00
|
|
|
|
2020-11-20 22:01:12 +00:00
|
|
|
use crate::key_table::PermissionSet;
|
|
|
|
|
|
|
|
use model010::bucket_table as prev;
|
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Bucket {
|
|
|
|
// Primary key
|
|
|
|
pub name: String,
|
|
|
|
|
2020-11-20 22:01:12 +00:00
|
|
|
pub state: crdt::LWW<BucketState>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum BucketState {
|
|
|
|
Deleted,
|
|
|
|
Present(crdt::LWWMap<String, PermissionSet>),
|
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
|
2020-11-20 22:01:12 +00:00
|
|
|
impl CRDT for BucketState {
|
|
|
|
fn merge(&mut self, o: &Self) {
|
|
|
|
match o {
|
|
|
|
BucketState::Deleted => *self = BucketState::Deleted,
|
|
|
|
BucketState::Present(other_ak) => {
|
|
|
|
if let BucketState::Present(ak) = self {
|
|
|
|
ak.merge(other_ak);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Bucket {
|
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,
|
2020-11-20 22:01:12 +00:00
|
|
|
state: crdt::LWW::new(BucketState::Present(crdt::LWWMap::new())),
|
2020-11-20 22:20:20 +00:00
|
|
|
}
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-11-20 22:01:12 +00:00
|
|
|
pub fn authorized_keys(&self) -> &[(String, u64, PermissionSet)] {
|
|
|
|
match self.state.get() {
|
|
|
|
BucketState::Deleted => &[],
|
|
|
|
BucketState::Present(ak) => ak.items(),
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
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-04-19 20:52:20 +00:00
|
|
|
async fn updated(&self, _old: Option<Self::E>, _new: Option<Self::E>) -> Result<(), Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_migrate(bytes: &[u8]) -> Option<Self::E> {
|
|
|
|
let old = match rmp_serde::decode::from_read_ref::<_, prev::Bucket>(bytes) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => return None,
|
|
|
|
};
|
|
|
|
if old.deleted {
|
|
|
|
Some(Bucket {
|
|
|
|
name: old.name,
|
|
|
|
state: crdt::LWW::migrate_from_raw(old.timestamp, BucketState::Deleted),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let mut keys = crdt::LWWMap::new();
|
|
|
|
for ak in old.authorized_keys() {
|
|
|
|
keys.merge(&crdt::LWWMap::migrate_from_raw_item(
|
|
|
|
ak.key_id.clone(),
|
|
|
|
ak.timestamp,
|
|
|
|
PermissionSet {
|
|
|
|
allow_read: ak.allow_read,
|
|
|
|
allow_write: ak.allow_write,
|
|
|
|
},
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Some(Bucket {
|
|
|
|
name: old.name,
|
|
|
|
state: crdt::LWW::migrate_from_raw(old.timestamp, BucketState::Present(keys)),
|
|
|
|
})
|
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
}
|
|
|
|
}
|