Cache only stores references.

This commit is contained in:
Jeff Baskin 2023-07-06 17:28:10 -04:00
parent 4dad6f7a05
commit ea7dec2f4e
2 changed files with 80 additions and 4 deletions

View File

@ -132,10 +132,10 @@ impl Cache {
} }
}; };
for name in data.list() { for name in data.list() {
match store.add(name) { let id = self.ids.next().unwrap();
match store.add_by_id(name, &id) {
Ok(_) => { Ok(_) => {
self.data self.data.insert(id, FromCache::DB(Database::new()));
.insert(self.ids.next().unwrap(), FromCache::DB(Database::new()));
} }
Err(err) => return FromCache::Error(err), Err(err) => return FromCache::Error(err),
} }
@ -228,7 +228,21 @@ mod engine {
db_out, cache.data 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
),
}
} }
} }

View File

@ -27,6 +27,21 @@ impl Store {
} }
} }
pub fn add_by_id<S, D>(&mut self, name: S, id: D) -> Result<(), MTTError>
where
S: Into<String>,
D: Into<String>,
{
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<Database>> { pub fn get(&self, name: &str) -> Option<&Data<Database>> {
self.data.get(name) 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] #[test]
fn get_bad_database() -> Result<(), MTTError> { fn get_bad_database() -> Result<(), MTTError> {
let store = Store::new(); let store = Store::new();