opendal_core/services/dashmap/
core.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;
19
20use dashmap::DashMap;
21
22use crate::*;
23
24/// Value stored in dashmap cache containing both metadata and content
25#[derive(Clone)]
26pub struct DashmapValue {
27    /// Stored metadata in dashmap cache.
28    pub metadata: Metadata,
29    /// Stored content in dashmap cache.
30    pub content: Buffer,
31}
32
33#[derive(Clone)]
34pub struct DashmapCore {
35    pub cache: DashMap<String, DashmapValue>,
36}
37
38impl Debug for DashmapCore {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        f.debug_struct("DashmapCore")
41            .field("size", &self.cache.len())
42            .finish()
43    }
44}
45
46impl DashmapCore {
47    pub fn get(&self, key: &str) -> Result<Option<DashmapValue>> {
48        Ok(self.cache.get(key).map(|v| v.value().clone()))
49    }
50
51    pub fn set(&self, key: &str, value: DashmapValue) -> Result<()> {
52        self.cache.insert(key.to_string(), value);
53        Ok(())
54    }
55
56    pub fn delete(&self, key: &str) -> Result<()> {
57        self.cache.remove(key);
58        Ok(())
59    }
60}