aboutsummaryrefslogtreecommitdiff
path: root/src/model/bucket_table.rs
blob: 11f853f9063abaa0feb861a05c96a2b92fbee05d (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
use async_trait::async_trait;
use serde::{Deserialize, Serialize};

use garage_table::*;
use garage_util::error::Error;

#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct Bucket {
	// Primary key
	pub name: String,

	// Timestamp and deletion
	// Upon version increment, all info is replaced
	pub timestamp: u64,
	pub deleted: bool,

	// Authorized keys
	authorized_keys: Vec<AllowedKey>,
}

impl Bucket {
	pub fn new(
		name: String,
		timestamp: u64,
		deleted: bool,
		authorized_keys: Vec<AllowedKey>,
	) -> Self {
		let mut ret = Bucket {
			name,
			timestamp,
			deleted,
			authorized_keys: vec![],
		};
		for key in authorized_keys {
			ret.add_key(key)
				.expect("Duplicate AllowedKey in Bucket constructor");
		}
		ret
	}
	/// Add a key only if it is not already present
	pub fn add_key(&mut self, key: AllowedKey) -> Result<(), ()> {
		match self
			.authorized_keys
			.binary_search_by(|k| k.key_id.cmp(&key.key_id))
		{
			Err(i) => {
				self.authorized_keys.insert(i, key);
				Ok(())
			}
			Ok(_) => Err(()),
		}
	}
	pub fn authorized_keys(&self) -> &[AllowedKey] {
		&self.authorized_keys[..]
	}
	pub fn clear_keys(&mut self) {
		self.authorized_keys.clear();
	}
}

#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct AllowedKey {
	pub key_id: String,
	pub timestamp: u64,
	pub allow_read: bool,
	pub allow_write: bool,
}

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) {
		if other.timestamp > self.timestamp {
			*self = other.clone();
			return;
		}
		if self.timestamp > other.timestamp || self.deleted {
			return;
		}

		for ak in other.authorized_keys.iter() {
			match self
				.authorized_keys
				.binary_search_by(|our_ak| our_ak.key_id.cmp(&ak.key_id))
			{
				Ok(i) => {
					let our_ak = &mut self.authorized_keys[i];
					if ak.timestamp > our_ak.timestamp {
						*our_ak = ak.clone();
					}
				}
				Err(i) => {
					self.authorized_keys.insert(i, ak.clone());
				}
			}
		}
	}
}

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.deleted)
	}
}