1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use std::cmp;
use std::future::Future;
use std::pin::Pin;
use std::task::ready;
use std::task::Context;
use std::task::Poll;

use flagset::FlagSet;
use futures::Stream;
use futures::StreamExt;

use crate::raw::*;
use crate::*;

/// Lister is designed to list entries at given path in an asynchronous
/// manner.
///
/// Users can construct Lister by [`Operator::lister`] or [`Operator::lister_with`], and can use `metakey` along with list.
/// For example, suppose you need to access `content_length`, you can bring the corresponding field in metakey when listing:
/// `op.list_with("dir/").metakey(Metakey::ContentLength).await?;`.
///
/// - Lister implements `Stream<Item = Result<Entry>>`.
/// - Lister will return `None` if there is no more entries or error has been returned.
pub struct Lister {
    acc: FusedAccessor,
    lister: Option<oio::Lister>,
    /// required_metakey is the metakey required by users.
    required_metakey: FlagSet<Metakey>,

    fut: Option<BoxedStaticFuture<(oio::Lister, Result<Option<oio::Entry>>)>>,

    /// tasks is used to store tasks that are run in concurrent.
    ///
    /// TODO: maybe we should move logic inside?
    tasks: ConcurrentFutures<StatTask>,
    errored: bool,
}

/// StatTask is used to store the task that is run in concurrent.
///
/// # Note for clippy
///
/// Clippy will raise error for this enum like the following:
///
/// ```shell
/// error: large size difference between variants
///   --> core/src/types/list.rs:64:1
///    |
/// 64 | / enum StatTask {
/// 65 | |     /// BoxFuture is used to store the join handle of spawned task.
/// 66 | |     Handle(BoxFuture<(String, Result<RpStat>)>),
///    | |     -------------------------------------------- the second-largest variant contains at least 0 bytes
/// 67 | |     /// KnownEntry is used to store the entry that already contains the required metakey.
/// 68 | |     KnownEntry(Option<Entry>),
///    | |     ------------------------- the largest variant contains at least 264 bytes
/// 69 | | }
///    | |_^ the entire enum is at least 0 bytes
///    |
///    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
///    = note: `-D clippy::large-enum-variant` implied by `-D warnings`
///    = help: to override `-D warnings` add `#[allow(clippy::large_enum_variant)]`
/// help: consider boxing the large fields to reduce the total size of the enum
///    |
/// 68 |     KnownEntry(Box<Option<Entry>>),
///    |                ~~~~~~~~~~~~~~~~~~
/// ```
/// But this lint is wrong since it doesn't take the generic param JoinHandle into account. In fact, they have exactly
/// the same size:
///
/// ```rust
/// use std::mem::size_of;
///
/// use opendal::Entry;
/// use opendal::Result;
///
/// assert_eq!(256, size_of::<(String, Result<opendal::raw::RpStat>)>());
/// assert_eq!(256, size_of::<Option<Entry>>());
/// ```
///
/// So let's ignore this lint:
#[allow(clippy::large_enum_variant)]
enum StatTask {
    /// Stating is used to store the join handle of spawned task.
    ///
    /// TODO: Replace with static future type after rust supported.
    Stating(BoxedStaticFuture<(String, Result<Metadata>)>),
    /// Known is used to store the entry that already contains the required metakey.
    Known(Option<(String, Metadata)>),
}

impl Future for StatTask {
    type Output = (String, Result<Metadata>);

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match self.get_mut() {
            StatTask::Stating(fut) => Pin::new(fut).poll(cx),
            StatTask::Known(entry) => {
                let (path, metadata) = entry.take().expect("entry should not be None");
                Poll::Ready((path, Ok(metadata)))
            }
        }
    }
}

/// # Safety
///
/// Lister will only be accessed by `&mut Self`
unsafe impl Sync for Lister {}

impl Lister {
    /// Create a new lister.
    pub(crate) async fn create(acc: FusedAccessor, path: &str, args: OpList) -> Result<Self> {
        let required_metakey = args.metakey();
        let concurrent = cmp::max(1, args.concurrent());

        let (_, lister) = acc.list(path, args).await?;

        Ok(Self {
            acc,
            lister: Some(lister),
            required_metakey,

            fut: None,
            tasks: ConcurrentFutures::new(concurrent),
            errored: false,
        })
    }
}

impl Stream for Lister {
    type Item = Result<Entry>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        // Returns `None` if we have errored.
        if self.errored {
            return Poll::Ready(None);
        }

        // Trying to pull more tasks if there are more space.
        if self.tasks.has_remaining() {
            // Building future is we have a lister available.
            if let Some(mut lister) = self.lister.take() {
                let fut = async move {
                    let res = lister.next_dyn().await;
                    (lister, res)
                };
                self.fut = Some(Box::pin(fut));
            }

            if let Some(fut) = self.fut.as_mut() {
                if let Poll::Ready((lister, entry)) = fut.as_mut().poll(cx) {
                    self.lister = Some(lister);
                    self.fut = None;

                    match entry {
                        Ok(Some(oe)) => {
                            let (path, metadata) = oe.into_entry().into_parts();
                            if metadata.contains_metakey(self.required_metakey) {
                                self.tasks
                                    .push_back(StatTask::Known(Some((path, metadata))));
                            } else {
                                let acc = self.acc.clone();
                                let fut = async move {
                                    let res = acc.stat(&path, OpStat::default()).await;
                                    (path, res.map(|rp| rp.into_metadata()))
                                };
                                self.tasks.push_back(StatTask::Stating(Box::pin(fut)));
                            }
                        }
                        Ok(None) => {
                            self.lister = None;
                        }
                        Err(err) => {
                            self.errored = true;
                            return Poll::Ready(Some(Err(err)));
                        }
                    }
                }
            }
        }

        // Try to poll tasks
        if let Some((path, rp)) = ready!(self.tasks.poll_next_unpin(cx)) {
            let metadata = rp?;
            return Poll::Ready(Some(Ok(Entry::new(path, metadata))));
        }

        if self.lister.is_some() || self.fut.is_some() {
            Poll::Pending
        } else {
            Poll::Ready(None)
        }
    }
}

/// BlockingLister is designed to list entries at given path in a blocking
/// manner.
///
/// Users can construct Lister by [`BlockingOperator::lister`] or [`BlockingOperator::lister_with`].
///
/// - Lister implements `Iterator<Item = Result<Entry>>`.
/// - Lister will return `None` if there is no more entries or error has been returned.
pub struct BlockingLister {
    acc: FusedAccessor,
    /// required_metakey is the metakey required by users.
    required_metakey: FlagSet<Metakey>,

    lister: oio::BlockingLister,
    errored: bool,
}

/// # Safety
///
/// BlockingLister will only be accessed by `&mut Self`
unsafe impl Sync for BlockingLister {}

impl BlockingLister {
    /// Create a new lister.
    pub(crate) fn create(acc: FusedAccessor, path: &str, args: OpList) -> Result<Self> {
        let required_metakey = args.metakey();
        let (_, lister) = acc.blocking_list(path, args)?;

        Ok(Self {
            acc,
            required_metakey,

            lister,
            errored: false,
        })
    }
}

/// TODO: we can implement next_chunk.
impl Iterator for BlockingLister {
    type Item = Result<Entry>;

    fn next(&mut self) -> Option<Self::Item> {
        // Returns `None` if we have errored.
        if self.errored {
            return None;
        }

        let entry = match self.lister.next() {
            Ok(Some(entry)) => entry,
            Ok(None) => return None,
            Err(err) => {
                self.errored = true;
                return Some(Err(err));
            }
        };

        let (path, metadata) = entry.into_entry().into_parts();
        if metadata.contains_metakey(self.required_metakey) {
            return Some(Ok(Entry::new(path, metadata)));
        }

        let metadata = match self.acc.blocking_stat(&path, OpStat::default()) {
            Ok(rp) => rp.into_metadata(),
            Err(err) => {
                self.errored = true;
                return Some(Err(err));
            }
        };
        Some(Ok(Entry::new(path, metadata)))
    }
}

#[cfg(test)]
#[cfg(feature = "services-azblob")]
mod tests {
    use futures::future;
    use futures::StreamExt;

    use super::*;
    use crate::services::Azblob;

    /// Inspired by <https://gist.github.com/kyle-mccarthy/1e6ae89cc34495d731b91ebf5eb5a3d9>
    ///
    /// Invalid lister should not panic nor endless loop.
    #[tokio::test]
    async fn test_invalid_lister() -> Result<()> {
        let _ = tracing_subscriber::fmt().try_init();

        let mut builder = Azblob::default();

        builder
            .container("container")
            .account_name("account_name")
            .account_key("account_key")
            .endpoint("https://account_name.blob.core.windows.net");

        let operator = Operator::new(builder)?.finish();

        let lister = operator.lister("/").await?;

        lister
            .filter_map(|entry| {
                dbg!(&entry);
                future::ready(entry.ok())
            })
            .for_each(|entry| {
                println!("{:?}", entry);
                future::ready(())
            })
            .await;

        Ok(())
    }
}