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/12/26 13:01:40 UTC
(incubator-opendal) 01/01: fix: Add tests for listing recursively on not supported services
This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch add-list-test
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
commit 413175c7e079d787a0dc3a4fa4f61895d41edc26
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Tue Dec 26 21:00:54 2023 +0800
fix: Add tests for listing recursively on not supported services
Signed-off-by: Xuanwo <gi...@xuanwo.io>
---
core/src/layers/complete.rs | 27 +++++--
core/src/types/operator/blocking_operator.rs | 25 ++++---
core/src/types/operator/operator.rs | 102 +++++++++++++++++++++------
core/tests/behavior/list.rs | 38 +++++++++-
4 files changed, 154 insertions(+), 38 deletions(-)
diff --git a/core/src/layers/complete.rs b/core/src/layers/complete.rs
index d130149af..12a9aa44c 100644
--- a/core/src/layers/complete.rs
+++ b/core/src/layers/complete.rs
@@ -372,8 +372,16 @@ impl<A: Accessor> CompleteAccessor<A> {
}
// If recursive is true but service can't list_with_recursive
(true, false) => {
- let p = FlatLister::new(self.inner.clone(), path);
- Ok((RpList::default(), CompleteLister::NeedFlat(p)))
+ // Forward path that ends with /
+ if path.ends_with('/') {
+ let p = FlatLister::new(self.inner.clone(), path);
+ Ok((RpList::default(), CompleteLister::NeedFlat(p)))
+ } else {
+ let parent = get_parent(path);
+ let p = FlatLister::new(self.inner.clone(), parent);
+ let p = PrefixLister::new(p, path);
+ Ok((RpList::default(), CompleteLister::Both(p)))
+ }
}
// If recursive and service doesn't support list_with_recursive, we need to handle
// list prefix by ourselves.
@@ -412,8 +420,16 @@ impl<A: Accessor> CompleteAccessor<A> {
}
// If recursive is true but service can't list_with_recursive
(true, false) => {
- let p = FlatLister::new(self.inner.clone(), path);
- Ok((RpList::default(), CompleteLister::NeedFlat(p)))
+ // Forward path that ends with /
+ if path.ends_with('/') {
+ let p = FlatLister::new(self.inner.clone(), path);
+ Ok((RpList::default(), CompleteLister::NeedFlat(p)))
+ } else {
+ let parent = get_parent(path);
+ let p = FlatLister::new(self.inner.clone(), parent);
+ let p = PrefixLister::new(p, path);
+ Ok((RpList::default(), CompleteLister::Both(p)))
+ }
}
// If recursive and service doesn't support list_with_recursive, we need to handle
// list prefix by ourselves.
@@ -733,6 +749,7 @@ pub enum CompleteLister<A: Accessor, P> {
AlreadyComplete(P),
NeedFlat(FlatLister<Arc<A>, P>),
NeedPrefix(PrefixLister<P>),
+ Both(PrefixLister<FlatLister<Arc<A>, P>>),
}
impl<A, P> oio::List for CompleteLister<A, P>
@@ -747,6 +764,7 @@ where
AlreadyComplete(p) => p.poll_next(cx),
NeedFlat(p) => p.poll_next(cx),
NeedPrefix(p) => p.poll_next(cx),
+ Both(p) => p.poll_next(cx),
}
}
}
@@ -763,6 +781,7 @@ where
AlreadyComplete(p) => p.next(),
NeedFlat(p) => p.next(),
NeedPrefix(p) => p.next(),
+ Both(p) => p.next(),
}
}
}
diff --git a/core/src/types/operator/blocking_operator.rs b/core/src/types/operator/blocking_operator.rs
index 9d15bafe9..fa1a1bcbb 100644
--- a/core/src/types/operator/blocking_operator.rs
+++ b/core/src/types/operator/blocking_operator.rs
@@ -886,17 +886,17 @@ impl BlockingOperator {
Ok(())
}
- /// List entries within a given directory.
+ /// List entries that starts with given `path` in parent dir.
///
/// # Notes
///
- /// ## Listing recursively
+ /// ## Recursively List
///
/// This function only read the children of the given directory. To read
/// all entries recursively, use `BlockingOperator::list_with("path").recursive(true)`
/// instead.
///
- /// ## Streaming
+ /// ## Streaming List
///
/// This function will read all entries in the given directory. It could
/// take very long time and consume a lot of memory if the directory
@@ -905,7 +905,7 @@ impl BlockingOperator {
/// In order to avoid this, you can use [`BlockingOperator::lister`] to list entries in
/// a streaming way.
///
- /// ## Metadata
+ /// ## Reuse Metadata
///
/// The only metadata that is guaranteed to be available is the `Mode`.
/// For fetching more metadata, please use [`BlockingOperator::list_with`] and `metakey`.
@@ -937,20 +937,20 @@ impl BlockingOperator {
self.list_with(path).call()
}
- /// List entries within a given directory with options.
+ /// List entries that starts with given `path` in parent dir. with options.
///
/// # Notes
///
- /// ## For streaming
+ /// ## Streaming List
///
/// This function will read all entries in the given directory. It could
/// take very long time and consume a lot of memory if the directory
/// contains a lot of entries.
///
- /// In order to avoid this, you can use [`Operator::lister`] to list entries in
+ /// In order to avoid this, you can use [`BlockingOperator::lister`] to list entries in
/// a streaming way.
///
- /// ## Metadata
+ /// ## Reuse Metadata
///
/// The only metadata that is guaranteed to be available is the `Mode`.
/// For fetching more metadata, please specify the `metakey`.
@@ -1029,15 +1029,14 @@ impl BlockingOperator {
))
}
- /// List entries within a given directory as an iterator.
+ /// List entries that starts with given `path` in parent dir.
///
- /// This function will create a new handle to list entries.
- ///
- /// An error will be returned if given path doesn't end with `/`.
+ /// This function will create a new [`BlockingLister`] to list entries. Users can stop listing
+ /// via dropping this [`Lister`].
///
/// # Notes
///
- /// ## Listing recursively
+ /// ## Recursively List
///
/// This function only read the children of the given directory. To read
/// all entries recursively, use [`BlockingOperator::lister_with`] and `delimiter("")`
diff --git a/core/src/types/operator/operator.rs b/core/src/types/operator/operator.rs
index 9475606c9..111df5af8 100644
--- a/core/src/types/operator/operator.rs
+++ b/core/src/types/operator/operator.rs
@@ -1012,17 +1012,17 @@ impl Operator {
Ok(())
}
- /// List entries within a given directory.
+ /// List entries that starts with given `path` in parent dir.
///
/// # Notes
///
- /// ## Listing recursively
+ /// ## Recursively list
///
/// This function only read the children of the given directory. To read
/// all entries recursively, use `Operator::list_with("path").recursive(true)`
/// instead.
///
- /// ## Streaming
+ /// ## Streaming list
///
/// This function will read all entries in the given directory. It could
/// take very long time and consume a lot of memory if the directory
@@ -1031,13 +1031,17 @@ impl Operator {
/// In order to avoid this, you can use [`Operator::lister`] to list entries in
/// a streaming way.
///
- /// ## Metadata
+ /// ## Reuse Metadata
///
/// The only metadata that is guaranteed to be available is the `Mode`.
/// For fetching more metadata, please use [`Operator::list_with`] and `metakey`.
///
/// # Examples
///
+ /// ## List entries under a dir
+ ///
+ /// This example will list all entries under the dir `path/to/dir/`.
+ ///
/// ```no_run
/// # use anyhow::Result;
/// use opendal::EntryMode;
@@ -1060,15 +1064,46 @@ impl Operator {
/// # Ok(())
/// # }
/// ```
+ ///
+ /// ## List entries with prefix
+ ///
+ /// This example will list all entries under the prefix `path/to/prefix`.
+ ///
+ /// NOTE: it's possible that the prefix itself is also a dir. In this case, you could get
+ /// `path/to/prefix/`, `path/to/prefix_1` and so on. If you do want to list a dir, please
+ /// make sure the path is end with `/`.
+ ///
+ /// ```no_run
+ /// # use anyhow::Result;
+ /// use opendal::EntryMode;
+ /// use opendal::Metakey;
+ /// use opendal::Operator;
+ /// # #[tokio::main]
+ /// # async fn test(op: Operator) -> Result<()> {
+ /// let mut entries = op.list("path/to/prefix").await?;
+ /// for entry in entries {
+ /// match entry.metadata().mode() {
+ /// EntryMode::FILE => {
+ /// println!("Handling file")
+ /// }
+ /// EntryMode::DIR => {
+ /// println!("Handling dir {}", entry.path())
+ /// }
+ /// EntryMode::Unknown => continue,
+ /// }
+ /// }
+ /// # Ok(())
+ /// # }
+ /// ```
pub async fn list(&self, path: &str) -> Result<Vec<Entry>> {
self.list_with(path).await
}
- /// List entries within a given directory with options.
+ /// List entries that starts with given `path` in parent dir with more options.
///
/// # Notes
///
- /// ## For streaming
+ /// ## Streaming list
///
/// This function will read all entries in the given directory. It could
/// take very long time and consume a lot of memory if the directory
@@ -1084,9 +1119,9 @@ impl Operator {
///
/// # Examples
///
- /// ## List entries with prefix
+ /// ## List all entries recursively
///
- /// This function can also be used to list entries in recursive way.
+ /// This example will list all entries under the dir `path/to/dir/`
///
/// ```no_run
/// # use anyhow::Result;
@@ -1095,7 +1130,34 @@ impl Operator {
/// use opendal::Operator;
/// # #[tokio::main]
/// # async fn test(op: Operator) -> Result<()> {
- /// let mut entries = op.list_with("prefix/").recursive(true).await?;
+ /// let mut entries = op.list_with("path/to/dir/").recursive(true).await?;
+ /// for entry in entries {
+ /// match entry.metadata().mode() {
+ /// EntryMode::FILE => {
+ /// println!("Handling file")
+ /// }
+ /// EntryMode::DIR => {
+ /// println!("Handling dir like start a new list via meta.path()")
+ /// }
+ /// EntryMode::Unknown => continue,
+ /// }
+ /// }
+ /// # Ok(())
+ /// # }
+ /// ```
+ ///
+ /// ## List all entries start with prefix
+ ///
+ /// This example will list all entries starts with prefix `path/to/prefix`
+ ///
+ /// ```no_run
+ /// # use anyhow::Result;
+ /// use opendal::EntryMode;
+ /// use opendal::Metakey;
+ /// use opendal::Operator;
+ /// # #[tokio::main]
+ /// # async fn test(op: Operator) -> Result<()> {
+ /// let mut entries = op.list_with("path/to/prefix").recursive(true).await?;
/// for entry in entries {
/// match entry.metadata().mode() {
/// EntryMode::FILE => {
@@ -1113,6 +1175,9 @@ impl Operator {
///
/// ## List entries with metakey for more metadata
///
+ /// Some storage services like `s3` could return more metadata like `content-length` and
+ /// `last-modified`. By using `metakey`, we can fetch those metadata without an extra `stat` call.
+ ///
/// ```no_run
/// # use anyhow::Result;
/// use opendal::EntryMode;
@@ -1162,21 +1227,20 @@ impl Operator {
fut
}
- /// List entries within a given directory as a stream.
+ /// List entries that starts with given `path` in parent dir.
///
- /// This function will create a new handle to list entries.
- ///
- /// An error will be returned if given path doesn't end with `/`.
+ /// This function will create a new [`Lister`] to list entries. Users can stop listing via
+ /// dropping this [`Lister`].
///
/// # Notes
///
- /// ## Listing recursively
+ /// ## Recursively list
///
/// This function only read the children of the given directory. To read
/// all entries recursively, use [`Operator::lister_with`] and `recursive(true)`
/// instead.
///
- /// ## Metadata
+ /// ## Reuse Metadata
///
/// The only metadata that is guaranteed to be available is the `Mode`.
/// For fetching more metadata, please use [`Operator::lister_with`] and `metakey`.
@@ -1211,11 +1275,10 @@ impl Operator {
self.lister_with(path).await
}
- /// List entries within a given directory as a stream with options.
- ///
- /// This function will create a new handle to list entries.
+ /// List entries that starts with given `path` in parent dir with options.
///
- /// An error will be returned if given path doesn't end with `/`.
+ /// This function will create a new [`Lister`] to list entries. Users can stop listing via
+ /// dropping this [`Lister`].
///
/// # Examples
///
@@ -1232,7 +1295,6 @@ impl Operator {
/// # async fn test(op: Operator) -> Result<()> {
/// let mut ds = op
/// .lister_with("path/to/dir/")
- /// .limit(10)
/// .start_after("start")
/// .await?;
/// while let Some(mut entry) = ds.try_next().await? {
diff --git a/core/tests/behavior/list.rs b/core/tests/behavior/list.rs
index c29f0e2dc..7d7b36b8e 100644
--- a/core/tests/behavior/list.rs
+++ b/core/tests/behavior/list.rs
@@ -243,15 +243,51 @@ pub async fn test_list_empty_dir(op: Operator) -> Result<()> {
op.create_dir(&dir).await.expect("write must succeed");
+ // List "dir/" should return empty object.
let mut obs = op.lister(&dir).await?;
let mut objects = HashMap::new();
while let Some(de) = obs.try_next().await? {
objects.insert(de.path().to_string(), de);
}
- debug!("got objects: {:?}", objects);
+ assert_eq!(objects.len(), 0, "dir should only return empty");
+ // List "dir" should return "dir/".
+ let mut obs = op.lister(dir.trim_end_matches('/')).await?;
+ let mut objects = HashMap::new();
+ while let Some(de) = obs.try_next().await? {
+ objects.insert(de.path().to_string(), de);
+ }
+ assert_eq!(objects.len(), 1, "dir should only return empty");
+ assert_eq!(
+ objects[&dir].metadata().mode(),
+ EntryMode::DIR,
+ "given dir should exist and must be dir"
+ );
+
+ // List "dir/" should return empty object.
+ let mut obs = op.lister_with(&dir).recursive(true).await?;
+ let mut objects = HashMap::new();
+ while let Some(de) = obs.try_next().await? {
+ objects.insert(de.path().to_string(), de);
+ }
assert_eq!(objects.len(), 0, "dir should only return empty");
+ // List "dir" should return "dir/".
+ let mut obs = op
+ .lister_with(dir.trim_end_matches('/'))
+ .recursive(true)
+ .await?;
+ let mut objects = HashMap::new();
+ while let Some(de) = obs.try_next().await? {
+ objects.insert(de.path().to_string(), de);
+ }
+ assert_eq!(objects.len(), 1, "dir should only return empty");
+ assert_eq!(
+ objects[&dir].metadata().mode(),
+ EntryMode::DIR,
+ "given dir should exist and must be dir"
+ );
+
op.delete(&dir).await.expect("delete must succeed");
Ok(())
}