diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-09-19 08:12:54 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-09-19 10:13:49 +0300 |
commit | 70b8d232c2b7e2892a117f61e3a9892bf4994ea4 (patch) | |
tree | 691804e9e8a63d1f5bb6750a20a0d3c0cd6fa6f7 /src/index.rs | |
parent | e1c4683b73ec2a207321377636f4ed722d0674dc (diff) | |
download | obnam2-70b8d232c2b7e2892a117f61e3a9892bf4994ea4.tar.gz |
feat: search, delete chunks on chunk server
Also heavily refactor the now-long scenario by splitting out a happy
path and some unhappy paths.
Diffstat (limited to 'src/index.rs')
-rw-r--r-- | src/index.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/index.rs b/src/index.rs index ed0183e..0166b0f 100644 --- a/src/index.rs +++ b/src/index.rs @@ -30,6 +30,11 @@ impl Index { } } + pub fn remove(&mut self, key: &str, value: &str) { + let kv = kv(key, value); + self.map.remove(&kv); + } + pub fn find(&self, key: &str, value: &str) -> Vec<ChunkId> { let kv = kv(key, value); if let Some(v) = self.map.get(&kv) { @@ -43,6 +48,15 @@ impl Index { self.generations.push(id) } + pub fn remove_generation(&mut self, id: &ChunkId) { + self.generations = self + .generations + .iter() + .cloned() + .filter(|x| x != id) + .collect(); + } + pub fn find_generations(&self) -> Vec<ChunkId> { self.generations.clone() } @@ -82,6 +96,16 @@ mod test { } #[test] + fn removes_inserted() { + let id: ChunkId = "id001".parse().unwrap(); + let mut idx = Index::default(); + idx.insert(id.clone(), "sha256", "abc"); + idx.remove("sha256", "abc"); + let ids: Vec<ChunkId> = idx.find("sha256", "abc"); + assert_eq!(ids, vec![]); + } + + #[test] fn has_no_generations_initially() { let idx = Index::default(); assert_eq!(idx.find_generations(), vec![]); @@ -94,4 +118,13 @@ mod test { idx.insert_generation(id.clone()); assert_eq!(idx.find_generations(), vec![id]); } + + #[test] + fn removes_generaion() { + let id: ChunkId = "id001".parse().unwrap(); + let mut idx = Index::default(); + idx.insert_generation(id.clone()); + idx.remove_generation(&id); + assert_eq!(idx.find_generations(), vec![]); + } } |