diff --git a/plate-tool-lib/src/lib.rs b/plate-tool-lib/src/lib.rs index 70231ed..21e5c72 100644 --- a/plate-tool-lib/src/lib.rs +++ b/plate-tool-lib/src/lib.rs @@ -3,6 +3,7 @@ pub mod plate; pub mod plate_instances; pub mod transfer; pub mod transfer_region; +pub mod transfer_region_cache; pub mod transfer_volume; pub mod util; diff --git a/plate-tool-lib/src/transfer_region_cache.rs b/plate-tool-lib/src/transfer_region_cache.rs new file mode 100644 index 0000000..bb453a3 --- /dev/null +++ b/plate-tool-lib/src/transfer_region_cache.rs @@ -0,0 +1,89 @@ +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; + +use crate::transfer::Transfer; +use crate::Well; + +use uuid::Uuid; + +#[derive(Debug)] +pub struct TransferRegionCache { + interior: Arc>, +} + +impl TransferRegionCache { + pub fn new() -> Self { + TransferRegionCache { + interior: Arc::new(Mutex::new(TransferRegionCacheInterior::new())) + } + } + + pub fn add_overwrite(&self, tr: &Transfer) { + let source = tr.transfer_region.get_source_wells(); + let destination = tr.transfer_region.get_destination_wells(); + let uuid = tr.id; + + if let Ok(mut interior) = self.interior.lock() { + interior.cache.insert(uuid, InteriorWellSlices { + source: source.into(), + destination: destination.into(), + }); + } + } + + pub fn invalidate(&self, tr: &Transfer) { + if let Ok(mut interior) = self.interior.lock() { + interior.cache.remove(&tr.id); + } + } + + pub fn get_source(&self, tr: &Transfer) -> Option> { + self.get(tr, true) + } + + pub fn get_destination(&self, tr: &Transfer) -> Option> { + self.get(tr, false) + } + + fn get(&self, tr: &Transfer, is_source: bool) -> Option> { + if let Ok(interior) = self.interior.lock() { + interior.cache.get(&tr.id).map(|x| + if is_source { + x.source.clone() + } else { + x.destination.clone() + }) + } else { + None + } + } +} + +impl Clone for TransferRegionCache { + fn clone(&self) -> Self { + // Clone the interior RC without letting anyone know + // shhhh! + TransferRegionCache { + interior: self.interior.clone(), + } + } +} + +#[derive(Debug)] +struct TransferRegionCacheInterior { + cache: HashMap, +} + +impl TransferRegionCacheInterior { + fn new() -> Self { + TransferRegionCacheInterior { + cache: HashMap::new() + } + } +} + +#[derive(Debug)] +struct InteriorWellSlices { + source: Arc<[Well]>, + destination: Arc<[Well]>, +}