Skip to main content

coven_database/
database_mode.rs

1//! Which handle opened the store: its one writer, or a read-only handle
2//! (§4, E1). Both read the store through the same connections; only the
3//! writer writes, transfers files and keeps their records.
4
5use super::file_database::FileDatabase;
6
7mod sealed {
8    use crate::database::{file_database::FileDatabase, DatabaseAccess};
9
10    /// What each mode holds. Sealed: a store opens only as one of these two.
11    pub trait Sealed: Sized {
12        /// The open store, behind the handle's lock until it closes.
13        type Open: Send + Sync + 'static;
14        /// The lock the open store holds on its directory.
15        type Lock: Send + Sync + 'static;
16        /// What only a writer handle holds beside the open store.
17        type WriterParts: Clone + Send + Sync + 'static;
18
19        /// The connections every mode reads through.
20        fn access(open: &Self::Open) -> &DatabaseAccess<Self::Lock>;
21
22        /// The file owner that keeps file records: the writer's own, and
23        /// none for a read-only handle.
24        fn records(files: &FileDatabase<Self>) -> Option<&FileDatabase<super::Writer>>
25        where
26            Self: super::Mode;
27    }
28}
29
30/// The mode a store's handle opened in: [`Writer`] or [`ReadOnly`].
31pub trait Mode: sealed::Sealed + Send + Sync + 'static {}
32
33/// The store's one writer: it writes, migrates, transfers files and keeps
34/// their records (§4).
35pub enum Writer {}
36
37/// A read-only handle: it reads synced rows and keeps its own file cache,
38/// and nothing else (E1).
39pub enum ReadOnly {}
40
41impl Mode for Writer {}
42impl Mode for ReadOnly {}
43
44impl sealed::Sealed for Writer {
45    type Open = super::DatabaseInner;
46    type Lock = coven_foundation::files::StoreLock;
47    type WriterParts = super::WriterParts;
48
49    fn access(open: &super::DatabaseInner) -> &super::DatabaseAccess<Self::Lock> {
50        &open.access
51    }
52
53    fn records(files: &FileDatabase<Self>) -> Option<&FileDatabase<Writer>> {
54        Some(files)
55    }
56}
57
58impl sealed::Sealed for ReadOnly {
59    type Open = super::DatabaseAccess<Self::Lock>;
60    type Lock = coven_foundation::files::StoreReadLock;
61    type WriterParts = ();
62
63    fn access(open: &Self::Open) -> &Self::Open {
64        open
65    }
66
67    fn records(_: &FileDatabase<Self>) -> Option<&FileDatabase<Writer>> {
68        None
69    }
70}