Skip to main content

opendal_service_mini_moka/
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 log::debug;
21use opendal_core::raw::*;
22use opendal_core::*;
23
24use super::MINI_MOKA_SCHEME;
25use super::config::MiniMokaConfig;
26use super::core::*;
27use super::deleter::MiniMokaDeleter;
28use super::lister::MiniMokaLister;
29use super::reader::*;
30use super::writer::MiniMokaWriter;
31
32/// [mini-moka](https://github.com/moka-rs/mini-moka) backend support.
33#[doc = include_str!("docs.md")]
34#[derive(Debug, Default)]
35pub struct MiniMokaBuilder {
36    pub(super) config: MiniMokaConfig,
37    pub(super) time_to_live: Option<Duration>,
38    pub(super) time_to_idle: Option<Duration>,
39}
40
41impl MiniMokaBuilder {
42    /// Create a [`MiniMokaBuilder`] with default configuration.
43    pub fn new() -> Self {
44        Self::default()
45    }
46
47    /// Sets the max capacity of the cache.
48    ///
49    /// Refer to [`mini-moka::sync::CacheBuilder::max_capacity`](https://docs.rs/mini-moka/latest/mini_moka/sync/struct.CacheBuilder.html#method.max_capacity)
50    pub fn max_capacity(mut self, v: u64) -> Self {
51        if v != 0 {
52            self.config.max_capacity = Some(v);
53        }
54        self
55    }
56
57    /// Sets the time to live of the cache.
58    ///
59    /// Refer to [`mini-moka::sync::CacheBuilder::time_to_live`](https://docs.rs/mini-moka/latest/mini_moka/sync/struct.CacheBuilder.html#method.time_to_live)
60    pub fn time_to_live(mut self, v: Duration) -> Self {
61        if !v.is_zero() {
62            self.time_to_live = Some(v);
63        }
64        self
65    }
66
67    /// Sets the time to idle of the cache.
68    ///
69    /// Refer to [`mini-moka::sync::CacheBuilder::time_to_idle`](https://docs.rs/mini-moka/latest/mini_moka/sync/struct.CacheBuilder.html#method.time_to_idle)
70    pub fn time_to_idle(mut self, v: Duration) -> Self {
71        if !v.is_zero() {
72            self.time_to_idle = Some(v);
73        }
74        self
75    }
76
77    /// Set root path of this backend
78    pub fn root(mut self, path: &str) -> Self {
79        self.config.root = if path.is_empty() {
80            None
81        } else {
82            Some(path.to_string())
83        };
84
85        self
86    }
87}
88
89impl Builder for MiniMokaBuilder {
90    type Config = MiniMokaConfig;
91
92    fn build(self) -> Result<impl Service> {
93        debug!("backend build started: {:?}", self);
94
95        let mut builder: mini_moka::sync::CacheBuilder<String, MiniMokaValue, _> =
96            mini_moka::sync::Cache::builder();
97
98        // Use entries' bytes as capacity weigher.
99        builder = builder.weigher(|k, v| (k.len() + v.content.len()) as u32);
100
101        if let Some(v) = self.config.max_capacity {
102            builder = builder.max_capacity(v);
103        }
104        let time_to_live = match self.time_to_live {
105            Some(value) => Some(value),
106            None => self
107                .config
108                .time_to_live
109                .map(signed_duration_to_duration)
110                .transpose()?,
111        };
112        if let Some(duration) = time_to_live {
113            builder = builder.time_to_live(duration);
114        }
115        let time_to_idle = match self.time_to_idle {
116            Some(value) => Some(value),
117            None => self
118                .config
119                .time_to_idle
120                .map(signed_duration_to_duration)
121                .transpose()?,
122        };
123        if let Some(duration) = time_to_idle {
124            builder = builder.time_to_idle(duration);
125        }
126
127        let cache = builder.build();
128
129        let root = normalize_root(self.config.root.as_deref().unwrap_or("/"));
130
131        let core = Arc::new(MiniMokaCore { cache });
132
133        debug!("backend build finished: {root}");
134        Ok(MiniMokaBackend::new(core, root))
135    }
136}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141
142    #[test]
143    fn builder_preserves_subsecond_durations() {
144        let time_to_live = Duration::new(1, 500_000_001);
145        let time_to_idle = Duration::from_millis(750);
146        let builder = MiniMokaBuilder::default()
147            .time_to_live(time_to_live)
148            .time_to_idle(time_to_idle);
149
150        assert_eq!(builder.time_to_live, Some(time_to_live));
151        assert_eq!(builder.time_to_idle, Some(time_to_idle));
152    }
153}
154
155#[derive(Debug, Clone)]
156pub(crate) struct MiniMokaBackend {
157    pub(crate) core: Arc<MiniMokaCore>,
158    pub(crate) root: String,
159    capability: Capability,
160}
161
162impl MiniMokaBackend {
163    fn new(core: Arc<MiniMokaCore>, root: String) -> Self {
164        let capability = Capability {
165            stat: true,
166            read: true,
167            write: true,
168            write_can_empty: true,
169            delete: true,
170            list: true,
171
172            ..Default::default()
173        };
174
175        Self {
176            core,
177            root,
178            capability,
179        }
180    }
181}
182
183impl Service for MiniMokaBackend {
184    type Reader = oio::StreamReader<MiniMokaReader>;
185    type Writer = MiniMokaWriter;
186    type Lister = oio::HierarchyLister<MiniMokaLister>;
187    type Deleter = oio::OneShotDeleter<MiniMokaDeleter>;
188    type Copier = ();
189    type Composer = ();
190
191    fn info(&self) -> ServiceInfo {
192        ServiceInfo::new(MINI_MOKA_SCHEME, &self.root, "")
193    }
194
195    fn capability(&self) -> Capability {
196        self.capability
197    }
198
199    async fn create_dir(
200        &self,
201        _ctx: &OperationContext,
202        _path: &str,
203        _args: OpCreateDir,
204    ) -> Result<RpCreateDir> {
205        Err(Error::new(
206            ErrorKind::Unsupported,
207            "operation is not supported",
208        ))
209    }
210
211    async fn stat(&self, _ctx: &OperationContext, path: &str, _: OpStat) -> Result<RpStat> {
212        let p = build_abs_path(&self.root, path);
213
214        // Check if path exists directly in cache
215        match self.core.get(&p) {
216            Some(value) => {
217                let mut metadata = value.metadata.clone().into_builder();
218                if p.ends_with('/') {
219                    metadata.set_dir();
220                } else {
221                    metadata.set_file(value.metadata.content_length());
222                }
223                Ok(RpStat::new(metadata.build()))
224            }
225            None => {
226                if p.ends_with('/') {
227                    let is_prefix = self
228                        .core
229                        .cache
230                        .iter()
231                        .any(|entry| entry.key().starts_with(&p) && entry.key() != &p);
232
233                    if is_prefix {
234                        let mut metadata = MetadataBuilder::unknown();
235                        metadata.set_dir();
236                        return Ok(RpStat::new(metadata.build()));
237                    }
238                }
239
240                Err(Error::new(ErrorKind::NotFound, "path not found"))
241            }
242        }
243    }
244    fn read(&self, _ctx: &OperationContext, path: &str, op: OpRead) -> Result<Self::Reader> {
245        let output: oio::StreamReader<MiniMokaReader> = {
246            Ok(oio::StreamReader::new(MiniMokaReader::new(
247                self.clone(),
248                path,
249                op,
250            )))
251        }?;
252
253        Ok(output)
254    }
255
256    fn write(&self, _ctx: &OperationContext, path: &str, op: OpWrite) -> Result<Self::Writer> {
257        let output: MiniMokaWriter = {
258            let p = build_abs_path(&self.root, path);
259            let writer = MiniMokaWriter::new(self.core.clone(), p, op);
260            Ok(writer)
261        }?;
262
263        Ok(output)
264    }
265
266    fn delete(&self, _ctx: &OperationContext) -> Result<Self::Deleter> {
267        let output: oio::OneShotDeleter<MiniMokaDeleter> = {
268            let deleter = oio::OneShotDeleter::new(MiniMokaDeleter::new(
269                self.core.clone(),
270                self.root.clone(),
271            ));
272            Ok(deleter)
273        }?;
274
275        Ok(output)
276    }
277
278    fn list(&self, _ctx: &OperationContext, path: &str, op: OpList) -> Result<Self::Lister> {
279        let output: oio::HierarchyLister<MiniMokaLister> = {
280            let p = build_abs_path(&self.root, path);
281
282            let mini_moka_lister = MiniMokaLister::new(self.core.clone(), self.root.clone(), p);
283            let lister = oio::HierarchyLister::new(mini_moka_lister, path, op.recursive());
284
285            Ok(lister)
286        }?;
287
288        Ok(output)
289    }
290
291    fn copy(
292        &self,
293        _ctx: &OperationContext,
294        _from: &str,
295        _to: &str,
296        _args: OpCopy,
297    ) -> Result<Self::Copier> {
298        Err(Error::new(
299            ErrorKind::Unsupported,
300            "operation is not supported",
301        ))
302    }
303
304    async fn rename(
305        &self,
306        _ctx: &OperationContext,
307        _from: &str,
308        _to: &str,
309        _args: OpRename,
310    ) -> Result<RpRename> {
311        Err(Error::new(
312            ErrorKind::Unsupported,
313            "operation is not supported",
314        ))
315    }
316
317    async fn presign(
318        &self,
319        _ctx: &OperationContext,
320        _path: &str,
321        _args: OpPresign,
322    ) -> Result<RpPresign> {
323        Err(Error::new(
324            ErrorKind::Unsupported,
325            "operation is not supported",
326        ))
327    }
328}