use crate::probabilistic::streaming_log::StreamingLog;
use wasm_bindgen::prelude::*;
#[allow(static_mut_refs)]
static mut STREAMING_LOGS: Option<std::collections::HashMap<usize, StreamingLog>> = None;
#[allow(static_mut_refs)]
fn ensure_store() {
unsafe {
if STREAMING_LOGS.is_none() {
STREAMING_LOGS = Some(std::collections::HashMap::new());
}
}
}
fn with_store<F, R>(f: F) -> R
where
F: FnOnce(&mut std::collections::HashMap<usize, StreamingLog>) -> R,
{
ensure_store();
let store = unsafe { STREAMING_LOGS.as_mut().unwrap() };
f(store)
}
static mut NEXT_HANDLE: usize = 1;
fn next_handle() -> usize {
let handle = unsafe { NEXT_HANDLE };
unsafe { NEXT_HANDLE += 1 };
handle
}
#[wasm_bindgen]
pub fn create_streaming_log() -> usize {
let handle = next_handle();
with_store(|store| {
store.insert(handle, StreamingLog::new());
});
handle
}
#[wasm_bindgen]
pub fn streaming_log_add_trace(handle: usize, activities: &JsValue) -> Result<(), JsValue> {
with_store(|store| {
let slog = store.get_mut(&handle).ok_or_else(|| {
crate::error::js_val(&format!("Invalid StreamingLog handle: {}", handle))
})?;
let arr: Vec<String> = serde_wasm_bindgen::from_value(activities.clone()).map_err(|e| {
crate::error::js_val(&format!("Failed to parse activities array: {}", e))
})?;
let refs: Vec<&str> = arr.iter().map(|s| s.as_str()).collect();
slog.add_trace(&refs);
Ok(())
})
}
#[wasm_bindgen]
pub fn streaming_log_estimate_dfg(handle: usize) -> Result<JsValue, JsValue> {
with_store(|store| {
let slog = store.get(&handle).ok_or_else(|| {
crate::error::js_val(&format!("Invalid StreamingLog handle: {}", handle))
})?;
let dfg = slog.estimate_dfg();
let json = serde_json::to_string(&dfg)
.map_err(|e| crate::error::js_val(&format!("Failed to serialize DFG: {}", e)))?;
Ok(crate::error::js_val(&json))
})
}
#[wasm_bindgen]
pub fn streaming_log_estimate_cardinality(handle: usize) -> Result<usize, JsValue> {
with_store(|store| {
let slog = store.get(&handle).ok_or_else(|| {
crate::error::js_val(&format!("Invalid StreamingLog handle: {}", handle))
})?;
Ok(slog.estimate_cardinality())
})
}
#[wasm_bindgen]
pub fn streaming_log_event_count(handle: usize) -> Result<usize, JsValue> {
with_store(|store| {
let slog = store.get(&handle).ok_or_else(|| {
crate::error::js_val(&format!("Invalid StreamingLog handle: {}", handle))
})?;
Ok(slog.event_count())
})
}
#[wasm_bindgen]
pub fn streaming_log_activity_count(handle: usize) -> Result<usize, JsValue> {
with_store(|store| {
let slog = store.get(&handle).ok_or_else(|| {
crate::error::js_val(&format!("Invalid StreamingLog handle: {}", handle))
})?;
Ok(slog.activity_count())
})
}
#[wasm_bindgen]
pub fn streaming_log_memory_bytes(handle: usize) -> Result<usize, JsValue> {
with_store(|store| {
let slog = store.get(&handle).ok_or_else(|| {
crate::error::js_val(&format!("Invalid StreamingLog handle: {}", handle))
})?;
Ok(slog.memory_bytes())
})
}
#[wasm_bindgen]
pub fn free_streaming_log(handle: usize) -> Result<(), JsValue> {
with_store(|store| {
if store.remove(&handle).is_none() {
return Err(crate::error::js_val(&format!(
"Invalid StreamingLog handle: {}",
handle
)));
}
Ok(())
})
}