kyoto/data/warehouse/
db.rs1use bytes::Bytes;
2use std::collections::HashMap;
3use std::sync::Mutex;
4
5#[derive(Debug, Clone)]
6struct Entry {
7 data: Bytes,
8}
9
10#[derive(Debug)]
14pub struct Db {
15 hashmap: Mutex<HashMap<String, Entry>>,
16}
17
18impl Db {
19 pub fn new() -> Self {
20 let hashmap_mutex: HashMap<String, Entry> = HashMap::new();
21 Db {
22 hashmap: Mutex::new(hashmap_mutex),
23 }
24 }
25
26 pub fn get(&self, key: &str) -> Option<Bytes> {
27 let state = self.hashmap.lock().unwrap();
28 state.get(key).map(|entry| entry.data.clone())
29 }
30
31 pub fn set(&self, key: &str, val: Bytes) -> crate::Result<()> {
32 let mut state = self.hashmap.lock().unwrap();
33 let entry = Entry {
34 data: val,
35 };
36 state.insert(key.into(), entry);
37 Ok(())
38 }
39}