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