Skip to main content

sqlive/
checkpointed_size.rs

1//! The main database file's size after each checkpoint, for the host.
2
3use tokio::sync::watch;
4
5/// A host's view of the main database file's size in bytes after the latest
6/// checkpoint (`Database::checkpointed_size`). Each view keeps its own mark
7/// of the sizes it has returned.
8pub struct CheckpointedSize {
9    sizes: watch::Receiver<u64>,
10}
11
12impl CheckpointedSize {
13    pub(crate) fn new(sizes: watch::Receiver<u64>) -> Self {
14        Self { sizes }
15    }
16
17    /// The size now. A later `changed` waits for a size after this one.
18    pub fn bytes(&mut self) -> u64 {
19        *self.sizes.borrow_and_update()
20    }
21
22    /// Wait until a checkpoint changes the size from the one this view last
23    /// returned, and return the new size. Returns `None` once the database
24    /// has closed, since no checkpoint sends a size after that.
25    pub async fn changed(&mut self) -> Option<u64> {
26        self.sizes.changed().await.ok()?;
27        Some(self.bytes())
28    }
29}