clm-kernel 0.0.5

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

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

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

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

pub fn setter_mcard_cache(hash: &str, content: &str) {
    get_cards_cache().insert(hash.to_string(), content.to_string());
}

pub fn getter_mcard_cache(hash: &str) -> Option<String> {
    get_cards_cache().get(hash).map(|r| r.value().clone())
}

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())
}

pub fn mcard_cache_prepopulate(clm_root: &str) -> crate::error::Result<()> {
    let conn = get_connection("content_memory", clm_root)?;

    let mut stmt = conn
        .prepare("SELECT handle, current_hash FROM handle_registry")?;

    let rows = stmt
        .query_map([], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
        })?;

    let handles = get_handles_cache();
    for (handle, current_hash) in rows.flatten() {
        handles.insert(handle, current_hash);
    }
    Ok(())
}