opendal/raw/oio/list/
page_list.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::collections::VecDeque;
19use std::future::Future;
20
21use crate::raw::*;
22use crate::*;
23
24/// PageList is used to implement [`oio::List`] based on API supporting pagination. By implementing
25/// PageList, services don't need to care about the details of page list.
26///
27/// # Architecture
28///
29/// The architecture after adopting [`PageList`]:
30///
31/// - Services impl `PageList`
32/// - `PageLister` impl `List`
33/// - Expose `PageLister` as `Accessor::Lister`
34pub trait PageList: Send + Sync + Unpin + 'static {
35    /// next_page is used to fetch next page of entries from underlying storage.
36    #[cfg(not(target_arch = "wasm32"))]
37    fn next_page(&self, ctx: &mut PageContext) -> impl Future<Output = Result<()>> + MaybeSend;
38    #[cfg(target_arch = "wasm32")]
39    /// next_page is used to fetch next page of entries from underlying storage.
40    fn next_page(&self, ctx: &mut PageContext) -> impl Future<Output = Result<()>>;
41}
42
43/// PageContext is the context passing between `PageList`.
44///
45/// [`PageLister`] will init the PageContext, and implementer of [`PageList`] should fill the `PageContext`
46/// based on their needs.
47///
48/// - Set `done` to `true` if all page have been fetched.
49/// - Update `token` if there is more page to fetch. `token` is not exposed to users, it's internal used only.
50/// - Push back into the entries for each entry fetched from underlying storage.
51///
52/// NOTE: `entries` is a `VecDeque` to avoid unnecessary memory allocation. Only `push_back` is allowed.
53pub struct PageContext {
54    /// done is used to indicate whether the list operation is done.
55    pub done: bool,
56    /// token is used by underlying storage services to fetch next page.
57    pub token: String,
58    /// entries are used to store entries fetched from underlying storage.
59    ///
60    /// Please always reuse the same `VecDeque` to avoid unnecessary memory allocation.
61    /// PageLister makes sure that entries is reset before calling `next_page`. Implementer
62    /// can call `push_back` on `entries` directly.
63    pub entries: VecDeque<oio::Entry>,
64}
65
66/// PageLister implements [`oio::List`] based on [`PageList`].
67pub struct PageLister<L: PageList> {
68    inner: L,
69    ctx: PageContext,
70}
71
72impl<L> PageLister<L>
73where
74    L: PageList,
75{
76    /// Create a new PageLister.
77    pub fn new(l: L) -> Self {
78        Self {
79            inner: l,
80            ctx: PageContext {
81                done: false,
82                token: "".to_string(),
83                entries: VecDeque::new(),
84            },
85        }
86    }
87}
88
89impl<L> oio::List for PageLister<L>
90where
91    L: PageList,
92{
93    async fn next(&mut self) -> Result<Option<oio::Entry>> {
94        loop {
95            if let Some(entry) = self.ctx.entries.pop_front() {
96                return Ok(Some(entry));
97            }
98            if self.ctx.done {
99                return Ok(None);
100            }
101
102            self.inner.next_page(&mut self.ctx).await?;
103        }
104    }
105}