-
Notifications
You must be signed in to change notification settings - Fork 0
feat(link): auto-group split archives into one package (task 31) #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e3ab362
feat(link): auto-group split archives into one package (task 31)
mpiton 0b5dc38
refactor(link): harden split-archive grouper from /simplify review
mpiton b642345
fix(link): detect legacy RAR header + release playlist lock before pu…
mpiton 8c11130
fix(link): bound part indices + gate on distinct part numbers
mpiton 144c2ba
docs(changelog): correct split-archive test count (31, not 35)
mpiton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src-tauri/src/application/commands/group_split_archives.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| //! Handler for [`GroupSplitArchivesCommand`](super::GroupSplitArchivesCommand). | ||
| //! | ||
| //! Routes the request through [`SplitArchiveGrouper`] so the same | ||
| //! idempotent natural-key logic backs both the IPC entry-point and any | ||
| //! future internal caller (e.g. the Link Grabber commit flow once it | ||
| //! learns to bundle split-archive links). The handler does NOT attach | ||
| //! downloads itself — it only ensures one [`Package`](crate::domain::model::package::Package) | ||
| //! exists per detected base name. Attaching member downloads is the | ||
| //! caller's responsibility once the resolved links have produced | ||
| //! [`DownloadId`](crate::domain::model::download::DownloadId)s. | ||
|
|
||
| use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
|
||
| use crate::application::command_bus::CommandBus; | ||
| use crate::application::error::AppError; | ||
| use crate::application::services::{SplitArchiveGroupResult, SplitArchiveGrouper}; | ||
|
|
||
| impl CommandBus { | ||
| pub async fn handle_group_split_archives( | ||
| &self, | ||
| cmd: super::GroupSplitArchivesCommand, | ||
| ) -> Result<Vec<SplitArchiveGroupResult>, AppError> { | ||
| let repo = self | ||
| .package_repo_arc() | ||
| .ok_or_else(|| AppError::Validation("package repository not configured".into()))?; | ||
| let grouper = SplitArchiveGrouper::new(repo, self.event_bus_arc()); | ||
|
|
||
| let now_ms = SystemTime::now() | ||
| .duration_since(UNIX_EPOCH) | ||
| .map(|d| d.as_millis() as u64) | ||
| .unwrap_or(0); | ||
|
|
||
| grouper.group_all(&cmd.links, now_ms) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::application::commands::GroupSplitArchivesCommand; | ||
| use crate::application::commands::tests_support::{ | ||
| CapturingEventBus, InMemoryCredentialStore, InMemoryDownloadRepo, InMemoryPackageRepo, | ||
| build_package_bus, bus_without_account_ports, | ||
| }; | ||
| use crate::application::error::AppError; | ||
| use crate::application::services::SplitArchiveLink; | ||
| use crate::domain::ports::driven::PackageRepository; | ||
|
|
||
| fn link(url: &str, filename: &str) -> SplitArchiveLink { | ||
| SplitArchiveLink { | ||
| url: url.to_string(), | ||
| filename: filename.to_string(), | ||
| } | ||
| } | ||
|
|
||
| fn ten_part_links(base: &str) -> Vec<SplitArchiveLink> { | ||
| (1..=10) | ||
| .map(|n| { | ||
| let name = format!("{base}.part{:02}.rar", n); | ||
| let url = format!("https://ex.com/{name}"); | ||
| link(&url, &name) | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_handle_group_split_archives_creates_one_package_per_base() { | ||
| let repo = Arc::new(InMemoryPackageRepo::new()); | ||
| let creds = Arc::new(InMemoryCredentialStore::new()); | ||
| let dl_repo = Arc::new(InMemoryDownloadRepo::new()); | ||
| let events = Arc::new(CapturingEventBus::new()); | ||
| let bus = build_package_bus(repo.clone(), creds, events, dl_repo); | ||
|
|
||
| let mut links = ten_part_links("alpha"); | ||
| links.extend(ten_part_links("bravo")); | ||
|
|
||
| let results = bus | ||
| .handle_group_split_archives(GroupSplitArchivesCommand { links }) | ||
| .await | ||
| .expect("group"); | ||
|
|
||
| assert_eq!(results.len(), 2); | ||
| assert!(results.iter().all(|r| r.created)); | ||
| assert_eq!(repo.list().unwrap().len(), 2); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_handle_group_split_archives_reuses_existing_package_on_re_resolve() { | ||
| let repo = Arc::new(InMemoryPackageRepo::new()); | ||
| let creds = Arc::new(InMemoryCredentialStore::new()); | ||
| let dl_repo = Arc::new(InMemoryDownloadRepo::new()); | ||
| let events = Arc::new(CapturingEventBus::new()); | ||
| let bus = build_package_bus(repo.clone(), creds, events, dl_repo); | ||
|
|
||
| let first = bus | ||
| .handle_group_split_archives(GroupSplitArchivesCommand { | ||
| links: ten_part_links("movie"), | ||
| }) | ||
| .await | ||
| .unwrap(); | ||
| let second = bus | ||
| .handle_group_split_archives(GroupSplitArchivesCommand { | ||
| links: ten_part_links("movie"), | ||
| }) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| assert!(first[0].created); | ||
| assert!(!second[0].created); | ||
| assert_eq!(first[0].package_id, second[0].package_id); | ||
| assert_eq!(repo.list().unwrap().len(), 1, "no duplicate package"); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_handle_group_split_archives_returns_validation_when_repo_missing() { | ||
| let events = Arc::new(CapturingEventBus::new()); | ||
| let bus = bus_without_account_ports(events); | ||
|
|
||
| let err = bus | ||
| .handle_group_split_archives(GroupSplitArchivesCommand { | ||
| links: ten_part_links("movie"), | ||
| }) | ||
| .await | ||
| .expect_err("missing repo"); | ||
| assert!(matches!(err, AppError::Validation(_))); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| //! Process-wide lock shared by the package groupers | ||
| //! ([`crate::application::services::PlaylistGrouper`], | ||
| //! [`crate::application::services::SplitArchiveGrouper`]) to serialise | ||
| //! find-then-save sequences. | ||
| //! | ||
| //! Without this lock, two concurrent IPC invocations for the same | ||
| //! natural key could both observe "not found" in `find_by_external_id` | ||
| //! and each insert a new `Package`, breaking the idempotent-reuse | ||
| //! guarantee. The lock window covers only the lookup + save, never the | ||
| //! downstream event publish, so the contention window stays tiny (a | ||
| //! few SQLite writes). | ||
| //! | ||
| //! A single shared mutex is intentional. The cost of mild cross-grouper | ||
| //! serialisation is negligible (groupers run only at Link-Grabber | ||
| //! commit time, far from any hot path), and a shared mutex makes | ||
| //! reasoning about the SQLite UNIQUE-index contract trivial: at most | ||
| //! one writer per process competes for any given external_id at a | ||
| //! time. | ||
|
|
||
| use std::sync::{Mutex, MutexGuard, OnceLock}; | ||
|
|
||
| fn lock() -> &'static Mutex<()> { | ||
| static GROUP_LOCK: OnceLock<Mutex<()>> = OnceLock::new(); | ||
| GROUP_LOCK.get_or_init(|| Mutex::new(())) | ||
| } | ||
|
|
||
| /// Acquire the shared grouper lock, recovering from a poisoned mutex | ||
| /// (a previous panic while holding the guard) instead of panicking | ||
| /// again. Domain state lives in SQLite, not in the guard, so the next | ||
| /// caller can safely proceed. | ||
| pub(crate) fn acquire_grouper_lock() -> MutexGuard<'static, ()> { | ||
| match lock().lock() { | ||
| Ok(g) => g, | ||
| Err(poisoned) => poisoned.into_inner(), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.