icydb-core 0.264.9

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
Documentation
use crate::db::query::plan::CoveringHybridReadExecutionPlan;
use crate::db::query::plan::covering_hybrid_projection_execution_plan_with_schema_info;
#[cfg(feature = "sql")]
use crate::db::{executor::planning::route::AggregateRouteShape, query::plan::AggregateKind};
use crate::{
    db::{
        access::validate_access_runtime_invariants_with_schema,
        commit::CommitSchemaFingerprint,
        data::StructuralRowContract,
        executor::terminal::RowLayout,
        query::plan::{
            AccessPlannedQuery, CoveringReadExecutionPlan,
            covering_read_execution_plan_with_schema_info,
        },
        schema::{AcceptedSchemaAuthority, AcceptedSchemaRuntimeRootIdentity, SchemaInfo},
    },
    error::InternalError,
    types::EntityTag,
};
use std::rc::Rc;

///
/// EntityAuthority
///
/// EntityAuthority is the canonical structural entity-identity bundle used by
/// executor runtime preparation once a session has resolved accepted schema
/// authority. Its row layout and complete schema view are required at construction;
/// it deliberately carries no generated application model.
///

#[derive(Clone, Debug)]
pub(in crate::db) struct EntityAuthority {
    entity_path: Rc<str>,
    row_layout: RowLayout,
    entity_tag: EntityTag,
    store_path: &'static str,
    accepted_schema_info: Rc<SchemaInfo>,
    accepted_schema_fingerprint: CommitSchemaFingerprint,
    accepted_runtime_root_identity: AcceptedSchemaRuntimeRootIdentity,
}

impl EntityAuthority {
    /// Build complete runtime authority from one accepted runtime-root entity.
    #[must_use]
    pub(in crate::db) fn from_accepted_runtime_contracts(
        entity_path: impl Into<Rc<str>>,
        entity_tag: EntityTag,
        store_path: &'static str,
        row_contract: StructuralRowContract,
        accepted_schema_info: Rc<SchemaInfo>,
        accepted_schema_fingerprint: CommitSchemaFingerprint,
        accepted_runtime_root_identity: AcceptedSchemaRuntimeRootIdentity,
    ) -> Self {
        let entity_path = entity_path.into();
        debug_assert_eq!(row_contract.entity_path(), entity_path.as_ref());
        let row_layout = RowLayout::from_structural_row_contract(row_contract);

        Self {
            entity_path,
            row_layout,
            entity_tag,
            store_path,
            accepted_schema_info,
            accepted_schema_fingerprint,
            accepted_runtime_root_identity,
        }
    }

    /// Borrow the frozen structural row-decode layout for this entity.
    pub(in crate::db::executor) fn row_layout(&self) -> RowLayout {
        self.row_layout.clone()
    }

    /// Borrow the frozen structural row-decode layout for metadata-only callers.
    pub(in crate::db::executor) const fn row_layout_ref(&self) -> &RowLayout {
        &self.row_layout
    }

    /// Borrow the immutable store/revision authority that admitted this
    /// executor's accepted row layout.
    pub(in crate::db) fn accepted_schema_authority(&self) -> &AcceptedSchemaAuthority {
        self.accepted_value_catalog_handle().authority()
    }

    /// Borrow the immutable accepted catalog handle frozen into this
    /// executor's row layout.
    pub(in crate::db) fn accepted_value_catalog_handle(
        &self,
    ) -> &crate::db::schema::AcceptedValueCatalogHandle {
        self.row_layout_ref()
            .contract()
            .accepted_value_catalog_handle()
    }

    /// Borrow the accepted schema view attached to this executor authority.
    #[must_use]
    pub(in crate::db) fn accepted_schema_info(&self) -> &SchemaInfo {
        &self.accepted_schema_info
    }

    /// Share this authority's immutable schema view with detached preparation.
    #[must_use]
    pub(in crate::db) fn accepted_schema_info_handle(&self) -> Rc<SchemaInfo> {
        Rc::clone(&self.accepted_schema_info)
    }

    /// Return the entity snapshot fingerprint captured by the runtime root.
    #[must_use]
    pub(in crate::db) const fn accepted_schema_fingerprint(&self) -> CommitSchemaFingerprint {
        self.accepted_schema_fingerprint
    }

    /// Return the database-wide accepted runtime-root identity.
    #[must_use]
    pub(in crate::db) const fn accepted_runtime_root_identity(
        &self,
    ) -> AcceptedSchemaRuntimeRootIdentity {
        self.accepted_runtime_root_identity
    }

    /// Borrow structural entity-tag authority.
    #[must_use]
    pub(in crate::db) const fn entity_tag(&self) -> EntityTag {
        self.entity_tag
    }

    /// Borrow structural entity-path authority.
    #[must_use]
    pub(in crate::db) fn entity_path(&self) -> &str {
        &self.entity_path
    }

    /// Clone the shared accepted entity-path identity.
    #[must_use]
    pub(in crate::db) fn entity_path_handle(&self) -> Rc<str> {
        self.entity_path.clone()
    }

    /// Borrow structural store-path authority.
    #[must_use]
    pub(in crate::db) const fn store_path(&self) -> &'static str {
        self.store_path
    }

    /// Validate one access-planned query against authority-owned structural contracts.
    pub(in crate::db::executor) fn validate_executor_plan(
        &self,
        plan: &AccessPlannedQuery,
    ) -> Result<(), InternalError> {
        if !plan.has_static_execution_planning_contract() {
            return Err(InternalError::query_executor_invariant());
        }

        validate_access_runtime_invariants_with_schema(&self.accepted_schema_info, &plan.access)
            .map_err(crate::db::access::AccessPlanError::into_internal_error)
    }

    /// Resolve one aggregate route shape through authority-owned schema metadata.
    #[cfg(feature = "sql")]
    pub(in crate::db) fn aggregate_route_shape<'a>(
        &self,
        kind: AggregateKind,
        target_field: Option<&'a str>,
    ) -> AggregateRouteShape<'a> {
        AggregateRouteShape::new_from_schema_info(kind, target_field, &self.accepted_schema_info)
    }

    /// Derive one covering-read execution contract through authority-owned schema metadata.
    #[must_use]
    pub(in crate::db::executor) fn covering_read_execution_plan(
        &self,
        plan: &AccessPlannedQuery,
        strict_predicate_compatible: bool,
    ) -> Option<CoveringReadExecutionPlan> {
        covering_read_execution_plan_with_schema_info(
            &self.accepted_schema_info,
            plan,
            strict_predicate_compatible,
        )
    }

    /// Derive one hybrid covering projection contract through authority-owned schema metadata.
    #[must_use]
    pub(in crate::db::executor) fn covering_hybrid_projection_plan(
        &self,
        plan: &AccessPlannedQuery,
        strict_predicate_compatible: bool,
    ) -> Option<CoveringHybridReadExecutionPlan> {
        covering_hybrid_projection_execution_plan_with_schema_info(
            &self.accepted_schema_info,
            plan,
            strict_predicate_compatible,
        )
    }
}

// Exhaustive cache-retention coverage; new owned fields require accounting.
crate::retained::retained_fields!(EntityAuthority {
Self{entity_path,row_layout,entity_tag,store_path,accepted_schema_info,accepted_schema_fingerprint,accepted_runtime_root_identity} => [entity_path,row_layout,entity_tag,store_path,accepted_schema_info,accepted_schema_fingerprint,accepted_runtime_root_identity],
});