use std::collections::HashMap;
use std::sync::RwLock;
use super::types::{Pipeline, PipelineId};
pub struct PipelineRegistry {
pipelines: RwLock<HashMap<PipelineId, Pipeline>>,
}
impl PipelineRegistry {
pub fn new() -> Self {
Self {
pipelines: RwLock::new(HashMap::new()),
}
}
pub fn register(&self, pipeline: Pipeline) {
let id = pipeline.id().clone();
self.pipelines
.write()
.unwrap_or_else(|e| e.into_inner())
.insert(id, pipeline);
}
pub fn get(&self, id: &PipelineId) -> Option<Pipeline> {
self.pipelines
.read()
.unwrap_or_else(|e| e.into_inner())
.get(id)
.cloned()
}
pub fn list(&self) -> Vec<(PipelineId, String)> {
self.pipelines
.read()
.unwrap_or_else(|e| e.into_inner())
.values()
.map(|p| (p.id().clone(), p.name().to_owned()))
.collect()
}
pub fn remove(&self, id: &PipelineId) -> bool {
self.pipelines
.write()
.unwrap_or_else(|e| e.into_inner())
.remove(id)
.is_some()
}
pub fn len(&self) -> usize {
self.pipelines
.read()
.unwrap_or_else(|e| e.into_inner())
.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl Default for PipelineRegistry {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
#[path = "registry_tests.rs"]
mod tests;