use nodedb_types::DatabaseId;
use tracing::{debug, warn};
use crate::control::security::catalog::auth_types::object_type;
use crate::control::security::catalog::{StoredCollection, SystemCatalog};
pub fn put(stored: &StoredCollection, catalog: &SystemCatalog) {
if let Err(e) = catalog.put_collection(stored.database_id, stored) {
warn!(
collection = %stored.name,
tenant = stored.tenant_id,
error = %e,
"catalog_entry: put_collection failed"
);
}
super::owner::put_parent_owner_in_database(
object_type::COLLECTION,
stored.database_id.as_u64(),
stored.tenant_id,
&stored.name,
&stored.owner,
catalog,
);
}
pub fn put_if_absent(stored: &StoredCollection, catalog: &SystemCatalog) {
match catalog.put_collection_if_absent(stored.database_id, stored) {
Ok(true) => super::owner::put_parent_owner_in_database(
object_type::COLLECTION,
stored.database_id.as_u64(),
stored.tenant_id,
&stored.name,
&stored.owner,
catalog,
),
Ok(false) => debug!(
collection = %stored.name,
tenant = stored.tenant_id,
"catalog_entry: put_collection_if_absent skipped existing collection"
),
Err(e) => warn!(
collection = %stored.name,
tenant = stored.tenant_id,
error = %e,
"catalog_entry: atomic put_collection_if_absent failed"
),
}
}
pub fn prepare_purge(
database_id: u64,
tenant_id: u64,
name: &str,
catalog: &SystemCatalog,
) -> crate::Result<()> {
let database_id = DatabaseId::new(database_id);
if let Some(mut stored) = catalog.get_collection(database_id, tenant_id, name)? {
stored.is_active = false;
catalog.put_collection(database_id, &stored)?;
}
Ok(())
}
pub fn finalize_purge(
database_id: u64,
tenant_id: u64,
name: &str,
catalog: &SystemCatalog,
) -> crate::Result<()> {
let database_id = DatabaseId::new(database_id);
super::owner::delete_parent_owner_in_database_checked(
object_type::COLLECTION,
database_id.as_u64(),
tenant_id,
name,
catalog,
)?;
catalog.delete_all_surrogates_for_collection(
database_id,
nodedb_types::TenantId::new(tenant_id),
name,
)?;
let removed = catalog.delete_collection(database_id, tenant_id, name)?;
debug!(
collection = %name,
tenant = tenant_id,
removed,
"catalog_entry: purge_collection finalized"
);
Ok(())
}
pub fn deactivate(database_id: u64, tenant_id: u64, name: &str, catalog: &SystemCatalog) {
let database_id = DatabaseId::new(database_id);
match catalog.get_collection(database_id, tenant_id, name) {
Ok(Some(mut stored)) => {
stored.is_active = false;
if let Err(e) = catalog.put_collection(database_id, &stored) {
warn!(
collection = %name,
tenant = tenant_id,
error = %e,
"catalog_entry: deactivate_collection put failed"
);
}
}
Ok(None) => {
debug!(
collection = %name,
tenant = tenant_id,
"catalog_entry: deactivate on missing collection (fresh follower)"
);
}
Err(e) => warn!(
collection = %name,
tenant = tenant_id,
error = %e,
"catalog_entry: deactivate_collection get failed"
),
}
}