From 4dad6f7a050a1fd8c6ebd338cbf574ee53d2eee8 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sat, 1 Jul 2023 15:56:12 -0400 Subject: [PATCH] Database is now stored as an entry. --- src/morethantext/cache.rs | 85 ++++++++++++++++++++++++++++++--------- src/morethantext/error.rs | 4 +- src/morethantext/mod.rs | 3 +- 3 files changed, 69 insertions(+), 23 deletions(-) diff --git a/src/morethantext/cache.rs b/src/morethantext/cache.rs index 0c4936b..5373052 100644 --- a/src/morethantext/cache.rs +++ b/src/morethantext/cache.rs @@ -1,7 +1,10 @@ -use super::{ErrorCode, FromCache, MTTError, Store, ToCache, ENTRY}; +use super::{Database, ErrorCode, FromCache, MTTError, Store, ToCache, ENTRY}; use async_std::{channel::Receiver, path::PathBuf}; use rand::{distributions::Alphanumeric, thread_rng, Rng}; -use std::{collections::{HashMap, VecDeque}, iter::Iterator}; +use std::{ + collections::{HashMap, VecDeque}, + iter::Iterator, +}; struct IDGenerator { ids: Option>, @@ -9,20 +12,20 @@ struct IDGenerator { impl IDGenerator { fn new() -> Self { - Self { - ids: None, - } + Self { ids: None } } - fn with_ids(ids: T) -> Self where T: Into>, D: Into { + fn with_ids(ids: T) -> Self + where + T: Into>, + D: Into, + { let id_list = ids.into(); let mut data = VecDeque::new(); for id in id_list { data.push_back(id.into()); } - Self { - ids: Some(data), - } + Self { ids: Some(data) } } } @@ -33,10 +36,7 @@ impl Iterator for IDGenerator { 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, - }; + let output = ids.pop_front(); self.ids = Some(ids); output } @@ -62,7 +62,7 @@ mod genid { #[test] fn controlled_ids() { - let ids = ["one", "two", "three"]; + let ids = ["one", "two", "three"]; let mut gen = IDGenerator::with_ids(ids.clone()); for id in ids { assert_eq!(id, gen.next().unwrap()); @@ -71,7 +71,7 @@ mod genid { } pub struct Cache { - data: HashMap, + data: HashMap, ids: IDGenerator, } @@ -81,13 +81,24 @@ impl Cache { P: Into, { let mut data = HashMap::new(); - data.insert(ENTRY.to_string(), Store::new()); - Self { + data.insert(ENTRY.to_string(), FromCache::Str(Store::new())); + Self { data: data, ids: IDGenerator::new(), } } + async fn with_ids(dir: P, ids: T) -> Self + where + P: Into, + T: Into>, + D: Into, + { + let mut output = Self::new(dir).await; + output.ids = IDGenerator::with_ids(ids); + output + } + pub async fn listen(&mut self, listener: Receiver) { loop { match listener.recv().await.unwrap() { @@ -107,19 +118,31 @@ impl Cache { { let idd = id.into(); match self.data.get(&idd) { - Some(store) => FromCache::Str(store.clone()), + Some(data) => data.clone(), None => FromCache::Error(MTTError::from_code(ErrorCode::IDNotFound(idd))), } } pub fn commit(&mut self, data: Store) -> FromCache { - let store = self.data.get_mut(ENTRY).unwrap(); + let entry_data = self.data.get(ENTRY).unwrap(); + let mut store = match entry_data { + FromCache::Str(ep) => ep.clone(), + _ => { + unreachable!() + } + }; for name in data.list() { match store.add(name) { - Ok(_) => (), + Ok(_) => { + self.data + .insert(self.ids.next().unwrap(), FromCache::DB(Database::new())); + } Err(err) => return FromCache::Error(err), } } + self.data + .insert(ENTRY.to_string(), FromCache::Str(store)) + .unwrap(); FromCache::Ok } } @@ -173,6 +196,7 @@ mod engine { #[async_std::test] async fn commit_database() { + // remove this one for the one below, maybe. let dir = tempdir().unwrap(); let mut cache = Cache::new(dir.path()).await; let mut store = Store::new(); @@ -185,6 +209,27 @@ mod engine { _ => assert!(false, "{:?} is not FromCache::Str", output), } } + + #[async_std::test] + async fn add_database_entry() { + let id = "an_id"; + let name = "garfield"; + let dir = tempdir().unwrap(); + let mut cache = Cache::with_ids(dir.path(), [id]).await; + let mut store = Store::new(); + store.add(name).unwrap(); + cache.commit(store.clone()); + let db_out = cache.get(id); + match db_out { + FromCache::DB(_) => (), + _ => assert!( + false, + "{:?} is not FromCache::DB -- cache is {:?}", + db_out, cache.data + ), + } + // Add test that only ids are in the cache. + } } #[cfg(test)] diff --git a/src/morethantext/error.rs b/src/morethantext/error.rs index 641f604..9297fa0 100644 --- a/src/morethantext/error.rs +++ b/src/morethantext/error.rs @@ -1,6 +1,6 @@ use std::{error::Error, fmt}; -#[derive(Debug)] +#[derive(Clone, Debug)] pub enum ErrorCode { // General Undefined(String), @@ -53,7 +53,7 @@ mod errorcodes { } } -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct MTTError { pub code: ErrorCode, } diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index e574e40..81de5b9 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -27,10 +27,11 @@ pub enum ToCache { Commit(ToCacheMsg), } -#[derive(Debug)] +#[derive(Clone, Debug)] pub enum FromCache { Ok, Str(Store), + DB(Database), Error(MTTError), }