clm-kernel 0.0.23

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
use dashmap::DashMap;
use std::sync::OnceLock;
use crate::layer0::mcard_record::MCardRecord;

static CARDS_CACHE: OnceLock<DashMap<String, MCardRecord>> = OnceLock::new();
static HANDLES_CACHE: OnceLock<DashMap<String, String>> = OnceLock::new();

fn get_cards_cache() -> &'static DashMap<String, MCardRecord> {
    CARDS_CACHE.get_or_init(DashMap::new)
}

fn get_handles_cache() -> &'static DashMap<String, String> {
    HANDLES_CACHE.get_or_init(DashMap::new)
}

pub fn setter_mcard_cache(record: &MCardRecord) -> crate::error::Result<()> {
    crate::layer0::record_port::require_verified(record)?;
    get_cards_cache().insert(record.hash.clone(), record.clone());
    Ok(())
}

pub fn getter_mcard_cache(reference: &str) -> crate::error::Result<Option<MCardRecord>> {
    let digest = reference.split_once(':').map_or(reference, |(_, hash)| hash);
    let record = get_cards_cache().get(reference).or_else(|| get_cards_cache().get(digest)).map(|row| row.value().clone());
    if let Some(record) = &record {
        let algorithm = crate::layer0::record_port::require_verified(record)?;
        if reference.split_once(':').is_some_and(|(prefix, _)| crate::layer0::gtime::canonical_algorithm_name(prefix) != algorithm) {
            return Err(crate::error::ClmError::Evaluation("cache reference conflicts with g_time".into()));
        }
    }
    Ok(record)
}

pub fn setter_mcard_cache_handle(handle: &str, hash: &str) {
    get_handles_cache().insert(handle.to_string(), hash.to_string());
}

pub fn getter_mcard_cache_handle(handle: &str) -> Option<String> {
    get_handles_cache().get(handle).map(|r| r.value().clone())
        .filter(|hash| getter_mcard_cache(hash).is_ok_and(|record| record.is_some()))
}