This is an automated email from the ASF dual-hosted git repository. Xuanwo pushed a commit to branch xuanwo/fix-moka-lister-prefix-filter in repository https://gitbox.apache.org/repos/asf/opendal.git
commit c7d7d40bd61f7c41adf08eba02d708142b904fef Author: Xuanwo <[email protected]> AuthorDate: Sun Nov 22 23:01:48 2026 +0100 fix(services/moka,services/mini_moka): filter lister snapshot by requested prefix Both MokaLister and MiniMokaLister ignored the requested path and snapshotted the entire cache. In recursive mode the wrapping HierarchyLister trusts the inner lister, so list("a/") also returned entries from sibling prefixes like "ab/" and "b/"; even non-recursive lists cloned every unrelated key. Build the absolute listing prefix with build_absolute_path and filter cache entries by it before collecting the snapshot. Also normalize the empty relative path to "/" in MiniMokaLister, matching MokaLister and MemoryLister. --- core/Cargo.lock | 1 + core/services/mini_moka/Cargo.toml | 3 ++ core/services/mini_moka/src/backend.rs | 54 ++++++++++++++++++++++++++++++++ core/services/mini_moka/src/lister.rs | 12 +++++-- core/services/moka/src/backend.rs | 57 +++++++++++++++++++++++++++++++++- core/services/moka/src/lister.rs | 5 ++- 6 files changed, 127 insertions(+), 5 deletions(-) diff --git a/core/Cargo.lock b/core/Cargo.lock index 1b5608bc54..49c99a4fe5 100644 --- a/core/Cargo.lock +++ b/core/Cargo.lock @@ -8339,6 +8339,7 @@ dependencies = [ "mini-moka", "opendal-core", "serde", + "tokio", ] [[package]] diff --git a/core/services/mini_moka/Cargo.toml b/core/services/mini_moka/Cargo.toml index b69b83b4dd..62388f937e 100644 --- a/core/services/mini_moka/Cargo.toml +++ b/core/services/mini_moka/Cargo.toml @@ -36,3 +36,6 @@ log = { workspace = true } mini-moka = "0.10" opendal-core = { path = "../../core", version = "0.59.5", default-features = false } serde = { workspace = true, features = ["derive"] } + +[dev-dependencies] +tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } diff --git a/core/services/mini_moka/src/backend.rs b/core/services/mini_moka/src/backend.rs index ecded34320..e2b888ac82 100644 --- a/core/services/mini_moka/src/backend.rs +++ b/core/services/mini_moka/src/backend.rs @@ -150,6 +150,60 @@ mod tests { assert_eq!(builder.time_to_live, Some(time_to_live)); assert_eq!(builder.time_to_idle, Some(time_to_idle)); } + + async fn list_paths(op: &Operator, path: &str, recursive: bool) -> Result<Vec<String>> { + let mut paths: Vec<String> = op + .list_with(path) + .recursive(recursive) + .await? + .into_iter() + .map(|entry| entry.path().to_string()) + .collect(); + paths.sort_unstable(); + Ok(paths) + } + + #[tokio::test] + async fn recursive_list_only_returns_entries_under_prefix() -> Result<()> { + let op = Operator::new(MiniMokaBuilder::default())?; + for path in ["a/1", "a/nested/2", "ab/1", "b/1"] { + op.write(path, "x").await?; + } + + assert_eq!(list_paths(&op, "a/", true).await?, ["a/1", "a/nested/2"]); + Ok(()) + } + + #[tokio::test] + async fn recursive_list_only_returns_entries_under_prefix_with_root() -> Result<()> { + let op = Operator::new(MiniMokaBuilder::default().root("/sub"))?; + for path in ["a/1", "a/nested/2", "ab/1", "b/1"] { + op.write(path, "x").await?; + } + + assert_eq!(list_paths(&op, "a/", true).await?, ["a/1", "a/nested/2"]); + assert_eq!( + list_paths(&op, "/", true).await?, + ["a/1", "a/nested/2", "ab/1", "b/1"] + ); + Ok(()) + } + + #[tokio::test] + async fn non_recursive_list_and_root_list_still_work() -> Result<()> { + let op = Operator::new(MiniMokaBuilder::default())?; + for path in ["a/1", "a/nested/2", "ab/1", "b/1"] { + op.write(path, "x").await?; + } + + assert_eq!(list_paths(&op, "a/", false).await?, ["a/1", "a/nested/"]); + assert_eq!(list_paths(&op, "/", false).await?, ["a/", "ab/", "b/"]); + assert_eq!( + list_paths(&op, "/", true).await?, + ["a/1", "a/nested/2", "ab/1", "b/1"] + ); + Ok(()) + } } #[derive(Debug, Clone)] diff --git a/core/services/mini_moka/src/lister.rs b/core/services/mini_moka/src/lister.rs index d41a562314..9f0f60f05f 100644 --- a/core/services/mini_moka/src/lister.rs +++ b/core/services/mini_moka/src/lister.rs @@ -29,10 +29,13 @@ pub struct MiniMokaLister { } impl MiniMokaLister { - pub fn new(core: Arc<MiniMokaCore>, root: String, _path: String) -> Self { + /// `path` is the absolute listing prefix inside the cache, built by the + /// backend with `build_absolute_path`. + pub fn new(core: Arc<MiniMokaCore>, root: String, path: String) -> Self { let entries: Vec<(String, Metadata)> = core .cache .iter() + .filter(|entry| entry.key().starts_with(path.as_str())) .map(|entry| (entry.key().to_string(), entry.value().metadata.clone())) .collect(); @@ -47,9 +50,12 @@ impl oio::List for MiniMokaLister { async fn next(&mut self) -> Result<Option<oio::Entry>> { match self.entries.next() { Some((key, metadata)) => { - let rel_path = build_relative_path(&self.root, &key); + let mut path = build_relative_path(&self.root, &key); + if path.is_empty() { + path = "/".to_string(); + } - Ok(Some(oio::Entry::new(&rel_path, metadata))) + Ok(Some(oio::Entry::new(&path, metadata))) } None => Ok(None), } diff --git a/core/services/moka/src/backend.rs b/core/services/moka/src/backend.rs index 925127cad9..c063ffe3ed 100644 --- a/core/services/moka/src/backend.rs +++ b/core/services/moka/src/backend.rs @@ -339,7 +339,8 @@ impl Service for MokaBackend { let output: oio::HierarchyLister<MokaLister> = { // For moka, we don't distinguish between files and directories // Just return the lister to iterate through all matching keys - let lister = MokaLister::new(self.core.clone(), self.root.clone(), path.to_string()); + let p = build_absolute_path(&self.root, path); + let lister = MokaLister::new(self.core.clone(), self.root.clone(), p); let lister = oio::HierarchyLister::new(lister, path, args.recursive()); Ok(lister) }?; @@ -401,4 +402,58 @@ mod tests { assert_eq!(builder.time_to_live, Some(time_to_live)); assert_eq!(builder.time_to_idle, Some(time_to_idle)); } + + async fn list_paths(op: &Operator, path: &str, recursive: bool) -> Result<Vec<String>> { + let mut paths: Vec<String> = op + .list_with(path) + .recursive(recursive) + .await? + .into_iter() + .map(|entry| entry.path().to_string()) + .collect(); + paths.sort_unstable(); + Ok(paths) + } + + #[tokio::test] + async fn recursive_list_only_returns_entries_under_prefix() -> Result<()> { + let op = Operator::new(MokaBuilder::default())?; + for path in ["a/1", "a/nested/2", "ab/1", "b/1"] { + op.write(path, "x").await?; + } + + assert_eq!(list_paths(&op, "a/", true).await?, ["a/1", "a/nested/2"]); + Ok(()) + } + + #[tokio::test] + async fn recursive_list_only_returns_entries_under_prefix_with_root() -> Result<()> { + let op = Operator::new(MokaBuilder::default().root("/sub"))?; + for path in ["a/1", "a/nested/2", "ab/1", "b/1"] { + op.write(path, "x").await?; + } + + assert_eq!(list_paths(&op, "a/", true).await?, ["a/1", "a/nested/2"]); + assert_eq!( + list_paths(&op, "/", true).await?, + ["a/1", "a/nested/2", "ab/1", "b/1"] + ); + Ok(()) + } + + #[tokio::test] + async fn non_recursive_list_and_root_list_still_work() -> Result<()> { + let op = Operator::new(MokaBuilder::default())?; + for path in ["a/1", "a/nested/2", "ab/1", "b/1"] { + op.write(path, "x").await?; + } + + assert_eq!(list_paths(&op, "a/", false).await?, ["a/1", "a/nested/"]); + assert_eq!(list_paths(&op, "/", false).await?, ["a/", "ab/", "b/"]); + assert_eq!( + list_paths(&op, "/", true).await?, + ["a/1", "a/nested/2", "ab/1", "b/1"] + ); + Ok(()) + } } diff --git a/core/services/moka/src/lister.rs b/core/services/moka/src/lister.rs index 9fa80aa9ee..636e83a49f 100644 --- a/core/services/moka/src/lister.rs +++ b/core/services/moka/src/lister.rs @@ -29,10 +29,13 @@ pub struct MokaLister { } impl MokaLister { - pub fn new(core: Arc<MokaCore>, root: String, _path: String) -> Self { + /// `path` is the absolute listing prefix inside the cache, built by the + /// backend with `build_absolute_path`. + pub fn new(core: Arc<MokaCore>, root: String, path: String) -> Self { let entries: Vec<(String, Metadata)> = core .cache .iter() + .filter(|(key, _)| key.starts_with(path.as_str())) .map(|(key, value)| (key.to_string(), value.metadata.clone())) .collect();
