Skip to main content

coven_database/
library_bytes.rs

1//! The sizes of the store's own files in `coven_library_bytes` (§17.8):
2//! each database file's size as its last checkpoint left it, which coven
3//! writes as the store opens and again after each checkpoint that changes
4//! it, the snapshot and floor bodies coven-sync captured to upload, and a
5//! rebuild's store's own files while one runs (§18.3). Between a
6//! change and the write after it, a row holds the size before it; opening
7//! writes each database file's row again.
8
9use crate::database::{finish_blocking, process, start_call, DatabaseInner, StoreDatabase};
10use crate::sqlite::StoreSql;
11use crate::store_failure::{StoreFailure, StoreHandle};
12use crate::{next_result, DbError, LiveQuery};
13use coven_foundation::files::{FileError, StoreDirAccess};
14use coven_foundation::task::BugSinkRef;
15use sqlive::CheckpointedSize;
16use std::sync::{Arc, RwLock, Weak};
17
18/// Write the store's database file's size, `database`, and its input
19/// database's, `inputs`, in the transaction under way. The UPDATE skips a
20/// row that already holds its size, so a live query that reads it does not
21/// run again.
22fn record(db: &StoreSql, database: u64, inputs: u64) -> Result<(), DbError> {
23    for (part, bytes) in [("database", database), ("inputs", inputs)] {
24        db.execute(
25            "UPDATE coven_library_bytes SET bytes=?2 WHERE part=?1 AND bytes<>?2",
26            (part, crate::sqlite::integer("database file size", bytes)?),
27        )?;
28    }
29    Ok(())
30}
31
32/// Write `bytes` into the `rebuild` row, in the transaction under way, when
33/// that row exists.
34fn record_rebuild(db: &StoreSql, bytes: u64) -> Result<(), DbError> {
35    db.execute(
36        "UPDATE coven_library_bytes SET bytes=?1 WHERE part='rebuild' AND bytes<>?1",
37        [crate::sqlite::integer("rebuild store size", bytes)?],
38    )?;
39    Ok(())
40}
41
42/// Delete the `rebuild` row, in the transaction under way.
43fn forget_rebuild(db: &StoreSql) -> Result<(), DbError> {
44    db.execute("DELETE FROM coven_library_bytes WHERE part='rebuild'", [])?;
45    Ok(())
46}
47
48/// Count the body coven-sync captured into the file `name`, of `size`
49/// bytes, among the store's own files, in the transaction under way: the one
50/// that journals the capture's bytes.
51pub(crate) fn count_capture(db: &StoreSql, name: &str, size: u64) -> Result<(), DbError> {
52    db.execute(
53        "INSERT INTO _coven_capture_sizes(name,size) VALUES(?1,?2)",
54        (name, crate::sqlite::integer("captured body size", size)?),
55    )?;
56    Ok(())
57}
58
59/// Sum the sizes of the store's own files in `coven_library_bytes`: the
60/// value of `LocalBytes::library`.
61pub(crate) fn library(db: &StoreSql) -> Result<u64, DbError> {
62    crate::sqlite::unsigned(db.query_row(
63        "SELECT coalesce(sum(bytes),0) FROM coven_library_bytes",
64        [],
65        |r| r.get::<_, i64>(0),
66    )?)
67}
68
69/// Move a folder whose bytes a row counts out of the store's directory with
70/// `aside`, then delete its row with `forget`, on `writer`, the store's
71/// writer, which the caller holds: one step, so no row names a folder that
72/// is partly removed. When `forget` fails, `back` moves the folder back and
73/// the failure returns; when that fails too, both failures return. A folder
74/// `aside` didn't find has only its row deleted.
75pub(crate) fn uncount(
76    writer: &mut StoreSql,
77    aside: impl FnOnce() -> Result<bool, FileError>,
78    back: impl FnOnce() -> Result<(), FileError>,
79    forget: impl FnOnce(&StoreSql) -> Result<(), DbError>,
80) -> Result<(), DbError> {
81    let moved = aside()?;
82    match writer.transaction(forget) {
83        Ok(()) => Ok(()),
84        Err(error) if !moved => Err(error),
85        Err(error) => match back() {
86            Ok(()) => Err(error),
87            Err(returning) => Err(DbError::FileCleanup {
88                write: Err(Box::new(error)),
89                failures: vec![DbError::Disk(returning)],
90            }),
91        },
92    }
93}
94
95/// Run `write` under the writer of the store `store` points to, failing its
96/// handle if the write fails. Returns whether the store took the write:
97/// false once it has closed or failed.
98async fn record_on(
99    store: &Weak<RwLock<Option<DatabaseInner>>>,
100    bugs: &BugSinkRef,
101    operation: &'static str,
102    write: impl FnOnce(&StoreSql) -> Result<(), DbError> + Send + 'static,
103) -> bool {
104    let Some(open) = store.upgrade() else { return false };
105    let written = start_call(operation, bugs, open, move |open: &DatabaseInner| {
106        open.access.call(|| {
107            open.access.with_writer(|writer, _| crate::pending::state_transaction(writer, write))
108        })
109    })
110    .await;
111    finish_blocking::<(), DbError>(written).is_ok()
112}
113
114/// How a store's two database files report their sizes after each
115/// checkpoint: the store's own and its input database's.
116pub(crate) struct DatabaseSizes {
117    database: CheckpointedSize,
118    inputs: CheckpointedSize,
119}
120
121impl DatabaseSizes {
122    pub(crate) fn new(database: CheckpointedSize, inputs: CheckpointedSize) -> Self {
123        Self { database, inputs }
124    }
125
126    /// Both sizes now. A later `changed` waits for a size after these.
127    fn now(&mut self) -> (u64, u64) {
128        (self.database.bytes(), self.inputs.bytes())
129    }
130
131    /// Both sizes once a checkpoint changes either, or `None` once the
132    /// store has closed.
133    async fn changed(&mut self) -> Option<(u64, u64)> {
134        tokio::select! {
135            size = self.database.changed() => size?,
136            size = self.inputs.changed() => size?,
137        };
138        Some(self.now())
139    }
140}
141
142/// Write both of `sizes` as they are now on `writer`, the opening store's,
143/// failing the handle if the write fails.
144pub(crate) fn record_opened(
145    writer: &mut StoreSql,
146    sizes: &mut DatabaseSizes,
147) -> Result<(), DbError> {
148    let (database, inputs) = sizes.now();
149    crate::pending::state_transaction(writer, |db| record(db, database, inputs))
150}
151
152impl StoreDatabase {
153    /// Write both of the store's database files' sizes after each checkpoint
154    /// that changes one, until the store closes, which ends both views. The
155    /// follower holds the store only while it writes, so dropping the store's
156    /// last handle still closes it. A write that fails has failed the handle
157    /// with its cause, and the follower stops.
158    pub(crate) fn follow_database_sizes(&self, mut sizes: DatabaseSizes) {
159        let store = Arc::downgrade(&self.inner);
160        let bugs = self.bugs.clone();
161        let operation = "record the database files' sizes";
162        let follow = async move {
163            while let Some((database, inputs)) = sizes.changed().await {
164                let write = move |db: &StoreSql| record(db, database, inputs);
165                if !record_on(&store, &bugs, operation, write).await {
166                    break;
167                }
168            }
169        };
170        drop(coven_foundation::task::spawn_owned(
171            "the database sizes follower",
172            self.bugs.clone(),
173            follow,
174            |_| async {},
175        ));
176    }
177}
178
179impl StoreDatabase {
180    /// Stop counting the captured body in the file `name` among the store's
181    /// own files, before coven-sync deletes the file. A name with no row,
182    /// such as a capture's that its journal hasn't recorded, changes nothing.
183    pub async fn forget_capture(&self, name: String) -> Result<(), DbError> {
184        self.call(move |inner| {
185            inner.access.with_writer(|writer, _| {
186                writer.transaction(|db| {
187                    db.execute("DELETE FROM _coven_capture_sizes WHERE name=?1", [&name])?;
188                    Ok(())
189                })
190            })
191        })
192        .await
193    }
194
195    /// Make this store's `rebuild` row, at no bytes, as a rebuild is about
196    /// to make its folder (§18.3).
197    pub(crate) async fn begin_counting_rebuild(&self) -> Result<(), DbError> {
198        self.call(|inner| {
199            inner.access.with_writer(|writer, _| {
200                crate::pending::state_transaction(writer, |db| {
201                    db.execute(
202                        "INSERT INTO coven_library_bytes(part,bytes) VALUES('rebuild',0)
203                         ON CONFLICT(part) DO UPDATE SET bytes=0",
204                        [],
205                    )?;
206                    Ok(())
207                })
208            })
209        })
210        .await
211    }
212
213    /// Count `rebuild`, the rebuild's store, just opened, in this store's
214    /// `rebuild` row: the sizes of its own files together now, then after
215    /// each write that changes them, until it closes.
216    pub(crate) async fn count_rebuild(&self, rebuild: &StoreDatabase) -> Result<(), DbError> {
217        let (total, first) = next_result(rebuild.follow(library)).await;
218        let bytes = first.ok_or(DbError::StoreClosed)??;
219        self.call(move |inner| {
220            inner.access.with_writer(|writer, _| {
221                crate::pending::state_transaction(writer, |db| record_rebuild(db, bytes))
222            })
223        })
224        .await?;
225        self.follow_rebuild(total, rebuild.observer.clone());
226        Ok(())
227    }
228
229    /// Write each total `total` gives into the `rebuild` row until the
230    /// rebuild's store closes, which ends the query. The follower holds this
231    /// store only while it writes, so dropping its last handle still closes
232    /// it; a write that fails has failed this store's handle, and the
233    /// follower stops. A run of the query that fails would leave the row
234    /// wrong, so the follower logs the failure, fails `rebuild`, the
235    /// rebuild's handle, with it, and stops. The owner's next call on the
236    /// rebuild returns the failure, and the owner discards the rebuild,
237    /// which deletes the row.
238    fn follow_rebuild(&self, mut total: LiveQuery<u64, DbError>, rebuild: StoreHandle) {
239        let store = Arc::downgrade(&self.inner);
240        let bugs = self.bugs.clone();
241        let operation = "record the rebuild store's size";
242        let follow = async move {
243            loop {
244                let (query, run) = next_result(total).await;
245                total = query;
246                let bytes = match run {
247                    None => break,
248                    Some(Ok(bytes)) => bytes,
249                    Some(Err(error)) => {
250                        tracing::error!(%error, "reading the rebuild store's size failed");
251                        rebuild.fail(StoreFailure::State(Arc::new(error)));
252                        break;
253                    }
254                };
255                let write = move |db: &StoreSql| record_rebuild(db, bytes);
256                if !record_on(&store, &bugs, operation, write).await {
257                    break;
258                }
259            }
260        };
261        drop(coven_foundation::task::spawn_owned(
262            "the rebuild size follower",
263            self.bugs.clone(),
264            follow,
265            |_| async {},
266        ));
267    }
268
269    /// Delete the rebuild's folder and its row. One step moves the folder
270    /// aside and deletes the row; then the method deletes the folder it
271    /// moved aside, on the blocking pool. After a failure, either the folder
272    /// and its row remain, or the folder moved aside remains and the next
273    /// removal deletes it.
274    pub(crate) async fn remove_rebuild(&self) -> Result<(), DbError> {
275        let directory = self
276            .call(|inner| {
277                let directory = &inner.access.directory;
278                inner.access.with_writer(|writer, _| {
279                    uncount(
280                        writer,
281                        || directory.retire_rebuild_directory(),
282                        || directory.return_rebuild_directory(),
283                        forget_rebuild,
284                    )
285                })?;
286                Ok::<_, DbError>(directory.clone())
287            })
288            .await?;
289        process(&self.bugs, move || Ok(directory.remove_retired_rebuild()?)).await
290    }
291}
292
293#[cfg(test)]
294#[path = "library_bytes_tests.rs"]
295pub(crate) mod tests;