use crate::cluster::scan::ScanStateRC;
use crate::value::Result;
use nanoid::nanoid;
use once_cell::sync::Lazy;
use std::{collections::HashMap, sync::Mutex};
static CONTAINER: Lazy<Mutex<HashMap<String, ScanStateRC>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
pub fn insert_cluster_scan_cursor(scan_state: ScanStateRC) -> String {
let id = nanoid!();
CONTAINER.lock().unwrap().insert(id.clone(), scan_state);
tracing::debug!("scan_state_cursor insert - Inserted to container scan_state_cursor with id: `{id:?}`");
id
}
pub fn get_cluster_scan_cursor(id: String) -> Result<ScanStateRC> {
let scan_state_rc = CONTAINER.lock().unwrap().get(&id).cloned();
tracing::debug!("scan_state_cursor get - Retrieved from container scan_state_cursor with id: `{id:?}`");
match scan_state_rc {
Some(scan_state_rc) => Ok(scan_state_rc),
None => Err(crate::value::Error::from((
crate::value::ErrorKind::ResponseError,
"Invalid scan_state_cursor id",
format!("The scan_state_cursor sent with id: `{id:?}` does not exist"),
))),
}
}
pub fn remove_scan_state_cursor(id: String) {
tracing::debug!("scan_state_cursor remove - Removed from container scan_state_cursor with id: `{id:?}`");
CONTAINER.lock().unwrap().remove(&id);
}