opendal/services/dashmap/
backend.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::sync::Arc;
19
20use dashmap::DashMap;
21use log::debug;
22
23use super::DASHMAP_SCHEME;
24use super::config::DashmapConfig;
25use super::core::DashmapCore;
26use super::deleter::DashmapDeleter;
27use super::lister::DashmapLister;
28use super::writer::DashmapWriter;
29use crate::raw::*;
30use crate::*;
31
32/// [dashmap](https://github.com/xacrimon/dashmap) backend support.
33#[doc = include_str!("docs.md")]
34#[derive(Debug, Default)]
35pub struct DashmapBuilder {
36    pub(super) config: DashmapConfig,
37}
38
39impl DashmapBuilder {
40    /// Set the root for dashmap.
41    pub fn root(mut self, path: &str) -> Self {
42        self.config.root = if path.is_empty() {
43            None
44        } else {
45            Some(path.to_string())
46        };
47
48        self
49    }
50}
51
52impl Builder for DashmapBuilder {
53    type Config = DashmapConfig;
54
55    fn build(self) -> Result<impl Access> {
56        debug!("backend build started: {:?}", &self);
57
58        let root = normalize_root(
59            self.config
60                .root
61                .clone()
62                .unwrap_or_else(|| "/".to_string())
63                .as_str(),
64        );
65
66        debug!("backend build finished: {:?}", self.config);
67
68        let core = DashmapCore {
69            cache: DashMap::new(),
70        };
71
72        Ok(DashmapBackend::new(core, root))
73    }
74}
75
76#[derive(Debug, Clone)]
77pub struct DashmapBackend {
78    core: Arc<DashmapCore>,
79    root: String,
80    info: Arc<AccessorInfo>,
81}
82
83impl DashmapBackend {
84    fn new(core: DashmapCore, root: String) -> Self {
85        let info = AccessorInfo::default();
86        info.set_scheme(DASHMAP_SCHEME);
87        info.set_name("dashmap");
88        info.set_root(&root);
89        info.set_native_capability(Capability {
90            read: true,
91
92            write: true,
93            write_can_empty: true,
94            write_with_cache_control: true,
95            write_with_content_type: true,
96            write_with_content_disposition: true,
97            write_with_content_encoding: true,
98
99            delete: true,
100            stat: true,
101            list: true,
102            shared: false,
103            ..Default::default()
104        });
105
106        Self {
107            core: Arc::new(core),
108            root,
109            info: Arc::new(info),
110        }
111    }
112}
113
114impl Access for DashmapBackend {
115    type Reader = Buffer;
116    type Writer = DashmapWriter;
117    type Lister = oio::HierarchyLister<DashmapLister>;
118    type Deleter = oio::OneShotDeleter<DashmapDeleter>;
119
120    fn info(&self) -> Arc<AccessorInfo> {
121        self.info.clone()
122    }
123
124    async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
125        let p = build_abs_path(&self.root, path);
126
127        match self.core.get(&p)? {
128            Some(value) => {
129                let metadata = value.metadata;
130                Ok(RpStat::new(metadata))
131            }
132            None => {
133                if p.ends_with('/') {
134                    let has_children = self.core.cache.iter().any(|kv| kv.key().starts_with(&p));
135                    if has_children {
136                        return Ok(RpStat::new(Metadata::new(EntryMode::DIR)));
137                    }
138                }
139                Err(Error::new(ErrorKind::NotFound, "key not found in dashmap"))
140            }
141        }
142    }
143
144    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
145        let p = build_abs_path(&self.root, path);
146
147        match self.core.get(&p)? {
148            Some(value) => {
149                let buffer = if args.range().is_full() {
150                    value.content
151                } else {
152                    let range = args.range();
153                    let start = range.offset() as usize;
154                    let end = match range.size() {
155                        Some(size) => (range.offset() + size) as usize,
156                        None => value.content.len(),
157                    };
158                    value.content.slice(start..end.min(value.content.len()))
159                };
160                Ok((RpRead::new(), buffer))
161            }
162            None => Err(Error::new(ErrorKind::NotFound, "key not found in dashmap")),
163        }
164    }
165
166    async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
167        let p = build_abs_path(&self.root, path);
168        Ok((
169            RpWrite::new(),
170            DashmapWriter::new(self.core.clone(), p, args),
171        ))
172    }
173
174    async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
175        Ok((
176            RpDelete::default(),
177            oio::OneShotDeleter::new(DashmapDeleter::new(self.core.clone(), self.root.clone())),
178        ))
179    }
180
181    async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
182        let lister = DashmapLister::new(self.core.clone(), self.root.clone(), path.to_string());
183        let lister = oio::HierarchyLister::new(lister, path, args.recursive());
184        Ok((RpList::default(), lister))
185    }
186}