use std::sync::Arc;
use crate::control::array_catalog::entry::ArrayCatalogEntry;
use crate::control::state::SharedState;
pub(crate) fn register_array_catalog_entry(
state: &Arc<SharedState>,
array: &str,
) -> crate::Result<()> {
use nodedb_types::TenantId as NdTenantId;
let schema = state.array_sync_schemas.to_array_schema(array).ok_or_else(|| {
crate::Error::Internal {
detail: format!(
"register_array_catalog_entry: to_array_schema returned None for '{array}' after import"
),
}
})?;
let schema_msgpack = zerompk::to_msgpack_vec(&schema).map_err(|e| crate::Error::Internal {
detail: format!("register_array_catalog_entry: schema_msgpack encode failed: {e}"),
})?;
let array_id = nodedb_array::types::ArrayId::new(NdTenantId::new(0), array);
let entry = ArrayCatalogEntry {
array_id,
name: array.to_string(),
schema_msgpack,
schema_hash: 0,
created_at_ms: 0,
prefix_bits: 8,
audit_retain_ms: None,
minimum_audit_retain_ms: None,
};
{
let mut cat = state
.array_catalog
.write()
.unwrap_or_else(|p| p.into_inner());
if cat.lookup_by_name(array).is_some() {
return Ok(());
}
cat.register(entry.clone())
.map_err(|e| crate::Error::Internal {
detail: format!("register_array_catalog_entry: catalog register failed: {e}"),
})?;
}
crate::control::array_catalog::persist::persist(state.credentials.catalog(), &entry).map_err(
|e| crate::Error::Internal {
detail: format!("register_array_catalog_entry: catalog persist failed: {e}"),
},
)?;
Ok(())
}