opendal/services/memory/
core.rs1use std::collections::BTreeMap;
19use std::fmt::Debug;
20use std::sync::Arc;
21use std::sync::Mutex;
22
23use crate::*;
24
25#[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}