use std::sync::{Arc, Mutex};
use crate::control::planner::descriptor_set::DescriptorVersionSet;
use crate::control::security::credential::CredentialStore;
use crate::control::state::SharedState;
use crate::types::DatabaseId;
pub struct OriginCatalog {
pub(super) credentials: Arc<CredentialStore>,
pub(super) tenant_id: u64,
pub(super) database_id: DatabaseId,
pub(super) retention_policy_registry:
Option<Arc<crate::engine::timeseries::retention_policy::RetentionPolicyRegistry>>,
pub(super) array_catalog: Option<crate::control::array_catalog::ArrayCatalogHandle>,
pub(super) drain_tracker: Option<Arc<crate::control::lease::DescriptorDrainTracker>>,
pub(super) recorded_versions: Mutex<DescriptorVersionSet>,
}
impl OriginCatalog {
pub fn new(
credentials: Arc<CredentialStore>,
tenant_id: u64,
database_id: DatabaseId,
retention_policy_registry: Option<
Arc<crate::engine::timeseries::retention_policy::RetentionPolicyRegistry>,
>,
) -> Self {
Self {
credentials,
tenant_id,
database_id,
retention_policy_registry,
drain_tracker: None,
recorded_versions: Mutex::new(DescriptorVersionSet::new()),
array_catalog: None,
}
}
pub fn new_with_lease(
shared: &Arc<SharedState>,
tenant_id: u64,
database_id: DatabaseId,
retention_policy_registry: Option<
Arc<crate::engine::timeseries::retention_policy::RetentionPolicyRegistry>,
>,
) -> Self {
Self {
credentials: Arc::clone(&shared.credentials),
tenant_id,
database_id,
retention_policy_registry,
drain_tracker: Some(Arc::clone(&shared.lease_drain)),
recorded_versions: Mutex::new(DescriptorVersionSet::new()),
array_catalog: Some(shared.array_catalog.clone()),
}
}
pub fn take_recorded_versions(&self) -> DescriptorVersionSet {
let mut guard = self
.recorded_versions
.lock()
.unwrap_or_else(|p| p.into_inner());
std::mem::take(&mut *guard)
}
pub(super) fn has_auto_tier(&self, collection: &str) -> bool {
let registry = match &self.retention_policy_registry {
Some(r) => r,
None => return false,
};
registry
.get(self.database_id.as_u64(), self.tenant_id, collection)
.is_some_and(|p| p.auto_tier)
}
}