From 9976abb9d9899d08c0fd5947af1e809ef1cd63d4 Mon Sep 17 00:00:00 2001 From: abdulrahman1s <61483023+abdulrahman1s@users.noreply.github.com> Date: Sat, 3 Sep 2022 18:48:51 +0000 Subject: [PATCH] perf: Reduce cloud calls --- src/cloud/adapters/s3.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/cloud/adapters/s3.rs b/src/cloud/adapters/s3.rs index 1bf2a75..0dc39df 100644 --- a/src/cloud/adapters/s3.rs +++ b/src/cloud/adapters/s3.rs @@ -3,7 +3,7 @@ use crate::util::*; use crate::{SETTINGS, SYNC_DIR}; use s3::serde_types::ListBucketResult; use s3::{creds::Credentials, Bucket, Region}; -use std::{fs, io::Write, path::PathBuf}; +use std::{collections::HashSet, fs, io::Write, path::PathBuf}; fn init_bucket() -> Result { let s3_secret = SETTINGS.get_string("s3.secret")?; @@ -39,12 +39,17 @@ impl CloudAdapter for Cloud { fn sync(&self) -> Result { let mut synced = 0; + let mut objects = HashSet::new(); for list in self.bucket.list("/".to_owned(), None)? as Vec { for obj in list.contents { let path = key_to_path(&obj.key); - fs::create_dir_all(path.parent().unwrap())?; + objects.insert(path.clone()); + + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } let mut file = fs::File::options() .read(true) @@ -65,7 +70,7 @@ impl CloudAdapter for Cloud { for entry in walk_dir(SYNC_DIR.to_path_buf())? { let path = entry.path(); - if !self.exists(&path)? { + if !objects.contains(&path) { self.save(&path)?; synced += 1; } @@ -95,10 +100,10 @@ impl CloudAdapter for Cloud { Ok(()) } - fn rename(&self, oldpath: &Path, path: &Path) -> Result<()> { + fn rename(&self, from: &Path, to: &Path) -> Result<()> { self.bucket - .copy_object_internal(normalize_path(oldpath), normalize_path(path))?; - self.delete(oldpath)?; + .copy_object_internal(normalize_path(from), normalize_path(to))?; + self.delete(from)?; Ok(()) }