Add a library identifier to disambiguate clashes in the library #238

Merged
wojtek merged 16 commits from 231---differentiate-release-groups-with-same-title-but-different-type-clash into main 2025-01-02 15:50:55 +01:00
2 changed files with 13 additions and 28 deletions
Showing only changes of commit 38fa7f82c6 - Show all commits

View File

@ -269,9 +269,7 @@ impl Ord for AlbumMeta {
impl Merge for AlbumMeta { impl Merge for AlbumMeta {
fn merge_in_place(&mut self, other: Self) { fn merge_in_place(&mut self, other: Self) {
assert!(self.id.compatible(&other.id)); assert!(self.id.compatible(&other.id));
self.seq = std::cmp::max(self.seq, other.seq); self.seq = std::cmp::max(self.seq, other.seq);
self.info.merge_in_place(other.info); self.info.merge_in_place(other.info);
} }
} }

View File

@ -61,32 +61,26 @@ impl Artist {
primary_albums: &mut [Album], primary_albums: &mut [Album],
mut secondary_album: Album, mut secondary_album: Album,
) -> Option<Album> { ) -> Option<Album> {
if let lib_id @ AlbumLibId::Value(_) | lib_id @ AlbumLibId::Singleton = let id_opt = secondary_album.meta.id.lib_id;
secondary_album.meta.id.lib_id if let id @ AlbumLibId::Value(_) | id @ AlbumLibId::Singleton = id_opt {
{ let mut iter = primary_albums.iter_mut();
if let Some(ref mut primary_album) = primary_albums if let Some(ref mut primary_album) = iter.find(|a| a.meta.id.lib_id == id) {
.iter_mut()
.find(|album| album.meta.id.lib_id == lib_id)
{
primary_album.merge_in_place(secondary_album); primary_album.merge_in_place(secondary_album);
return None; return None;
} }
secondary_album.meta.id.lib_id = AlbumLibId::None; secondary_album.meta.id.lib_id = AlbumLibId::None;
} }
Some(secondary_album) Some(secondary_album)
} }
fn merge_albums_with_lib_id( fn merge_albums_by_lib_id(
primary_albums: &mut [Album], primary_albums: &mut [Album],
mut secondary_albums: Vec<Album>, mut secondary_albums: Vec<Album>,
) -> HashMap<String, Vec<Album>> { ) -> HashMap<String, Vec<Album>> {
let mut secondary_without_id = HashMap::<String, Vec<Album>>::new(); let mut secondary_without_id = HashMap::<String, Vec<Album>>::new();
for secondary_album in secondary_albums.drain(..) { for secondary_album in secondary_albums.drain(..) {
if let Some(secondary_album) = let unmerged = Artist::merge_album_with_lib_id(primary_albums, secondary_album);
Artist::merge_album_with_lib_id(primary_albums, secondary_album) if let Some(secondary_album) = unmerged {
{
secondary_without_id secondary_without_id
.entry(secondary_album.meta.id.title.clone()) .entry(secondary_album.meta.id.title.clone())
.or_default() .or_default()
@ -96,15 +90,13 @@ impl Artist {
secondary_without_id secondary_without_id
} }
fn merge_albums_with_title( fn merge_albums_by_title(
primary_albums: &mut Vec<Album>, primary_albums: &mut Vec<Album>,
mut secondary_without_id: HashMap<String, Vec<Album>>, mut secondary_without_id: HashMap<String, Vec<Album>>,
) { ) {
for (title, mut secondary_albums) in secondary_without_id.drain() { for (title, mut secondary_albums) in secondary_without_id.drain() {
match primary_albums let mut iter = primary_albums.iter_mut();
.iter_mut() match iter.find(|album| album.meta.id.title == title) {
.find(|album| album.meta.id.title == title)
{
Some(ref mut primary_album) => { Some(ref mut primary_album) => {
// We do not support merging multiple DB albums with same title yet. // We do not support merging multiple DB albums with same title yet.
assert_eq!(secondary_albums.len(), 1); assert_eq!(secondary_albums.len(), 1);
@ -132,15 +124,10 @@ impl Merge for Artist {
fn merge_in_place(&mut self, other: Self) { fn merge_in_place(&mut self, other: Self) {
self.meta.merge_in_place(other.meta); self.meta.merge_in_place(other.meta);
let mut primary_albums = mem::take(&mut self.albums); let other_without_id = Artist::merge_albums_by_lib_id(&mut self.albums, other.albums);
let secondary_albums = other.albums; Artist::merge_albums_by_title(&mut self.albums, other_without_id);
let secondary_without_id = self.albums.sort_unstable();
Artist::merge_albums_with_lib_id(&mut primary_albums, secondary_albums);
Artist::merge_albums_with_title(&mut primary_albums, secondary_without_id);
primary_albums.sort_unstable();
self.albums = primary_albums;
} }
} }