aboutsummaryrefslogtreecommitdiff
path: root/src/model/bucket_table.rs
blob: ac40407e1adecea4db697f0563464a256c5f3f30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use serde::{Deserialize, Serialize};

use garage_table::crdt::Crdt;
use garage_table::*;
use garage_util::data::*;
use garage_util::time::*;

use crate::permission::BucketKeyPerm;

/// 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 {
	/// ID of the bucket
	pub id: Uuid,
	/// State, and configuration if not deleted, of the bucket
	pub state: crdt::Deletable<BucketParams>,
}

/// Configuration for a bucket
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct BucketParams {
	/// Bucket's creation date
	pub creation_date: u64,
	/// Map of key with access to the bucket, and what kind of access they give
	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>,
}

impl BucketParams {
	/// Create an empty BucketParams with no authorized keys and no website accesss
	pub fn new() -> Self {
		BucketParams {
			creation_date: now_msec(),
			authorized_keys: crdt::Map::new(),
			aliases: crdt::LwwMap::new(),
			local_aliases: crdt::LwwMap::new(),
		}
	}
}

impl Crdt for BucketParams {
	fn merge(&mut self, o: &Self) {
		self.authorized_keys.merge(&o.authorized_keys);
		self.aliases.merge(&o.aliases);
		self.local_aliases.merge(&o.local_aliases);
	}
}

impl Default for Bucket {
	fn default() -> Self {
		Self::new()
	}
}

impl Default for BucketParams {
	fn default() -> Self {
		Self::new()
	}
}

impl Bucket {
	/// Initializes a new instance of the Bucket struct
	pub fn new() -> Self {
		Bucket {
			id: gen_uuid(),
			state: crdt::Deletable::present(BucketParams::new()),
		}
	}

	/// Returns true if this represents a deleted bucket
	pub fn is_deleted(&self) -> bool {
		self.state.is_deleted()
	}

	/// Return the list of authorized keys, when each was updated, and the permission associated to
	/// the key
	pub fn authorized_keys(&self) -> &[(String, BucketKeyPerm)] {
		match &self.state {
			crdt::Deletable::Deleted => &[],
			crdt::Deletable::Present(state) => state.authorized_keys.items(),
		}
	}
}

impl Entry<Uuid, EmptyKey> for Bucket {
	fn partition_key(&self) -> &Uuid {
		&self.id
	}
	fn sort_key(&self) -> &EmptyKey {
		&EmptyKey
	}
}

impl Crdt for Bucket {
	fn merge(&mut self, other: &Self) {
		self.state.merge(&other.state);
	}
}

pub struct BucketTable;

impl TableSchema for BucketTable {
	const TABLE_NAME: &'static str = "bucket_v2";

	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())
	}
}