opendal/services/memory/
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::collections::BTreeMap;
19use std::fmt::Debug;
20use std::sync::Arc;
21use std::sync::Mutex;
22
23use crate::*;
24
25/// Value stored in memory containing both metadata and content
26#[derive(Clone)]
27pub struct MemoryValue {
28    pub metadata: Metadata,
29    pub content: Buffer,
30}
31
32#[derive(Clone)]
33pub struct MemoryCore {
34    pub data: Arc<Mutex<BTreeMap<String, MemoryValue>>>,
35}
36
37impl Debug for MemoryCore {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        f.debug_struct("MemoryCore").finish_non_exhaustive()
40    }
41}
42
43impl MemoryCore {
44    pub fn new() -> Self {
45        Self {
46            data: Arc::new(Mutex::new(BTreeMap::new())),
47        }
48    }
49
50    pub fn get(&self, key: &str) -> Result<Option<MemoryValue>> {
51        Ok(self.data.lock().unwrap().get(key).cloned())
52    }
53
54    pub fn set(&self, key: &str, value: MemoryValue) -> Result<()> {
55        self.data.lock().unwrap().insert(key.to_string(), value);
56        Ok(())
57    }
58
59    pub fn delete(&self, key: &str) -> Result<()> {
60        self.data.lock().unwrap().remove(key);
61        Ok(())
62    }
63
64    pub fn scan(&self, prefix: &str) -> Result<Vec<String>> {
65        let data = self.data.lock().unwrap();
66
67        if prefix.is_empty() {
68            return Ok(data.keys().cloned().collect());
69        }
70
71        let mut keys = Vec::new();
72        for (key, _) in data.range(prefix.to_string()..) {
73            if !key.starts_with(prefix) {
74                break;
75            }
76            keys.push(key.clone());
77        }
78        Ok(keys)
79    }
80}