use super::{key, Dependency, State};
use std::collections::BTreeSet;
impl State {
pub fn restrict_blocks_drop(
&self,
names: &[String],
catalog: Option<&dyn crate::CatalogLifecycleLookup>,
) -> bool {
let mut removed: BTreeSet<_> = names
.iter()
.filter(|name| self.resolves_temporary(name, catalog))
.map(|name| key(name))
.collect();
loop {
let before = removed.len();
for (child, parent) in &self.partitions {
if removed.contains(parent) {
removed.insert(child.clone());
}
}
if removed.len() == before {
break;
}
}
self.relations.iter().any(|(name, dependencies)| {
!removed.contains(name)
&& dependencies.iter().any(|dependency| {
matches!(dependency, Dependency::Temporary(source) if removed.contains(source))
})
})
}
pub fn partitioned_parent(&self, parent: &str) -> bool {
self.matches_identity(parent) && self.partitioned.contains(&key(parent))
}
pub fn attach_partition(
&mut self,
parent: &str,
child: &str,
catalog: Option<&dyn crate::CatalogLifecycleLookup>,
) {
if self.partitioned.contains(&key(parent))
&& self.resolves_temporary(parent, catalog)
&& self.resolves_temporary(child, catalog)
{
self.partitions.entry(key(child)).or_insert(key(parent));
}
}
pub fn attach_created_partition(&mut self, parent: &str, child: &str) {
self.partitions.insert(key(child), key(parent));
}
pub fn detach_partition(
&mut self,
parent: &str,
child: &str,
catalog: Option<&dyn crate::CatalogLifecycleLookup>,
) {
if self.resolves_temporary(parent, catalog) && self.resolves_temporary(child, catalog) {
let child = key(child);
if self
.partitions
.get(&child)
.is_some_and(|owner| *owner == key(parent))
{
self.partitions.remove(&child);
}
}
}
}