2023-06-20 11:51:32 -04:00
|
|
|
use super::{ErrorCode, FromCache, MTTError, Store, ToCache, ENTRY};
|
2023-05-29 15:42:32 -04:00
|
|
|
use async_std::{channel::Receiver, path::PathBuf};
|
2023-07-01 13:26:36 -04:00
|
|
|
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
|
|
|
use std::{collections::{HashMap, VecDeque}, iter::Iterator};
|
|
|
|
|
|
|
|
struct IDGenerator {
|
|
|
|
ids: Option<VecDeque<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IDGenerator {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
ids: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_ids<T, D>(ids: T) -> Self where T: Into<Vec<D>>, D: Into<String> {
|
|
|
|
let id_list = ids.into();
|
|
|
|
let mut data = VecDeque::new();
|
|
|
|
for id in id_list {
|
|
|
|
data.push_back(id.into());
|
|
|
|
}
|
|
|
|
Self {
|
|
|
|
ids: Some(data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for IDGenerator {
|
|
|
|
type Item = String;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match &self.ids {
|
|
|
|
Some(id_list) => {
|
|
|
|
let mut ids = id_list.clone();
|
|
|
|
let output = match ids.pop_front() {
|
|
|
|
Some(id) => Some(id.to_string()),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
self.ids = Some(ids);
|
|
|
|
output
|
|
|
|
}
|
|
|
|
None => Some(thread_rng().sample_iter(&Alphanumeric).take(64).collect()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod genid {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unique_ids() {
|
|
|
|
let mut gen = IDGenerator::new();
|
|
|
|
let mut output: Vec<String> = Vec::new();
|
|
|
|
for _ in 0..10 {
|
|
|
|
let id = gen.next().unwrap();
|
|
|
|
assert!(!output.contains(&id), "{} found in {:?}", id, output);
|
|
|
|
output.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn controlled_ids() {
|
|
|
|
let ids = ["one", "two", "three"];
|
|
|
|
let mut gen = IDGenerator::with_ids(ids.clone());
|
|
|
|
for id in ids {
|
|
|
|
assert_eq!(id, gen.next().unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-29 15:42:32 -04:00
|
|
|
|
2023-06-29 00:17:49 -04:00
|
|
|
pub struct Cache {
|
|
|
|
data: HashMap<String, Store>,
|
2023-07-01 13:26:36 -04:00
|
|
|
ids: IDGenerator,
|
2023-06-29 00:17:49 -04:00
|
|
|
}
|
2023-05-29 15:42:32 -04:00
|
|
|
|
|
|
|
impl Cache {
|
|
|
|
pub async fn new<P>(_dir: P) -> Self
|
|
|
|
where
|
|
|
|
P: Into<PathBuf>,
|
|
|
|
{
|
2023-06-29 00:17:49 -04:00
|
|
|
let mut data = HashMap::new();
|
|
|
|
data.insert(ENTRY.to_string(), Store::new());
|
2023-07-01 13:26:36 -04:00
|
|
|
Self {
|
|
|
|
data: data,
|
|
|
|
ids: IDGenerator::new(),
|
|
|
|
}
|
2023-05-29 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
2023-06-29 00:17:49 -04:00
|
|
|
pub async fn listen(&mut self, listener: Receiver<ToCache>) {
|
2023-05-29 15:42:32 -04:00
|
|
|
loop {
|
2023-06-20 11:51:32 -04:00
|
|
|
match listener.recv().await.unwrap() {
|
|
|
|
ToCache::Get(data) => {
|
2023-06-29 00:17:49 -04:00
|
|
|
data.result.send(self.get(data.data)).await.unwrap();
|
|
|
|
}
|
|
|
|
ToCache::Commit(data) => {
|
|
|
|
data.result.send(self.commit(data.data)).await.unwrap();
|
2023-06-20 11:51:32 -04:00
|
|
|
}
|
|
|
|
}
|
2023-05-29 15:42:32 -04:00
|
|
|
}
|
|
|
|
}
|
2023-06-17 12:01:58 -04:00
|
|
|
|
2023-06-20 11:51:32 -04:00
|
|
|
pub fn get<S>(&self, id: S) -> FromCache
|
2023-06-17 12:01:58 -04:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
|
|
|
let idd = id.into();
|
2023-06-29 00:17:49 -04:00
|
|
|
match self.data.get(&idd) {
|
|
|
|
Some(store) => FromCache::Str(store.clone()),
|
|
|
|
None => FromCache::Error(MTTError::from_code(ErrorCode::IDNotFound(idd))),
|
2023-06-17 12:01:58 -04:00
|
|
|
}
|
|
|
|
}
|
2023-06-29 00:17:49 -04:00
|
|
|
|
|
|
|
pub fn commit(&mut self, data: Store) -> FromCache {
|
2023-06-30 12:23:28 -04:00
|
|
|
let store = self.data.get_mut(ENTRY).unwrap();
|
|
|
|
for name in data.list() {
|
2023-06-30 22:38:18 -04:00
|
|
|
match store.add(name) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => return FromCache::Error(err),
|
|
|
|
}
|
2023-06-30 12:23:28 -04:00
|
|
|
}
|
2023-06-29 00:17:49 -04:00
|
|
|
FromCache::Ok
|
|
|
|
}
|
2023-05-29 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod engine {
|
|
|
|
use super::*;
|
|
|
|
use tempfile::tempdir;
|
|
|
|
|
|
|
|
#[async_std::test]
|
2023-06-17 12:01:58 -04:00
|
|
|
async fn get_entry() {
|
|
|
|
let dir = tempdir().unwrap();
|
|
|
|
let cache = Cache::new(dir.path()).await;
|
|
|
|
let expected: Vec<String> = Vec::new();
|
2023-06-20 11:51:32 -04:00
|
|
|
let result = cache.get(ENTRY);
|
|
|
|
match result {
|
|
|
|
FromCache::Str(store) => assert_eq!(store.list(), expected),
|
|
|
|
_ => assert!(false, "{:?} should be FromCache::Str", result),
|
|
|
|
}
|
2023-06-17 12:01:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
2023-06-20 11:51:32 -04:00
|
|
|
let output = cache.get(id);
|
|
|
|
match output {
|
|
|
|
FromCache::Error(err) => match err.code {
|
2023-06-17 12:01:58 -04:00
|
|
|
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))),
|
|
|
|
},
|
2023-06-20 11:51:32 -04:00
|
|
|
_ => {
|
|
|
|
return Err(MTTError::new(format!(
|
|
|
|
"{:?} is not FromCache::Error",
|
|
|
|
output
|
|
|
|
)))
|
|
|
|
}
|
2023-06-17 12:01:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-06-29 00:17:49 -04:00
|
|
|
|
|
|
|
#[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),
|
|
|
|
}
|
|
|
|
}
|
2023-06-17 12:01:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod messages {
|
2023-06-20 11:51:32 -04:00
|
|
|
use super::{
|
2023-06-29 00:17:49 -04:00
|
|
|
super::{start_db, ToCacheMsg},
|
2023-06-20 11:51:32 -04:00
|
|
|
*,
|
|
|
|
};
|
|
|
|
use async_std::channel::unbounded;
|
2023-06-17 12:01:58 -04:00
|
|
|
use tempfile::tempdir;
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn get_the_store() {
|
2023-05-29 15:42:32 -04:00
|
|
|
let dir = tempdir().unwrap();
|
2023-06-17 12:01:58 -04:00
|
|
|
let mtt = start_db(dir.path()).await.unwrap();
|
|
|
|
let in_s = mtt.to_cache.clone();
|
2023-06-20 11:51:32 -04:00
|
|
|
let (out_s, out_r) = unbounded();
|
2023-06-29 00:17:49 -04:00
|
|
|
let msg = ToCacheMsg {
|
|
|
|
data: ENTRY.to_string(),
|
2023-06-20 11:51:32 -04:00
|
|
|
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();
|
2023-06-29 00:17:49 -04:00
|
|
|
let msg = ToCacheMsg {
|
|
|
|
data: "bad_id!".to_string(),
|
2023-06-20 11:51:32 -04:00
|
|
|
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),
|
|
|
|
}
|
2023-05-29 15:42:32 -04:00
|
|
|
}
|
|
|
|
}
|