Skip to content
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

fix(integrations/compat): Fix dead loop happened during list #5240

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 51 additions & 19 deletions integrations/compat/src/v0_50_to_v0_49.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ impl<A: Debug> Debug for CompatAccessor<A> {
impl<A: opendal_v0_50::raw::Access> opendal_v0_49::raw::Access for CompatAccessor<A> {
type Reader = CompatWrapper<A::Reader>;
type Writer = CompatWrapper<A::Writer>;
type Lister = CompatWrapper<A::Lister>;
type Lister = CompatListWrapper<A::Lister>;
type BlockingReader = CompatWrapper<A::BlockingReader>;
type BlockingWriter = CompatWrapper<A::BlockingWriter>;
type BlockingLister = CompatWrapper<A::BlockingLister>;
type BlockingLister = CompatListWrapper<A::BlockingLister>;

fn info(&self) -> Arc<AccessorInfo> {
let new_info = self.0.info().deref().clone();
Expand Down Expand Up @@ -131,7 +131,10 @@ impl<A: opendal_v0_50::raw::Access> opendal_v0_49::raw::Access for CompatAccesso
.list(path, convert::raw_op_list_from(args))
.await
.map_err(convert::error_into)?;
Ok((convert::raw_rp_list_into(rp), CompatWrapper(lister)))
Ok((
convert::raw_rp_list_into(rp),
CompatListWrapper(path.to_string(), lister),
))
}

async fn copy(&self, from: &str, to: &str, args: OpCopy) -> opendal_v0_49::Result<RpCopy> {
Expand Down Expand Up @@ -229,7 +232,10 @@ impl<A: opendal_v0_50::raw::Access> opendal_v0_49::raw::Access for CompatAccesso
.0
.blocking_list(path, convert::raw_op_list_from(args))
.map_err(convert::error_into)?;
Ok((convert::raw_rp_list_into(rp), CompatWrapper(lister)))
Ok((
convert::raw_rp_list_into(rp),
CompatListWrapper(path.to_string(), lister),
))
}

fn blocking_copy(&self, from: &str, to: &str, args: OpCopy) -> opendal_v0_49::Result<RpCopy> {
Expand Down Expand Up @@ -281,16 +287,6 @@ impl<I: opendal_v0_50::raw::oio::Write> opendal_v0_49::raw::oio::Write for Compa
}
}

impl<I: opendal_v0_50::raw::oio::List> opendal_v0_49::raw::oio::List for CompatWrapper<I> {
async fn next(&mut self) -> opendal_v0_49::Result<Option<opendal_v0_49::raw::oio::Entry>> {
self.0
.next()
.await
.map(|v| v.map(convert::raw_oio_entry_into))
.map_err(convert::error_into)
}
}

impl<I: opendal_v0_50::raw::oio::BlockingRead> opendal_v0_49::raw::oio::BlockingRead
for CompatWrapper<I>
{
Expand All @@ -316,14 +312,50 @@ impl<I: opendal_v0_50::raw::oio::BlockingWrite> opendal_v0_49::raw::oio::Blockin
}
}

/// A wrapper to convert `List` from v0.50 to v0.49.
///
/// The first `String` is the path of parent. We save it to check if the entry is itself.
/// In OpenDAL v0.50, lister will return itself, this behavior is different from v0.49.
struct CompatListWrapper<I>(String, I);

impl<I: opendal_v0_50::raw::oio::List> opendal_v0_49::raw::oio::List for CompatListWrapper<I> {
async fn next(&mut self) -> opendal_v0_49::Result<Option<opendal_v0_49::raw::oio::Entry>> {
loop {
let Some(de) = self
.1
.next()
.await
.map(|v| v.map(convert::raw_oio_entry_into))
.map_err(convert::error_into)?
else {
return Ok(None);
};
if de.path() == self.0 {
continue;
}
return Ok(Some(de));
}
}
}

impl<I: opendal_v0_50::raw::oio::BlockingList> opendal_v0_49::raw::oio::BlockingList
for CompatWrapper<I>
for CompatListWrapper<I>
{
fn next(&mut self) -> opendal_v0_49::Result<Option<opendal_v0_49::raw::oio::Entry>> {
self.0
.next()
.map(|v| v.map(convert::raw_oio_entry_into))
.map_err(convert::error_into)
loop {
let Some(de) = self
.1
.next()
.map(|v| v.map(convert::raw_oio_entry_into))
.map_err(convert::error_into)?
else {
return Ok(None);
};
if de.path() == self.0 {
continue;
}
return Ok(Some(de));
}
}
}

Expand Down
Loading