use super::{
resource_error, Arc, BudgetedVec, CommandMutationOverlay, CommandStoredDocument, DocId,
Document, DocumentChanges, DocumentMetadata, KeyKind, RetainedDocumentFields, SQLError,
StorageBackendResult, StorageReadControl, Value,
};
use crate::catalog::index::physical::PhysicalIndexDefinitions;
use crate::mutation::constraints::index_keys::IndexExpressionContext;
impl CommandStoredDocument {
pub fn indexed(
fields: Arc<Document>,
metadata: DocumentMetadata,
table: &str,
indexes: &PhysicalIndexDefinitions,
expressions: IndexExpressionContext<'_>,
control: &StorageReadControl,
) -> Result<Self, SQLError> {
let values = indexes.command_expression_values(expressions, table, &fields)?;
Self::new(fields, metadata, control)
.map_err(resource_error)?
.with_index_values(values, control)
.map_err(resource_error)
}
pub(super) fn with_index_values(
mut self,
values: Document,
control: &StorageReadControl,
) -> StorageBackendResult<Self> {
if !values.is_empty() {
self.index_values = Some(RetainedDocumentFields::new(Arc::new(values), control)?);
}
Ok(self)
}
}
pub struct CommandIndexProbe {
pub matches: BudgetedVec<DocId>,
pub changes: DocumentChanges,
}
impl CommandMutationOverlay {
pub fn visit_column_matches_with_catalog(
overlays: &mut [Self],
table: &str,
fields: &[String],
values: &[Value],
control: &StorageReadControl,
catalog: Option<&dyn uqa_sql::expr::SQLValueCatalog>,
visit: &mut dyn FnMut(DocId) -> Result<(), SQLError>,
) -> Result<DocumentChanges, SQLError> {
Self::visit_matches_with_catalog(overlays, table, fields, values, control, catalog, visit)?;
Self::changes(overlays, table, DocumentChanges::default(), control)
}
pub fn column_matches(
overlays: &mut [Self],
table: &str,
fields: &[String],
values: &[Value],
control: &StorageReadControl,
) -> Result<CommandIndexProbe, SQLError> {
Self::column_matches_with_catalog(overlays, table, fields, values, control, None)
}
pub fn column_matches_with_catalog(
overlays: &mut [Self],
table: &str,
fields: &[String],
values: &[Value],
control: &StorageReadControl,
catalog: Option<&dyn uqa_sql::expr::SQLValueCatalog>,
) -> Result<CommandIndexProbe, SQLError> {
let matches =
Self::matches_with_catalog(overlays, table, fields, values, control, catalog)?;
let changes = Self::changes(overlays, table, DocumentChanges::default(), control)?;
Ok(CommandIndexProbe { matches, changes })
}
pub fn visit_expression_matches_with_catalog(
overlays: &mut [Self],
table: &str,
physical_key: &str,
values: &[Value],
control: &StorageReadControl,
catalog: Option<&dyn uqa_sql::expr::SQLValueCatalog>,
visit: &mut dyn FnMut(DocId) -> Result<(), SQLError>,
) -> Result<DocumentChanges, SQLError> {
super::matching::visit_matches(
overlays,
table,
super::matching::MatchKeys {
fields: &[physical_key.to_owned()],
values: &[Value::Row(values.to_vec().into())],
kind: KeyKind::Expressions,
},
control,
catalog,
visit,
)?;
Self::changes(overlays, table, DocumentChanges::default(), control)
}
pub fn expression_matches(
overlays: &mut [Self],
table: &str,
physical_key: &str,
values: &[Value],
control: &StorageReadControl,
) -> Result<CommandIndexProbe, SQLError> {
Self::expression_matches_with_catalog(overlays, table, physical_key, values, control, None)
}
pub fn expression_matches_with_catalog(
overlays: &mut [Self],
table: &str,
physical_key: &str,
values: &[Value],
control: &StorageReadControl,
catalog: Option<&dyn uqa_sql::expr::SQLValueCatalog>,
) -> Result<CommandIndexProbe, SQLError> {
let matches = Self::matches_keys(
overlays,
table,
&[physical_key.to_owned()],
&[Value::Row(values.to_vec().into())],
KeyKind::Expressions,
control,
catalog,
)?;
let changes = Self::changes(overlays, table, DocumentChanges::default(), control)?;
Ok(CommandIndexProbe { matches, changes })
}
}