Xuanwo commented on code in PR #3599:
URL:
https://github.com/apache/incubator-opendal/pull/3599#discussion_r1401515193
##########
core/src/types/list.rs:
##########
@@ -80,43 +83,87 @@ impl Stream for Lister {
return Poll::Ready(None);
}
- if let Some(fut) = self.stating.as_mut() {
- let (path, rp) = ready!(fut.poll_unpin(cx));
-
- // Make sure we will not poll this future again.
- self.stating = None;
- let metadata = match rp {
- Ok(rp) => rp.into_metadata(),
- Err(err) => {
- self.errored = true;
- return Poll::Ready(Some(Err(err)));
- }
- };
-
- return Poll::Ready(Some(Ok(Entry::new(path, metadata))));
+ if !self.task_queue.is_empty() {
+ if let Some(handle) = self.task_queue.back_mut() {
+ let (path, rp) = ready!(handle.poll_unpin(cx)).map_err(|err| {
+ Error::new(ErrorKind::Unexpected, "join handle error")
+ .with_operation("types::Lister::poll_next")
+ .set_source(err)
+ })?;
+
+ return match rp {
+ Ok(rp) => {
+ self.task_queue.pop_back();
+ let metadata = rp.into_metadata();
+ Poll::Ready(Some(Ok(Entry::new(path, metadata))))
+ }
+ Err(err) => {
+ self.errored = true;
+ Poll::Ready(Some(Err(err)))
+ }
+ };
+ }
}
- match ready!(self.lister.poll_next(cx)) {
- Ok(Some(oe)) => {
- let (path, metadata) = oe.into_entry().into_parts();
- if metadata.contains_metakey(self.required_metakey) {
- return Poll::Ready(Some(Ok(Entry::new(path, metadata))));
- }
-
+ if let Some(entry) = self.buf.clone() {
+ let (path, metadata) = entry.into_parts();
+ return if metadata.contains_metakey(self.required_metakey) {
+ self.buf = None;
+ Poll::Ready(Some(Ok(Entry::new(path, metadata))))
+ } else {
let acc = self.acc.clone();
let fut = async move {
let res = acc.stat(&path, OpStat::default()).await;
-
(path, res)
};
- self.stating = Some(Box::pin(fut));
+
+ self.task_queue.push_front(tokio::spawn(fut));
+ self.buf = None;
self.poll_next(cx)
- }
- Ok(None) => Poll::Ready(None),
- Err(err) => {
- self.errored = true;
- Poll::Ready(Some(Err(err)))
- }
+ };
+ }
+
+ loop {
Review Comment:
> By the way, I think I need some guidance regarding the `buf` problem. If
the `metakey` is already known and the `task_queue` is not empty, what should
we do then?
Nice catch.
I believe we should store an enum in `task_queue`, for example:
```rust
enum Abc {
X(JoinHandle),
Y(Option<(String, RpStat)>)
}
```
(nameing is on your decision)
In this way, we can return the already known entry directly without an extra
poll.
##########
core/src/types/list.rs:
##########
@@ -80,43 +83,87 @@ impl Stream for Lister {
return Poll::Ready(None);
}
- if let Some(fut) = self.stating.as_mut() {
- let (path, rp) = ready!(fut.poll_unpin(cx));
-
- // Make sure we will not poll this future again.
- self.stating = None;
- let metadata = match rp {
- Ok(rp) => rp.into_metadata(),
- Err(err) => {
- self.errored = true;
- return Poll::Ready(Some(Err(err)));
- }
- };
-
- return Poll::Ready(Some(Ok(Entry::new(path, metadata))));
+ if !self.task_queue.is_empty() {
+ if let Some(handle) = self.task_queue.back_mut() {
+ let (path, rp) = ready!(handle.poll_unpin(cx)).map_err(|err| {
+ Error::new(ErrorKind::Unexpected, "join handle error")
+ .with_operation("types::Lister::poll_next")
+ .set_source(err)
+ })?;
+
+ return match rp {
+ Ok(rp) => {
+ self.task_queue.pop_back();
+ let metadata = rp.into_metadata();
+ Poll::Ready(Some(Ok(Entry::new(path, metadata))))
+ }
+ Err(err) => {
+ self.errored = true;
+ Poll::Ready(Some(Err(err)))
+ }
+ };
+ }
}
- match ready!(self.lister.poll_next(cx)) {
- Ok(Some(oe)) => {
- let (path, metadata) = oe.into_entry().into_parts();
- if metadata.contains_metakey(self.required_metakey) {
- return Poll::Ready(Some(Ok(Entry::new(path, metadata))));
- }
-
+ if let Some(entry) = self.buf.clone() {
+ let (path, metadata) = entry.into_parts();
+ return if metadata.contains_metakey(self.required_metakey) {
+ self.buf = None;
+ Poll::Ready(Some(Ok(Entry::new(path, metadata))))
+ } else {
let acc = self.acc.clone();
let fut = async move {
let res = acc.stat(&path, OpStat::default()).await;
-
(path, res)
};
- self.stating = Some(Box::pin(fut));
+
+ self.task_queue.push_front(tokio::spawn(fut));
+ self.buf = None;
self.poll_next(cx)
- }
- Ok(None) => Poll::Ready(None),
- Err(err) => {
- self.errored = true;
- Poll::Ready(Some(Err(err)))
- }
+ };
+ }
+
+ loop {
Review Comment:
> By the way, I think I need some guidance regarding the `buf` problem. If
the `metakey` is already known and the `task_queue` is not empty, what should
we do then?
Nice catch.
I believe we should store an enum in `task_queue`, for example:
```rust
enum Abc {
X(JoinHandle),
Y(Option<(String, RpStat)>)
}
```
(naming is on your decision)
In this way, we can return the already known entry directly without an extra
poll.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]