Skip to main content

coven_database/
database_live_queries.rs

1//! The store's live queries: each runs on the store's readers, and runs again
2//! when a commit changes what it read (ยง4).
3
4use super::StoreDatabase;
5use crate::sqlite::StoreSql;
6use crate::write_failure::Hosted;
7use crate::{DbError, LiveQuery, LostValue, ReconfigurableLiveQuery, SqlReadContext};
8use coven_foundation::task::CarriesBug;
9use std::sync::Arc;
10
11impl StoreDatabase {
12    /// Observe the tables and columns a query reads, and the rows its
13    /// conditions allow. Its results fail with the closure's own error type.
14    pub fn subscribe<F, R, E>(&self, query: F) -> LiveQuery<R, E>
15    where
16        F: Fn(SqlReadContext<'_>) -> Result<R, E> + Send + Sync + 'static,
17        R: Send + 'static,
18        E: From<DbError> + CarriesBug + Send + 'static,
19    {
20        LiveQuery::new(self.subscribe_reconfigurable((), move |(), sql| query(sql)))
21    }
22
23    /// Observe a query whose request the caller can replace through a shared
24    /// handle.
25    pub fn subscribe_reconfigurable<Q, F, R, E>(
26        &self,
27        initial_request: Q,
28        query: F,
29    ) -> ReconfigurableLiveQuery<Q, R, E>
30    where
31        Q: Clone + PartialEq + Send + Sync + 'static,
32        F: Fn(&Q, SqlReadContext<'_>) -> Result<R, E> + Send + Sync + 'static,
33        R: Send + 'static,
34        E: From<DbError> + CarriesBug + Send + 'static,
35    {
36        self.live(initial_request, move |request, reader| {
37            query(request, SqlReadContext::new(reader))
38        })
39    }
40
41    /// Follow `query`, coven's own SQL on its tables, live: it runs again
42    /// after each commit that changes what it read.
43    pub(crate) fn follow<F, R, E>(&self, query: F) -> LiveQuery<R, E>
44    where
45        F: Fn(&StoreSql) -> Result<R, E> + Send + Sync + 'static,
46        R: Send + 'static,
47        E: From<DbError> + CarriesBug + Send + 'static,
48    {
49        LiveQuery::new(self.live((), move |(), reader| query(reader)))
50    }
51
52    /// A live query that runs `query` on the store's readers for each
53    /// request.
54    fn live<Q, F, R, E>(&self, initial_request: Q, query: F) -> ReconfigurableLiveQuery<Q, R, E>
55    where
56        Q: Clone + PartialEq + Send + Sync + 'static,
57        F: Fn(&Q, &StoreSql) -> Result<R, E> + Send + Sync + 'static,
58        R: Send + 'static,
59        E: From<DbError> + CarriesBug + Send + 'static,
60    {
61        // A subscription retains a database only for its active read.
62        // Idle queries and undelivered results cannot keep a closing lock held.
63        let database = Arc::downgrade(&self.inner);
64        ReconfigurableLiveQuery::new(
65            initial_request,
66            Arc::new(move |request, completion| {
67                let database = database.upgrade().ok_or(DbError::StoreClosed)?;
68                let slot = coven_foundation::locks::read_unpoisoned(&database);
69                let inner = slot.as_ref().ok_or(DbError::StoreClosed)?;
70                inner.call(|inner| {
71                    inner.access.with_reader(|reader, call| {
72                        let _constants = crate::transaction_constants::keep_constants(reader, call);
73                        reader
74                            .live_read(
75                                &mut |reads, lookups| completion.finish_judging(reads, lookups),
76                                |reader| query(request, reader).map_err(Hosted),
77                            )
78                            .map_err(Hosted::into_inner)
79                    })
80                })
81            }),
82            |error| E::from(DbError::from(error)),
83            &self.observer,
84            self.bugs.clone(),
85        )
86    }
87
88    /// Observe the decoded lost cells and removed rows.
89    pub fn subscribe_lost_values<E>(&self) -> LiveQuery<Vec<LostValue>, E>
90    where
91        E: From<DbError> + coven_foundation::task::CarriesBug + Send + 'static,
92    {
93        self.follow(|db| crate::loss_record::lost_values(db).map_err(E::from))
94    }
95}
96
97#[cfg(test)]
98#[path = "database_live_queries_tests.rs"]
99mod tests;