kyoto/data/warehouse/
db.rs

1use bytes::Bytes;
2use std::collections::HashMap;
3use std::sync::Mutex;
4
5#[derive(Debug, Clone)]
6struct Entry {
7    data: Bytes,
8}
9
10/* Db struct. The entity of the whole collection of data structures.
11 * In order to be shared between threads, what the Db struct essentially
12 * contains is an Arc of the actual data structures. */
13#[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}