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