From ea7dec2f4e9aac1e92b083c77348514ff1f6b5e2 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 6 Jul 2023 17:28:10 -0400 Subject: [PATCH] Cache only stores references. --- src/morethantext/cache.rs | 22 +++++++++++--- src/morethantext/store.rs | 62 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/morethantext/cache.rs b/src/morethantext/cache.rs index 5373052..de536bb 100644 --- a/src/morethantext/cache.rs +++ b/src/morethantext/cache.rs @@ -132,10 +132,10 @@ impl Cache { } }; for name in data.list() { - match store.add(name) { + let id = self.ids.next().unwrap(); + match store.add_by_id(name, &id) { Ok(_) => { - self.data - .insert(self.ids.next().unwrap(), FromCache::DB(Database::new())); + self.data.insert(id, FromCache::DB(Database::new())); } Err(err) => return FromCache::Error(err), } @@ -228,7 +228,21 @@ mod engine { db_out, cache.data ), } - // Add test that only ids are in the cache. + let store_out = cache.get(ENTRY); + match store_out { + FromCache::Str(updated_store) => match updated_store.get(name) { + Some(output) => { + assert_eq!(output.id, Some(id.to_string())); + assert!(output.data.is_none(), "Should have removed the database."); + } + None => assert!(true, "Store should have stored the database."), + }, + _ => assert!( + false, + "{:?} is not FromCache::Str -- cache is {:?}", + db_out, cache.data + ), + } } } diff --git a/src/morethantext/store.rs b/src/morethantext/store.rs index 735f13b..c4b4061 100644 --- a/src/morethantext/store.rs +++ b/src/morethantext/store.rs @@ -27,6 +27,21 @@ impl Store { } } + pub fn add_by_id(&mut self, name: S, id: D) -> Result<(), MTTError> + where + S: Into, + D: Into, + { + let db_name = name.into(); + match self.get(&db_name) { + Some(_) => Err(MTTError::from_code(ErrorCode::DuplicateDatabase(db_name))), + None => { + self.data.insert(db_name, Data::from_id(id.into())); + Ok(()) + } + } + } + pub fn get(&self, name: &str) -> Option<&Data> { self.data.get(name) } @@ -87,6 +102,53 @@ mod storage { } } + #[test] + fn add_using_cache_id() { + let mut store = Store::new(); + let name = "fred"; + let id = "12345"; + store.add_by_id(name, id).unwrap(); + let output = store.get(name).unwrap(); + assert!(output.data.is_none(), "there should be no data"); + assert_eq!(output.id, Some(id.to_string())); + } + + #[test] + fn add_by_cache_id_name_string() { + let mut store = Store::new(); + let name = "barney"; + let id = "67890"; + store.add_by_id(name.to_string(), id).unwrap(); + let output = store.get(name).unwrap(); + assert!(output.data.is_none(), "there should be no data"); + assert_eq!(output.id, Some(id.to_string())); + } + + #[test] + fn no_duplicate_databases_for_add_by_id() { + let mut store = Store::new(); + let name = "betty"; + store.add_by_id(name, "fghij").unwrap(); + match store.add_by_id(name, "klmno") { + Ok(_) => assert!(false, "Duplicates should error."), + Err(err) => match err.code { + ErrorCode::DuplicateDatabase(db_name) => assert_eq!(db_name, name), + _ => assert!(false, "{:?} is not DuplicateDatabase", err), + }, + } + } + + #[test] + fn add_by_cache_id_string() { + let mut store = Store::new(); + let name = "wilma"; + let id = "abcdef"; + store.add_by_id(name, id.to_string()).unwrap(); + let output = store.get(name).unwrap(); + assert!(output.data.is_none(), "there should be no data"); + assert_eq!(output.id, Some(id.to_string())); + } + #[test] fn get_bad_database() -> Result<(), MTTError> { let store = Store::new();