aboutsummaryrefslogtreecommitdiff
path: root/src/model/bucket_table.rs
blob: a101555f9e937f671338850061925ef1bb063a91 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use async_trait::async_trait;
use serde::{Deserialize, Serialize};

use garage_table::crdt::CRDT;
use garage_table::*;

use garage_util::error::Error;

use crate::key_table::PermissionSet;

// We import the same file but in its version 0.1.0.
// We can then access v0.1.0 data structures.
// We use them to perform migrations.
use model010::bucket_table as prev;

/// 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 {
	// Primary key
	pub name: String,

	pub state: crdt::LWW<BucketState>,
}

#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub enum BucketState {
	Deleted,
	Present(BucketParams),
}

impl CRDT for BucketState {
	fn merge(&mut self, o: &Self) {
		match o {
			BucketState::Deleted => *self = BucketState::Deleted,
			BucketState::Present(other_params) => {
				if let BucketState::Present(params) = self {
					params.merge(other_params);
				}
			}
		}
	}
}

#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct BucketParams {
	pub authorized_keys: crdt::LWWMap<String, PermissionSet>,
	pub website: crdt::LWW<bool>,
}

impl CRDT for BucketParams {
	fn merge(&mut self, o: &Self) {
		self.authorized_keys.merge(&o.authorized_keys);
		self.website.merge(&o.website);
	}
}

impl BucketParams {
	pub fn new() -> Self {
		BucketParams {
			authorized_keys: crdt::LWWMap::new(),
			website: crdt::LWW::new(false),
		}
	}
}

impl Bucket {
	pub fn new(name: String) -> Self {
		Bucket {
			name,
			state: crdt::LWW::new(BucketState::Present(BucketParams::new())),
		}
	}
	pub fn is_deleted(&self) -> bool {
		*self.state.get() == BucketState::Deleted
	}
	pub fn authorized_keys(&self) -> &[(String, u64, PermissionSet)] {
		match self.state.get() {
			BucketState::Deleted => &[],
			BucketState::Present(state) => state.authorized_keys.items(),
		}
	}
}

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) {
		self.state.merge(&other.state);
	}
}

pub struct BucketTable;

#[async_trait]
impl TableSchema for BucketTable {
	type P = EmptyKey;
	type S = String;
	type E = Bucket;
	type Filter = DeletedFilter;

	async fn updated(&self, _old: Option<Self::E>, _new: Option<Self::E>) -> Result<(), Error> {
		Ok(())
	}

	fn matches_filter(entry: &Self::E, filter: &Self::Filter) -> bool {
		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,
					},
				));
			}

			let params = BucketParams {
				authorized_keys: keys,
				website: crdt::LWW::new(false),
			};

			Some(Bucket {
				name: old.name,
				state: crdt::LWW::migrate_from_raw(old.timestamp, BucketState::Present(params)),
			})
		}
	}
}