-
Notifications
You must be signed in to change notification settings - Fork 709
feat: Implement path cache and refactor gdrive #3975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3a8a667
Implement path cache
Xuanwo 446a1ae
feat: Implement path cache and refactor gdrive
Xuanwo e5803ba
Fix gdrive build
Xuanwo 2cf48b8
Fix build
Xuanwo 51cccc4
Fix tests
Xuanwo ad9d819
Fix list
Xuanwo 9055bf9
Fix gdrive
Xuanwo 6612086
Save work
Xuanwo bb6b0c1
Refactor
Xuanwo 887ed66
FIx path cache
Xuanwo bb0b507
Merge branch 'main' into path-cache
Xuanwo c1c019a
Update cache after write file
Xuanwo c8aafab
Update cache with list result
Xuanwo ae2f6f9
Introduce lock for path cache
Xuanwo 8a0845a
Fix ensure path
Xuanwo 3eaa330
Fix build
Xuanwo 53f0670
Fix build
Xuanwo 5543e40
ignore empty path
Xuanwo 03371dd
Make sure ensure_path is guard by lock
Xuanwo 2fa0921
Fix build
Xuanwo b4e0ef3
FIx deadlock
Xuanwo 41b5d7d
Fix assert
Xuanwo d32def9
Fix path
Xuanwo fc096ee
Fix rename
Xuanwo 227f1d1
Fix typo
Xuanwo 9f783e3
Polish
Xuanwo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use crate::raw::*; | ||
| use crate::*; | ||
| use async_trait::async_trait; | ||
| use moka::sync::Cache; | ||
| use std::collections::VecDeque; | ||
| use tokio::sync::{Mutex, MutexGuard}; | ||
|
|
||
| /// The trait required for path cacher. | ||
| #[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
| #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
| pub trait PathQuery { | ||
| /// Fetch the id for the root of the service. | ||
| async fn root(&self) -> Result<String>; | ||
| /// Query the id by parent_id and name. | ||
| async fn query(&self, parent_id: &str, name: &str) -> Result<Option<String>>; | ||
| /// Create a dir by parent_id and name. | ||
| async fn create_dir(&self, parent_id: &str, name: &str) -> Result<String>; | ||
| } | ||
|
|
||
| /// PathCacher is a cache for path query. | ||
| /// | ||
| /// OpenDAL is designed for path based storage systems, such as S3, HDFS, etc. But there are many | ||
| /// services that are not path based, such as OneDrive, Google Drive, etc. For these services, we | ||
| /// lookup files based on id. The lookup of id is very expensive, so we cache the path to id mapping | ||
| /// in PathCacher. | ||
| /// | ||
| /// # Behavior | ||
| /// | ||
| /// The `path` in the cache is always an absolute one. For example, if the service root is `/root/`, | ||
| /// then the path of file `a/b` in cache will be `/root/a/b`. | ||
| pub struct PathCacher<Q: PathQuery> { | ||
| query: Q, | ||
| cache: Cache<String, String>, | ||
|
|
||
| /// This optional lock here is used to prevent concurrent insertions of the same path. | ||
| /// | ||
| /// Some services like gdrive allows the same name to exist in the same directory. We need to introduce | ||
| /// a global lock to prevent concurrent insertions of the same path. | ||
| lock: Option<Mutex<()>>, | ||
| } | ||
|
|
||
| impl<Q: PathQuery> PathCacher<Q> { | ||
| /// Create a new path cacher. | ||
| pub fn new(query: Q) -> Self { | ||
| Self { | ||
| query, | ||
| cache: Cache::new(64 * 1024), | ||
| lock: None, | ||
| } | ||
| } | ||
|
|
||
| /// Enable the lock for the path cacher. | ||
| pub fn with_lock(mut self) -> Self { | ||
| self.lock = Some(Mutex::default()); | ||
| self | ||
| } | ||
|
|
||
| async fn lock(&self) -> Option<MutexGuard<()>> { | ||
| if let Some(l) = &self.lock { | ||
| Some(l.lock().await) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| /// Insert a new cache entry. | ||
| pub async fn insert(&self, path: &str, id: &str) { | ||
| let _guard = self.lock().await; | ||
|
|
||
| // This should never happen, but let's ignore the insert if happened. | ||
| if self.cache.contains_key(path) { | ||
| debug_assert!( | ||
| self.cache.get(path) == Some(id.to_string()), | ||
| "path {path} exists but it's value is inconsistent" | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| self.cache.insert(path.to_string(), id.to_string()); | ||
| } | ||
|
|
||
| /// Remove a cache entry. | ||
| pub async fn remove(&self, path: &str) { | ||
| let _guard = self.lock().await; | ||
|
|
||
| self.cache.invalidate(path) | ||
| } | ||
|
|
||
| /// Get the id for the given path. | ||
| pub async fn get(&self, path: &str) -> Result<Option<String>> { | ||
| let _guard = self.lock().await; | ||
|
|
||
| if let Some(id) = self.cache.get(path) { | ||
| return Ok(Some(id)); | ||
| } | ||
|
|
||
| let mut paths = VecDeque::new(); | ||
| let mut current_path = path; | ||
|
|
||
| while current_path != "/" && !current_path.is_empty() { | ||
| paths.push_front(current_path.to_string()); | ||
| current_path = get_parent(current_path); | ||
| if let Some(id) = self.cache.get(current_path) { | ||
| return self.query_down(&id, paths).await; | ||
| } | ||
| } | ||
|
|
||
| let root_id = self.query.root().await?; | ||
| self.cache.insert("/".to_string(), root_id.clone()); | ||
| self.query_down(&root_id, paths).await | ||
| } | ||
|
|
||
| /// `start_id` is the `file_id` to the start dir to query down. | ||
| /// `paths` is in the order like `["/a/", "/a/b/", "/a/b/c/"]`. | ||
| /// | ||
| /// We should fetch the next `file_id` by sending `query`. | ||
| async fn query_down(&self, start_id: &str, paths: VecDeque<String>) -> Result<Option<String>> { | ||
| let mut current_id = start_id.to_string(); | ||
| for path in paths.into_iter() { | ||
| let name = get_basename(&path); | ||
| current_id = match self.query.query(¤t_id, name).await? { | ||
| Some(id) => { | ||
| self.cache.insert(path, id.clone()); | ||
| id | ||
| } | ||
| None => return Ok(None), | ||
| }; | ||
| } | ||
| Ok(Some(current_id)) | ||
| } | ||
|
|
||
| /// Ensure input dir exists. | ||
| pub async fn ensure_dir(&self, path: &str) -> Result<String> { | ||
| let _guard = self.lock().await; | ||
|
|
||
| let mut tmp = "".to_string(); | ||
| // All parents that need to check. | ||
| let mut parents = vec![]; | ||
| for component in path.split('/') { | ||
| if component.is_empty() { | ||
| continue; | ||
| } | ||
|
|
||
| tmp.push_str(component); | ||
| tmp.push('/'); | ||
| parents.push(tmp.to_string()); | ||
| } | ||
|
|
||
| let mut parent_id = match self.cache.get("/") { | ||
| Some(v) => v, | ||
| None => self.query.root().await?, | ||
| }; | ||
| for parent in parents { | ||
| parent_id = match self.cache.get(&parent) { | ||
| Some(value) => value, | ||
| None => { | ||
| let value = match self.query.query(&parent_id, get_basename(&parent)).await? { | ||
| Some(value) => value, | ||
| None => { | ||
| self.query | ||
| .create_dir(&parent_id, get_basename(&parent)) | ||
| .await? | ||
| } | ||
| }; | ||
| self.cache.insert(parent, value.clone()); | ||
| value | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Ok(parent_id) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::raw::{PathCacher, PathQuery}; | ||
| use crate::*; | ||
| use async_trait::async_trait; | ||
|
|
||
| struct TestQuery {} | ||
|
|
||
| #[async_trait] | ||
| impl PathQuery for TestQuery { | ||
| async fn root(&self) -> Result<String> { | ||
| Ok("root/".to_string()) | ||
| } | ||
|
|
||
| async fn query(&self, parent_id: &str, name: &str) -> Result<Option<String>> { | ||
| if name.starts_with("not_exist") { | ||
| return Ok(None); | ||
| } | ||
| Ok(Some(format!("{parent_id}{name}"))) | ||
| } | ||
|
|
||
| async fn create_dir(&self, parent_id: &str, name: &str) -> Result<String> { | ||
| Ok(format!("{parent_id}{name}")) | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_path_cacher_get() { | ||
| let cases = vec![ | ||
| ("root", "/", Some("root/")), | ||
| ("normal path", "/a", Some("root/a")), | ||
| ("not exist normal dir", "/not_exist/a", None), | ||
| ("not exist normal file", "/a/b/not_exist", None), | ||
| ("nest path", "/a/b/c/d", Some("root/a/b/c/d")), | ||
| ]; | ||
|
|
||
| for (name, input, expect) in cases { | ||
| let cache = PathCacher::new(TestQuery {}); | ||
|
|
||
| let actual = cache.get(input).await.unwrap(); | ||
| assert_eq!(actual.as_deref(), expect, "{}", name) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.