use crate::db::access::{AccessPath, AccessPlan, execution_contract::ExecutionPathPayload};
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) enum ExecutableAccessNode<'a, K> {
Path(ExecutionPathPayload<'a, K>),
Union(Vec<ExecutableAccessPlan<'a, K>>),
Intersection(Vec<ExecutableAccessPlan<'a, K>>),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) struct ExecutableAccessPlan<'a, K> {
node: ExecutableAccessNode<'a, K>,
}
impl<'a, K> ExecutableAccessPlan<'a, K> {
#[must_use]
pub(in crate::db) fn from_access_plan(access: &'a AccessPlan<K>) -> Self {
match access {
AccessPlan::Path(path) => Self::from_access_path(path.as_ref()),
AccessPlan::Union(children) => {
Self::union(Self::from_access_plan_children(children.as_slice()))
}
AccessPlan::Intersection(children) => {
Self::intersection(Self::from_access_plan_children(children.as_slice()))
}
}
}
#[must_use]
pub(in crate::db::access) const fn from_access_path(path: &'a AccessPath<K>) -> Self {
Self::for_path(ExecutionPathPayload::from_access_path(path))
}
#[must_use]
pub(in crate::db) const fn for_path(path: ExecutionPathPayload<'a, K>) -> Self {
Self {
node: ExecutableAccessNode::Path(path),
}
}
#[must_use]
pub(in crate::db) const fn union(children: Vec<Self>) -> Self {
Self {
node: ExecutableAccessNode::Union(children),
}
}
#[must_use]
pub(in crate::db) const fn intersection(children: Vec<Self>) -> Self {
Self {
node: ExecutableAccessNode::Intersection(children),
}
}
#[must_use]
pub(in crate::db) const fn node(&self) -> &ExecutableAccessNode<'a, K> {
&self.node
}
#[must_use]
pub(in crate::db) const fn as_path(&self) -> Option<&ExecutionPathPayload<'a, K>> {
match &self.node {
ExecutableAccessNode::Path(path) => Some(path),
ExecutableAccessNode::Union(_) | ExecutableAccessNode::Intersection(_) => None,
}
}
fn from_access_plan_children(children: &'a [AccessPlan<K>]) -> Vec<Self> {
children.iter().map(Self::from_access_plan).collect()
}
}