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 commit48c1debe0700fcdf2cdab5d66488ab31ba7d8006Parent: e23224b2 changed files +104 -61
crates/in-core/src/store/turso_store.rsmodified+104 -54
| @@ -13,13 +13,13 @@ use turso::transaction::{Transaction, TransactionBehavior}; | ||
| 13 | 13 | use turso::{Builder, Connection, Row, params}; |
| 14 | 14 | use ulid::Ulid; |
| 15 | 15 | |
| 16 | +use super::blobs::{Blobs, LocalBlobs}; | |
| 16 | 17 | use super::secret; |
| 17 | 18 | use super::{ |
| 18 | 19 | CHUNK_SIZE, CreatedLink, File, Folder, Listing, Result, ShareKind, ShareLink, ShareUser, |
| 19 | 20 | SharedItem, Store, StoreError, ThumbState, UPLOAD_TTL_HOURS, UploadSession, UploadState, User, |
| 20 | 21 | }; |
| 21 | 22 | use super::{ReconcileOptions, reconcile, schema, sniff}; |
| 22 | -use super::blobs::{Blobs, LocalBlobs}; | |
| 23 | 23 | use crate::live::{Change, Topic}; |
| 24 | 24 | use crate::thumbs; |
| 25 | 25 | |
| @@ -211,7 +211,6 @@ impl TursoStore { | ||
| 211 | 211 | self.sweep_orphan_files().await |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | - | |
| 215 | 214 | /// Sets the storage tree against the database, once per boot. The |
| 216 | 215 | /// database and the tree are two halves of one state, and a crash between |
| 217 | 216 | /// a row write and its file write — either order — leaves exactly one |
| @@ -251,11 +250,8 @@ impl TursoStore { | ||
| 251 | 250 | .map_err(backend)?; |
| 252 | 251 | let files = known_ids(&tx, "SELECT id FROM file").await?; |
| 253 | 252 | 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?; | |
| 259 | 255 | // The watermark: the newest moment any row carries. Read under the |
| 260 | 256 | // same lock as the ids, so a writer that commits mid-sweep either |
| 261 | 257 | // is in both sets or in neither. |
| @@ -301,7 +297,10 @@ impl TursoStore { | ||
| 301 | 297 | continue; |
| 302 | 298 | } |
| 303 | 299 | 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 | + ); | |
| 305 | 304 | } |
| 306 | 305 | } |
| 307 | 306 | } |
| @@ -2019,10 +2018,7 @@ impl Store for TursoStore { | ||
| 2019 | 2018 | for file_id in &deleted_files { |
| 2020 | 2019 | let _ = self |
| 2021 | 2020 | .blobs |
| 2022 | - .delete(&[ | |
| 2023 | - &format!("files/{file_id}"), | |
| 2024 | - &format!("thumbs/{file_id}"), | |
| 2025 | - ]) | |
| 2021 | + .delete(&[&format!("files/{file_id}"), &format!("thumbs/{file_id}")]) | |
| 2026 | 2022 | .await; |
| 2027 | 2023 | } |
| 2028 | 2024 | let purged = (deleted_files.len() + deleted_folders.len()) as u64; |
| @@ -2593,8 +2589,7 @@ impl Store for TursoStore { | ||
| 2593 | 2589 | // Staged in the files directory under a temp name: the sniffer and |
| 2594 | 2590 | // the thumbnailer need a real local file, and the adopt that places |
| 2595 | 2591 | // 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())); | |
| 2598 | 2593 | { |
| 2599 | 2594 | use std::io::Write as _; |
| 2600 | 2595 | let mut out = |
| @@ -3198,10 +3193,8 @@ async fn target_owner( | ||
| 3198 | 3193 | /// guarded deletes re-check `deleted_at` under the write lock the |
| 3199 | 3194 | /// immediate transaction holds from begin to commit. |
| 3200 | 3195 | 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`). | |
| 3205 | 3198 | Trashed, |
| 3206 | 3199 | /// The row must still be trashed before the cutoff (`purge_expired`). |
| 3207 | 3200 | Expired { before: &'a str }, |
| @@ -3218,7 +3211,6 @@ async fn passes_trash_guard( | ||
| 3218 | 3211 | guard: &TrashGuard<'_>, |
| 3219 | 3212 | ) -> Result<bool> { |
| 3220 | 3213 | let (sql, bind) = match guard { |
| 3221 | - TrashGuard::None => return Ok(true), | |
| 3222 | 3214 | TrashGuard::Trashed => ( |
| 3223 | 3215 | format!("SELECT 1 FROM {table} WHERE id = ?1 AND deleted_at IS NOT NULL"), |
| 3224 | 3216 | vec![id], |
| @@ -3605,14 +3597,16 @@ mod tests { | ||
| 3605 | 3597 | |
| 3606 | 3598 | let dir = tempfile::tempdir().unwrap(); |
| 3607 | 3599 | 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(); | |
| 3614 | 3603 | 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 | + ) | |
| 3616 | 3610 | .await |
| 3617 | 3611 | .unwrap(); |
| 3618 | 3612 | |
| @@ -3668,7 +3662,15 @@ mod tests { | ||
| 3668 | 3662 | .unwrap(); |
| 3669 | 3663 | assert!(deleted_files.is_empty()); |
| 3670 | 3664 | 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 | + ); | |
| 3672 | 3674 | assert!(storage.join(FILES_DIR).join(&file.id).is_file()); |
| 3673 | 3675 | |
| 3674 | 3676 | // The guards refuse only the resurrected: real trash still purges |
| @@ -3698,14 +3700,16 @@ mod tests { | ||
| 3698 | 3700 | async fn a_purge_file_refuses_what_a_restore_brought_back() { |
| 3699 | 3701 | let dir = tempfile::tempdir().unwrap(); |
| 3700 | 3702 | 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(); | |
| 3707 | 3706 | 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 | + ) | |
| 3709 | 3713 | .await |
| 3710 | 3714 | .unwrap(); |
| 3711 | 3715 | let file = store |
| @@ -3717,7 +3721,15 @@ mod tests { | ||
| 3717 | 3721 | // The restore won the window: the purge answers false for a live |
| 3718 | 3722 | // file and leaves row and bytes alone. |
| 3719 | 3723 | 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 | + ); | |
| 3721 | 3733 | assert!(storage.join(FILES_DIR).join(&file.id).is_file()); |
| 3722 | 3734 | // Actual trash still purges, rows then bytes. |
| 3723 | 3735 | store.delete_file(&file.id).await.unwrap(); |
| @@ -3730,14 +3742,16 @@ mod tests { | ||
| 3730 | 3742 | async fn a_purge_folder_refuses_what_a_restore_brought_back() { |
| 3731 | 3743 | let dir = tempfile::tempdir().unwrap(); |
| 3732 | 3744 | 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(); | |
| 3739 | 3748 | 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 | + ) | |
| 3741 | 3755 | .await |
| 3742 | 3756 | .unwrap(); |
| 3743 | 3757 | let root = store.create_folder(&user.id, None, "root").await.unwrap(); |
| @@ -3758,9 +3772,33 @@ mod tests { | ||
| 3758 | 3772 | store.purge_folder(&root.id).await, |
| 3759 | 3773 | Err(StoreError::NotFound) |
| 3760 | 3774 | )); |
| 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 | + ); | |
| 3764 | 3802 | assert!(storage.join(FILES_DIR).join(&file.id).is_file()); |
| 3765 | 3803 | // Trashed again, the whole subtree goes, rows then bytes. |
| 3766 | 3804 | store.delete_folder(&root.id).await.unwrap(); |
| @@ -3775,14 +3813,16 @@ mod tests { | ||
| 3775 | 3813 | async fn an_upload_finishes_once_and_never_after_an_abort() { |
| 3776 | 3814 | let dir = tempfile::tempdir().unwrap(); |
| 3777 | 3815 | 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(); | |
| 3784 | 3819 | 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 | + ) | |
| 3786 | 3826 | .await |
| 3787 | 3827 | .unwrap(); |
| 3788 | 3828 | let session = store |
| @@ -3802,7 +3842,12 @@ mod tests { | ||
| 3802 | 3842 | store.finish_upload(&session.id).await, |
| 3803 | 3843 | Err(StoreError::UploadExpired) |
| 3804 | 3844 | )); |
| 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(); | |
| 3806 | 3851 | assert_eq!(files, 1); |
| 3807 | 3852 | // An aborted session never finishes: no file row, ever. |
| 3808 | 3853 | let session = store |
| @@ -3815,7 +3860,12 @@ mod tests { | ||
| 3815 | 3860 | store.finish_upload(&session.id).await, |
| 3816 | 3861 | Err(StoreError::UploadExpired) |
| 3817 | 3862 | )); |
| 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(); | |
| 3819 | 3869 | assert_eq!(files, 1); |
| 3820 | 3870 | } |
| 3821 | 3871 | } |
crates/in-web/src/share.rsmodified+0 -7
| @@ -1221,13 +1221,6 @@ impl SharedRow { | ||
| 1221 | 1221 | fn size(&self) -> u64 { |
| 1222 | 1222 | self.file.as_ref().map(|file| file.size_bytes).unwrap_or(0) |
| 1223 | 1223 | } |
| 1224 | - | |
| 1225 | - fn downloads(&self) -> u64 { | |
| 1226 | - self.file | |
| 1227 | - .as_ref() | |
| 1228 | - .map(|file| file.download_count) | |
| 1229 | - .unwrap_or(0) | |
| 1230 | - } | |
| 1231 | 1224 | } |
| 1232 | 1225 | |
| 1233 | 1226 | /// Everything others shared with the reader, folders and files in one |