use crate::core::ribosome::{real_ribosome::RealRibosome, RibosomeT};
use holochain_types::{prelude::*, share::RwShare};
use holochain_zome_types::entry_def::EntryDef;
use std::collections::{HashMap, HashSet};
#[derive(Default)]
pub struct RibosomeStore {
ribosomes: HashMap<CellId, RealRibosome>,
entry_defs: HashMap<EntryDefBufferKey, EntryDef>,
}
impl RibosomeStore {
pub fn new() -> RwShare<Self> {
RwShare::new(RibosomeStore {
ribosomes: HashMap::new(),
entry_defs: HashMap::new(),
})
}
pub fn add_ribosome(&mut self, cell_id: CellId, ribosome: RealRibosome) {
self.ribosomes.insert(cell_id, ribosome);
}
pub fn add_ribosomes<T: IntoIterator<Item = (CellId, RealRibosome)> + 'static>(
&mut self,
ribosomes: T,
) {
self.ribosomes.extend(ribosomes);
}
#[cfg_attr(feature = "instrument", tracing::instrument(skip(self)))]
pub fn list_dna_hashes(&self) -> HashSet<DnaHash> {
self.ribosomes
.keys()
.map(|c| c.dna_hash().clone())
.collect()
}
#[cfg_attr(feature = "instrument", tracing::instrument(skip(self)))]
pub fn get_dna_def(&self, cell_id: &CellId) -> Option<DnaDef> {
self.ribosomes
.get(cell_id)
.map(|d| d.dna_def_hashed().clone().into_content())
}
#[cfg_attr(feature = "instrument", tracing::instrument(skip(self)))]
pub fn get_dna_file(&self, cell_id: &CellId) -> Option<DnaFile> {
self.ribosomes.get(cell_id).map(|r| r.dna_file().clone())
}
pub fn get_ribosome(&self, cell_id: &CellId) -> Option<RealRibosome> {
self.ribosomes.get(cell_id).cloned()
}
pub fn get_any_ribosome_for_dna_hash(&self, dna_hash: &DnaHash) -> Option<RealRibosome> {
if let Some(cell_id) = self.ribosomes.keys().find(|c| c.dna_hash() == dna_hash) {
return self.ribosomes.get(cell_id).cloned();
}
None
}
pub fn add_entry_def(&mut self, k: EntryDefBufferKey, entry_def: EntryDef) {
self.entry_defs.insert(k, entry_def);
}
pub fn add_entry_defs<T: IntoIterator<Item = (EntryDefBufferKey, EntryDef)> + 'static>(
&mut self,
entry_defs: T,
) {
self.entry_defs.extend(entry_defs);
}
pub fn get_entry_def(&self, k: &EntryDefBufferKey) -> Option<EntryDef> {
self.entry_defs.get(k).cloned()
}
}