use uqa_core::memory::{Produced, ProductionControl};
use uqa_storage::{CatalogFacade, StorageBackendResult};
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum TablePublicationKind {
Definition,
ExpressionNames,
Declaration,
}
pub fn table_key(object: [u8; 16]) -> String {
table_key_with_control(object, &ProductionControl::uncontrolled())
.expect("ordinary definition key")
.into_uncontrolled()
.expect("ordinary definition key owner")
}
pub(super) fn table_key_with_control(
object: [u8; 16],
control: &ProductionControl<'_>,
) -> Result<Produced<String>, uqa_core::ValueRetentionError> {
control.format(format_args!(
"__uqa_prepared_table_definition_{:032x}",
u128::from_be_bytes(object)
))
}
pub fn publish_table(catalog: &dyn CatalogFacade, object: [u8; 16]) -> StorageBackendResult<()> {
publish(catalog, &table_key(object))
}
fn publish(catalog: &dyn CatalogFacade, key: &str) -> StorageBackendResult<()> {
let revision = super::identity::new_nonzero_catalog_identity("relation", "analysis revision")?;
catalog.set_metadata(key, &format!("{:032x}", u128::from_be_bytes(revision)))
}
pub fn remove_table(catalog: &dyn CatalogFacade, object: [u8; 16]) -> StorageBackendResult<()> {
catalog.delete_metadata(&table_key(object))
}
pub fn view_key(object: [u8; 16]) -> String {
view_key_with_control(object, &ProductionControl::uncontrolled())
.expect("ordinary definition key")
.into_uncontrolled()
.expect("ordinary definition key owner")
}
pub(super) fn view_key_with_control(
object: [u8; 16],
control: &ProductionControl<'_>,
) -> Result<Produced<String>, uqa_core::ValueRetentionError> {
control.format(format_args!(
"__uqa_prepared_view_definition_{:032x}",
u128::from_be_bytes(object)
))
}
pub fn publish_view(
catalog: &dyn CatalogFacade,
row: &uqa_storage::ViewRow,
) -> StorageBackendResult<()> {
#[derive(serde::Deserialize)]
struct Identity {
object_id: [u8; 16],
}
let identity: Identity = serde_json::from_str(&row.definition_json)?;
catalog.save_view(row)?;
publish(catalog, &view_key(identity.object_id))
}
pub fn remove_view(catalog: &dyn CatalogFacade, object: [u8; 16]) -> StorageBackendResult<()> {
catalog.delete_metadata(&view_key(object))
}