aboutsummaryrefslogtreecommitdiff
path: root/src/model/bucket_helper.rs
blob: c1280afad2317ac7d4687d23331e6eea5772ea09 (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
use garage_util::data::*;
use garage_util::error::*;

use garage_table::util::EmptyKey;

use crate::bucket_table::Bucket;
use crate::garage::Garage;

pub struct BucketHelper<'a>(pub(crate) &'a Garage);

#[allow(clippy::ptr_arg)]
impl<'a> BucketHelper<'a> {
	pub async fn resolve_global_bucket_name(
		&self,
		bucket_name: &String,
	) -> Result<Option<Uuid>, Error> {
		let hexbucket = hex::decode(bucket_name.as_str())
			.ok()
			.map(|by| Uuid::try_from(&by))
			.flatten();
		if let Some(bucket_id) = hexbucket {
			Ok(self
				.0
				.bucket_table
				.get(&bucket_id, &EmptyKey)
				.await?
				.filter(|x| !x.state.is_deleted())
				.map(|_| bucket_id))
		} else {
			Ok(self
				.0
				.bucket_alias_table
				.get(&EmptyKey, bucket_name)
				.await?
				.map(|x| x.state.get().as_option().map(|x| x.bucket_id))
				.flatten())
		}
	}

	#[allow(clippy::ptr_arg)]
	pub async fn get_existing_bucket(&self, bucket_id: Uuid) -> Result<Bucket, Error> {
		self.0
			.bucket_table
			.get(&bucket_id, &EmptyKey)
			.await?
			.filter(|b| !b.is_deleted())
			.map(Ok)
			.unwrap_or_else(|| {
				Err(Error::BadRpc(format!(
					"Bucket {:?} does not exist",
					bucket_id
				)))
			})
	}
}