Skip to main content

coven_database/
database_store.rs

1//! Store names share the transaction that commits their sequence prefix; the
2//! store a join publishes also keeps its new person's name (§13.2).
3
4use super::*;
5
6impl StoreDatabase {
7    /// Save the name chosen for a local store before publishing its
8    /// directory, which slot 1 will set (§10.3). This row is not sequence
9    /// membership until the applied history creates the store and sets its
10    /// name.
11    pub async fn initialize_store_name(&self, name: String) -> Result<(), DbError> {
12        self.call(move |inner| {
13            inner.access.with_writer(|writer, _| {
14                writer.transaction(|db| {
15                    crate::store_state_tables::name_before_creation(
16                        db,
17                        inner.access.directory.id(),
18                        &name,
19                        coven_merge::Slot::FIRST,
20                        false,
21                    )
22                })
23            })
24        })
25        .await
26    }
27
28    /// Save what the store a join publishes keeps from its invite before it
29    /// loads, in one transaction: the store's `name`, set by `named_at`, the
30    /// slot the inviting device had applied (§13.2).
31    pub async fn initialize_joined_store(
32        &self,
33        name: String,
34        named_at: coven_merge::Slot,
35    ) -> Result<(), DbError> {
36        self.call(move |inner| {
37            inner.access.with_writer(|writer, _| {
38                writer.transaction(|db| {
39                    crate::store_state_tables::name_before_creation(
40                        db,
41                        inner.access.directory.id(),
42                        &name,
43                        named_at,
44                        true,
45                    )
46                })
47            })
48        })
49        .await
50    }
51
52    /// The same name query, live (E4).
53    pub fn subscribe_store_name<E>(&self) -> LiveQuery<String, E>
54    where
55        E: From<DbError> + coven_foundation::task::CarriesBug + Send + 'static,
56    {
57        self.follow(|db| crate::store_state_tables::name(db).map_err(E::from))
58    }
59
60    /// The committed name and the slot that set it, including a local
61    /// creation's name, which slot 1 will set. A saved restore code keeps
62    /// whichever name has the later slot (§13.1).
63    pub async fn named_store(&self) -> Result<(String, coven_merge::Slot), crate::DbError> {
64        self.read_committed(crate::store_state_tables::named).await
65    }
66}
67
68impl<M: Mode> StoreDatabase<M> {
69    /// The name in this device's committed state, including a local creation
70    /// on the writer (E9).
71    pub async fn store_name(&self) -> Result<String, crate::DbError> {
72        self.read_committed(crate::store_state_tables::name).await
73    }
74}