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 crate::raw::oio;
30use crate::raw::*;
31use crate::services::DashmapConfig;
32use crate::*;
33
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    const SCHEME: Scheme = Scheme::Dashmap;
71    type Config = DashmapConfig;
72
73    fn build(self) -> Result<impl Access> {
74        debug!("backend build started: {:?}", &self);
75
76        let root = normalize_root(
77            self.config
78                .root
79                .clone()
80                .unwrap_or_else(|| "/".to_string())
81                .as_str(),
82        );
83
84        debug!("backend build finished: {:?}", self.config);
85
86        let core = DashmapCore {
87            cache: DashMap::new(),
88        };
89
90        Ok(DashmapAccessor::new(core, root))
91    }
92}
93
94#[derive(Debug, Clone)]
95pub struct DashmapAccessor {
96    core: Arc<DashmapCore>,
97    root: String,
98    info: Arc<AccessorInfo>,
99}
100
101impl DashmapAccessor {
102    fn new(core: DashmapCore, root: String) -> Self {
103        let info = AccessorInfo::default();
104        info.set_scheme(Scheme::Dashmap);
105        info.set_name("dashmap");
106        info.set_root(&root);
107        info.set_native_capability(Capability {
108            read: true,
109
110            write: true,
111            write_can_empty: true,
112            write_with_cache_control: true,
113            write_with_content_type: true,
114            write_with_content_disposition: true,
115            write_with_content_encoding: true,
116
117            delete: true,
118            stat: true,
119            list: true,
120            shared: false,
121            ..Default::default()
122        });
123
124        Self {
125            core: Arc::new(core),
126            root,
127            info: Arc::new(info),
128        }
129    }
130}
131
132impl Access for DashmapAccessor {
133    type Reader = Buffer;
134    type Writer = DashmapWriter;
135    type Lister = oio::HierarchyLister<DashmapLister>;
136    type Deleter = oio::OneShotDeleter<DashmapDeleter>;
137
138    fn info(&self) -> Arc<AccessorInfo> {
139        self.info.clone()
140    }
141
142    async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
143        let p = build_abs_path(&self.root, path);
144
145        match self.core.get(&p)? {
146            Some(value) => {
147                let metadata = value.metadata;
148                Ok(RpStat::new(metadata))
149            }
150            None => {
151                if p.ends_with('/') {
152                    let has_children = self.core.cache.iter().any(|kv| kv.key().starts_with(&p));
153                    if has_children {
154                        return Ok(RpStat::new(Metadata::new(EntryMode::DIR)));
155                    }
156                }
157                Err(Error::new(ErrorKind::NotFound, "key not found in dashmap"))
158            }
159        }
160    }
161
162    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
163        let p = build_abs_path(&self.root, path);
164
165        match self.core.get(&p)? {
166            Some(value) => {
167                let buffer = if args.range().is_full() {
168                    value.content
169                } else {
170                    let range = args.range();
171                    let start = range.offset() as usize;
172                    let end = match range.size() {
173                        Some(size) => (range.offset() + size) as usize,
174                        None => value.content.len(),
175                    };
176                    value.content.slice(start..end.min(value.content.len()))
177                };
178                Ok((RpRead::new(), buffer))
179            }
180            None => Err(Error::new(ErrorKind::NotFound, "key not found in dashmap")),
181        }
182    }
183
184    async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
185        let p = build_abs_path(&self.root, path);
186        Ok((
187            RpWrite::new(),
188            DashmapWriter::new(self.core.clone(), p, args),
189        ))
190    }
191
192    async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
193        Ok((
194            RpDelete::default(),
195            oio::OneShotDeleter::new(DashmapDeleter::new(self.core.clone(), self.root.clone())),
196        ))
197    }
198
199    async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
200        let lister = DashmapLister::new(self.core.clone(), self.root.clone(), path.to_string());
201        let lister = oio::HierarchyLister::new(lister, path, args.recursive());
202        Ok((RpList::default(), lister))
203    }
204}