uqa_engine/capabilities/
sequence_restoration.rs1use crate::Engine;
9use uqa_execution::catalog::sequence::restoration::{
10 RestoredSequenceRegistry, SequencePersistenceRead, SequenceRestoreContext,
11 SequenceRestoreRegistry,
12};
13impl Engine {
14 pub(crate) fn sequence_restore_context(&self) -> SequenceRestoreContext<'_> {
15 SequenceRestoreContext {
16 sequences: self,
17 security: self,
18 registry: self,
19 roles: self,
20 }
21 }
22
23 pub(crate) fn install_sequence_read_snapshot(
24 &self,
25 snapshot: &uqa_execution::catalog::sequence::snapshot::SequenceReadSnapshot,
26 ) {
27 self.durable.roles.restore(&snapshot.roles.roles);
28 self.durable
29 .role_memberships
30 .restore(&snapshot.roles.memberships);
31 self.durable.sequences.restore(&snapshot.sequences);
32 self.durable
33 .sequence_object_ids
34 .restore(&snapshot.object_ids);
35 self.durable
36 .sequence_catalog_oids
37 .restore(&snapshot.catalog_oids);
38 self.durable
39 .sequence_persistence
40 .restore(&snapshot.persistence);
41 self.durable.sequence_security.restore(&snapshot.security);
42 }
43}
44impl SequenceRestoreRegistry for Engine {
45 fn persistence(&self) -> SequencePersistenceRead<'_> {
46 Box::new(self.durable.sequence_persistence.read())
47 }
48 fn install(&self, registry: RestoredSequenceRegistry) {
49 *self.durable.sequences.write() = registry.sequences;
50 *self.durable.sequence_object_ids.write() = registry.object_ids;
51 *self.durable.sequence_persistence.write() = registry.persistence;
52 *self.durable.sequence_security.write() = registry.security;
53 }
54}
55
56impl crate::DurableCatalogSnapshot {
57 pub(crate) fn sequence_read_snapshot(
58 &self,
59 ) -> uqa_execution::catalog::sequence::snapshot::SequenceReadSnapshot {
60 uqa_execution::catalog::sequence::snapshot::SequenceReadSnapshot {
61 sequences: self.sequences.clone(),
62 object_ids: self.sequence_object_ids.clone(),
63 persistence: self.sequence_persistence.clone(),
64 security: self.sequence_security.clone(),
65 catalog_oids: self.sequence_catalog_oids.clone(),
66 roles: uqa_execution::catalog::security::roles::persistence::RoleCatalogSnapshot {
67 roles: self.roles.clone(),
68 memberships: self.role_memberships.clone(),
69 },
70 }
71 }
72}
73
74impl uqa_execution::catalog::sequence::snapshot::SequenceSnapshotSource for Engine {
75 fn sequence_read_snapshot(
76 &self,
77 ) -> uqa_storage::StorageBackendResult<
78 uqa_execution::catalog::sequence::snapshot::SequenceReadSnapshot,
79 > {
80 self.latest_sequence_snapshot()
81 }
82
83 fn sequence_position(
84 &self,
85 key: crate::row_locks::SequencePositionKey,
86 ) -> uqa_storage::StorageBackendResult<Option<crate::row_locks::RecordedSequencePosition>> {
87 let Some(positions) = self.shared_sequence_positions() else {
88 return Ok(None);
89 };
90 positions
91 .sequence_position(key)
92 .map_err(|error| uqa_storage::StorageBackendError::Other(error.to_string()))
93 }
94}
95
96impl Engine {
97 pub(crate) fn sequence_states_at_positions(
99 &self,
100 snapshot: &uqa_execution::catalog::sequence::snapshot::SequenceReadSnapshot,
101 ) -> uqa_storage::StorageBackendResult<
102 std::collections::BTreeMap<uqa_core::RelationIdentity, crate::SequenceState>,
103 > {
104 let Some(positions) = self.shared_sequence_positions() else {
105 return Ok((*snapshot.sequences).clone());
106 };
107 let positions = positions
108 .sequence_positions()
109 .map_err(|error| uqa_storage::StorageBackendError::Other(error.to_string()))?;
110 Ok(snapshot
111 .sequences
112 .iter()
113 .map(|(relation, state)| {
114 let position = snapshot
115 .object_ids
116 .get(relation)
117 .and_then(|object_id| positions.get(&state.position_key(*object_id)));
118 (relation.clone(), state.at_position(position.copied()))
119 })
120 .collect())
121 }
122
123 pub(crate) fn query_sequence_snapshot(
125 &self,
126 ) -> uqa_storage::StorageBackendResult<
127 uqa_execution::catalog::sequence::snapshot::SequenceReadSnapshot,
128 > {
129 if let Some(snapshot) = self.query_catalog_snapshot.as_ref() {
130 return Ok(snapshot.sequence_read_snapshot());
131 }
132 let attached = self.transaction_depth() == 0
133 && self
134 .storage
135 .backend
136 .as_ref()
137 .is_some_and(|backend| backend.in_transaction());
138 let independent = if attached {
139 self.synchronize_catalog_registries()?;
140 None
141 } else {
142 self.open_independent_catalog_session(None)?
143 };
144 self.sequence_snapshot_from_catalog(independent.as_ref())
145 }
146
147 fn sequence_snapshot_from_catalog(
148 &self,
149 independent: Option<&uqa_storage::PersistentStorageSession>,
150 ) -> uqa_storage::StorageBackendResult<
151 uqa_execution::catalog::sequence::snapshot::SequenceReadSnapshot,
152 > {
153 uqa_execution::catalog::sequence::snapshot::read_sequence_snapshot(
154 self.sequence_registries(),
155 self.storage.catalog.as_deref(),
156 independent,
157 self.versioned_backend_transactions(),
158 )
159 }
160}