morethantext-web/src/morethantext/cache.rs

157 lines
4.6 KiB
Rust

use super::{ErrorCode, FromCache, MTTError, Store, ToCache, ENTRY};
use async_std::{channel::Receiver, path::PathBuf};
use std::collections::HashMap;
pub struct Cache {
data: HashMap<String, Store>,
}
impl Cache {
pub async fn new<P>(_dir: P) -> Self
where
P: Into<PathBuf>,
{
let mut data = HashMap::new();
data.insert(ENTRY.to_string(), Store::new());
Self { data: data }
}
pub async fn listen(&mut self, listener: Receiver<ToCache>) {
loop {
match listener.recv().await.unwrap() {
ToCache::Get(data) => {
data.result.send(self.get(data.data)).await.unwrap();
}
ToCache::Commit(data) => {
data.result.send(self.commit(data.data)).await.unwrap();
}
}
}
}
pub fn get<S>(&self, id: S) -> FromCache
where
S: Into<String>,
{
let idd = id.into();
match self.data.get(&idd) {
Some(store) => FromCache::Str(store.clone()),
None => FromCache::Error(MTTError::from_code(ErrorCode::IDNotFound(idd))),
}
}
pub fn commit(&mut self, data: Store) -> FromCache {
self.data.insert(ENTRY.to_string(), data).unwrap();
FromCache::Ok
}
}
#[cfg(test)]
mod engine {
use super::*;
use tempfile::tempdir;
#[async_std::test]
async fn get_entry() {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await;
let expected: Vec<String> = Vec::new();
let result = cache.get(ENTRY);
match result {
FromCache::Str(store) => assert_eq!(store.list(), expected),
_ => assert!(false, "{:?} should be FromCache::Str", result),
}
}
#[async_std::test]
async fn get_bad_entry() -> Result<(), MTTError> {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await;
let ids = ["bad1", "bad2"];
for id in ids {
let output = cache.get(id);
match output {
FromCache::Error(err) => match err.code {
ErrorCode::IDNotFound(_) => {
assert!(
err.to_string().contains(id),
"Had error: {}, Did not contain: {}",
err.to_string(),
id
);
}
_ => return Err(MTTError::new(format!("{:?} is not IDNotFound", err.code))),
},
_ => {
return Err(MTTError::new(format!(
"{:?} is not FromCache::Error",
output
)))
}
}
}
Ok(())
}
#[async_std::test]
async fn commit_database() {
let dir = tempdir().unwrap();
let mut cache = Cache::new(dir.path()).await;
let mut store = Store::new();
let db = "garfield";
store.add(db).unwrap();
cache.commit(store.clone());
let output = cache.get(ENTRY);
match output {
FromCache::Str(result) => assert_eq!(result.list(), store.list()),
_ => assert!(false, "{:?} is not FromCache::Str", output),
}
}
}
#[cfg(test)]
mod messages {
use super::{
super::{start_db, ToCacheMsg},
*,
};
use async_std::channel::unbounded;
use tempfile::tempdir;
#[async_std::test]
async fn get_the_store() {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
let in_s = mtt.to_cache.clone();
let (out_s, out_r) = unbounded();
let msg = ToCacheMsg {
data: ENTRY.to_string(),
result: out_s,
};
in_s.send(ToCache::Get(msg)).await.unwrap();
let result = out_r.recv().await.unwrap();
match result {
FromCache::Str(_) => (),
_ => assert!(false, "{:?} is not FromCache::Str", result),
}
}
#[async_std::test]
async fn get_bad_id() {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
let in_s = mtt.to_cache.clone();
let (out_s, out_r) = unbounded();
let msg = ToCacheMsg {
data: "bad_id!".to_string(),
result: out_s,
};
in_s.send(ToCache::Get(msg)).await.unwrap();
let output = out_r.recv().await.unwrap();
match output {
FromCache::Error(_) => (),
_ => assert!(false, "{:?} is not FromCache::Error", output),
}
}
}