opendal/services/dashmap/
lister.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 super::core::DashmapCore;
19use crate::raw::oio::Entry;
20use crate::raw::{build_abs_path, build_rel_path, oio};
21use crate::*;
22use std::sync::Arc;
23use std::vec::IntoIter;
24
25pub struct DashmapLister {
26    root: String,
27    path: String,
28    iter: IntoIter<String>,
29}
30
31impl DashmapLister {
32    pub fn new(core: Arc<DashmapCore>, root: String, path: String) -> Self {
33        let entries: Vec<_> = core.cache.iter().map(|item| item.key().clone()).collect();
34        let path = build_abs_path(&root, &path);
35
36        Self {
37            root,
38            path,
39            iter: entries.into_iter(),
40        }
41    }
42}
43
44impl oio::List for DashmapLister {
45    async fn next(&mut self) -> Result<Option<Entry>> {
46        for key in self.iter.by_ref() {
47            if key.starts_with(&self.path) {
48                let path = build_rel_path(&self.root, &key);
49
50                // Determine if it's a file or directory based on trailing slash
51                let mode = if key.ends_with('/') {
52                    EntryMode::DIR
53                } else {
54                    EntryMode::FILE
55                };
56                let entry = Entry::new(&path, Metadata::new(mode));
57                return Ok(Some(entry));
58            }
59        }
60
61        Ok(None)
62    }
63}