This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new fd92b29256 fix(layers/immutable-index): honor start_after and own list 
capabilities (#8730)
fd92b29256 is described below

commit fd92b292561b99e5258cf6f6b421fb76bc2d0c9e
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 23 06:00:15 2026 +0800

    fix(layers/immutable-index): honor start_after and own list capabilities 
(#8730)
    
    * fix(layers/immutable-index): honor start_after and own list capabilities
    
    The layer answered every list call from the index but inherited the inner
    service's list_with_* capability flags unchanged, except enabling
    list_with_recursive. When the wrapped backend advertised
    list_with_start_after (for example the memory service), the layer
    advertised it too while list() ignored args.start_after() entirely, so
    lister_with().start_after() silently returned entries at or before the
    marker.
    
    - Emit index entries in lexicographical order and filter entries at or
      before start_after for both recursive and hierarchy listings, matching
      the memory service's semantics.
    - Report only the list options the index implements:
      list_with_start_after and list_with_recursive stay enabled;
      list_with_limit, list_with_versions, and list_with_deleted are no
      longer inherited from the inner service.
    - Reject list calls with versions or deleted set to true, since no
      CorrectnessCheckLayer sits above this layer to gate them.
    
    * fix(layers/immutable-index): stop inheriting glob from the wrapped stack
    
    Operator::new installs GlobLayer inside the default layers, so the service
    ImmutableIndexLayer wraps advertises list_with_glob. The layer inherited
    that flag while list() ignored args.glob(), silently returning unfiltered
    entries.
    
    - Clear list_with_glob in capability() and reject glob patterns with the
      same Unsupported error contract as versions and deleted.
    - Cover empty-index, nonexistent-prefix, and past-the-end start_after
      listings, and exercise the option guards through a direct Service::list
      call with no Operator in front.
---
 core/layers/immutable-index/src/lib.rs | 291 ++++++++++++++++++++++++++++++++-
 1 file changed, 290 insertions(+), 1 deletion(-)

diff --git a/core/layers/immutable-index/src/lib.rs 
b/core/layers/immutable-index/src/lib.rs
index 8883ac8552..ca8e6cdf89 100644
--- a/core/layers/immutable-index/src/lib.rs
+++ b/core/layers/immutable-index/src/lib.rs
@@ -26,6 +26,18 @@ use std::vec::IntoIter;
 use opendal_core::raw::*;
 use opendal_core::*;
 
+/// Same contract as `CorrectnessCheckLayer`: report a list option the index
+/// cannot honor instead of silently ignoring it.
+fn new_unsupported_error(scheme: &'static str, op: Operation, args: &str) -> 
Error {
+    let op = op.into_static();
+
+    Error::new(
+        ErrorKind::Unsupported,
+        format!("The service {scheme} does not support the operation {op} with 
the arguments {args}. Please verify if the relevant flags have been enabled, or 
submit an issue if you believe this is incorrect."),
+    )
+    .with_operation(op)
+}
+
 /// `ImmutableIndexLayer` adds an immutable in-memory index to a storage 
service.
 ///
 /// Especially useful for services without list capability like HTTP.
@@ -163,8 +175,25 @@ impl Service for ImmutableIndexService {
 
     fn capability(&self) -> Capability {
         let mut capability = self.inner.capability();
+
+        // List operations are answered by the index, so all `list_with_*`
+        // options must reflect what the index actually implements instead of
+        // inheriting the inner service's capabilities.
         capability.list = true;
         capability.list_with_recursive = true;
+        capability.list_with_start_after = true;
+        // `limit` is a per-request size hint; the index materializes the whole
+        // listing, so it is ignored rather than advertised.
+        capability.list_with_limit = false;
+        // The index only stores paths, so it cannot return versions or
+        // deleted entries.
+        capability.list_with_versions = false;
+        capability.list_with_deleted = false;
+        // `GlobLayer` sits below this layer and advertises `list_with_glob` on
+        // its wrapped stack, but this layer answers list calls directly. The
+        // index cannot honor glob patterns.
+        capability.list_with_glob = false;
+
         capability
     }
 
@@ -204,17 +233,51 @@ impl Service for ImmutableIndexService {
     }
 
     fn list(&self, _ctx: &OperationContext, path: &str, args: OpList) -> 
Result<Self::Lister> {
+        // CorrectnessCheckLayer sits below this layer, so reject the list
+        // options the index cannot honor instead of silently ignoring them.
+        if args.versions() {
+            return Err(new_unsupported_error(
+                self.info().scheme(),
+                Operation::List,
+                "versions",
+            ));
+        }
+        if args.deleted() {
+            return Err(new_unsupported_error(
+                self.info().scheme(),
+                Operation::List,
+                "deleted",
+            ));
+        }
+        if args.glob().is_some() {
+            return Err(new_unsupported_error(
+                self.info().scheme(),
+                Operation::List,
+                "glob",
+            ));
+        }
+
         let mut path = path;
         if path == "/" {
             path = ""
         }
 
-        let idx = if args.recursive() {
+        let mut idx = if args.recursive() {
             self.children_flat(path)
         } else {
             self.children_hierarchy(path)
         };
 
+        // Entries must be emitted in lexicographical order for `start_after`
+        // to mark a resumable position.
+        idx.sort_unstable();
+
+        if let Some(start_after) = args.start_after()
+            && start_after != "/"
+        {
+            idx.retain(|v| v.as_str() > start_after);
+        }
+
         Ok(ImmutableDir::new(idx))
     }
 
@@ -311,7 +374,12 @@ mod tests {
         fn capability(&self) -> Capability {
             Capability {
                 list: true,
+                list_with_limit: true,
+                list_with_start_after: true,
                 list_with_recursive: true,
+                list_with_versions: true,
+                list_with_deleted: true,
+                list_with_glob: true,
                 ..Default::default()
             }
         }
@@ -569,4 +637,225 @@ mod tests {
         );
         Ok(())
     }
+
+    /// The index owns all list options: `start_after` and `recursive` are
+    /// implemented; `limit`, `versions`, `deleted`, and `glob` must not be
+    /// inherited from the inner service, or the layer would advertise options
+    /// it cannot honor.
+    #[tokio::test]
+    async fn test_capability_does_not_inherit_inner_list_options() -> 
Result<()> {
+        let mut iil = ImmutableIndexLayer::default();
+        iil.insert("file".to_string());
+
+        let op = build_operator(iil);
+        let capability = op.info().capability();
+
+        assert!(capability.list);
+        assert!(capability.list_with_recursive);
+        assert!(capability.list_with_start_after);
+        assert!(!capability.list_with_limit);
+        assert!(!capability.list_with_versions);
+        assert!(!capability.list_with_deleted);
+        assert!(!capability.list_with_glob);
+
+        // `versions`, `deleted`, and `glob` must fail fast instead of
+        // returning the current entries as if they were versioned, deleted,
+        // or filtered data.
+        let err = op
+            .list_with("/")
+            .versions(true)
+            .await
+            .expect_err("versions must not be silently ignored");
+        assert_eq!(err.kind(), ErrorKind::Unsupported);
+
+        let err = op
+            .list_with("/")
+            .deleted(true)
+            .await
+            .expect_err("deleted must not be silently ignored");
+        assert_eq!(err.kind(), ErrorKind::Unsupported);
+
+        let err = op
+            .list_with("/")
+            .glob("*.txt")
+            .await
+            .expect_err("glob must not be silently ignored");
+        assert_eq!(err.kind(), ErrorKind::Unsupported);
+        Ok(())
+    }
+
+    /// `start_after` resumes after the given key in lexicographical order, on
+    /// the entries emitted by the list operation.
+    #[tokio::test]
+    async fn test_list_start_after() -> Result<()> {
+        // (name, path, recursive, start_after, expected)
+        let cases: &[(&str, &str, bool, &str, &[&str])] = &[
+            ("before prefix", "a/d/", true, "0", &["a/d/x", "a/d/y"]),
+            ("at key", "a/", true, "a/2", &["a/d/x", "a/d/y", "a/z"]),
+            ("between keys", "a/d/", true, "a/d/w", &["a/d/x", "a/d/y"]),
+            ("key in folder", "a/", true, "a/d/x", &["a/d/y", "a/z"]),
+            ("after prefix", "a/", true, "a0", &[]),
+            ("folds folders", "a/", false, "a/1", &["a/2", "a/d/", "a/z"]),
+            ("before folder", "a/", false, "a/c", &["a/d/", "a/z"]),
+            ("at folder skips it", "a/", false, "a/d/", &["a/z"]),
+            ("in folder skips it", "a/", false, "a/d/x", &["a/z"]),
+            ("prefix without slash", "a/d", false, "a/d/x", &[]),
+            ("list root", "/", false, "a/2", &["a0", "b/"]),
+            ("list root recursively", "/", true, "a0", &["b/1"]),
+        ];
+
+        let mut iil = ImmutableIndexLayer::default();
+        for i in ["a/1", "a/2", "a/d/x", "a/d/y", "a/z", "a0", "b/1"] {
+            iil.insert(i.to_string())
+        }
+
+        let op = build_operator(iil);
+
+        for &(name, path, recursive, start_after, expected) in cases {
+            let actual: Vec<String> = op
+                .lister_with(path)
+                .recursive(recursive)
+                .start_after(start_after)
+                .await?
+                .map_ok(|entry| entry.path().to_string())
+                .try_collect()
+                .await?;
+            assert_eq!(actual, expected, "{name}");
+        }
+        Ok(())
+    }
+
+    /// Listing emits entries in lexicographical order so `start_after` marks
+    /// a resumable position.
+    #[tokio::test]
+    async fn test_list_emits_sorted_order() -> Result<()> {
+        let mut iil = ImmutableIndexLayer::default();
+        for i in ["b/2", "a/c", "b/1", "a/b", "a/d/1", "f.txt"] {
+            iil.insert(i.to_string())
+        }
+
+        let op = build_operator(iil);
+
+        let recursive: Vec<String> = op
+            .lister_with("/")
+            .recursive(true)
+            .await?
+            .map_ok(|entry| entry.path().to_string())
+            .try_collect()
+            .await?;
+        assert_eq!(recursive, ["a/b", "a/c", "a/d/1", "b/1", "b/2", "f.txt"]);
+
+        let hierarchy: Vec<String> = op
+            .lister("/")
+            .await?
+            .map_ok(|entry| entry.path().to_string())
+            .try_collect()
+            .await?;
+        assert_eq!(hierarchy, ["a/", "b/", "f.txt"]);
+        Ok(())
+    }
+
+    /// Wrapping a backend that advertises `list_with_start_after` must honor
+    /// the option instead of silently returning earlier entries.
+    #[tokio::test]
+    async fn test_start_after_with_memory_backend() -> Result<()> {
+        let mut iil = ImmutableIndexLayer::default();
+        for i in ["a", "b"] {
+            iil.insert(i.to_string())
+        }
+
+        let op = Operator::from_iter::<services::Memory>(HashMap::<_, 
_>::default())?.layer(iil);
+
+        let actual: Vec<String> = op
+            .lister_with("/")
+            .start_after("a")
+            .await?
+            .map_ok(|entry| entry.path().to_string())
+            .try_collect()
+            .await?;
+        assert_eq!(actual, ["b"]);
+        Ok(())
+    }
+
+    /// `start_after` on an empty index or a prefix that has no entries returns
+    /// nothing instead of leaking entries outside the prefix.
+    #[tokio::test]
+    async fn test_list_start_after_empty() -> Result<()> {
+        let op = build_operator(ImmutableIndexLayer::default());
+
+        for recursive in [false, true] {
+            let actual: Vec<String> = op
+                .lister_with("/")
+                .recursive(recursive)
+                .start_after("a")
+                .await?
+                .map_ok(|entry| entry.path().to_string())
+                .try_collect()
+                .await?;
+            assert!(actual.is_empty(), "recursive={recursive}");
+        }
+
+        let mut iil = ImmutableIndexLayer::default();
+        for i in ["a/1", "a/2"] {
+            iil.insert(i.to_string())
+        }
+
+        let op = build_operator(iil);
+
+        // `start_after` beyond every key under the prefix.
+        for recursive in [false, true] {
+            let actual: Vec<String> = op
+                .lister_with("a/")
+                .recursive(recursive)
+                .start_after("a/9")
+                .await?
+                .map_ok(|entry| entry.path().to_string())
+                .try_collect()
+                .await?;
+            assert!(actual.is_empty(), "recursive={recursive}");
+        }
+
+        // A prefix with no entries must not leak entries from other prefixes,
+        // even when `start_after` would admit them.
+        let actual: Vec<String> = op
+            .lister_with("missing/")
+            .start_after("0")
+            .await?
+            .map_ok(|entry| entry.path().to_string())
+            .try_collect()
+            .await?;
+        assert!(actual.is_empty());
+        Ok(())
+    }
+
+    /// The option guards live in `ImmutableIndexService::list` itself, so they
+    /// must hold even when the caller drives the `Service` trait directly
+    /// without an `Operator` in front.
+    #[tokio::test]
+    async fn test_list_rejects_unsupported_options_at_service_level() -> 
Result<()> {
+        let mut iil = ImmutableIndexLayer::default();
+        iil.insert("file".to_string());
+
+        let service = iil.layer(Arc::new(MockService));
+        let ctx = OperationContext::default();
+
+        for args in [
+            OpList::from(options::ListOptions {
+                versions: true,
+                ..Default::default()
+            }),
+            OpList::from(options::ListOptions {
+                deleted: true,
+                ..Default::default()
+            }),
+            OpList::new().with_glob("*.txt"),
+        ] {
+            let err = match service.list(&ctx, "/", args) {
+                Err(err) => err,
+                Ok(_) => panic!("unsupported options must be rejected"),
+            };
+            assert_eq!(err.kind(), ErrorKind::Unsupported);
+        }
+        Ok(())
+    }
 }

Reply via email to