1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::sync::{Arc, RwLock};
use std::collections::HashMap;
use snafu::Snafu;
pub struct PeregrineDB (pub Arc<RwLock<HashMap<String, String>>>);
impl PeregrineDB {
pub fn new() -> PeregrineDB {
let hm: HashMap<String, String> = HashMap::new();
let db = PeregrineDB(Arc::new(RwLock::new(hm)));
return db;
}
pub fn start(&self) {
}
pub async fn read(&self, keys: Vec<String>) -> Result<HashMap<String, String>, PeregrineError> {
let read_write = Arc::clone(&self.0);
let hash_map = read_write.read().unwrap();
let mut response: HashMap<String, String> = HashMap::new();
for query in keys {
if hash_map.contains_key(&query) {
response.insert(query.clone(), hash_map.get(&query).unwrap().to_string());
} else {
response.insert(query.clone(), String::from(""));
}
}
Ok(response)
}
pub async fn write(&self, map: HashMap<String, String>) -> Result<HashMap<String, String>, PeregrineError> {
let read_write = Arc::clone(&self.0);
let mut hash_map = read_write.write().unwrap();
for (key, val) in map.iter() {
hash_map.entry(key.to_string()).and_modify(|e| { *e = val.to_string() }).or_insert(val.to_string());
}
Ok(map)
}
}
#[derive(Debug, Snafu)]
pub enum PeregrineError {
#[snafu(display("Unspecified Error"))]
Unspecified
}
type Result<T, E = PeregrineError> = std::result::Result<T, E>;