From 12fcdd45b62605965730a70e4e78aca9f6d85e7e Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Fri, 1 Mar 2024 13:32:28 +0100 Subject: [PATCH] Library rescans commit to the database --- src/core/musichoard/musichoard.rs | 53 +++++++++++++++++++++---------- src/tui/lib.rs | 2 +- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/core/musichoard/musichoard.rs b/src/core/musichoard/musichoard.rs index fcb5828..06720a7 100644 --- a/src/core/musichoard/musichoard.rs +++ b/src/core/musichoard/musichoard.rs @@ -72,7 +72,7 @@ impl MusicHoard { } } - fn merge_collections(&mut self) { + fn merge_collections(&mut self) -> Collection { let mut primary = self.library_cache.clone(); for secondary_artist in self.database_cache.iter().cloned() { if let Some(ref mut primary_artist) = primary.get_mut(&secondary_artist.id) { @@ -82,10 +82,10 @@ impl MusicHoard { } } - self.collection = primary.into_values().collect(); - Self::sort_artists(&mut self.collection); + let mut collection: Collection = primary.into_values().collect(); + Self::sort_artists(&mut collection); - self.pre_commit = self.collection.clone(); + collection } fn items_to_artists(items: Vec) -> Result, Error> { @@ -190,17 +190,22 @@ impl MusicHoard { database_cache: vec![], } } + + /// Rescan the library and merge with the in-memory collection. + pub fn rescan_library(&mut self) -> Result<(), Error> { + self.pre_commit = self.rescan_library_inner()?; + self.collection = self.pre_commit.clone(); + Ok(()) + } } impl MusicHoard { - /// Rescan the library and merge with the in-memory collection. - pub fn rescan_library(&mut self) -> Result<(), Error> { + fn rescan_library_inner(&mut self) -> Result { let items = self.library.list(&Query::new())?; self.library_cache = Self::items_to_artists(items)?; Self::sort_albums_and_tracks(self.library_cache.values_mut()); - self.merge_collections(); - Ok(()) + Ok(self.merge_collections()) } } @@ -226,7 +231,9 @@ impl MusicHoard { self.database_cache = self.database.load()?; Self::sort_albums_and_tracks(self.database_cache.iter_mut()); - self.merge_collections(); + self.collection = self.merge_collections(); + self.pre_commit = self.collection.clone(); + Ok(()) } @@ -381,6 +388,12 @@ impl MusicHoard { mh.reload_database()?; Ok(mh) } + + /// Rescan the library and merge with the in-memory collection. + pub fn rescan_library(&mut self) -> Result<(), Error> { + self.pre_commit = self.rescan_library_inner()?; + self.commit() + } } #[cfg(test)] @@ -672,7 +685,7 @@ mod tests { ..Default::default() }; - mh.merge_collections(); + mh.collection = mh.merge_collections(); assert_eq!(expected, mh.collection); // The merge is completely non-overlapping so it should be commutative. @@ -686,7 +699,7 @@ mod tests { ..Default::default() }; - mh.merge_collections(); + mh.collection = mh.merge_collections(); assert_eq!(expected, mh.collection); } @@ -710,7 +723,7 @@ mod tests { ..Default::default() }; - mh.merge_collections(); + mh.collection = mh.merge_collections(); assert_eq!(expected, mh.collection); // The merge does not overwrite any data so it should be commutative. @@ -724,7 +737,7 @@ mod tests { ..Default::default() }; - mh.merge_collections(); + mh.collection = mh.merge_collections(); assert_eq!(expected, mh.collection); } @@ -761,7 +774,7 @@ mod tests { ..Default::default() }; - mh.merge_collections(); + mh.collection = mh.merge_collections(); assert_eq!(expected, mh.collection); // The merge overwrites the sort data, but no data is erased so it should be commutative. @@ -775,13 +788,14 @@ mod tests { ..Default::default() }; - mh.merge_collections(); + mh.collection = mh.merge_collections(); assert_eq!(expected, mh.collection); } #[test] fn rescan_library_ordered() { let mut library = MockILibrary::new(); + let mut database = MockIDatabase::new(); let library_input = Query::new(); let library_result = Ok(LIBRARY_ITEMS.to_owned()); @@ -792,7 +806,14 @@ mod tests { .times(1) .return_once(|_| library_result); - let mut music_hoard = MusicHoard::library(library); + database.expect_load().times(1).returning(|| Ok(vec![])); + database + .expect_save() + .with(predicate::eq(&*LIBRARY_COLLECTION)) + .times(1) + .return_once(|_| Ok(())); + + let mut music_hoard = MusicHoard::new(library, database).unwrap(); music_hoard.rescan_library().unwrap(); assert_eq!(music_hoard.get_collection(), &*LIBRARY_COLLECTION); diff --git a/src/tui/lib.rs b/src/tui/lib.rs index f85b7c3..990851c 100644 --- a/src/tui/lib.rs +++ b/src/tui/lib.rs @@ -13,7 +13,7 @@ pub trait IMusicHoard { // GRCOV_EXCL_START impl IMusicHoard for MusicHoard { fn rescan_library(&mut self) -> Result<(), musichoard::Error> { - MusicHoard::rescan_library(self) + MusicHoard::::rescan_library(self) } fn reload_database(&mut self) -> Result<(), musichoard::Error> {