forge
Explore
Log in

Don't you dare go Hollow

the sweep leaves nothing dead behind it: with every purge path guarding under the write lock, the unconditional TrashGuard variant and the shared page's unused download counter go — the tree builds with only the five long-standing sniff.rs notices that predate this work

Browse files at this commit

48c1debe0700fcdf2cdab5d66488ab31ba7d8006Ahmet Kaan GümüşParent: e23224b
crates/in-core/src/store/turso_store.rsmodified+104 -54
@@ -13,13 +13,13 @@ use turso::transaction::{Transaction, TransactionBehavior};
1313 use turso::{Builder, Connection, Row, params};
1414 use ulid::Ulid;
1515
16+use super::blobs::{Blobs, LocalBlobs};
1617 use super::secret;
1718 use super::{
1819 CHUNK_SIZE, CreatedLink, File, Folder, Listing, Result, ShareKind, ShareLink, ShareUser,
1920 SharedItem, Store, StoreError, ThumbState, UPLOAD_TTL_HOURS, UploadSession, UploadState, User,
2021 };
2122 use super::{ReconcileOptions, reconcile, schema, sniff};
22-use super::blobs::{Blobs, LocalBlobs};
2323 use crate::live::{Change, Topic};
2424 use crate::thumbs;
2525
@@ -211,7 +211,6 @@ impl TursoStore {
211211 self.sweep_orphan_files().await
212212 }
213213
214-
215214 /// Sets the storage tree against the database, once per boot. The
216215 /// database and the tree are two halves of one state, and a crash between
217216 /// a row write and its file write — either order — leaves exactly one
@@ -251,11 +250,8 @@ impl TursoStore {
251250 .map_err(backend)?;
252251 let files = known_ids(&tx, "SELECT id FROM file").await?;
253252 let thumbs = known_ids(&tx, "SELECT id FROM file WHERE thumb_state = 'ready'").await?;
254- let sessions = known_ids(
255- &tx,
256- "SELECT id FROM upload_session WHERE state = 'active'",
257- )
258- .await?;
253+ let sessions =
254+ known_ids(&tx, "SELECT id FROM upload_session WHERE state = 'active'").await?;
259255 // The watermark: the newest moment any row carries. Read under the
260256 // same lock as the ids, so a writer that commits mid-sweep either
261257 // is in both sets or in neither.
@@ -301,7 +297,10 @@ impl TursoStore {
301297 continue;
302298 }
303299 if let Err(e) = self.blobs.delete(&[&format!("{dir}/{}", entry.name)]).await {
304- eprintln!("could not delete orphaned storage file {dir}/{}: {e}", entry.name);
300+ eprintln!(
301+ "could not delete orphaned storage file {dir}/{}: {e}",
302+ entry.name
303+ );
305304 }
306305 }
307306 }
@@ -2019,10 +2018,7 @@ impl Store for TursoStore {
20192018 for file_id in &deleted_files {
20202019 let _ = self
20212020 .blobs
2022- .delete(&[
2023- &format!("files/{file_id}"),
2024- &format!("thumbs/{file_id}"),
2025- ])
2021+ .delete(&[&format!("files/{file_id}"), &format!("thumbs/{file_id}")])
20262022 .await;
20272023 }
20282024 let purged = (deleted_files.len() + deleted_folders.len()) as u64;
@@ -2593,8 +2589,7 @@ impl Store for TursoStore {
25932589 // Staged in the files directory under a temp name: the sniffer and
25942590 // the thumbnailer need a real local file, and the adopt that places
25952591 // the assembled bytes is a same-tree rename away.
2596- let tmp = file_path(&self.storage, &file_id)
2597- .with_extension(format!("{}.tmp", Ulid::new()));
2592+ let tmp = file_path(&self.storage, &file_id).with_extension(format!("{}.tmp", Ulid::new()));
25982593 {
25992594 use std::io::Write as _;
26002595 let mut out =
@@ -3198,10 +3193,8 @@ async fn target_owner(
31983193 /// guarded deletes re-check `deleted_at` under the write lock the
31993194 /// immediate transaction holds from begin to commit.
32003195 enum TrashGuard<'a> {
3201- /// Unconditional: the caller vetted the rows itself (`purge_file`,
3202- /// `purge_folder`).
3203- None,
3204- /// The row must still be trashed (`empty_trash`).
3196+ /// The row must still be trashed (`purge_file`, `purge_folder`,
3197+ /// `empty_trash`).
32053198 Trashed,
32063199 /// The row must still be trashed before the cutoff (`purge_expired`).
32073200 Expired { before: &'a str },
@@ -3218,7 +3211,6 @@ async fn passes_trash_guard(
32183211 guard: &TrashGuard<'_>,
32193212 ) -> Result<bool> {
32203213 let (sql, bind) = match guard {
3221- TrashGuard::None => return Ok(true),
32223214 TrashGuard::Trashed => (
32233215 format!("SELECT 1 FROM {table} WHERE id = ?1 AND deleted_at IS NOT NULL"),
32243216 vec![id],
@@ -3605,14 +3597,16 @@ mod tests {
36053597
36063598 let dir = tempfile::tempdir().unwrap();
36073599 let storage = dir.path().join("storage");
3608- let store = TursoStore::open(
3609- dir.path().join("in.db").to_str().unwrap(),
3610- Some(&storage),
3611- )
3612- .await
3613- .unwrap();
3600+ let store = TursoStore::open(dir.path().join("in.db").to_str().unwrap(), Some(&storage))
3601+ .await
3602+ .unwrap();
36143603 let user = store
3615- .provision_user("sub-alice", "alice@example.com", "Alice", 1024 * 1024 * 1024)
3604+ .provision_user(
3605+ "sub-alice",
3606+ "alice@example.com",
3607+ "Alice",
3608+ 1024 * 1024 * 1024,
3609+ )
36163610 .await
36173611 .unwrap();
36183612
@@ -3668,7 +3662,15 @@ mod tests {
36683662 .unwrap();
36693663 assert!(deleted_files.is_empty());
36703664 assert!(deleted_folders.is_empty());
3671- assert!(store.file(&file.id).await.unwrap().unwrap().deleted_at.is_none());
3665+ assert!(
3666+ store
3667+ .file(&file.id)
3668+ .await
3669+ .unwrap()
3670+ .unwrap()
3671+ .deleted_at
3672+ .is_none()
3673+ );
36723674 assert!(storage.join(FILES_DIR).join(&file.id).is_file());
36733675
36743676 // The guards refuse only the resurrected: real trash still purges
@@ -3698,14 +3700,16 @@ mod tests {
36983700 async fn a_purge_file_refuses_what_a_restore_brought_back() {
36993701 let dir = tempfile::tempdir().unwrap();
37003702 let storage = dir.path().join("storage");
3701- let store = TursoStore::open(
3702- dir.path().join("in.db").to_str().unwrap(),
3703- Some(&storage),
3704- )
3705- .await
3706- .unwrap();
3703+ let store = TursoStore::open(dir.path().join("in.db").to_str().unwrap(), Some(&storage))
3704+ .await
3705+ .unwrap();
37073706 let user = store
3708- .provision_user("sub-alice", "alice@example.com", "Alice", 1024 * 1024 * 1024)
3707+ .provision_user(
3708+ "sub-alice",
3709+ "alice@example.com",
3710+ "Alice",
3711+ 1024 * 1024 * 1024,
3712+ )
37093713 .await
37103714 .unwrap();
37113715 let file = store
@@ -3717,7 +3721,15 @@ mod tests {
37173721 // The restore won the window: the purge answers false for a live
37183722 // file and leaves row and bytes alone.
37193723 assert!(!store.purge_file(&file.id).await.unwrap());
3720- assert!(store.file(&file.id).await.unwrap().unwrap().deleted_at.is_none());
3724+ assert!(
3725+ store
3726+ .file(&file.id)
3727+ .await
3728+ .unwrap()
3729+ .unwrap()
3730+ .deleted_at
3731+ .is_none()
3732+ );
37213733 assert!(storage.join(FILES_DIR).join(&file.id).is_file());
37223734 // Actual trash still purges, rows then bytes.
37233735 store.delete_file(&file.id).await.unwrap();
@@ -3730,14 +3742,16 @@ mod tests {
37303742 async fn a_purge_folder_refuses_what_a_restore_brought_back() {
37313743 let dir = tempfile::tempdir().unwrap();
37323744 let storage = dir.path().join("storage");
3733- let store = TursoStore::open(
3734- dir.path().join("in.db").to_str().unwrap(),
3735- Some(&storage),
3736- )
3737- .await
3738- .unwrap();
3745+ let store = TursoStore::open(dir.path().join("in.db").to_str().unwrap(), Some(&storage))
3746+ .await
3747+ .unwrap();
37393748 let user = store
3740- .provision_user("sub-alice", "alice@example.com", "Alice", 1024 * 1024 * 1024)
3749+ .provision_user(
3750+ "sub-alice",
3751+ "alice@example.com",
3752+ "Alice",
3753+ 1024 * 1024 * 1024,
3754+ )
37413755 .await
37423756 .unwrap();
37433757 let root = store.create_folder(&user.id, None, "root").await.unwrap();
@@ -3758,9 +3772,33 @@ mod tests {
37583772 store.purge_folder(&root.id).await,
37593773 Err(StoreError::NotFound)
37603774 ));
3761- assert!(store.folder(&root.id).await.unwrap().unwrap().deleted_at.is_none());
3762- assert!(store.folder(&kid.id).await.unwrap().unwrap().deleted_at.is_none());
3763- assert!(store.file(&file.id).await.unwrap().unwrap().deleted_at.is_none());
3775+ assert!(
3776+ store
3777+ .folder(&root.id)
3778+ .await
3779+ .unwrap()
3780+ .unwrap()
3781+ .deleted_at
3782+ .is_none()
3783+ );
3784+ assert!(
3785+ store
3786+ .folder(&kid.id)
3787+ .await
3788+ .unwrap()
3789+ .unwrap()
3790+ .deleted_at
3791+ .is_none()
3792+ );
3793+ assert!(
3794+ store
3795+ .file(&file.id)
3796+ .await
3797+ .unwrap()
3798+ .unwrap()
3799+ .deleted_at
3800+ .is_none()
3801+ );
37643802 assert!(storage.join(FILES_DIR).join(&file.id).is_file());
37653803 // Trashed again, the whole subtree goes, rows then bytes.
37663804 store.delete_folder(&root.id).await.unwrap();
@@ -3775,14 +3813,16 @@ mod tests {
37753813 async fn an_upload_finishes_once_and_never_after_an_abort() {
37763814 let dir = tempfile::tempdir().unwrap();
37773815 let storage = dir.path().join("storage");
3778- let store = TursoStore::open(
3779- dir.path().join("in.db").to_str().unwrap(),
3780- Some(&storage),
3781- )
3782- .await
3783- .unwrap();
3816+ let store = TursoStore::open(dir.path().join("in.db").to_str().unwrap(), Some(&storage))
3817+ .await
3818+ .unwrap();
37843819 let user = store
3785- .provision_user("sub-alice", "alice@example.com", "Alice", 1024 * 1024 * 1024)
3820+ .provision_user(
3821+ "sub-alice",
3822+ "alice@example.com",
3823+ "Alice",
3824+ 1024 * 1024 * 1024,
3825+ )
37863826 .await
37873827 .unwrap();
37883828 let session = store
@@ -3802,7 +3842,12 @@ mod tests {
38023842 store.finish_upload(&session.id).await,
38033843 Err(StoreError::UploadExpired)
38043844 ));
3805- let files = store.list_children(&user.id, None).await.unwrap().files.len();
3845+ let files = store
3846+ .list_children(&user.id, None)
3847+ .await
3848+ .unwrap()
3849+ .files
3850+ .len();
38063851 assert_eq!(files, 1);
38073852 // An aborted session never finishes: no file row, ever.
38083853 let session = store
@@ -3815,7 +3860,12 @@ mod tests {
38153860 store.finish_upload(&session.id).await,
38163861 Err(StoreError::UploadExpired)
38173862 ));
3818- let files = store.list_children(&user.id, None).await.unwrap().files.len();
3863+ let files = store
3864+ .list_children(&user.id, None)
3865+ .await
3866+ .unwrap()
3867+ .files
3868+ .len();
38193869 assert_eq!(files, 1);
38203870 }
38213871 }
crates/in-web/src/share.rsmodified+0 -7
@@ -1221,13 +1221,6 @@ impl SharedRow {
12211221 fn size(&self) -> u64 {
12221222 self.file.as_ref().map(|file| file.size_bytes).unwrap_or(0)
12231223 }
1224-
1225- fn downloads(&self) -> u64 {
1226- self.file
1227- .as_ref()
1228- .map(|file| file.download_count)
1229- .unwrap_or(0)
1230- }
12311224 }
12321225
12331226 /// Everything others shared with the reader, folders and files in one