Skip to main content

uqa_storage_sqlite/connection/
notifications.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! The connection retains the session gate while shared storage stages or recovers publication.
8
9mod serialized;
10
11use super::ManagedConnection;
12use uqa_storage::{
13    notifications::{
14        NotificationPublication, NotificationPublicationStore, NotificationPublicationView,
15    },
16    read_control::StorageReadControl,
17    StorageBackendResult,
18};
19
20impl NotificationPublicationStore for ManagedConnection {
21    fn stage_notification_publication(
22        &self,
23        publication: &NotificationPublication,
24    ) -> StorageBackendResult<()> {
25        if self.session.logical.get().is_none() {
26            return serialized::stage(self, publication, None);
27        }
28        self.with_notification_publications(|store| {
29            store.stage_notification_publication(publication)
30        })
31    }
32
33    fn stage_notification_publication_after(
34        &self,
35        publication: &NotificationPublication,
36        acknowledged: Option<[u8; 32]>,
37    ) -> StorageBackendResult<()> {
38        if self.session.logical.get().is_none() {
39            return serialized::stage(self, publication, acknowledged);
40        }
41        self.with_notification_publications(|store| {
42            store.stage_notification_publication_after(publication, acknowledged)
43        })
44    }
45
46    fn visit_notification_publication(
47        &self,
48        control: &StorageReadControl,
49        visit: &mut dyn FnMut(Option<NotificationPublicationView<'_>>) -> StorageBackendResult<()>,
50    ) -> StorageBackendResult<()> {
51        if self.session.logical.get().is_none() {
52            return serialized::visit(self, control, visit);
53        }
54        self.with_notification_publications(|store| {
55            store.visit_notification_publication(control, visit)
56        })
57    }
58
59    fn acknowledge_notification_publication(
60        &self,
61        fingerprint: [u8; 32],
62        control: &StorageReadControl,
63    ) -> StorageBackendResult<()> {
64        if self.session.logical.get().is_none() {
65            if serialized::acknowledge(self, fingerprint, control, false)? {
66                return Ok(());
67            }
68            unreachable!("blocking acknowledgement either completes or returns an error");
69        }
70        self.with_notification_publications(|store| {
71            store.acknowledge_notification_publication(fingerprint, control)
72        })
73    }
74
75    fn try_acknowledge_notification_publication(
76        &self,
77        fingerprint: [u8; 32],
78        control: &StorageReadControl,
79    ) -> StorageBackendResult<bool> {
80        if self.session.logical.get().is_none() {
81            return serialized::acknowledge(self, fingerprint, control, true);
82        }
83        self.with_notification_publications(|store| {
84            store.try_acknowledge_notification_publication(fingerprint, control)
85        })
86    }
87}
88
89impl ManagedConnection {
90    fn with_notification_publications<T>(
91        &self,
92        operation: impl FnOnce(&dyn NotificationPublicationStore) -> StorageBackendResult<T>,
93    ) -> StorageBackendResult<T> {
94        self.surface_cleanup_failure()?;
95        let _gate = self.session.gate.read();
96        let logical = self
97            .session
98            .logical
99            .get()
100            .ok_or(crate::SQLiteError::LogicalSessionRequired)?;
101        operation(logical.store.as_ref())
102    }
103}