uqa-execution 0.4.9

Volcano physical operators with row-batch pipelines
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Durable row writes and transaction publication state.
use crate::mutation::{constraints::context::ConstraintCatalog, identity::MutationIdentifiers};
use std::collections::BTreeMap;
use uqa_core::{DocId, FieldName};
use uqa_sql::SQLError;
use uqa_storage::document_store::Document;
pub type DocumentVectors = BTreeMap<FieldName, Vec<Vec<f32>>>;
pub trait MutationStorage {
    /// Deferral is valid only while physical row publication cannot invoke expressions that observe earlier text changes.
    fn can_defer_document_text(&self, table: &str) -> Result<bool, SQLError>;
    fn delete_document(&self, table: &str, doc_id: DocId) -> Result<(), SQLError>;
    /// Delete under the original tuple lock; the publication owner removes text before observers run.
    fn delete_document_deferred_text(&self, table: &str, doc_id: DocId) -> Result<(), SQLError>;
    fn insert_document(
        &self,
        table: &str,
        doc_id: DocId,
        document: Document,
        vectors: DocumentVectors,
        inserted: super::InsertedIdentity,
    ) -> Result<(), SQLError>;
    fn insert_document_deferred_text(
        &self,
        table: &str,
        doc_id: DocId,
        document: Document,
        vectors: DocumentVectors,
        inserted: super::InsertedIdentity,
    ) -> Result<(), SQLError>;
    fn rewrite_document(
        &self,
        table: &str,
        doc_id: DocId,
        document: Document,
    ) -> Result<(), SQLError>;
    /// Publish a prepared rewrite under its existing tuple lock; the caller publishes text before allowing statement observers to run.
    fn rewrite_document_deferred_text(
        &self,
        table: &str,
        doc_id: DocId,
        document: Document,
    ) -> Result<(), SQLError>;
    /// Raise the table's document identity watermark to a supplied identity before the rows that carry identities up to it are published. The answer tells which identities no document of the table ever had.
    fn observe_document_identity(
        &self,
        table: &str,
        doc_id: DocId,
    ) -> Result<uqa_storage::mvcc::ObservedIdentifier, SQLError>;
}
pub trait MutationTextIndex {
    fn text_fields(
        &self,
        table: &str,
        document: &Document,
    ) -> Result<BTreeMap<FieldName, String>, SQLError>;
    fn add_documents(
        &self,
        table: &str,
        documents: Vec<(DocId, BTreeMap<FieldName, String>)>,
    ) -> Result<(), SQLError>;
}
pub trait MutationHistory {
    fn note_rewrite(
        &self,
        old_table: &str,
        old_doc_id: DocId,
        new_table: &str,
        new_doc_id: DocId,
    ) -> Result<(), SQLError>;
}
pub trait MutationConstraintDeferrals {
    fn inserted(&self, table: &str, doc_id: DocId) -> Result<(), SQLError>;
    fn rewritten(
        &self,
        table: &str,
        doc_id: DocId,
        old: Option<&Document>,
        new: &Document,
    ) -> Result<(), SQLError>;
}
#[derive(Clone, Copy)]
pub struct PublicationContext<'a> {
    pub observations: &'a dyn crate::serializable::SerializableWrites,
    pub storage: &'a dyn MutationStorage,
    pub text: &'a dyn MutationTextIndex,
    pub history: &'a dyn MutationHistory,
    pub deferrals: &'a dyn MutationConstraintDeferrals,
    pub identifiers: &'a dyn MutationIdentifiers,
    pub catalog: &'a dyn ConstraintCatalog,
}