//! Contains a mock store for internal testing. //! //! Might make this public alter to users can test their store handlers. use std::{cell::RefCell, collections::HashMap, rc::Rc}; use substreams::{ prelude::{BigInt, StoreDelete, StoreGet, StoreNew}, store::StoreAdd, }; type BigIntStore = HashMap>; #[derive(Debug, Clone)] pub struct MockStore { data: Rc>, } impl StoreDelete for MockStore { fn delete_prefix(&self, _ord: i64, prefix: &String) { self.data .borrow_mut() .retain(|k, _| !k.starts_with(prefix)); } } impl StoreNew for MockStore { fn new() -> Self { Self { data: Rc::new(RefCell::new(HashMap::new())) } } } impl StoreAdd for MockStore { fn add>(&self, ord: u64, key: K, value: BigInt) { let mut guard = self.data.borrow_mut(); guard .entry(key.as_ref().to_string()) .and_modify(|v| { let prev_value = v.last().unwrap().1.clone(); v.push((ord, prev_value + value.clone())); }) .or_insert(vec![(ord, value)]); } fn add_many>(&self, _ord: u64, _keys: &Vec, _value: BigInt) { todo!() } } impl StoreGet for MockStore { fn new(_idx: u32) -> Self { Self { data: Rc::new(RefCell::new(HashMap::new())) } } fn get_at>(&self, ord: u64, key: K) -> Option { self.data .borrow() .get(&key.as_ref().to_string()) .map(|v| { v.iter() .find(|(current_ord, _)| *current_ord == ord) .unwrap() .1 .clone() }) } fn get_last>(&self, key: K) -> Option { self.data .borrow() .get(&key.as_ref().to_string()) .map(|v| v.last().unwrap().1.clone()) } fn get_first>(&self, key: K) -> Option { self.data .borrow() .get(&key.as_ref().to_string()) .map(|v| v.first().unwrap().1.clone()) } fn has_at>(&self, ord: u64, key: K) -> bool { self.data .borrow() .get(&key.as_ref().to_string()) .map(|v| v.iter().any(|(v, _)| *v == ord)) .unwrap_or(false) } fn has_last>(&self, _key: K) -> bool { todo!() } fn has_first>(&self, _key: K) -> bool { todo!() } }