From f1fa141e21f55ed60016de8cd7795b25c0eaea1c Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Thu, 31 Oct 2024 23:30:20 +0100 Subject: [PATCH 1/2] fix(taggedfilecollection): Fix wrong tag matching during copy/write --- src/cli/ui/handle_candidate.rs | 2 +- src/distance/release.rs | 14 +++++++++++--- src/taggedfilecollection.rs | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/cli/ui/handle_candidate.rs b/src/cli/ui/handle_candidate.rs index d87e1ac..f620f05 100644 --- a/src/cli/ui/handle_candidate.rs +++ b/src/cli/ui/handle_candidate.rs @@ -194,7 +194,7 @@ pub fn show_candidate( // // First, show the matched tracks. let track_assignment = candidate.similarity().track_assignment(); - let matched_track_map = track_assignment.matched_tracks_map(); + let matched_track_map = track_assignment.map_rhs_indices_to_lhs(); let mut rhs_track_index: usize = 0; for (media_index, media) in release.media().enumerate() { let format = media.media_format().unwrap_or_else(|| "Medium".into()); diff --git a/src/distance/release.rs b/src/distance/release.rs index 97f99c8..4ff7898 100644 --- a/src/distance/release.rs +++ b/src/distance/release.rs @@ -233,9 +233,17 @@ impl TrackAssignment { self.matched_tracks.iter() } - /// Returns a [`HashMap`] that maps the matched tracks from the left hand side side to the - /// corresponding track on the right hand side. - pub fn matched_tracks_map(&self) -> HashMap { + /// Returns a [`HashMap`] that allows retrieving the matched tracks from the right hand side + /// by the corresponding track index from the left hand side. + pub fn map_lhs_indices_to_rhs(&self) -> HashMap { + self.matched_tracks() + .map(|pair| (pair.lhs, (pair.rhs, &pair.similarity))) + .collect() + } + + /// Returns a [`HashMap`] that allows retrieving the matched tracks from the left hand side + /// by the corresponding track index from the right hand side. + pub fn map_rhs_indices_to_lhs(&self) -> HashMap { self.matched_tracks() .map(|pair| (pair.rhs, (pair.lhs, &pair.similarity))) .collect() diff --git a/src/taggedfilecollection.rs b/src/taggedfilecollection.rs index 24d2261..97d3a81 100644 --- a/src/taggedfilecollection.rs +++ b/src/taggedfilecollection.rs @@ -150,7 +150,7 @@ impl TaggedFileCollection { let matched_track_map = release_candidate .similarity() .track_assignment() - .matched_tracks_map(); + .map_lhs_indices_to_rhs(); self = self .into_iter() .enumerate() From 7695cb61719641fdd7e1ccafd7cb53c9efb2c468 Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Thu, 31 Oct 2024 23:53:43 +0100 Subject: [PATCH 2/2] fix(scanner): Always sort tagged file collections by path --- src/scanner.rs | 8 ++++---- src/taggedfile.rs | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/scanner.rs b/src/scanner.rs index 89d5260..4650174 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -17,7 +17,7 @@ use crate::release_candidate::ReleaseCandidateCollection; use crate::util::walk_dir; use crate::Cache; use crate::{Config, TaggedFile, TaggedFileCollection}; -use std::collections::{HashMap, HashSet}; +use std::collections::{BinaryHeap, HashMap, HashSet}; use std::path::PathBuf; use std::time::Duration; use tokio::sync::mpsc::{error::TryRecvError, Receiver}; @@ -103,7 +103,7 @@ impl Scanner { }; group_track_counts.insert(group_id, num_tracks); - group_tracks.insert(group_id, Vec::with_capacity(num_tracks)); + group_tracks.insert(group_id, BinaryHeap::with_capacity(num_tracks)); } while analyzer_output_rx_connected { @@ -140,7 +140,7 @@ impl Scanner { continue; }; - let collection = TaggedFileCollection::new(tracks); + let collection = TaggedFileCollection::new(tracks.into_sorted_vec()); if let Err(err) = post_analysis_tx.send(collection).await { log::error!("Receiver dropped on sending collection: {err}"); continue; @@ -159,7 +159,7 @@ impl Scanner { log::error!("Missing track count for group {group_id}"); } - let collection = TaggedFileCollection::new(tracks); + let collection = TaggedFileCollection::new(tracks.into_sorted_vec()); if let Err(err) = post_analysis_tx.send(collection).await { log::error!("Receiver dropped on sending collection: {err}"); } diff --git a/src/taggedfile.rs b/src/taggedfile.rs index 503cbb7..7933901 100644 --- a/src/taggedfile.rs +++ b/src/taggedfile.rs @@ -13,6 +13,7 @@ use crate::release::ReleaseLike; use crate::tag::{read_tags_from_path, Tag, TagKey}; use crate::track::{AnalyzedTrackMetadata, TrackLike}; use std::borrow::Cow; +use std::cmp::Ordering; use std::ffi::OsStr; use std::fmt; use std::path::{Path, PathBuf}; @@ -269,6 +270,26 @@ impl TaggedFile { } } +impl PartialEq for TaggedFile { + fn eq(&self, other: &Self) -> bool { + self.path.as_path().eq(other.path.as_path()) + } +} + +impl Eq for TaggedFile {} + +impl Ord for TaggedFile { + fn cmp(&self, other: &Self) -> Ordering { + self.path.as_path().cmp(other.path.as_path()) + } +} + +impl PartialOrd for TaggedFile { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + impl TrackLike for TaggedFile { fn acoustid(&self) -> Option> { self.first_tag_value(TagKey::AcoustId).map(Cow::from)