aboutsummaryrefslogtreecommitdiff
path: root/src/model/helper/bucket.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2024-03-19 17:17:46 +0100
committerAlex Auvolat <alex@adnab.me>2024-03-19 17:17:46 +0100
commitce69dc302c6eaad4fe5268cca3511620fcca12f8 (patch)
treea0a406a26ca15721a3a9d52caecc90a15512f1ab /src/model/helper/bucket.rs
parent65853a48634d662809eee5dafbe48535d400f4a0 (diff)
parent26310f3242319c9ad093f5121cff0fe0c0108542 (diff)
downloadgarage-ce69dc302c6eaad4fe5268cca3511620fcca12f8.tar.gz
garage-ce69dc302c6eaad4fe5268cca3511620fcca12f8.zip
Merge branch 'main' into next-0.10
Diffstat (limited to 'src/model/helper/bucket.rs')
-rw-r--r--src/model/helper/bucket.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/model/helper/bucket.rs b/src/model/helper/bucket.rs
index a712d683..e5506d7e 100644
--- a/src/model/helper/bucket.rs
+++ b/src/model/helper/bucket.rs
@@ -67,6 +67,49 @@ impl<'a> BucketHelper<'a> {
}
}
+ /// Find a bucket by its global alias or a prefix of its uuid
+ pub async fn admin_get_existing_matching_bucket(
+ &self,
+ pattern: &String,
+ ) -> Result<Uuid, Error> {
+ if let Some(uuid) = self.resolve_global_bucket_name(pattern).await? {
+ return Ok(uuid);
+ } else if pattern.len() >= 2 {
+ let hexdec = pattern
+ .get(..pattern.len() & !1)
+ .and_then(|x| hex::decode(x).ok());
+ if let Some(hex) = hexdec {
+ let mut start = [0u8; 32];
+ start
+ .as_mut_slice()
+ .get_mut(..hex.len())
+ .ok_or_bad_request("invalid length")?
+ .copy_from_slice(&hex);
+ let mut candidates = self
+ .0
+ .bucket_table
+ .get_range(
+ &EmptyKey,
+ Some(start.into()),
+ Some(DeletedFilter::NotDeleted),
+ 10,
+ EnumerationOrder::Forward,
+ )
+ .await?
+ .into_iter()
+ .collect::<Vec<_>>();
+ candidates.retain(|x| hex::encode(x.id).starts_with(pattern));
+ if candidates.len() == 1 {
+ return Ok(candidates.into_iter().next().unwrap().id);
+ }
+ }
+ }
+ Err(Error::BadRequest(format!(
+ "Bucket not found / several matching buckets: {}",
+ pattern
+ )))
+ }
+
/// Returns a Bucket if it is present in bucket table,
/// even if it is in deleted state. Querying a non-existing
/// bucket ID returns an internal error.