You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2023/10/07 08:40:22 UTC
[incubator-opendal] 02/02: refactor(services/gdrive): Use moka instead of Arc>
This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch polish-gdrive
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
commit 2674208a65db4add16460c8eff18dbe3f4220f60
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Sat Oct 7 16:39:48 2023 +0800
refactor(services/gdrive): Use moka instead of Arc<Mutex<HashMap>>
Signed-off-by: Xuanwo <gi...@xuanwo.io>
---
core/src/services/gdrive/backend.rs | 22 ++++--------------
core/src/services/gdrive/core.rs | 46 ++++++++++++++++++++++---------------
2 files changed, 33 insertions(+), 35 deletions(-)
diff --git a/core/src/services/gdrive/backend.rs b/core/src/services/gdrive/backend.rs
index 1cccca607..ad0a4ba58 100644
--- a/core/src/services/gdrive/backend.rs
+++ b/core/src/services/gdrive/backend.rs
@@ -104,11 +104,7 @@ impl Accessor for GdriveBackend {
.map_err(new_json_deserialize_error)?;
if !meta.files.is_empty() {
- self.core.path_cache.insert(
- build_abs_path(&self.core.root, path),
- meta.files[0].id.clone(),
- );
-
+ self.core.cache_insert(path, &meta.files[0].id);
return Ok(RpCreateDir::default());
}
}
@@ -125,9 +121,7 @@ impl Accessor for GdriveBackend {
let meta = serde_json::from_slice::<GdriveFile>(&body)
.map_err(new_json_deserialize_error)?;
- self.core
- .path_cache
- .insert(build_abs_path(&self.core.root, path), meta.id.clone());
+ self.core.cache_insert(path, &meta.id);
Ok(RpCreateDir::default())
}
@@ -205,12 +199,8 @@ impl Accessor for GdriveBackend {
let meta = serde_json::from_slice::<GdriveFile>(&body)
.map_err(new_json_deserialize_error)?;
- self.core
- .path_cache
- .invalidate(&build_abs_path(&self.core.root, from));
- self.core
- .path_cache
- .insert(build_abs_path(&self.core.root, to), meta.id.clone());
+ self.core.cache_remove(from);
+ self.core.cache_insert(to, &meta.id);
Ok(RpRename::default())
}
@@ -225,9 +215,7 @@ impl Accessor for GdriveBackend {
match status {
StatusCode::NO_CONTENT => {
- self.core
- .path_cache
- .invalidate(&build_abs_path(&self.core.root, path));
+ self.core.cache_remove(path);
return Ok(RpDelete::default());
}
diff --git a/core/src/services/gdrive/core.rs b/core/src/services/gdrive/core.rs
index 785895a41..d68d36344 100644
--- a/core/src/services/gdrive/core.rs
+++ b/core/src/services/gdrive/core.rs
@@ -67,6 +67,26 @@ impl Debug for GdriveCore {
}
impl GdriveCore {
+ #[inline]
+ pub fn cache_get(&self, path: &str) -> Option<String> {
+ let path = build_abs_path(&self.root, path);
+ self.path_cache.get(&path)
+ }
+
+ /// Insert the path and file id into the cache.
+ #[inline]
+ pub fn cache_insert(&self, path: &str, id: &str) {
+ let path = build_abs_path(&self.root, path);
+ self.path_cache.insert(path.to_string(), id.to_string());
+ }
+
+ /// Remove the cache for path.
+ #[inline]
+ pub fn cache_remove(&self, path: &str) {
+ let path = build_abs_path(&self.root, path);
+ self.path_cache.invalidate(&path);
+ }
+
/// Get the file id by path.
/// Including file and folder.
///
@@ -77,12 +97,8 @@ impl GdriveCore {
/// - A path is a sequence of file names separated by slashes.
/// - A file only knows its parent id, but not its name.
/// - To find the file id of a file, we need to traverse the path from the root to the file.
- pub(crate) async fn get_file_id_by_path(&self, file_path: &str) -> Result<String> {
- let path = build_abs_path(&self.root, file_path);
-
- let cache = &self.path_cache;
-
- if let Some(id) = cache.get(&path) {
+ pub(crate) async fn get_file_id_by_path(&self, path: &str) -> Result<String> {
+ if let Some(id) = self.cache_get(path) {
return Ok(id.to_owned());
}
@@ -91,7 +107,7 @@ impl GdriveCore {
for (i, item) in file_path_items.iter().enumerate() {
let path_part = file_path_items[0..=i].join("/");
- if let Some(id) = cache.get(&path_part) {
+ if let Some(id) = self.cache_get(&path_part) {
parent_id = id.to_owned();
continue;
}
@@ -136,7 +152,7 @@ impl GdriveCore {
parent_id = gdrive_file_list.files[0].id.clone();
- cache.insert(path_part, parent_id.clone());
+ self.cache_insert(&path_part, &parent_id);
}
_ => {
return Err(parse_error(resp).await?);
@@ -155,17 +171,13 @@ impl GdriveCore {
/// - The path is rooted at the root of the Google Drive.
/// - Will create the parent path recursively.
pub(crate) async fn ensure_parent_path(&self, path: &str) -> Result<String> {
- let path = build_abs_path(&self.root, path);
-
let mut parent: String = "root".to_owned();
let mut file_path_items: Vec<&str> = path.split('/').filter(|&x| !x.is_empty()).collect();
file_path_items.pop();
- let cache = &self.path_cache;
-
for (i, item) in file_path_items.iter().enumerate() {
let path_part = file_path_items[0..=i].join("/");
- if let Some(id) = cache.get(&path_part) {
+ if let Some(id) = self.cache_get(&path_part) {
parent = id.to_owned();
continue;
}
@@ -209,7 +221,7 @@ impl GdriveCore {
parent = gdrive_file_list.files[0].id.clone();
}
- cache.insert(path_part, parent.clone());
+ self.cache_insert(&path_part, &parent);
}
StatusCode::NOT_FOUND => {
let parent_name = file_path_items[i];
@@ -224,7 +236,7 @@ impl GdriveCore {
let parent_id = res.into_body().bytes().await?;
parent = String::from_utf8_lossy(&parent_id).to_string();
- cache.insert(path_part, parent.clone());
+ self.cache_insert(&path_part, &parent);
}
_ => {
return Err(parse_error(res).await?);
@@ -406,8 +418,6 @@ impl GdriveCore {
.collect();
source_parent.pop();
- let cache = &self.path_cache;
-
let file_name = build_abs_path(&self.root, target)
.split('/')
.filter(|&x| !x.is_empty())
@@ -417,7 +427,7 @@ impl GdriveCore {
let metadata = &json!({
"name": file_name,
- "removeParents": [cache.get(&source_parent.join("/")).unwrap().to_string()],
+ "removeParents": [self.cache_get(&source_parent.join("/")).unwrap().to_string()],
"addParents": [parent],
});