arcon_backend/
in_memory.rs1use crate::StateBackend;
2use arcon_error::*;
3use std::collections::HashMap;
4
5pub struct InMemory {
6 db: HashMap<Vec<u8>, Vec<u8>>,
7}
8
9impl StateBackend for InMemory {
10 fn create(_name: &str) -> InMemory {
11 InMemory { db: HashMap::new() }
12 }
13
14 fn put(&mut self, key: &[u8], value: &[u8]) -> ArconResult<()> {
15 self.db.insert(key.to_vec(), value.to_vec());
16 Ok(())
17 }
18
19 fn get(&self, key: &[u8]) -> ArconResult<Vec<u8>> {
20 if let Some(v) = self.db.get(key) {
21 Ok(v.to_vec())
22 } else {
23 return arcon_err!("{}", "Value not found");
24 }
25 }
26
27 fn checkpoint(&self, _id: String) -> ArconResult<()> {
28 panic!("InMemory backend does not support snapshots");
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35
36 #[test]
37 fn in_mem_test() {
38 let mut db = InMemory::create("test");
39 let key = "key";
40 let value = "hej";
41 let _ = db.put(key.as_bytes(), value.as_bytes()).unwrap();
42 let fetched = db.get(key.as_bytes()).unwrap();
43 assert_eq!(value, String::from_utf8_lossy(&fetched));
44 }
45}