Transfer region cache draft

This commit is contained in:
Emilia Allison 2025-01-01 15:44:10 -05:00
parent 3982d1a7a1
commit 12a8e82015
Signed by: emilia
GPG Key ID: 05D5D1107E5100A1
2 changed files with 90 additions and 0 deletions

View File

@ -3,6 +3,7 @@ pub mod plate;
pub mod plate_instances; pub mod plate_instances;
pub mod transfer; pub mod transfer;
pub mod transfer_region; pub mod transfer_region;
pub mod transfer_region_cache;
pub mod transfer_volume; pub mod transfer_volume;
pub mod util; pub mod util;

View File

@ -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<Mutex<TransferRegionCacheInterior>>,
}
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<Arc<[Well]>> {
self.get(tr, true)
}
pub fn get_destination(&self, tr: &Transfer) -> Option<Arc<[Well]>> {
self.get(tr, false)
}
fn get(&self, tr: &Transfer, is_source: bool) -> Option<Arc<[Well]>> {
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<Uuid, InteriorWellSlices>,
}
impl TransferRegionCacheInterior {
fn new() -> Self {
TransferRegionCacheInterior {
cache: HashMap::new()
}
}
}
#[derive(Debug)]
struct InteriorWellSlices {
source: Arc<[Well]>,
destination: Arc<[Well]>,
}