Skip to main content

uqa_engine/capabilities/
view_restoration.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind view restoration to live registry guards and the catalog-opening mode.
8use crate::{open::CatalogRestoreMode, CatalogFacade, Engine, StorageBackendResult, TableState};
9use parking_lot::RwLockReadGuard;
10use std::{collections::BTreeMap, sync::Arc};
11use uqa_core::RelationIdentity;
12use uqa_execution::catalog::view::{
13    restoration::{
14        self,
15        context::{
16            CatalogViewRows, ViewRestoreContext, ViewRestoreForeignRead, ViewRestoreGraphsRead,
17            ViewRestoreNamespace, ViewRestoreSchemas, ViewRestoreSequences, ViewRestoreTableNames,
18        },
19    },
20    StoredView, ViewRegistryRead, ViewRegistryState, ViewRegistryWrite,
21};
22use uqa_sql::{RowSchema, SQLError};
23
24struct TableNamesRead<'a>(RwLockReadGuard<'a, BTreeMap<RelationIdentity, Arc<TableState>>>);
25impl ViewRestoreTableNames for TableNamesRead<'_> {
26    fn names(&self) -> Box<dyn Iterator<Item = &RelationIdentity> + '_> {
27        Box::new(self.0.keys())
28    }
29}
30impl ViewRegistryState for Engine {
31    fn views_read(&self) -> ViewRegistryRead<'_> {
32        Box::new(self.durable.views.read())
33    }
34    fn views_write(&self) -> ViewRegistryWrite<'_> {
35        Box::new(self.durable.views.write())
36    }
37}
38impl ViewRestoreNamespace for Engine {
39    fn tables(&self) -> Box<dyn ViewRestoreTableNames + '_> {
40        Box::new(TableNamesRead(self.storage.tables.read()))
41    }
42    fn foreign_tables(&self) -> ViewRestoreForeignRead<'_> {
43        Box::new(self.durable.foreign_tables.read())
44    }
45    fn graphs(&self) -> ViewRestoreGraphsRead<'_> {
46        Box::new(self.durable.graphs.read())
47    }
48}
49impl ViewRestoreSchemas for Engine {
50    fn stored_schema(&self, view: &StoredView) -> Result<RowSchema, SQLError> {
51        self.stored_view_schema(view)
52    }
53}
54impl ViewRestoreSequences for Engine {
55    fn resolve_loaded(&self, reference: &str) -> StorageBackendResult<String> {
56        self.resolve_stored_sequence_reference_from_loaded_registry(reference)
57    }
58}
59impl Engine {
60    fn view_restore_context(&self) -> ViewRestoreContext<'_> {
61        ViewRestoreContext {
62            registry: self,
63            namespace: self,
64            roles: self,
65            identities: self,
66            bindings: self,
67            schemas: self,
68            sequences: self,
69            types: self,
70        }
71    }
72    pub(crate) fn restore_views_from_catalog(
73        &self,
74        catalog: &dyn CatalogFacade,
75        mode: CatalogRestoreMode,
76    ) -> StorageBackendResult<()> {
77        restoration::restore_views_from_catalog(
78            &self.view_restore_context(),
79            &CatalogViewRows { catalog },
80            mode.allows_migration(),
81        )
82    }
83}