coven_sync/file_cache.rs
1//! Pins, budgets and downloads of stored files.
2
3use crate::{
4 files::{Files, FilesInner},
5 FileReadError,
6};
7use coven_database::{CacheReservation, DbError, FilePlace, FileRef, PreparedPin};
8use coven_foundation::files::{FileArea, FileName, FileWriter, StoreDirAccess};
9use futures_util::{StreamExt, TryStreamExt};
10
11/// What preparing one file's pin found: a whole copy written for it, or one
12/// already kept.
13enum PreparedFile {
14 Copy(CacheReservation),
15 Kept(FileRef),
16}
17
18impl Files {
19 /// Prepare a pin of stored files (§17.8): download what each lacks, up
20 /// to the download limit at once, into a whole cache copy written,
21 /// synced and checked against its content hash, or note a checked whole
22 /// copy already kept. Nothing is pinned until a write commits the
23 /// result with its `pin` step. Each file's download failure is its
24 /// stored bytes' pending record, as for any pin; a download that
25 /// succeeds clears it. A file only on one device, this one included
26 /// while it uploads, is refused: only stored bytes are kept. So is one
27 /// whose record goes, or leaves its stored bytes, while they download
28 /// (`NoLongerStored`). While the network is metered and the app moves
29 /// files only on unmetered networks, the downloads wait before asking
30 /// storage for bytes (§17.8).
31 pub async fn prepare_pin(&self, files: &[FileRef]) -> Result<PreparedPin, FileReadError> {
32 self.inner.check_open()?;
33 let (mut copies, mut kept) = (Vec::new(), Vec::new());
34 let mut waiting = files.to_vec();
35 loop {
36 // A metered network the app keeps files off holds the downloads
37 // (§17.8): the wait holds no lock, so a connection change or the
38 // store's close goes on meanwhile. A file whose download it
39 // stopped starts again here; the ones done stay done.
40 self.inner.read_unmetered_only().await?;
41 self.inner.until_downloads_unheld().await?;
42 let _pins = self.inner.pins.lock().await;
43 self.inner.check_open()?;
44 let mut current = Vec::new();
45 for file in &waiting {
46 let file = self.inner.current(file).await?;
47 if let FilePlace::DeviceOnly { device } = file.place() {
48 return Err(FileReadError::DeviceOnly { file: file.id(), device });
49 }
50 current.push(file);
51 }
52 let limit = self.transfer_limits().downloads.get();
53 let prepared: Vec<Result<PreparedFile, FileRef>> = futures_util::stream::iter(current)
54 .map(|file| async move {
55 let work = async {
56 if self.inner.database.kept(&file).await? {
57 return Ok(Some(PreparedFile::Kept(file.clone())));
58 }
59 Ok(self.inner.download_whole(file.clone()).await?.map(PreparedFile::Copy))
60 }
61 .await;
62 // A held download failed at nothing: no record, and no
63 // condition settled.
64 match work {
65 Ok(None) => Ok(Err(file)),
66 Ok(Some(prepared)) => {
67 self.inner.complete_pin(&file, Ok(prepared)).await.map(Ok)
68 }
69 Err(error) => {
70 self.inner.complete_pin::<PreparedFile>(&file, Err(error)).await.map(Ok)
71 }
72 }
73 })
74 .buffer_unordered(limit)
75 .try_collect()
76 .await?;
77 waiting.clear();
78 for file in prepared {
79 match file {
80 Ok(PreparedFile::Copy(copy)) => copies.push(copy),
81 Ok(PreparedFile::Kept(file)) => kept.push(file),
82 Err(held) => waiting.push(held),
83 }
84 }
85 if waiting.is_empty() {
86 return Ok(self.inner.database.prepared_pin(copies, kept)?);
87 }
88 }
89 }
90 /// Prepare a pin and commit it in a write of its own, as an app does,
91 /// for tests.
92 #[cfg(test)]
93 pub(crate) async fn pin(&self, files: &[FileRef]) -> Result<(), FileReadError> {
94 let prepared = self.prepare_pin(files).await?;
95 Ok(self.inner.database.commit_pin(prepared).await?)
96 }
97 /// The namespace's budget, declared or since set (E8).
98 pub async fn get_cache_budget(&self, namespace: &str) -> Result<u64, DbError> {
99 self.inner.check_open()?;
100 self.inner.database.budget(namespace).await
101 }
102}
103impl FilesInner {
104 /// Reserve a whole cache file of `file` and create it for writing. The
105 /// writer syncs its bytes on finishing; the reservation then publishes
106 /// them, and a dropped one leaves them to their recorded removal.
107 pub(crate) async fn create_cache_file(
108 &self,
109 file: &FileRef,
110 ) -> Result<(CacheReservation, FileWriter), FileReadError> {
111 let name = FileName::new(self.ids.new_id().to_string()).expect("UUID cache name");
112 let reservation = self.database.reserve_cache_file(file, name.clone()).await?;
113 let directory = self.directory.clone();
114 // Cleanup cannot release the name while creation still runs after a
115 // cancelled await; the blocking task retains its reservation.
116 // A panic in the creation, or the pool dropping it unstarted, drops
117 // the reservation with it, which leaves the name to its recorded
118 // removal. The reservation comes back with the writer, or with why
119 // it wasn't made, so a caller that stopped waiting drops both, and
120 // only a panic goes to the sink.
121 let (reservation, writer) = coven_foundation::task::blocking_completing(
122 "create a cache file",
123 self.database.bugs().clone(),
124 move || {
125 let writer =
126 directory.create_writer(FileArea::Cache, &name).map_err(FileReadError::from);
127 Ok::<_, std::convert::Infallible>((reservation, writer))
128 },
129 )
130 .await
131 .map_err(coven_foundation::files::FileError::from)?
132 .unwrap_or_else(|never| match never {});
133 Ok((reservation, writer?))
134 }
135}